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" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 18 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 19 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 20 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 21 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 22 | /** Test if a buffer contains a constant byte value. |
| 23 | * |
| 24 | * `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] | 25 | * |
| 26 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 27 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 28 | * \param size Size of the buffer in bytes. |
| 29 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 30 | * \return 1 if the buffer is all-bits-zero. |
| 31 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 32 | */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 33 | 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] | 34 | { |
| 35 | size_t i; |
| 36 | for( i = 0; i < size; i++ ) |
| 37 | { |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 38 | if( ( (unsigned char *) buffer )[i] != c ) |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 39 | return( 0 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 40 | } |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 41 | return( 1 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 42 | } |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 43 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 44 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
| 45 | static int asn1_write_10x( unsigned char **p, |
| 46 | unsigned char *start, |
| 47 | size_t bits, |
| 48 | unsigned char x ) |
| 49 | { |
| 50 | int ret; |
| 51 | int len = bits / 8 + 1; |
Gilles Peskine | 480416a | 2018-06-28 19:04:07 +0200 | [diff] [blame] | 52 | if( bits == 0 ) |
| 53 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
| 54 | if( bits <= 8 && x >= 1 << ( bits - 1 ) ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 55 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 56 | if( *p < start || *p - start < (ptrdiff_t) len ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 57 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 58 | *p -= len; |
| 59 | ( *p )[len-1] = x; |
| 60 | if( bits % 8 == 0 ) |
| 61 | ( *p )[1] |= 1; |
| 62 | else |
| 63 | ( *p )[0] |= 1 << ( bits % 8 ); |
| 64 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 65 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
| 66 | MBEDTLS_ASN1_INTEGER ) ); |
| 67 | return( len ); |
| 68 | } |
| 69 | |
| 70 | static int construct_fake_rsa_key( unsigned char *buffer, |
| 71 | size_t buffer_size, |
| 72 | unsigned char **p, |
| 73 | size_t bits, |
| 74 | int keypair ) |
| 75 | { |
| 76 | size_t half_bits = ( bits + 1 ) / 2; |
| 77 | int ret; |
| 78 | int len = 0; |
| 79 | /* Construct something that looks like a DER encoding of |
| 80 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 81 | * RSAPrivateKey ::= SEQUENCE { |
| 82 | * version Version, |
| 83 | * modulus INTEGER, -- n |
| 84 | * publicExponent INTEGER, -- e |
| 85 | * privateExponent INTEGER, -- d |
| 86 | * prime1 INTEGER, -- p |
| 87 | * prime2 INTEGER, -- q |
| 88 | * exponent1 INTEGER, -- d mod (p-1) |
| 89 | * exponent2 INTEGER, -- d mod (q-1) |
| 90 | * coefficient INTEGER, -- (inverse of q) mod p |
| 91 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 92 | * } |
| 93 | * Or, for a public key, the same structure with only |
| 94 | * version, modulus and publicExponent. |
| 95 | */ |
| 96 | *p = buffer + buffer_size; |
| 97 | if( keypair ) |
| 98 | { |
| 99 | MBEDTLS_ASN1_CHK_ADD( len, /* pq */ |
| 100 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 101 | MBEDTLS_ASN1_CHK_ADD( len, /* dq */ |
| 102 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 103 | MBEDTLS_ASN1_CHK_ADD( len, /* dp */ |
| 104 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 105 | MBEDTLS_ASN1_CHK_ADD( len, /* q */ |
| 106 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 107 | MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */ |
| 108 | asn1_write_10x( p, buffer, half_bits, 3 ) ); |
| 109 | MBEDTLS_ASN1_CHK_ADD( len, /* d */ |
| 110 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 111 | } |
| 112 | MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */ |
| 113 | asn1_write_10x( p, buffer, 17, 1 ) ); |
| 114 | MBEDTLS_ASN1_CHK_ADD( len, /* n */ |
| 115 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 116 | if( keypair ) |
| 117 | MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */ |
| 118 | mbedtls_asn1_write_int( p, buffer, 0 ) ); |
| 119 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) ); |
| 120 | { |
| 121 | const unsigned char tag = |
| 122 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
| 123 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) ); |
| 124 | } |
| 125 | return( len ); |
| 126 | } |
| 127 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 128 | int exercise_mac_setup( psa_key_type_t key_type, |
| 129 | const unsigned char *key_bytes, |
| 130 | size_t key_length, |
| 131 | psa_algorithm_t alg, |
| 132 | psa_mac_operation_t *operation, |
| 133 | psa_status_t *status ) |
| 134 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 135 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 136 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 137 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 138 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 139 | psa_set_key_algorithm( &attributes, alg ); |
| 140 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 141 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 142 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 143 | *status = psa_mac_sign_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 144 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 145 | PSA_ASSERT( psa_mac_abort( operation ) ); |
| 146 | /* If setup failed, reproduce the failure, so that the caller can |
| 147 | * test the resulting state of the operation object. */ |
| 148 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 149 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 150 | TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 153 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 154 | return( 1 ); |
| 155 | |
| 156 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 157 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 158 | return( 0 ); |
| 159 | } |
| 160 | |
| 161 | int exercise_cipher_setup( psa_key_type_t key_type, |
| 162 | const unsigned char *key_bytes, |
| 163 | size_t key_length, |
| 164 | psa_algorithm_t alg, |
| 165 | psa_cipher_operation_t *operation, |
| 166 | psa_status_t *status ) |
| 167 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 168 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 169 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 170 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 171 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 172 | psa_set_key_algorithm( &attributes, alg ); |
| 173 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 174 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 175 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 176 | *status = psa_cipher_encrypt_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 177 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 178 | PSA_ASSERT( psa_cipher_abort( operation ) ); |
| 179 | /* If setup failed, reproduce the failure, so that the caller can |
| 180 | * test the resulting state of the operation object. */ |
| 181 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 182 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 183 | TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ), |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 184 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 185 | } |
| 186 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 187 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 188 | return( 1 ); |
| 189 | |
| 190 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 191 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 192 | return( 0 ); |
| 193 | } |
| 194 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 195 | 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] | 196 | { |
| 197 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 198 | 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] | 199 | uint8_t buffer[1]; |
| 200 | size_t length; |
| 201 | int ok = 0; |
| 202 | |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 203 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 204 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 205 | psa_set_key_algorithm( &attributes, PSA_ALG_CTR ); |
| 206 | psa_set_key_type( &attributes, PSA_KEY_TYPE_AES ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 207 | TEST_EQUAL( psa_get_key_attributes( key, &attributes ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 208 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 209 | TEST_EQUAL( |
| 210 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 ); |
| 211 | TEST_EQUAL( |
| 212 | 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] | 213 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 214 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 215 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 216 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
| 217 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
| 218 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 219 | TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 220 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 221 | TEST_EQUAL( psa_export_public_key( key, |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 222 | buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 223 | PSA_ERROR_INVALID_HANDLE ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 224 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 225 | ok = 1; |
| 226 | |
| 227 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 228 | /* |
| 229 | * Key attributes may have been returned by psa_get_key_attributes() |
| 230 | * thus reset them as required. |
| 231 | */ |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 232 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 233 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 234 | return( ok ); |
| 235 | } |
| 236 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 237 | /* Assert that a key isn't reported as having a slot number. */ |
| 238 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 239 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 240 | do \ |
| 241 | { \ |
| 242 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
| 243 | TEST_EQUAL( psa_get_key_slot_number( \ |
| 244 | attributes, \ |
| 245 | &ASSERT_NO_SLOT_NUMBER_slot_number ), \ |
| 246 | PSA_ERROR_INVALID_ARGUMENT ); \ |
| 247 | } \ |
| 248 | while( 0 ) |
| 249 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 250 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 251 | ( (void) 0 ) |
| 252 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 253 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 254 | /* An overapproximation of the amount of storage needed for a key of the |
| 255 | * given type and with the given content. The API doesn't make it easy |
| 256 | * to find a good value for the size. The current implementation doesn't |
| 257 | * care about the value anyway. */ |
| 258 | #define KEY_BITS_FROM_DATA( type, data ) \ |
| 259 | ( data )->len |
| 260 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 261 | typedef enum { |
| 262 | IMPORT_KEY = 0, |
| 263 | GENERATE_KEY = 1, |
| 264 | DERIVE_KEY = 2 |
| 265 | } generate_method; |
| 266 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 267 | /*! |
| 268 | * \brief Internal Function for AEAD multipart tests. |
| 269 | * |
| 270 | * \param key_type_arg Type of key passed in |
| 271 | * \param key_data The encryption / decryption key data |
| 272 | * \param alg_arg The type of algorithm used |
| 273 | * \param nonce Nonce data |
| 274 | * \param additional_data Additional data |
| 275 | * \param ad_part_len If not -1, the length of chunks to |
| 276 | * feed additional data in to be encrypted / |
| 277 | * decrypted. If -1, no chunking. |
| 278 | * \param input_data Data to encrypt / decrypt |
| 279 | * \param data_part_len If not -1, the length of chunks to feed the |
| 280 | * data in to be encrypted / decrypted. If -1, |
| 281 | * no chunking |
| 282 | * \param do_set_lengths If non-zero, then set lengths prior to |
| 283 | * calling encryption / decryption. |
| 284 | * \param expected_output Expected output |
| 285 | * \param expected_status_arg Expected status |
| 286 | * \param is_encrypt If non-zero this is an encryption operation. |
| 287 | * |
| 288 | * \return int Zero on failure, non-zero on success. |
| 289 | * |
| 290 | */ |
| 291 | static int aead_multipart_internal_func( int key_type_arg, data_t *key_data, |
| 292 | int alg_arg, |
| 293 | data_t *nonce, |
| 294 | data_t *additional_data, |
| 295 | int ad_part_len, |
| 296 | data_t *input_data, |
| 297 | int data_part_len, |
| 298 | int do_set_lengths, |
| 299 | data_t *expected_output, |
| 300 | int expect_valid_signature, |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 301 | int is_encrypt, |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 302 | int do_zero_parts, |
| 303 | int swap_set_functions ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 304 | { |
| 305 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 306 | psa_key_type_t key_type = key_type_arg; |
| 307 | psa_algorithm_t alg = alg_arg; |
| 308 | psa_aead_operation_t operation; |
| 309 | unsigned char *output_data = NULL; |
| 310 | unsigned char *part_data = NULL; |
| 311 | unsigned char *final_data = NULL; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 312 | size_t data_true_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 313 | size_t part_data_size = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 314 | size_t output_size = 0; |
| 315 | size_t final_output_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 316 | size_t output_length = 0; |
| 317 | size_t key_bits = 0; |
| 318 | size_t tag_length = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 319 | uint32_t part_offset = 0; |
| 320 | size_t part_length = 0; |
| 321 | size_t output_part_length = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 322 | size_t tag_size = 0; |
| 323 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 324 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 325 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 326 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 327 | int test_ok = 0; |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 328 | uint32_t part_count = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 329 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 330 | PSA_ASSERT( psa_crypto_init( ) ); |
| 331 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 332 | if( is_encrypt ) |
| 333 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 334 | else |
| 335 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 336 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 337 | psa_set_key_algorithm( &attributes, alg ); |
| 338 | psa_set_key_type( &attributes, key_type ); |
| 339 | |
| 340 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 341 | &key ) ); |
| 342 | |
| 343 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 344 | key_bits = psa_get_key_bits( &attributes ); |
| 345 | |
| 346 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 347 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 348 | if( is_encrypt ) |
| 349 | { |
| 350 | /* Tag gets written at end of buffer. */ |
| 351 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 352 | ( input_data->len + |
| 353 | tag_length ) ); |
| 354 | data_true_size = input_data->len; |
| 355 | } |
| 356 | else |
| 357 | { |
| 358 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 359 | ( input_data->len - |
| 360 | tag_length ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 361 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 362 | /* Do not want to attempt to decrypt tag. */ |
| 363 | data_true_size = input_data->len - tag_length; |
| 364 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 365 | |
| 366 | ASSERT_ALLOC( output_data, output_size ); |
| 367 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 368 | if( is_encrypt ) |
| 369 | { |
| 370 | final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg ); |
| 371 | TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE ); |
| 372 | } |
| 373 | else |
| 374 | { |
| 375 | final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 376 | TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
| 377 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 378 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 379 | ASSERT_ALLOC( final_data, final_output_size ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 380 | |
| 381 | operation = psa_aead_operation_init( ); |
| 382 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 383 | |
| 384 | if( is_encrypt ) |
| 385 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 386 | else |
| 387 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 388 | |
| 389 | /* If the operation is not supported, just skip and not fail in case the |
| 390 | * encryption involves a common limitation of cryptography hardwares and |
| 391 | * an alternative implementation. */ |
| 392 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 393 | { |
| 394 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 395 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 396 | } |
| 397 | |
| 398 | PSA_ASSERT( status ); |
| 399 | |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 400 | if( swap_set_functions ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 401 | { |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 402 | if( do_set_lengths ) |
| 403 | { |
| 404 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 405 | data_true_size ) ); |
| 406 | } |
| 407 | |
| 408 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 409 | } |
| 410 | else |
| 411 | { |
| 412 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 413 | |
| 414 | if( do_set_lengths ) |
| 415 | { |
| 416 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 417 | data_true_size ) ); |
| 418 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 419 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 420 | |
| 421 | if( ad_part_len != -1 ) |
| 422 | { |
| 423 | /* Pass additional data in parts */ |
| 424 | part_offset = 0; |
| 425 | |
| 426 | while( part_offset < additional_data->len ) |
| 427 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 428 | if( do_zero_parts && part_count++ & 0x01 ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 429 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 430 | part_length = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 431 | } |
| 432 | else |
| 433 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 434 | if( additional_data->len - part_offset < |
| 435 | ( uint32_t ) ad_part_len ) |
| 436 | { |
| 437 | part_length = additional_data->len - part_offset; |
| 438 | } |
| 439 | else |
| 440 | { |
| 441 | part_length = ad_part_len; |
| 442 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | PSA_ASSERT( psa_aead_update_ad( &operation, |
| 446 | additional_data->x + part_offset, |
| 447 | part_length ) ); |
| 448 | |
| 449 | part_offset += part_length; |
| 450 | } |
| 451 | } |
| 452 | else |
| 453 | { |
| 454 | /* Pass additional data in one go. */ |
| 455 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 456 | additional_data->len ) ); |
| 457 | } |
| 458 | |
| 459 | if( data_part_len != -1 ) |
| 460 | { |
| 461 | /* Pass data in parts */ |
| 462 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 463 | ( size_t ) data_part_len ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 464 | |
| 465 | ASSERT_ALLOC( part_data, part_data_size ); |
| 466 | |
| 467 | part_offset = 0; |
| 468 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 469 | while( part_offset < data_true_size ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 470 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 471 | if( do_zero_parts && part_count++ & 0x01 ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 472 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 473 | part_length = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 474 | } |
| 475 | else |
| 476 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 477 | if( ( data_true_size - part_offset ) < ( uint32_t ) data_part_len ) |
| 478 | { |
| 479 | part_length = ( data_true_size - part_offset ); |
| 480 | } |
| 481 | else |
| 482 | { |
| 483 | part_length = data_part_len; |
| 484 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | PSA_ASSERT( psa_aead_update( &operation, |
| 488 | ( input_data->x + part_offset ), |
| 489 | part_length, part_data, |
| 490 | part_data_size, |
| 491 | &output_part_length ) ); |
| 492 | |
| 493 | if( output_data && output_part_length ) |
| 494 | { |
| 495 | memcpy( ( output_data + part_offset ), part_data, |
| 496 | output_part_length ); |
| 497 | } |
| 498 | |
| 499 | part_offset += part_length; |
| 500 | output_length += output_part_length; |
| 501 | } |
| 502 | } |
| 503 | else |
| 504 | { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 505 | /* Pass all data in one go. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 506 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 507 | data_true_size, output_data, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 508 | output_size, &output_length ) ); |
| 509 | } |
| 510 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 511 | if( is_encrypt ) |
| 512 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 513 | final_output_size, |
| 514 | &output_part_length, |
| 515 | tag_buffer, tag_length, |
| 516 | &tag_size ) ); |
| 517 | else |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 518 | { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 519 | status = psa_aead_verify( &operation, final_data, |
| 520 | final_output_size, |
| 521 | &output_part_length, |
| 522 | ( input_data->x + data_true_size ), |
| 523 | tag_length ); |
| 524 | |
| 525 | if( status != PSA_SUCCESS ) |
| 526 | { |
| 527 | if( !expect_valid_signature ) |
| 528 | { |
| 529 | /* Expected failure. */ |
| 530 | test_ok = 1; |
| 531 | goto exit; |
| 532 | } |
| 533 | else |
| 534 | PSA_ASSERT( status ); |
| 535 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 536 | } |
| 537 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 538 | if( output_data && output_part_length ) |
| 539 | memcpy( ( output_data + output_length ), final_data, |
| 540 | output_part_length ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 541 | |
| 542 | output_length += output_part_length; |
| 543 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 544 | |
| 545 | /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE |
| 546 | * should be exact.*/ |
| 547 | if( is_encrypt ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 548 | { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 549 | TEST_EQUAL( tag_length, tag_size ); |
| 550 | |
| 551 | if( output_data && tag_length ) |
| 552 | memcpy( ( output_data + output_length ), tag_buffer, |
| 553 | tag_length ); |
| 554 | |
| 555 | output_length += tag_length; |
| 556 | |
| 557 | TEST_EQUAL( output_length, |
| 558 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 559 | input_data->len ) ); |
| 560 | TEST_ASSERT( output_length <= |
| 561 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 562 | } |
| 563 | else |
| 564 | { |
| 565 | TEST_EQUAL( output_length, |
| 566 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, |
| 567 | input_data->len ) ); |
| 568 | TEST_ASSERT( output_length <= |
| 569 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 570 | } |
| 571 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 572 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 573 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 574 | output_data, output_length ); |
| 575 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 576 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 577 | test_ok = 1; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 578 | |
| 579 | exit: |
| 580 | psa_destroy_key( key ); |
| 581 | psa_aead_abort( &operation ); |
| 582 | mbedtls_free( output_data ); |
| 583 | mbedtls_free( part_data ); |
| 584 | mbedtls_free( final_data ); |
| 585 | PSA_DONE( ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 586 | |
| 587 | return( test_ok ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 588 | } |
| 589 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 590 | /* END_HEADER */ |
| 591 | |
| 592 | /* BEGIN_DEPENDENCIES |
| 593 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 594 | * END_DEPENDENCIES |
| 595 | */ |
| 596 | |
| 597 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 598 | void static_checks( ) |
| 599 | { |
| 600 | size_t max_truncated_mac_size = |
| 601 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 602 | |
| 603 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 604 | * encoding. The shifted mask is the maximum truncated value. The |
| 605 | * untruncated algorithm may be one byte larger. */ |
| 606 | TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size ); |
| 607 | } |
| 608 | /* END_CASE */ |
| 609 | |
| 610 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 611 | void import_with_policy( int type_arg, |
| 612 | int usage_arg, int alg_arg, |
| 613 | int expected_status_arg ) |
| 614 | { |
| 615 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 616 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 617 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 618 | psa_key_type_t type = type_arg; |
| 619 | psa_key_usage_t usage = usage_arg; |
| 620 | psa_algorithm_t alg = alg_arg; |
| 621 | psa_status_t expected_status = expected_status_arg; |
| 622 | const uint8_t key_material[16] = {0}; |
| 623 | psa_status_t status; |
| 624 | |
| 625 | PSA_ASSERT( psa_crypto_init( ) ); |
| 626 | |
| 627 | psa_set_key_type( &attributes, type ); |
| 628 | psa_set_key_usage_flags( &attributes, usage ); |
| 629 | psa_set_key_algorithm( &attributes, alg ); |
| 630 | |
| 631 | status = psa_import_key( &attributes, |
| 632 | key_material, sizeof( key_material ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 633 | &key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 634 | TEST_EQUAL( status, expected_status ); |
| 635 | if( status != PSA_SUCCESS ) |
| 636 | goto exit; |
| 637 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 638 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 639 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 640 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 641 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 642 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 643 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 644 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 645 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 646 | |
| 647 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 648 | /* |
| 649 | * Key attributes may have been returned by psa_get_key_attributes() |
| 650 | * thus reset them as required. |
| 651 | */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 652 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 653 | |
| 654 | psa_destroy_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 655 | PSA_DONE( ); |
| 656 | } |
| 657 | /* END_CASE */ |
| 658 | |
| 659 | /* BEGIN_CASE */ |
| 660 | void import_with_data( data_t *data, int type_arg, |
| 661 | int attr_bits_arg, |
| 662 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 663 | { |
| 664 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 665 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 666 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 667 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 668 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 669 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 670 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 671 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 672 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 673 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 674 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 675 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 676 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 677 | status = psa_import_key( &attributes, data->x, data->len, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 678 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 679 | if( status != PSA_SUCCESS ) |
| 680 | goto exit; |
| 681 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 682 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 683 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 684 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 685 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 686 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 687 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 688 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 689 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 690 | |
| 691 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 692 | /* |
| 693 | * Key attributes may have been returned by psa_get_key_attributes() |
| 694 | * thus reset them as required. |
| 695 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 696 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 697 | |
| 698 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 699 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 700 | } |
| 701 | /* END_CASE */ |
| 702 | |
| 703 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 704 | void import_large_key( int type_arg, int byte_size_arg, |
| 705 | int expected_status_arg ) |
| 706 | { |
| 707 | psa_key_type_t type = type_arg; |
| 708 | size_t byte_size = byte_size_arg; |
| 709 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 710 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 711 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 712 | psa_status_t status; |
| 713 | uint8_t *buffer = NULL; |
| 714 | size_t buffer_size = byte_size + 1; |
| 715 | size_t n; |
| 716 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 717 | /* Skip the test case if the target running the test cannot |
| 718 | * accomodate large keys due to heap size constraints */ |
| 719 | ASSERT_ALLOC_WEAK( buffer, buffer_size ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 720 | memset( buffer, 'K', byte_size ); |
| 721 | |
| 722 | PSA_ASSERT( psa_crypto_init( ) ); |
| 723 | |
| 724 | /* Try importing the key */ |
| 725 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 726 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 727 | status = psa_import_key( &attributes, buffer, byte_size, &key ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 728 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 729 | TEST_EQUAL( status, expected_status ); |
| 730 | |
| 731 | if( status == PSA_SUCCESS ) |
| 732 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 733 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 734 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 735 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 736 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 737 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 738 | memset( buffer, 0, byte_size + 1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 739 | PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 740 | for( n = 0; n < byte_size; n++ ) |
| 741 | TEST_EQUAL( buffer[n], 'K' ); |
| 742 | for( n = byte_size; n < buffer_size; n++ ) |
| 743 | TEST_EQUAL( buffer[n], 0 ); |
| 744 | } |
| 745 | |
| 746 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 747 | /* |
| 748 | * Key attributes may have been returned by psa_get_key_attributes() |
| 749 | * thus reset them as required. |
| 750 | */ |
| 751 | psa_reset_key_attributes( &attributes ); |
| 752 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 753 | psa_destroy_key( key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 754 | PSA_DONE( ); |
| 755 | mbedtls_free( buffer ); |
| 756 | } |
| 757 | /* END_CASE */ |
| 758 | |
| 759 | /* BEGIN_CASE */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 760 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 761 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 762 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 763 | size_t bits = bits_arg; |
| 764 | psa_status_t expected_status = expected_status_arg; |
| 765 | psa_status_t status; |
| 766 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 767 | 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] | 768 | size_t buffer_size = /* Slight overapproximations */ |
| 769 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 770 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 771 | unsigned char *p; |
| 772 | int ret; |
| 773 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 774 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 775 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 776 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 777 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 778 | |
| 779 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 780 | bits, keypair ) ) >= 0 ); |
| 781 | length = ret; |
| 782 | |
| 783 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 784 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 785 | status = psa_import_key( &attributes, p, length, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 786 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 787 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 788 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 789 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 790 | |
| 791 | exit: |
| 792 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 793 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 794 | } |
| 795 | /* END_CASE */ |
| 796 | |
| 797 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 798 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 799 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 800 | int usage_arg, int alg_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 801 | int expected_bits, |
| 802 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 803 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 804 | int canonical_input ) |
| 805 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 806 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 807 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 808 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 809 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 810 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 811 | unsigned char *exported = NULL; |
| 812 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 813 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 814 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 815 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 816 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 817 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 818 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 819 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 820 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 821 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 822 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 823 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 824 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 825 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 826 | psa_set_key_algorithm( &attributes, alg ); |
| 827 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 828 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 829 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 830 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 831 | |
| 832 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 833 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 834 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 835 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 836 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 837 | |
| 838 | /* Export the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 839 | status = psa_export_key( key, exported, export_size, &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 840 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 841 | |
| 842 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 843 | * and export_size. On errors, the exported length must be 0. */ |
| 844 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 845 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
| 846 | TEST_ASSERT( exported_length <= export_size ); |
| 847 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 848 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 849 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 850 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 851 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 852 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 853 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 854 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 855 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 856 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 857 | * this validates the canonical representations. For canonical inputs, |
| 858 | * this doesn't directly validate the implementation, but it still helps |
| 859 | * by cross-validating the test data with the sanity check code. */ |
| 860 | if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) ) |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 861 | goto exit; |
| 862 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 863 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 864 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 865 | else |
| 866 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 867 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 868 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 869 | &key2 ) ); |
| 870 | PSA_ASSERT( psa_export_key( key2, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 871 | reexported, |
| 872 | export_size, |
| 873 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 874 | ASSERT_COMPARE( exported, exported_length, |
| 875 | reexported, reexported_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 876 | PSA_ASSERT( psa_destroy_key( key2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 877 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 878 | TEST_ASSERT( exported_length <= |
| 879 | PSA_EXPORT_KEY_OUTPUT_SIZE( type, |
| 880 | psa_get_key_bits( &got_attributes ) ) ); |
| 881 | TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 882 | |
| 883 | destroy: |
| 884 | /* Destroy the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 885 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 886 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 887 | |
| 888 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 889 | /* |
| 890 | * Key attributes may have been returned by psa_get_key_attributes() |
| 891 | * thus reset them as required. |
| 892 | */ |
| 893 | psa_reset_key_attributes( &got_attributes ); |
| 894 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 895 | mbedtls_free( exported ); |
| 896 | mbedtls_free( reexported ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 897 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 898 | } |
| 899 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 900 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 901 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 902 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 903 | int type_arg, |
| 904 | int alg_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 905 | int export_size_delta, |
| 906 | int expected_export_status_arg, |
| 907 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 908 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 909 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 910 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 911 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 912 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 913 | psa_status_t status; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 914 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 915 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 916 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 917 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 918 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 919 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 920 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 921 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 922 | psa_set_key_algorithm( &attributes, alg ); |
| 923 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 924 | |
| 925 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 926 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 927 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 928 | /* Export the public key */ |
| 929 | ASSERT_ALLOC( exported, export_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 930 | status = psa_export_public_key( key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 931 | exported, export_size, |
| 932 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 933 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 934 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 935 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 936 | 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] | 937 | size_t bits; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 938 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 939 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 940 | TEST_ASSERT( expected_public_key->len <= |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 941 | PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 942 | TEST_ASSERT( expected_public_key->len <= |
| 943 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 944 | TEST_ASSERT( expected_public_key->len <= |
| 945 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 946 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 947 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 948 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 949 | |
| 950 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 951 | /* |
| 952 | * Key attributes may have been returned by psa_get_key_attributes() |
| 953 | * thus reset them as required. |
| 954 | */ |
| 955 | psa_reset_key_attributes( &attributes ); |
| 956 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 957 | mbedtls_free( exported ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 958 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 959 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 960 | } |
| 961 | /* END_CASE */ |
| 962 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 963 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 964 | void import_and_exercise_key( data_t *data, |
| 965 | int type_arg, |
| 966 | int bits_arg, |
| 967 | int alg_arg ) |
| 968 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 969 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 970 | psa_key_type_t type = type_arg; |
| 971 | size_t bits = bits_arg; |
| 972 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 973 | 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] | 974 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 975 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 976 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 977 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 978 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 979 | psa_set_key_usage_flags( &attributes, usage ); |
| 980 | psa_set_key_algorithm( &attributes, alg ); |
| 981 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 982 | |
| 983 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 984 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 985 | |
| 986 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 987 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 988 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 989 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 990 | |
| 991 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 992 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 993 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 994 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 995 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 996 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 997 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 998 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 999 | /* |
| 1000 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1001 | * thus reset them as required. |
| 1002 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1003 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1004 | |
| 1005 | psa_reset_key_attributes( &attributes ); |
| 1006 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1007 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1008 | } |
| 1009 | /* END_CASE */ |
| 1010 | |
| 1011 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1012 | void effective_key_attributes( int type_arg, int expected_type_arg, |
| 1013 | int bits_arg, int expected_bits_arg, |
| 1014 | int usage_arg, int expected_usage_arg, |
| 1015 | int alg_arg, int expected_alg_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1016 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1017 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1018 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1019 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1020 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1021 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1022 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1023 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1024 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1025 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1026 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1027 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1028 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1029 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1030 | psa_set_key_usage_flags( &attributes, usage ); |
| 1031 | psa_set_key_algorithm( &attributes, alg ); |
| 1032 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1033 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1034 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1035 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1036 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1037 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1038 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1039 | TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type ); |
| 1040 | TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits ); |
| 1041 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1042 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1043 | |
| 1044 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1045 | /* |
| 1046 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1047 | * thus reset them as required. |
| 1048 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1049 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1050 | |
| 1051 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1052 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1053 | } |
| 1054 | /* END_CASE */ |
| 1055 | |
| 1056 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1057 | void check_key_policy( int type_arg, int bits_arg, |
| 1058 | int usage_arg, int alg_arg ) |
| 1059 | { |
| 1060 | test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg, |
| 1061 | usage_arg, usage_arg, alg_arg, alg_arg ); |
| 1062 | goto exit; |
| 1063 | } |
| 1064 | /* END_CASE */ |
| 1065 | |
| 1066 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1067 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1068 | { |
| 1069 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1070 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1071 | * though it's OK by the C standard. We could test for this, but we'd need |
| 1072 | * to supress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1073 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 1074 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1075 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1076 | |
| 1077 | memset( &zero, 0, sizeof( zero ) ); |
| 1078 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1079 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1080 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1081 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1082 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1083 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 1084 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 1085 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 1086 | |
| 1087 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 1088 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 1089 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 1090 | |
| 1091 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 1092 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 1093 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 1094 | |
| 1095 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 1096 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 1097 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1098 | } |
| 1099 | /* END_CASE */ |
| 1100 | |
| 1101 | /* BEGIN_CASE */ |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1102 | void mac_key_policy( int policy_usage, |
| 1103 | int policy_alg, |
| 1104 | int key_type, |
| 1105 | data_t *key_data, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1106 | int exercise_alg, |
| 1107 | int expected_status_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1108 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1109 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1110 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1111 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1112 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1113 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1114 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1115 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1116 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1117 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1118 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1119 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1120 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1121 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1122 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1123 | &key ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1124 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1125 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1126 | if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1127 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1128 | else |
| 1129 | TEST_EQUAL( status, expected_status ); |
| 1130 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1131 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1132 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1133 | memset( mac, 0, sizeof( mac ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1134 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1135 | if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1136 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1137 | else |
| 1138 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1139 | |
| 1140 | exit: |
| 1141 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1142 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1143 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1144 | } |
| 1145 | /* END_CASE */ |
| 1146 | |
| 1147 | /* BEGIN_CASE */ |
| 1148 | void cipher_key_policy( int policy_usage, |
| 1149 | int policy_alg, |
| 1150 | int key_type, |
| 1151 | data_t *key_data, |
| 1152 | int exercise_alg ) |
| 1153 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1154 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1155 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1156 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1157 | psa_status_t status; |
| 1158 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1159 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1160 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1161 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1162 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1163 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1164 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1165 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1166 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1167 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1168 | status = psa_cipher_encrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1169 | if( policy_alg == exercise_alg && |
| 1170 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1171 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1172 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1173 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1174 | psa_cipher_abort( &operation ); |
| 1175 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1176 | status = psa_cipher_decrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1177 | if( policy_alg == exercise_alg && |
| 1178 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1179 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1180 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1181 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1182 | |
| 1183 | exit: |
| 1184 | psa_cipher_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1185 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1186 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1187 | } |
| 1188 | /* END_CASE */ |
| 1189 | |
| 1190 | /* BEGIN_CASE */ |
| 1191 | void aead_key_policy( int policy_usage, |
| 1192 | int policy_alg, |
| 1193 | int key_type, |
| 1194 | data_t *key_data, |
| 1195 | int nonce_length_arg, |
| 1196 | int tag_length_arg, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1197 | int exercise_alg, |
| 1198 | int expected_status_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1199 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1200 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1201 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1202 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1203 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1204 | unsigned char nonce[16] = {0}; |
| 1205 | size_t nonce_length = nonce_length_arg; |
| 1206 | unsigned char tag[16]; |
| 1207 | size_t tag_length = tag_length_arg; |
| 1208 | size_t output_length; |
| 1209 | |
| 1210 | TEST_ASSERT( nonce_length <= sizeof( nonce ) ); |
| 1211 | TEST_ASSERT( tag_length <= sizeof( tag ) ); |
| 1212 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1213 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1214 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1215 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1216 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1217 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1218 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1219 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1220 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1221 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1222 | status = psa_aead_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1223 | nonce, nonce_length, |
| 1224 | NULL, 0, |
| 1225 | NULL, 0, |
| 1226 | tag, tag_length, |
| 1227 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1228 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1229 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1230 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1231 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1232 | |
| 1233 | memset( tag, 0, sizeof( tag ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1234 | status = psa_aead_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1235 | nonce, nonce_length, |
| 1236 | NULL, 0, |
| 1237 | tag, tag_length, |
| 1238 | NULL, 0, |
| 1239 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1240 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 1241 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1242 | else if( expected_status == PSA_SUCCESS ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1243 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1244 | else |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1245 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1246 | |
| 1247 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1248 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1249 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1250 | } |
| 1251 | /* END_CASE */ |
| 1252 | |
| 1253 | /* BEGIN_CASE */ |
| 1254 | void asymmetric_encryption_key_policy( int policy_usage, |
| 1255 | int policy_alg, |
| 1256 | int key_type, |
| 1257 | data_t *key_data, |
| 1258 | int exercise_alg ) |
| 1259 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1260 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1261 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1262 | psa_status_t status; |
| 1263 | size_t key_bits; |
| 1264 | size_t buffer_length; |
| 1265 | unsigned char *buffer = NULL; |
| 1266 | size_t output_length; |
| 1267 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1268 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1269 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1270 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1271 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1272 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1273 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1274 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1275 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1276 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1277 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1278 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1279 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 1280 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1281 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1282 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1283 | status = psa_asymmetric_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1284 | NULL, 0, |
| 1285 | NULL, 0, |
| 1286 | buffer, buffer_length, |
| 1287 | &output_length ); |
| 1288 | if( policy_alg == exercise_alg && |
| 1289 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1290 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1291 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1292 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1293 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 1294 | if( buffer_length != 0 ) |
| 1295 | memset( buffer, 0, buffer_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1296 | status = psa_asymmetric_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1297 | buffer, buffer_length, |
| 1298 | NULL, 0, |
| 1299 | buffer, buffer_length, |
| 1300 | &output_length ); |
| 1301 | if( policy_alg == exercise_alg && |
| 1302 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1303 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1304 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1305 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1306 | |
| 1307 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1308 | /* |
| 1309 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1310 | * thus reset them as required. |
| 1311 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1312 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1313 | |
| 1314 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1315 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1316 | mbedtls_free( buffer ); |
| 1317 | } |
| 1318 | /* END_CASE */ |
| 1319 | |
| 1320 | /* BEGIN_CASE */ |
| 1321 | void asymmetric_signature_key_policy( int policy_usage, |
| 1322 | int policy_alg, |
| 1323 | int key_type, |
| 1324 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1325 | int exercise_alg, |
| 1326 | int payload_length_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1327 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1328 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1329 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1330 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1331 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 1332 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 1333 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 1334 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 1335 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 1336 | int compatible_alg = payload_length_arg > 0; |
| 1337 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1338 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1339 | size_t signature_length; |
| 1340 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1341 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1342 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1343 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1344 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1345 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1346 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1347 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1348 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1349 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1350 | status = psa_sign_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1351 | payload, payload_length, |
| 1352 | signature, sizeof( signature ), |
| 1353 | &signature_length ); |
| 1354 | if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1355 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1356 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1357 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1358 | |
| 1359 | memset( signature, 0, sizeof( signature ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1360 | status = psa_verify_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1361 | payload, payload_length, |
| 1362 | signature, sizeof( signature ) ); |
| 1363 | if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1364 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1365 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1366 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1367 | |
| 1368 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1369 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1370 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1371 | } |
| 1372 | /* END_CASE */ |
| 1373 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1374 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1375 | void derive_key_policy( int policy_usage, |
| 1376 | int policy_alg, |
| 1377 | int key_type, |
| 1378 | data_t *key_data, |
| 1379 | int exercise_alg ) |
| 1380 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1381 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1382 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1383 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1384 | psa_status_t status; |
| 1385 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1386 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1387 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1388 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1389 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1390 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1391 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1392 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1393 | &key ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1394 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1395 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 1396 | |
| 1397 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 1398 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1399 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1400 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 1401 | &operation, |
| 1402 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 1403 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1404 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1405 | |
| 1406 | status = psa_key_derivation_input_key( &operation, |
| 1407 | PSA_KEY_DERIVATION_INPUT_SECRET, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1408 | key ); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1409 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1410 | if( policy_alg == exercise_alg && |
| 1411 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1412 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +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 | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1415 | |
| 1416 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1417 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1418 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1419 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1420 | } |
| 1421 | /* END_CASE */ |
| 1422 | |
| 1423 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1424 | void agreement_key_policy( int policy_usage, |
| 1425 | int policy_alg, |
| 1426 | int key_type_arg, |
| 1427 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1428 | int exercise_alg, |
| 1429 | int expected_status_arg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1430 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1431 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1432 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1433 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1434 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1435 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1436 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1437 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1438 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1439 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1440 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1441 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1442 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1443 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1444 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1445 | &key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1446 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1447 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1448 | status = mbedtls_test_psa_key_agreement_with_self( &operation, key ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1449 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1450 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1451 | |
| 1452 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1453 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1454 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1455 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1456 | } |
| 1457 | /* END_CASE */ |
| 1458 | |
| 1459 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1460 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 1461 | int usage_arg, int alg_arg, int alg2_arg ) |
| 1462 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1463 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1464 | psa_key_type_t key_type = key_type_arg; |
| 1465 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1466 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1467 | psa_key_usage_t usage = usage_arg; |
| 1468 | psa_algorithm_t alg = alg_arg; |
| 1469 | psa_algorithm_t alg2 = alg2_arg; |
| 1470 | |
| 1471 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1472 | |
| 1473 | psa_set_key_usage_flags( &attributes, usage ); |
| 1474 | psa_set_key_algorithm( &attributes, alg ); |
| 1475 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 1476 | psa_set_key_type( &attributes, key_type ); |
| 1477 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1478 | &key ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1479 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1480 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1481 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 1482 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 1483 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 1484 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1485 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1486 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1487 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1488 | goto exit; |
| 1489 | |
| 1490 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1491 | /* |
| 1492 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1493 | * thus reset them as required. |
| 1494 | */ |
| 1495 | psa_reset_key_attributes( &got_attributes ); |
| 1496 | |
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 | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1499 | } |
| 1500 | /* END_CASE */ |
| 1501 | |
| 1502 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1503 | void raw_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 | 04ee2d2 | 2019-04-11 21:25:46 +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 | 04ee2d2 | 2019-04-11 21:25:46 +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 | 04ee2d2 | 2019-04-11 21:25:46 +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 | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1516 | |
| 1517 | PSA_ASSERT( psa_crypto_init( ) ); |
| 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 | 04ee2d2 | 2019-04-11 21:25:46 +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 | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1525 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1526 | status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1527 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1528 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1529 | |
| 1530 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1531 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1532 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1533 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1534 | } |
| 1535 | /* END_CASE */ |
| 1536 | |
| 1537 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1538 | void copy_success( int source_usage_arg, |
| 1539 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1540 | int type_arg, data_t *material, |
| 1541 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1542 | int target_usage_arg, |
| 1543 | int target_alg_arg, int target_alg2_arg, |
| 1544 | int expected_usage_arg, |
| 1545 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1546 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1547 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1548 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1549 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 1550 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1551 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1552 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1553 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1554 | uint8_t *export_buffer = NULL; |
| 1555 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1556 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1557 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1558 | /* Prepare the source key. */ |
| 1559 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 1560 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1561 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1562 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1563 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 1564 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1565 | &source_key ) ); |
| 1566 | PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1567 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1568 | /* Prepare the target attributes. */ |
| 1569 | if( copy_attributes ) |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1570 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1571 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1572 | /* Set volatile lifetime to reset the key identifier to 0. */ |
| 1573 | psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE ); |
| 1574 | } |
| 1575 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1576 | if( target_usage_arg != -1 ) |
| 1577 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 1578 | if( target_alg_arg != -1 ) |
| 1579 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1580 | if( target_alg2_arg != -1 ) |
| 1581 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1582 | |
| 1583 | /* Copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1584 | PSA_ASSERT( psa_copy_key( source_key, |
| 1585 | &target_attributes, &target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1586 | |
| 1587 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1588 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1589 | |
| 1590 | /* Test that the target slot has the expected content and policy. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1591 | PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1592 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 1593 | psa_get_key_type( &target_attributes ) ); |
| 1594 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 1595 | psa_get_key_bits( &target_attributes ) ); |
| 1596 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 1597 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1598 | TEST_EQUAL( expected_alg2, |
| 1599 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1600 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 1601 | { |
| 1602 | size_t length; |
| 1603 | ASSERT_ALLOC( export_buffer, material->len ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1604 | PSA_ASSERT( psa_export_key( target_key, export_buffer, |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1605 | material->len, &length ) ); |
| 1606 | ASSERT_COMPARE( material->x, material->len, |
| 1607 | export_buffer, length ); |
| 1608 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1609 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1610 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1611 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1612 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) ) |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1613 | goto exit; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1614 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1615 | PSA_ASSERT( psa_destroy_key( target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1616 | |
| 1617 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1618 | /* |
| 1619 | * Source and target key attributes may have been returned by |
| 1620 | * psa_get_key_attributes() thus reset them as required. |
| 1621 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1622 | psa_reset_key_attributes( &source_attributes ); |
| 1623 | psa_reset_key_attributes( &target_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1624 | |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1625 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1626 | mbedtls_free( export_buffer ); |
| 1627 | } |
| 1628 | /* END_CASE */ |
| 1629 | |
| 1630 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1631 | void copy_fail( int source_usage_arg, |
| 1632 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1633 | int type_arg, data_t *material, |
| 1634 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1635 | int target_usage_arg, |
| 1636 | int target_alg_arg, int target_alg2_arg, |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 1637 | int target_id_arg, int target_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1638 | int expected_status_arg ) |
| 1639 | { |
| 1640 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1641 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1642 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1643 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 1644 | 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] | 1645 | |
| 1646 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1647 | |
| 1648 | /* Prepare the source key. */ |
| 1649 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 1650 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1651 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1652 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1653 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 1654 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1655 | &source_key ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1656 | |
| 1657 | /* Prepare the target attributes. */ |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 1658 | psa_set_key_id( &target_attributes, key_id ); |
| 1659 | psa_set_key_lifetime( &target_attributes, target_lifetime_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1660 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 1661 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 1662 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 1663 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1664 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1665 | |
| 1666 | /* Try to copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1667 | TEST_EQUAL( psa_copy_key( source_key, |
| 1668 | &target_attributes, &target_key ), |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1669 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1670 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1671 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1672 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1673 | exit: |
| 1674 | psa_reset_key_attributes( &source_attributes ); |
| 1675 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1676 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1677 | } |
| 1678 | /* END_CASE */ |
| 1679 | |
| 1680 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1681 | void hash_operation_init( ) |
| 1682 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1683 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1684 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1685 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1686 | * though it's OK by the C standard. We could test for this, but we'd need |
| 1687 | * to supress the Clang warning for the test. */ |
| 1688 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 1689 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 1690 | psa_hash_operation_t zero; |
| 1691 | |
| 1692 | memset( &zero, 0, sizeof( zero ) ); |
| 1693 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1694 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1695 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 1696 | PSA_ERROR_BAD_STATE ); |
| 1697 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 1698 | PSA_ERROR_BAD_STATE ); |
| 1699 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 1700 | PSA_ERROR_BAD_STATE ); |
| 1701 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1702 | /* A default hash operation should be abortable without error. */ |
| 1703 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 1704 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 1705 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1706 | } |
| 1707 | /* END_CASE */ |
| 1708 | |
| 1709 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1710 | void hash_setup( int alg_arg, |
| 1711 | int expected_status_arg ) |
| 1712 | { |
| 1713 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1714 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1715 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1716 | psa_status_t status; |
| 1717 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1718 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1719 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1720 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1721 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1722 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 1723 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 1724 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1725 | |
| 1726 | /* If setup failed, reproduce the failure, so as to |
| 1727 | * test the resulting state of the operation object. */ |
| 1728 | if( status != PSA_SUCCESS ) |
| 1729 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 1730 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1731 | /* Now the operation object should be reusable. */ |
| 1732 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 1733 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 1734 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1735 | #endif |
| 1736 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1737 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1738 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1739 | } |
| 1740 | /* END_CASE */ |
| 1741 | |
| 1742 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1743 | void hash_compute_fail( int alg_arg, data_t *input, |
| 1744 | int output_size_arg, int expected_status_arg ) |
| 1745 | { |
| 1746 | psa_algorithm_t alg = alg_arg; |
| 1747 | uint8_t *output = NULL; |
| 1748 | size_t output_size = output_size_arg; |
| 1749 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 1750 | psa_status_t expected_status = expected_status_arg; |
| 1751 | psa_status_t status; |
| 1752 | |
| 1753 | ASSERT_ALLOC( output, output_size ); |
| 1754 | |
| 1755 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1756 | |
| 1757 | status = psa_hash_compute( alg, input->x, input->len, |
| 1758 | output, output_size, &output_length ); |
| 1759 | TEST_EQUAL( status, expected_status ); |
| 1760 | TEST_ASSERT( output_length <= output_size ); |
| 1761 | |
| 1762 | exit: |
| 1763 | mbedtls_free( output ); |
| 1764 | PSA_DONE( ); |
| 1765 | } |
| 1766 | /* END_CASE */ |
| 1767 | |
| 1768 | /* BEGIN_CASE */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 1769 | void hash_compare_fail( int alg_arg, data_t *input, |
| 1770 | data_t *reference_hash, |
| 1771 | int expected_status_arg ) |
| 1772 | { |
| 1773 | psa_algorithm_t alg = alg_arg; |
| 1774 | psa_status_t expected_status = expected_status_arg; |
| 1775 | psa_status_t status; |
| 1776 | |
| 1777 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1778 | |
| 1779 | status = psa_hash_compare( alg, input->x, input->len, |
| 1780 | reference_hash->x, reference_hash->len ); |
| 1781 | TEST_EQUAL( status, expected_status ); |
| 1782 | |
| 1783 | exit: |
| 1784 | PSA_DONE( ); |
| 1785 | } |
| 1786 | /* END_CASE */ |
| 1787 | |
| 1788 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1789 | void hash_compute_compare( int alg_arg, data_t *input, |
| 1790 | data_t *expected_output ) |
| 1791 | { |
| 1792 | psa_algorithm_t alg = alg_arg; |
| 1793 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 1794 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 1795 | size_t i; |
| 1796 | |
| 1797 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1798 | |
| 1799 | /* Compute with tight buffer */ |
| 1800 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1801 | output, PSA_HASH_LENGTH( alg ), |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1802 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1803 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1804 | ASSERT_COMPARE( output, output_length, |
| 1805 | expected_output->x, expected_output->len ); |
| 1806 | |
| 1807 | /* Compute with larger buffer */ |
| 1808 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
| 1809 | output, sizeof( output ), |
| 1810 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1811 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1812 | ASSERT_COMPARE( output, output_length, |
| 1813 | expected_output->x, expected_output->len ); |
| 1814 | |
| 1815 | /* Compare with correct hash */ |
| 1816 | PSA_ASSERT( psa_hash_compare( alg, input->x, input->len, |
| 1817 | output, output_length ) ); |
| 1818 | |
| 1819 | /* Compare with trailing garbage */ |
| 1820 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 1821 | output, output_length + 1 ), |
| 1822 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1823 | |
| 1824 | /* Compare with truncated hash */ |
| 1825 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 1826 | output, output_length - 1 ), |
| 1827 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1828 | |
| 1829 | /* Compare with corrupted value */ |
| 1830 | for( i = 0; i < output_length; i++ ) |
| 1831 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 1832 | mbedtls_test_set_step( i ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1833 | output[i] ^= 1; |
| 1834 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 1835 | output, output_length ), |
| 1836 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1837 | output[i] ^= 1; |
| 1838 | } |
| 1839 | |
| 1840 | exit: |
| 1841 | PSA_DONE( ); |
| 1842 | } |
| 1843 | /* END_CASE */ |
| 1844 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 1845 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1846 | void hash_bad_order( ) |
| 1847 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1848 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1849 | unsigned char input[] = ""; |
| 1850 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1851 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1852 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 1853 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 1854 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1855 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1856 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1857 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1858 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1859 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1860 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1861 | /* Call setup twice in a row. */ |
| 1862 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1863 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 1864 | PSA_ERROR_BAD_STATE ); |
| 1865 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1866 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1867 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1868 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1869 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1870 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1871 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1872 | /* Call update after finish. */ |
| 1873 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1874 | PSA_ASSERT( psa_hash_finish( &operation, |
| 1875 | hash, sizeof( hash ), &hash_len ) ); |
| 1876 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1877 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1878 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1879 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1880 | /* Call verify without calling setup beforehand. */ |
| 1881 | TEST_EQUAL( psa_hash_verify( &operation, |
| 1882 | valid_hash, sizeof( valid_hash ) ), |
| 1883 | PSA_ERROR_BAD_STATE ); |
| 1884 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1885 | |
| 1886 | /* Call verify after finish. */ |
| 1887 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1888 | PSA_ASSERT( psa_hash_finish( &operation, |
| 1889 | hash, sizeof( hash ), &hash_len ) ); |
| 1890 | TEST_EQUAL( psa_hash_verify( &operation, |
| 1891 | valid_hash, sizeof( valid_hash ) ), |
| 1892 | PSA_ERROR_BAD_STATE ); |
| 1893 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1894 | |
| 1895 | /* Call verify twice in a row. */ |
| 1896 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1897 | PSA_ASSERT( psa_hash_verify( &operation, |
| 1898 | valid_hash, sizeof( valid_hash ) ) ); |
| 1899 | TEST_EQUAL( psa_hash_verify( &operation, |
| 1900 | valid_hash, sizeof( valid_hash ) ), |
| 1901 | PSA_ERROR_BAD_STATE ); |
| 1902 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1903 | |
| 1904 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1905 | TEST_EQUAL( psa_hash_finish( &operation, |
| 1906 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1907 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1908 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1909 | |
| 1910 | /* Call finish twice in a row. */ |
| 1911 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1912 | PSA_ASSERT( psa_hash_finish( &operation, |
| 1913 | hash, sizeof( hash ), &hash_len ) ); |
| 1914 | TEST_EQUAL( psa_hash_finish( &operation, |
| 1915 | hash, sizeof( hash ), &hash_len ), |
| 1916 | PSA_ERROR_BAD_STATE ); |
| 1917 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1918 | |
| 1919 | /* Call finish after calling verify. */ |
| 1920 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1921 | PSA_ASSERT( psa_hash_verify( &operation, |
| 1922 | valid_hash, sizeof( valid_hash ) ) ); |
| 1923 | TEST_EQUAL( psa_hash_finish( &operation, |
| 1924 | hash, sizeof( hash ), &hash_len ), |
| 1925 | PSA_ERROR_BAD_STATE ); |
| 1926 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1927 | |
| 1928 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1929 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1930 | } |
| 1931 | /* END_CASE */ |
| 1932 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 1933 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1934 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1935 | { |
| 1936 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1937 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 1938 | * appended to it */ |
| 1939 | unsigned char hash[] = { |
| 1940 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 1941 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 1942 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1943 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1944 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1945 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1946 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1947 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1948 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1949 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1950 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1951 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1952 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1953 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1954 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1955 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1956 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1957 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1958 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1959 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1960 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1961 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 1962 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1963 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1964 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1965 | } |
| 1966 | /* END_CASE */ |
| 1967 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 1968 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 1969 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1970 | { |
| 1971 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 1972 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1973 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1974 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1975 | size_t hash_len; |
| 1976 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1977 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1978 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1979 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1980 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1981 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1982 | hash, expected_size - 1, &hash_len ), |
| 1983 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1984 | |
| 1985 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1986 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1987 | } |
| 1988 | /* END_CASE */ |
| 1989 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 1990 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1991 | void hash_clone_source_state( ) |
| 1992 | { |
| 1993 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 1994 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 1995 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 1996 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 1997 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 1998 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 1999 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2000 | size_t hash_len; |
| 2001 | |
| 2002 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2003 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 2004 | |
| 2005 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2006 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2007 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2008 | hash, sizeof( hash ), &hash_len ) ); |
| 2009 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2010 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2011 | |
| 2012 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 2013 | PSA_ERROR_BAD_STATE ); |
| 2014 | |
| 2015 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 2016 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 2017 | hash, sizeof( hash ), &hash_len ) ); |
| 2018 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 2019 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2020 | hash, sizeof( hash ), &hash_len ) ); |
| 2021 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 2022 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 2023 | hash, sizeof( hash ), &hash_len ) ); |
| 2024 | |
| 2025 | exit: |
| 2026 | psa_hash_abort( &op_source ); |
| 2027 | psa_hash_abort( &op_init ); |
| 2028 | psa_hash_abort( &op_setup ); |
| 2029 | psa_hash_abort( &op_finished ); |
| 2030 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2031 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2032 | } |
| 2033 | /* END_CASE */ |
| 2034 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2035 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2036 | void hash_clone_target_state( ) |
| 2037 | { |
| 2038 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2039 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2040 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2041 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2042 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2043 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2044 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 2045 | size_t hash_len; |
| 2046 | |
| 2047 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2048 | |
| 2049 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2050 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2051 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2052 | hash, sizeof( hash ), &hash_len ) ); |
| 2053 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2054 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2055 | |
| 2056 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 2057 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 2058 | hash, sizeof( hash ), &hash_len ) ); |
| 2059 | |
| 2060 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 2061 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 2062 | PSA_ERROR_BAD_STATE ); |
| 2063 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 2064 | PSA_ERROR_BAD_STATE ); |
| 2065 | |
| 2066 | exit: |
| 2067 | psa_hash_abort( &op_target ); |
| 2068 | psa_hash_abort( &op_init ); |
| 2069 | psa_hash_abort( &op_setup ); |
| 2070 | psa_hash_abort( &op_finished ); |
| 2071 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2072 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2073 | } |
| 2074 | /* END_CASE */ |
| 2075 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2076 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2077 | void mac_operation_init( ) |
| 2078 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2079 | const uint8_t input[1] = { 0 }; |
| 2080 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2081 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2082 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2083 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2084 | * to supress the Clang warning for the test. */ |
| 2085 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 2086 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 2087 | psa_mac_operation_t zero; |
| 2088 | |
| 2089 | memset( &zero, 0, sizeof( zero ) ); |
| 2090 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2091 | /* A freshly-initialized MAC operation should not be usable. */ |
| 2092 | TEST_EQUAL( psa_mac_update( &func, |
| 2093 | input, sizeof( input ) ), |
| 2094 | PSA_ERROR_BAD_STATE ); |
| 2095 | TEST_EQUAL( psa_mac_update( &init, |
| 2096 | input, sizeof( input ) ), |
| 2097 | PSA_ERROR_BAD_STATE ); |
| 2098 | TEST_EQUAL( psa_mac_update( &zero, |
| 2099 | input, sizeof( input ) ), |
| 2100 | PSA_ERROR_BAD_STATE ); |
| 2101 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2102 | /* A default MAC operation should be abortable without error. */ |
| 2103 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 2104 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 2105 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2106 | } |
| 2107 | /* END_CASE */ |
| 2108 | |
| 2109 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2110 | void mac_setup( int key_type_arg, |
| 2111 | data_t *key, |
| 2112 | int alg_arg, |
| 2113 | int expected_status_arg ) |
| 2114 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2115 | psa_key_type_t key_type = key_type_arg; |
| 2116 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2117 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2118 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2119 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 2120 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2121 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2122 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2123 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2124 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2125 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2126 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 2127 | &operation, &status ) ) |
| 2128 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2129 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2130 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2131 | /* The operation object should be reusable. */ |
| 2132 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2133 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 2134 | smoke_test_key_data, |
| 2135 | sizeof( smoke_test_key_data ), |
| 2136 | KNOWN_SUPPORTED_MAC_ALG, |
| 2137 | &operation, &status ) ) |
| 2138 | goto exit; |
| 2139 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2140 | #endif |
| 2141 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2142 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2143 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2144 | } |
| 2145 | /* END_CASE */ |
| 2146 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2147 | /* 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] | 2148 | void mac_bad_order( ) |
| 2149 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2150 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2151 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 2152 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2153 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2154 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2155 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2156 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2157 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2158 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 2159 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 2160 | size_t sign_mac_length = 0; |
| 2161 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 2162 | const uint8_t verify_mac[] = { |
| 2163 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 2164 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 2165 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 2166 | |
| 2167 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2168 | 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] | 2169 | psa_set_key_algorithm( &attributes, alg ); |
| 2170 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2171 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2172 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 2173 | &key ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2174 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2175 | /* Call update without calling setup beforehand. */ |
| 2176 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2177 | PSA_ERROR_BAD_STATE ); |
| 2178 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2179 | |
| 2180 | /* Call sign finish without calling setup beforehand. */ |
| 2181 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 2182 | &sign_mac_length), |
| 2183 | PSA_ERROR_BAD_STATE ); |
| 2184 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2185 | |
| 2186 | /* Call verify finish without calling setup beforehand. */ |
| 2187 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2188 | verify_mac, sizeof( verify_mac ) ), |
| 2189 | PSA_ERROR_BAD_STATE ); |
| 2190 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2191 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2192 | /* Call setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2193 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
| 2194 | TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2195 | PSA_ERROR_BAD_STATE ); |
| 2196 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2197 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2198 | /* Call update after sign finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2199 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2200 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2201 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2202 | sign_mac, sizeof( sign_mac ), |
| 2203 | &sign_mac_length ) ); |
| 2204 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2205 | PSA_ERROR_BAD_STATE ); |
| 2206 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2207 | |
| 2208 | /* Call update after verify finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2209 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2210 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2211 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2212 | verify_mac, sizeof( verify_mac ) ) ); |
| 2213 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2214 | PSA_ERROR_BAD_STATE ); |
| 2215 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2216 | |
| 2217 | /* Call sign finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2218 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2219 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2220 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2221 | sign_mac, sizeof( sign_mac ), |
| 2222 | &sign_mac_length ) ); |
| 2223 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2224 | sign_mac, sizeof( sign_mac ), |
| 2225 | &sign_mac_length ), |
| 2226 | PSA_ERROR_BAD_STATE ); |
| 2227 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2228 | |
| 2229 | /* Call verify finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2230 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2231 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2232 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2233 | verify_mac, sizeof( verify_mac ) ) ); |
| 2234 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2235 | verify_mac, sizeof( verify_mac ) ), |
| 2236 | PSA_ERROR_BAD_STATE ); |
| 2237 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2238 | |
| 2239 | /* Setup sign but try verify. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2240 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2241 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2242 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2243 | verify_mac, sizeof( verify_mac ) ), |
| 2244 | PSA_ERROR_BAD_STATE ); |
| 2245 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2246 | |
| 2247 | /* Setup verify but try sign. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2248 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2249 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2250 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2251 | sign_mac, sizeof( sign_mac ), |
| 2252 | &sign_mac_length ), |
| 2253 | PSA_ERROR_BAD_STATE ); |
| 2254 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2255 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2256 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2257 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2258 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2259 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2260 | } |
| 2261 | /* END_CASE */ |
| 2262 | |
| 2263 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2264 | void mac_sign( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2265 | data_t *key_data, |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2266 | int alg_arg, |
| 2267 | data_t *input, |
| 2268 | data_t *expected_mac ) |
| 2269 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2270 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2271 | psa_key_type_t key_type = key_type_arg; |
| 2272 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2273 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2274 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2275 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2276 | size_t mac_buffer_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2277 | 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] | 2278 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2279 | const size_t output_sizes_to_test[] = { |
| 2280 | 0, |
| 2281 | 1, |
| 2282 | expected_mac->len - 1, |
| 2283 | expected_mac->len, |
| 2284 | expected_mac->len + 1, |
| 2285 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2286 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2287 | TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2288 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 3d404d6 | 2020-08-25 23:47:36 +0200 | [diff] [blame] | 2289 | TEST_ASSERT( expected_mac->len == mac_buffer_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2290 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2291 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2292 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2293 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2294 | psa_set_key_algorithm( &attributes, alg ); |
| 2295 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2296 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2297 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2298 | &key ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2299 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2300 | for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ ) |
| 2301 | { |
| 2302 | const size_t output_size = output_sizes_to_test[i]; |
| 2303 | psa_status_t expected_status = |
| 2304 | ( output_size >= expected_mac->len ? PSA_SUCCESS : |
| 2305 | PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2306 | |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2307 | mbedtls_test_set_step( output_size ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2308 | ASSERT_ALLOC( actual_mac, output_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2309 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2310 | /* Calculate the MAC. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2311 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2312 | PSA_ASSERT( psa_mac_update( &operation, |
| 2313 | input->x, input->len ) ); |
| 2314 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2315 | actual_mac, output_size, |
| 2316 | &mac_length ), |
| 2317 | expected_status ); |
| 2318 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2319 | |
| 2320 | if( expected_status == PSA_SUCCESS ) |
| 2321 | { |
| 2322 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 2323 | actual_mac, mac_length ); |
| 2324 | } |
| 2325 | mbedtls_free( actual_mac ); |
| 2326 | actual_mac = NULL; |
| 2327 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2328 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2329 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2330 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2331 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2332 | PSA_DONE( ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2333 | mbedtls_free( actual_mac ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2334 | } |
| 2335 | /* END_CASE */ |
| 2336 | |
| 2337 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2338 | void mac_verify( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2339 | data_t *key_data, |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2340 | int alg_arg, |
| 2341 | data_t *input, |
| 2342 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2343 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2344 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2345 | psa_key_type_t key_type = key_type_arg; |
| 2346 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2347 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2348 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2349 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2350 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 2351 | TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE ); |
| 2352 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2353 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2354 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2355 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2356 | psa_set_key_algorithm( &attributes, alg ); |
| 2357 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2358 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2359 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2360 | &key ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2361 | |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2362 | /* Test the correct MAC. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2363 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2364 | PSA_ASSERT( psa_mac_update( &operation, |
| 2365 | input->x, input->len ) ); |
| 2366 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2367 | expected_mac->x, |
| 2368 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2369 | |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2370 | /* Test a MAC that's too short. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2371 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2372 | PSA_ASSERT( psa_mac_update( &operation, |
| 2373 | input->x, input->len ) ); |
| 2374 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2375 | expected_mac->x, |
| 2376 | expected_mac->len - 1 ), |
| 2377 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2378 | |
| 2379 | /* Test a MAC that's too long. */ |
| 2380 | ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 ); |
| 2381 | memcpy( perturbed_mac, expected_mac->x, expected_mac->len ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2382 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2383 | PSA_ASSERT( psa_mac_update( &operation, |
| 2384 | input->x, input->len ) ); |
| 2385 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2386 | perturbed_mac, |
| 2387 | expected_mac->len + 1 ), |
| 2388 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2389 | |
| 2390 | /* Test changing one byte. */ |
| 2391 | for( size_t i = 0; i < expected_mac->len; i++ ) |
| 2392 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2393 | mbedtls_test_set_step( i ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2394 | perturbed_mac[i] ^= 1; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2395 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2396 | PSA_ASSERT( psa_mac_update( &operation, |
| 2397 | input->x, input->len ) ); |
| 2398 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2399 | perturbed_mac, |
| 2400 | expected_mac->len ), |
| 2401 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2402 | perturbed_mac[i] ^= 1; |
| 2403 | } |
| 2404 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2405 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2406 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2407 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2408 | PSA_DONE( ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2409 | mbedtls_free( perturbed_mac ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2410 | } |
| 2411 | /* END_CASE */ |
| 2412 | |
| 2413 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2414 | void cipher_operation_init( ) |
| 2415 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2416 | const uint8_t input[1] = { 0 }; |
| 2417 | unsigned char output[1] = { 0 }; |
| 2418 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2419 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2420 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2421 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2422 | * to supress the Clang warning for the test. */ |
| 2423 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 2424 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 2425 | psa_cipher_operation_t zero; |
| 2426 | |
| 2427 | memset( &zero, 0, sizeof( zero ) ); |
| 2428 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2429 | /* A freshly-initialized cipher operation should not be usable. */ |
| 2430 | TEST_EQUAL( psa_cipher_update( &func, |
| 2431 | input, sizeof( input ), |
| 2432 | output, sizeof( output ), |
| 2433 | &output_length ), |
| 2434 | PSA_ERROR_BAD_STATE ); |
| 2435 | TEST_EQUAL( psa_cipher_update( &init, |
| 2436 | input, sizeof( input ), |
| 2437 | output, sizeof( output ), |
| 2438 | &output_length ), |
| 2439 | PSA_ERROR_BAD_STATE ); |
| 2440 | TEST_EQUAL( psa_cipher_update( &zero, |
| 2441 | input, sizeof( input ), |
| 2442 | output, sizeof( output ), |
| 2443 | &output_length ), |
| 2444 | PSA_ERROR_BAD_STATE ); |
| 2445 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2446 | /* A default cipher operation should be abortable without error. */ |
| 2447 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 2448 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 2449 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2450 | } |
| 2451 | /* END_CASE */ |
| 2452 | |
| 2453 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2454 | void cipher_setup( int key_type_arg, |
| 2455 | data_t *key, |
| 2456 | int alg_arg, |
| 2457 | int expected_status_arg ) |
| 2458 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2459 | psa_key_type_t key_type = key_type_arg; |
| 2460 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2461 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2462 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2463 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 2464 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2465 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2466 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2467 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2468 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2469 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2470 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 2471 | &operation, &status ) ) |
| 2472 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2473 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2474 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2475 | /* The operation object should be reusable. */ |
| 2476 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 2477 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 2478 | smoke_test_key_data, |
| 2479 | sizeof( smoke_test_key_data ), |
| 2480 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 2481 | &operation, &status ) ) |
| 2482 | goto exit; |
| 2483 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2484 | #endif |
| 2485 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2486 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2487 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2488 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2489 | } |
| 2490 | /* END_CASE */ |
| 2491 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2492 | /* 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] | 2493 | void cipher_bad_order( ) |
| 2494 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2495 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2496 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 2497 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2498 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2499 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2500 | 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] | 2501 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2502 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2503 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 2504 | const uint8_t text[] = { |
| 2505 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 2506 | 0xbb, 0xbb, 0xbb, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2507 | 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] | 2508 | size_t length = 0; |
| 2509 | |
| 2510 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2511 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 2512 | psa_set_key_algorithm( &attributes, alg ); |
| 2513 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2514 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 2515 | &key ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2516 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2517 | /* Call encrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2518 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 2519 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2520 | PSA_ERROR_BAD_STATE ); |
| 2521 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2522 | |
| 2523 | /* Call decrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2524 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 2525 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2526 | PSA_ERROR_BAD_STATE ); |
| 2527 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2528 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2529 | /* Generate an IV without calling setup beforehand. */ |
| 2530 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2531 | buffer, sizeof( buffer ), |
| 2532 | &length ), |
| 2533 | PSA_ERROR_BAD_STATE ); |
| 2534 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2535 | |
| 2536 | /* Generate an IV twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2537 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2538 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 2539 | buffer, sizeof( buffer ), |
| 2540 | &length ) ); |
| 2541 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2542 | buffer, sizeof( buffer ), |
| 2543 | &length ), |
| 2544 | PSA_ERROR_BAD_STATE ); |
| 2545 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2546 | |
| 2547 | /* Generate an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2548 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2549 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2550 | iv, sizeof( iv ) ) ); |
| 2551 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2552 | buffer, sizeof( buffer ), |
| 2553 | &length ), |
| 2554 | PSA_ERROR_BAD_STATE ); |
| 2555 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2556 | |
| 2557 | /* Set an IV without calling setup beforehand. */ |
| 2558 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2559 | iv, sizeof( iv ) ), |
| 2560 | PSA_ERROR_BAD_STATE ); |
| 2561 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2562 | |
| 2563 | /* Set an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2564 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2565 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2566 | iv, sizeof( iv ) ) ); |
| 2567 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2568 | iv, sizeof( iv ) ), |
| 2569 | PSA_ERROR_BAD_STATE ); |
| 2570 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2571 | |
| 2572 | /* Set an IV after it's already generated. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2573 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2574 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 2575 | buffer, sizeof( buffer ), |
| 2576 | &length ) ); |
| 2577 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2578 | iv, sizeof( iv ) ), |
| 2579 | PSA_ERROR_BAD_STATE ); |
| 2580 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2581 | |
| 2582 | /* Call update without calling setup beforehand. */ |
| 2583 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2584 | text, sizeof( text ), |
| 2585 | buffer, sizeof( buffer ), |
| 2586 | &length ), |
| 2587 | PSA_ERROR_BAD_STATE ); |
| 2588 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2589 | |
| 2590 | /* Call update without an IV where an IV is required. */ |
| 2591 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2592 | text, sizeof( text ), |
| 2593 | buffer, sizeof( buffer ), |
| 2594 | &length ), |
| 2595 | PSA_ERROR_BAD_STATE ); |
| 2596 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2597 | |
| 2598 | /* Call update after finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2599 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2600 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2601 | iv, sizeof( iv ) ) ); |
| 2602 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2603 | buffer, sizeof( buffer ), &length ) ); |
| 2604 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2605 | text, sizeof( text ), |
| 2606 | buffer, sizeof( buffer ), |
| 2607 | &length ), |
| 2608 | PSA_ERROR_BAD_STATE ); |
| 2609 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2610 | |
| 2611 | /* Call finish without calling setup beforehand. */ |
| 2612 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2613 | buffer, sizeof( buffer ), &length ), |
| 2614 | PSA_ERROR_BAD_STATE ); |
| 2615 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2616 | |
| 2617 | /* Call finish without an IV where an IV is required. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2618 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2619 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 2620 | * for cipher modes with padding. */ |
| 2621 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2622 | buffer, sizeof( buffer ), &length ), |
| 2623 | PSA_ERROR_BAD_STATE ); |
| 2624 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2625 | |
| 2626 | /* Call finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2627 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2628 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2629 | iv, sizeof( iv ) ) ); |
| 2630 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2631 | buffer, sizeof( buffer ), &length ) ); |
| 2632 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2633 | buffer, sizeof( buffer ), &length ), |
| 2634 | PSA_ERROR_BAD_STATE ); |
| 2635 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2636 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2637 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2638 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2639 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2640 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2641 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2642 | } |
| 2643 | /* END_CASE */ |
| 2644 | |
| 2645 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2646 | void cipher_encrypt( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2647 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2648 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2649 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2650 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2651 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2652 | psa_status_t status; |
| 2653 | psa_key_type_t key_type = key_type_arg; |
| 2654 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2655 | psa_status_t expected_status = expected_status_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2656 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2657 | size_t output_buffer_size = 0; |
| 2658 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2659 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2660 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2661 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2662 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2663 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2664 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2665 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2666 | psa_set_key_algorithm( &attributes, alg ); |
| 2667 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2668 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2669 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2670 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2671 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2672 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2673 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2674 | if( iv->len > 0 ) |
| 2675 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 2676 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2677 | } |
| 2678 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2679 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 2680 | TEST_ASSERT( output_buffer_size <= |
| 2681 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2682 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2683 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2684 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2685 | input->x, input->len, |
| 2686 | output, output_buffer_size, |
| 2687 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2688 | TEST_ASSERT( function_output_length <= |
| 2689 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 2690 | TEST_ASSERT( function_output_length <= |
| 2691 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2692 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2693 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2694 | status = psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 2695 | ( output_buffer_size == 0 ? NULL : |
| 2696 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2697 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2698 | &function_output_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2699 | TEST_ASSERT( function_output_length <= |
| 2700 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 2701 | TEST_ASSERT( function_output_length <= |
| 2702 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2703 | total_output_length += function_output_length; |
| 2704 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2705 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2706 | if( expected_status == PSA_SUCCESS ) |
| 2707 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2708 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2709 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2710 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2711 | } |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2712 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2713 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2714 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2715 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2716 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2717 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2718 | } |
| 2719 | /* END_CASE */ |
| 2720 | |
| 2721 | /* BEGIN_CASE */ |
| 2722 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2723 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2724 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2725 | int first_part_size_arg, |
| 2726 | int output1_length_arg, int output2_length_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2727 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2728 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2729 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2730 | psa_key_type_t key_type = key_type_arg; |
| 2731 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2732 | size_t first_part_size = first_part_size_arg; |
| 2733 | size_t output1_length = output1_length_arg; |
| 2734 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2735 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2736 | size_t output_buffer_size = 0; |
| 2737 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2738 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2739 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2740 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2741 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2742 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2743 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2744 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2745 | psa_set_key_algorithm( &attributes, alg ); |
| 2746 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2747 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2748 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2749 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2750 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2751 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2752 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2753 | if( iv->len > 0 ) |
| 2754 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 2755 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2756 | } |
| 2757 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2758 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 2759 | TEST_ASSERT( output_buffer_size <= |
| 2760 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2761 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2762 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2763 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2764 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 2765 | output, output_buffer_size, |
| 2766 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2767 | TEST_ASSERT( function_output_length == output1_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2768 | TEST_ASSERT( function_output_length <= |
| 2769 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 2770 | TEST_ASSERT( function_output_length <= |
| 2771 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2772 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2773 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2774 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2775 | input->x + first_part_size, |
| 2776 | input->len - first_part_size, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 2777 | ( output_buffer_size == 0 ? NULL : |
| 2778 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2779 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2780 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2781 | TEST_ASSERT( function_output_length == output2_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2782 | TEST_ASSERT( function_output_length <= |
| 2783 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 2784 | alg, |
| 2785 | input->len - first_part_size ) ); |
| 2786 | TEST_ASSERT( function_output_length <= |
| 2787 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2788 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2789 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2790 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 2791 | ( output_buffer_size == 0 ? NULL : |
| 2792 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2793 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2794 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2795 | TEST_ASSERT( function_output_length <= |
| 2796 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 2797 | TEST_ASSERT( function_output_length <= |
| 2798 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2799 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2800 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2801 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2802 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2803 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2804 | |
| 2805 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2806 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2807 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2808 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2809 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2810 | } |
| 2811 | /* END_CASE */ |
| 2812 | |
| 2813 | /* BEGIN_CASE */ |
| 2814 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2815 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2816 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2817 | int first_part_size_arg, |
| 2818 | int output1_length_arg, int output2_length_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2819 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2820 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2821 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2822 | psa_key_type_t key_type = key_type_arg; |
| 2823 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2824 | size_t first_part_size = first_part_size_arg; |
| 2825 | size_t output1_length = output1_length_arg; |
| 2826 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2827 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2828 | size_t output_buffer_size = 0; |
| 2829 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2830 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2831 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2832 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2833 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2834 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2835 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2836 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 2837 | psa_set_key_algorithm( &attributes, alg ); |
| 2838 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2839 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2840 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2841 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2842 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2843 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2844 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 2845 | if( iv->len > 0 ) |
| 2846 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 2847 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2848 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2849 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2850 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 2851 | TEST_ASSERT( output_buffer_size <= |
| 2852 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2853 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2854 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2855 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2856 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2857 | input->x, first_part_size, |
| 2858 | output, output_buffer_size, |
| 2859 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2860 | TEST_ASSERT( function_output_length == output1_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2861 | TEST_ASSERT( function_output_length <= |
| 2862 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 2863 | TEST_ASSERT( function_output_length <= |
| 2864 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2865 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2866 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2867 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2868 | input->x + first_part_size, |
| 2869 | input->len - first_part_size, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 2870 | ( output_buffer_size == 0 ? NULL : |
| 2871 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2872 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2873 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2874 | TEST_ASSERT( function_output_length == output2_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2875 | TEST_ASSERT( function_output_length <= |
| 2876 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 2877 | alg, |
| 2878 | input->len - first_part_size ) ); |
| 2879 | TEST_ASSERT( function_output_length <= |
| 2880 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2881 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2882 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2883 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 2884 | ( output_buffer_size == 0 ? NULL : |
| 2885 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2886 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2887 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2888 | TEST_ASSERT( function_output_length <= |
| 2889 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 2890 | TEST_ASSERT( function_output_length <= |
| 2891 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2892 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2893 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2894 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2895 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2896 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2897 | |
| 2898 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2899 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2900 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2901 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2902 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2903 | } |
| 2904 | /* END_CASE */ |
| 2905 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2906 | /* BEGIN_CASE */ |
| 2907 | void cipher_decrypt( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2908 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2909 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2910 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2911 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2912 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2913 | psa_status_t status; |
| 2914 | psa_key_type_t key_type = key_type_arg; |
| 2915 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2916 | psa_status_t expected_status = expected_status_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2917 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2918 | size_t output_buffer_size = 0; |
| 2919 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2920 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2921 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2922 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2923 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2924 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2925 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2926 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 2927 | psa_set_key_algorithm( &attributes, alg ); |
| 2928 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2929 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2930 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2931 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2932 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2933 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2934 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 2935 | if( iv->len > 0 ) |
| 2936 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 2937 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2938 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2939 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2940 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 2941 | TEST_ASSERT( output_buffer_size <= |
| 2942 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2943 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2944 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2945 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2946 | input->x, input->len, |
| 2947 | output, output_buffer_size, |
| 2948 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2949 | TEST_ASSERT( function_output_length <= |
| 2950 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 2951 | TEST_ASSERT( function_output_length <= |
| 2952 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2953 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2954 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2955 | status = psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 2956 | ( output_buffer_size == 0 ? NULL : |
| 2957 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2958 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2959 | &function_output_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2960 | TEST_ASSERT( function_output_length <= |
| 2961 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 2962 | TEST_ASSERT( function_output_length <= |
| 2963 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2964 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2965 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2966 | |
| 2967 | if( expected_status == PSA_SUCCESS ) |
| 2968 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2969 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2970 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2971 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2972 | } |
| 2973 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2974 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2975 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2976 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2977 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2978 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2979 | } |
| 2980 | /* END_CASE */ |
| 2981 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2982 | /* BEGIN_CASE */ |
| 2983 | void cipher_verify_output( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2984 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2985 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2986 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2987 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2988 | psa_key_type_t key_type = key_type_arg; |
| 2989 | psa_algorithm_t alg = alg_arg; |
mohammad1603 | e6b67a1 | 2018-03-12 10:38:49 -0700 | [diff] [blame] | 2990 | unsigned char iv[16] = {0}; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2991 | size_t iv_size = 16; |
| 2992 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2993 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2994 | size_t output1_size = 0; |
| 2995 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2996 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2997 | size_t output2_size = 0; |
| 2998 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +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 operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3001 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3002 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3003 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3004 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3005 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3006 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3007 | psa_set_key_algorithm( &attributes, alg ); |
| 3008 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3009 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3010 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3011 | &key ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3012 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3013 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 3014 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3015 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3016 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3017 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3018 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3019 | iv, iv_size, |
| 3020 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3021 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3022 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3023 | TEST_ASSERT( output1_size <= |
| 3024 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3025 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3026 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3027 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len, |
| 3028 | output1, output1_size, |
| 3029 | &output1_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3030 | TEST_ASSERT( output1_length <= |
| 3031 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 3032 | TEST_ASSERT( output1_length <= |
| 3033 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
| 3034 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3035 | PSA_ASSERT( psa_cipher_finish( &operation1, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3036 | output1 + output1_length, |
| 3037 | output1_size - output1_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3038 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3039 | TEST_ASSERT( function_output_length <= |
| 3040 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3041 | TEST_ASSERT( function_output_length <= |
| 3042 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3043 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3044 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3045 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3046 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3047 | |
| 3048 | output2_size = output1_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3049 | TEST_ASSERT( output2_size <= |
| 3050 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 3051 | TEST_ASSERT( output2_size <= |
| 3052 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3053 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3054 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3055 | if( iv_length > 0 ) |
| 3056 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3057 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3058 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3059 | } |
| 3060 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3061 | PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length, |
| 3062 | output2, output2_size, |
| 3063 | &output2_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3064 | TEST_ASSERT( output2_length <= |
| 3065 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 3066 | TEST_ASSERT( output2_length <= |
| 3067 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) ); |
| 3068 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3069 | function_output_length = 0; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3070 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3071 | output2 + output2_length, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3072 | output2_size - output2_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3073 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3074 | TEST_ASSERT( function_output_length <= |
| 3075 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3076 | TEST_ASSERT( function_output_length <= |
| 3077 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3078 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3079 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3080 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3081 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3082 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3083 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3084 | |
| 3085 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3086 | psa_cipher_abort( &operation1 ); |
| 3087 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3088 | mbedtls_free( output1 ); |
| 3089 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3090 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3091 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3092 | } |
| 3093 | /* END_CASE */ |
| 3094 | |
| 3095 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3096 | void cipher_verify_output_multipart( int alg_arg, |
| 3097 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3098 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3099 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3100 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3101 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3102 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3103 | psa_key_type_t key_type = key_type_arg; |
| 3104 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3105 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3106 | unsigned char iv[16] = {0}; |
| 3107 | size_t iv_size = 16; |
| 3108 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3109 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3110 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3111 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3112 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3113 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3114 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3115 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3116 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3117 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3118 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3119 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3120 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3121 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3122 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3123 | psa_set_key_algorithm( &attributes, alg ); |
| 3124 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3125 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3126 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3127 | &key ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3128 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3129 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 3130 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3131 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3132 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3133 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3134 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3135 | iv, iv_size, |
| 3136 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3137 | } |
| 3138 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3139 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3140 | TEST_ASSERT( output1_buffer_size <= |
| 3141 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3142 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3143 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3144 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3145 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3146 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 3147 | output1, output1_buffer_size, |
| 3148 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3149 | TEST_ASSERT( function_output_length <= |
| 3150 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3151 | TEST_ASSERT( function_output_length <= |
| 3152 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3153 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3154 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3155 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 3156 | input->x + first_part_size, |
| 3157 | input->len - first_part_size, |
| 3158 | output1, output1_buffer_size, |
| 3159 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3160 | TEST_ASSERT( function_output_length <= |
| 3161 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3162 | alg, |
| 3163 | input->len - first_part_size ) ); |
| 3164 | TEST_ASSERT( function_output_length <= |
| 3165 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3166 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3167 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3168 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 3169 | output1 + output1_length, |
| 3170 | output1_buffer_size - output1_length, |
| 3171 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3172 | TEST_ASSERT( function_output_length <= |
| 3173 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3174 | TEST_ASSERT( function_output_length <= |
| 3175 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3176 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3177 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3178 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3179 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3180 | output2_buffer_size = output1_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3181 | TEST_ASSERT( output2_buffer_size <= |
| 3182 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 3183 | TEST_ASSERT( output2_buffer_size <= |
| 3184 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3185 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3186 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3187 | if( iv_length > 0 ) |
| 3188 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3189 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3190 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3191 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3192 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3193 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 3194 | output2, output2_buffer_size, |
| 3195 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3196 | TEST_ASSERT( function_output_length <= |
| 3197 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3198 | TEST_ASSERT( function_output_length <= |
| 3199 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3200 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3201 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3202 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 3203 | output1 + first_part_size, |
| 3204 | output1_length - first_part_size, |
| 3205 | output2, output2_buffer_size, |
| 3206 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3207 | TEST_ASSERT( function_output_length <= |
| 3208 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3209 | alg, |
| 3210 | output1_length - first_part_size ) ); |
| 3211 | TEST_ASSERT( function_output_length <= |
| 3212 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3213 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3214 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3215 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3216 | output2 + output2_length, |
| 3217 | output2_buffer_size - output2_length, |
| 3218 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3219 | TEST_ASSERT( function_output_length <= |
| 3220 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3221 | TEST_ASSERT( function_output_length <= |
| 3222 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3223 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3224 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3225 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3226 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3227 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3228 | |
| 3229 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3230 | psa_cipher_abort( &operation1 ); |
| 3231 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3232 | mbedtls_free( output1 ); |
| 3233 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3234 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3235 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3236 | } |
| 3237 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 3238 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3239 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3240 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3241 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3242 | data_t *nonce, |
| 3243 | data_t *additional_data, |
| 3244 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3245 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3246 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3247 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3248 | psa_key_type_t key_type = key_type_arg; |
| 3249 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3250 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3251 | unsigned char *output_data = NULL; |
| 3252 | size_t output_size = 0; |
| 3253 | size_t output_length = 0; |
| 3254 | unsigned char *output_data2 = NULL; |
| 3255 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 3256 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3257 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3258 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3259 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3260 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3261 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3262 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3263 | psa_set_key_algorithm( &attributes, alg ); |
| 3264 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3265 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3266 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3267 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3268 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3269 | key_bits = psa_get_key_bits( &attributes ); |
| 3270 | |
| 3271 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3272 | alg ); |
| 3273 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3274 | * should be exact. */ |
| 3275 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 3276 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 3277 | { |
| 3278 | TEST_EQUAL( output_size, |
| 3279 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3280 | TEST_ASSERT( output_size <= |
| 3281 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3282 | } |
| 3283 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3284 | |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 3285 | status = psa_aead_encrypt( key, alg, |
| 3286 | nonce->x, nonce->len, |
| 3287 | additional_data->x, |
| 3288 | additional_data->len, |
| 3289 | input_data->x, input_data->len, |
| 3290 | output_data, output_size, |
| 3291 | &output_length ); |
| 3292 | |
| 3293 | /* If the operation is not supported, just skip and not fail in case the |
| 3294 | * encryption involves a common limitation of cryptography hardwares and |
| 3295 | * an alternative implementation. */ |
| 3296 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 3297 | { |
| 3298 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3299 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 3300 | } |
| 3301 | |
| 3302 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3303 | |
| 3304 | if( PSA_SUCCESS == expected_result ) |
| 3305 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3306 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3307 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3308 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3309 | * should be exact. */ |
| 3310 | TEST_EQUAL( input_data->len, |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3311 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) ); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3312 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3313 | TEST_ASSERT( input_data->len <= |
| 3314 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) ); |
| 3315 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3316 | TEST_EQUAL( psa_aead_decrypt( key, alg, |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3317 | nonce->x, nonce->len, |
| 3318 | additional_data->x, |
| 3319 | additional_data->len, |
| 3320 | output_data, output_length, |
| 3321 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3322 | &output_length2 ), |
| 3323 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3324 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3325 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 3326 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3327 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3328 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3329 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3330 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3331 | mbedtls_free( output_data ); |
| 3332 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3333 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3334 | } |
| 3335 | /* END_CASE */ |
| 3336 | |
| 3337 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3338 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 3339 | int alg_arg, |
| 3340 | data_t *nonce, |
| 3341 | data_t *additional_data, |
| 3342 | data_t *input_data, |
| 3343 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3344 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3345 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3346 | psa_key_type_t key_type = key_type_arg; |
| 3347 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3348 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3349 | unsigned char *output_data = NULL; |
| 3350 | size_t output_size = 0; |
| 3351 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3352 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3353 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3354 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3355 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3356 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3357 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3358 | psa_set_key_algorithm( &attributes, alg ); |
| 3359 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3360 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3361 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3362 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3363 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3364 | key_bits = psa_get_key_bits( &attributes ); |
| 3365 | |
| 3366 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3367 | alg ); |
| 3368 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3369 | * should be exact. */ |
| 3370 | TEST_EQUAL( output_size, |
| 3371 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3372 | TEST_ASSERT( output_size <= |
| 3373 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3374 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3375 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3376 | status = psa_aead_encrypt( key, alg, |
| 3377 | nonce->x, nonce->len, |
| 3378 | additional_data->x, additional_data->len, |
| 3379 | input_data->x, input_data->len, |
| 3380 | output_data, output_size, |
| 3381 | &output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3382 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3383 | /* If the operation is not supported, just skip and not fail in case the |
| 3384 | * encryption involves a common limitation of cryptography hardwares and |
| 3385 | * an alternative implementation. */ |
| 3386 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3387 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3388 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3389 | 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] | 3390 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3391 | |
| 3392 | PSA_ASSERT( status ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3393 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 3394 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3395 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3396 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3397 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3398 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3399 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3400 | } |
| 3401 | /* END_CASE */ |
| 3402 | |
| 3403 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3404 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 3405 | int alg_arg, |
| 3406 | data_t *nonce, |
| 3407 | data_t *additional_data, |
| 3408 | data_t *input_data, |
| 3409 | data_t *expected_data, |
| 3410 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3411 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3412 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3413 | psa_key_type_t key_type = key_type_arg; |
| 3414 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3415 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3416 | unsigned char *output_data = NULL; |
| 3417 | size_t output_size = 0; |
| 3418 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3419 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3420 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3421 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3422 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3423 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3424 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3425 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3426 | psa_set_key_algorithm( &attributes, alg ); |
| 3427 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3428 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3429 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3430 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3431 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3432 | key_bits = psa_get_key_bits( &attributes ); |
| 3433 | |
| 3434 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3435 | alg ); |
| 3436 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 3437 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 3438 | { |
| 3439 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3440 | * should be exact. */ |
| 3441 | TEST_EQUAL( output_size, |
| 3442 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3443 | TEST_ASSERT( output_size <= |
| 3444 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3445 | } |
| 3446 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3447 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3448 | status = psa_aead_decrypt( key, alg, |
| 3449 | nonce->x, nonce->len, |
| 3450 | additional_data->x, |
| 3451 | additional_data->len, |
| 3452 | input_data->x, input_data->len, |
| 3453 | output_data, output_size, |
| 3454 | &output_length ); |
| 3455 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3456 | /* If the operation is not supported, just skip and not fail in case the |
| 3457 | * decryption involves a common limitation of cryptography hardwares and |
| 3458 | * an alternative implementation. */ |
| 3459 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3460 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3461 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3462 | 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] | 3463 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3464 | |
| 3465 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3466 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3467 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3468 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 3469 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3470 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3471 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3472 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3473 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3474 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3475 | } |
| 3476 | /* END_CASE */ |
| 3477 | |
| 3478 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3479 | void aead_multipart_encrypt( int key_type_arg, data_t *key_data, |
| 3480 | int alg_arg, |
| 3481 | data_t *nonce, |
| 3482 | data_t *additional_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3483 | int do_test_ad_chunked, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3484 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3485 | int do_test_data_chunked, |
| 3486 | int do_set_lengths, |
| 3487 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3488 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3489 | size_t ad_part_len = 0; |
| 3490 | size_t data_part_len = 0; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3491 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3492 | TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked ); |
| 3493 | |
| 3494 | /* Temporary whilst we have algorithms that cannot support chunking */ |
| 3495 | if( do_test_ad_chunked == 1 ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3496 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3497 | for( ad_part_len = 1; ad_part_len <= additional_data->len; |
| 3498 | ad_part_len++ ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3499 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3500 | mbedtls_test_set_step( ad_part_len ); |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3501 | |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 3502 | /* Split ad into length(ad_part_len) parts. */ |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3503 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3504 | alg_arg, nonce, |
| 3505 | additional_data, |
| 3506 | ad_part_len, |
| 3507 | input_data, -1, |
| 3508 | do_set_lengths, |
| 3509 | expected_output, |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 3510 | 1, 1, 0, |
| 3511 | ( ad_part_len & 0x01 ) ) ) |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 3512 | break; |
| 3513 | |
| 3514 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 3515 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 3516 | |
| 3517 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3518 | alg_arg, nonce, |
| 3519 | additional_data, |
| 3520 | ad_part_len, |
| 3521 | input_data, -1, |
| 3522 | do_set_lengths, |
| 3523 | expected_output, |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 3524 | 1, 1, 1, |
| 3525 | ( ad_part_len & 0x01 ) ) ) |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3526 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3527 | } |
| 3528 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3529 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3530 | /* Temporary whilst we have algorithms that cannot support chunking */ |
| 3531 | if( do_test_data_chunked == 1 ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3532 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3533 | for( data_part_len = 1; data_part_len <= input_data->len; |
| 3534 | data_part_len++ ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3535 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 3536 | /* Split data into length(data_part_len) parts. */ |
| 3537 | mbedtls_test_set_step( 2000 + data_part_len ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3538 | |
| 3539 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3540 | alg_arg, nonce, |
| 3541 | additional_data, -1, |
| 3542 | input_data, data_part_len, |
| 3543 | do_set_lengths, |
| 3544 | expected_output, |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 3545 | 1, 1, 0, |
| 3546 | ( data_part_len & 0x01 ) ) ) |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 3547 | break; |
| 3548 | |
| 3549 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 3550 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 3551 | |
| 3552 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3553 | alg_arg, nonce, |
| 3554 | additional_data, -1, |
| 3555 | input_data, data_part_len, |
| 3556 | do_set_lengths, |
| 3557 | expected_output, |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 3558 | 1, 1, 1, |
| 3559 | ( data_part_len & 0x01 ) ) ) |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3560 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3561 | } |
| 3562 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3563 | |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3564 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 3565 | /* Goto is required to silence warnings about unused labels, as we |
| 3566 | * don't actually do any test assertions in this function. */ |
| 3567 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3568 | } |
| 3569 | /* END_CASE */ |
| 3570 | |
| 3571 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3572 | void aead_multipart_decrypt( int key_type_arg, data_t *key_data, |
| 3573 | int alg_arg, |
| 3574 | data_t *nonce, |
| 3575 | data_t *additional_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3576 | int do_test_ad_chunked, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3577 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3578 | int do_test_data_chunked, |
| 3579 | int do_set_lengths, |
| 3580 | data_t *expected_output, |
| 3581 | int expect_valid_signature ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3582 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3583 | size_t ad_part_len = 0; |
| 3584 | size_t data_part_len = 0; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3585 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3586 | /* Temporary whilst we have algorithms that cannot support chunking */ |
| 3587 | if( do_test_ad_chunked == 1 ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3588 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3589 | for( ad_part_len = 1; ad_part_len <= additional_data->len; |
| 3590 | ad_part_len++ ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3591 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 3592 | /* Split ad into length(ad_part_len) parts. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3593 | mbedtls_test_set_step( ad_part_len ); |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3594 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3595 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3596 | alg_arg, nonce, |
| 3597 | additional_data, |
| 3598 | ad_part_len, |
| 3599 | input_data, -1, |
| 3600 | do_set_lengths, |
| 3601 | expected_output, |
| 3602 | expect_valid_signature, |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 3603 | 0, 0, |
| 3604 | ( ad_part_len & 0x01 ) ) ) |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 3605 | break; |
| 3606 | |
| 3607 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 3608 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 3609 | |
| 3610 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3611 | alg_arg, nonce, |
| 3612 | additional_data, |
| 3613 | ad_part_len, |
| 3614 | input_data, -1, |
| 3615 | do_set_lengths, |
| 3616 | expected_output, |
| 3617 | expect_valid_signature, |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 3618 | 0, 1, |
| 3619 | ( ad_part_len & 0x01 ) ) ) |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3620 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3621 | } |
| 3622 | } |
| 3623 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3624 | /* Temporary whilst we have algorithms that cannot support chunking */ |
| 3625 | if( do_test_data_chunked == 1 ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3626 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3627 | for( data_part_len = 1; data_part_len <= input_data->len; |
| 3628 | data_part_len++ ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3629 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 3630 | /* Split data into length(data_part_len) parts. */ |
| 3631 | mbedtls_test_set_step( 2000 + data_part_len ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3632 | |
| 3633 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3634 | alg_arg, nonce, |
| 3635 | additional_data, -1, |
| 3636 | input_data, data_part_len, |
| 3637 | do_set_lengths, |
| 3638 | expected_output, |
| 3639 | expect_valid_signature, |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 3640 | 0, 0, |
| 3641 | ( data_part_len & 0x01 ) ) ) |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 3642 | break; |
| 3643 | |
| 3644 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 3645 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 3646 | |
| 3647 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3648 | alg_arg, nonce, |
| 3649 | additional_data, -1, |
| 3650 | input_data, data_part_len, |
| 3651 | do_set_lengths, |
| 3652 | expected_output, |
| 3653 | expect_valid_signature, |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 3654 | 0, 1, |
| 3655 | ( data_part_len & 0x01 ) ) ) |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3656 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3657 | } |
| 3658 | } |
| 3659 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 3660 | /* Goto is required to silence warnings about unused labels, as we |
| 3661 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3662 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3663 | } |
| 3664 | /* END_CASE */ |
| 3665 | |
| 3666 | /* BEGIN_CASE */ |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3667 | void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data, |
| 3668 | int alg_arg, |
| 3669 | int nonce_len, |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 3670 | int expected_generated_len_arg, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 3671 | data_t *additional_data, |
| 3672 | data_t *input_data, |
| 3673 | int expected_status_arg ) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3674 | { |
| 3675 | |
| 3676 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3677 | psa_key_type_t key_type = key_type_arg; |
| 3678 | psa_algorithm_t alg = alg_arg; |
| 3679 | psa_aead_operation_t operation; |
| 3680 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 3681 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3682 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3683 | size_t nonce_generated_len = 0; |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 3684 | size_t expected_generated_len = expected_generated_len_arg; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 3685 | unsigned char *output_data = NULL; |
| 3686 | unsigned char *final_data = NULL; |
| 3687 | size_t output_size = 0; |
| 3688 | size_t finish_output_size = 0; |
| 3689 | size_t output_length = 0; |
| 3690 | size_t tag_length = 0; |
| 3691 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3692 | |
| 3693 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3694 | |
| 3695 | psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3696 | psa_set_key_algorithm( & attributes, alg ); |
| 3697 | psa_set_key_type( & attributes, key_type ); |
| 3698 | |
| 3699 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3700 | &key ) ); |
| 3701 | |
| 3702 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3703 | |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 3704 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 3705 | |
| 3706 | ASSERT_ALLOC( output_data, output_size ); |
| 3707 | |
| 3708 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 3709 | |
| 3710 | TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
| 3711 | |
| 3712 | ASSERT_ALLOC( final_data, finish_output_size ); |
| 3713 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3714 | operation = psa_aead_operation_init( ); |
| 3715 | |
| 3716 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 3717 | |
| 3718 | /* If the operation is not supported, just skip and not fail in case the |
| 3719 | * encryption involves a common limitation of cryptography hardwares and |
| 3720 | * an alternative implementation. */ |
| 3721 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 3722 | { |
| 3723 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3724 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_len ); |
| 3725 | } |
| 3726 | |
| 3727 | PSA_ASSERT( status ); |
| 3728 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3729 | status = psa_aead_generate_nonce( &operation, nonce_buffer, |
| 3730 | nonce_len, |
| 3731 | &nonce_generated_len ); |
| 3732 | |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 3733 | TEST_ASSERT( status == expected_status_arg ); |
| 3734 | |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 3735 | TEST_EQUAL( nonce_generated_len, expected_generated_len ); |
| 3736 | |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 3737 | TEST_ASSERT( nonce_generated_len < PSA_AEAD_NONCE_MAX_SIZE ); |
| 3738 | |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 3739 | if( expected_status_arg == PSA_SUCCESS ) |
| 3740 | { |
| 3741 | |
| 3742 | /* Ensure we can still complete operation. */ |
| 3743 | |
| 3744 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 3745 | additional_data->len ) ); |
| 3746 | |
| 3747 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
| 3748 | output_data, output_size, &output_length ) ); |
| 3749 | |
| 3750 | PSA_ASSERT( psa_aead_finish( &operation, final_data, finish_output_size, |
| 3751 | &output_length, tag_buffer, |
| 3752 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 3753 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3754 | |
| 3755 | exit: |
| 3756 | psa_destroy_key( key ); |
Paul Elliott | 16906f9 | 2021-06-24 09:57:01 +0100 | [diff] [blame] | 3757 | mbedtls_free( output_data ); |
| 3758 | mbedtls_free( final_data ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3759 | psa_aead_abort( &operation ); |
| 3760 | PSA_DONE( ); |
| 3761 | } |
| 3762 | /* END_CASE */ |
| 3763 | |
| 3764 | /* BEGIN_CASE */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 3765 | void aead_multipart_state_test( int key_type_arg, data_t *key_data, |
| 3766 | int alg_arg, |
| 3767 | data_t *nonce, |
| 3768 | data_t *additional_data, |
| 3769 | data_t *input_data ) |
| 3770 | { |
| 3771 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3772 | psa_key_type_t key_type = key_type_arg; |
| 3773 | psa_algorithm_t alg = alg_arg; |
| 3774 | psa_aead_operation_t operation; |
| 3775 | unsigned char *output_data = NULL; |
| 3776 | unsigned char *final_data = NULL; |
| 3777 | size_t output_size = 0; |
| 3778 | size_t finish_output_size = 0; |
| 3779 | size_t output_length = 0; |
| 3780 | size_t key_bits = 0; |
| 3781 | size_t tag_length = 0; |
| 3782 | size_t tag_size = 0; |
| 3783 | size_t nonce_length = 0; |
| 3784 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 3785 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 3786 | size_t output_part_length = 0; |
| 3787 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3788 | |
| 3789 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3790 | |
| 3791 | psa_set_key_usage_flags( & attributes, |
| 3792 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3793 | psa_set_key_algorithm( & attributes, alg ); |
| 3794 | psa_set_key_type( & attributes, key_type ); |
| 3795 | |
| 3796 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3797 | &key ) ); |
| 3798 | |
| 3799 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3800 | key_bits = psa_get_key_bits( &attributes ); |
| 3801 | |
| 3802 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 3803 | |
| 3804 | TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE ); |
| 3805 | |
| 3806 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 3807 | |
| 3808 | ASSERT_ALLOC( output_data, output_size ); |
| 3809 | |
| 3810 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 3811 | |
| 3812 | TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
| 3813 | |
| 3814 | ASSERT_ALLOC( final_data, finish_output_size ); |
| 3815 | |
| 3816 | /* Test all operations error without calling setup first. */ |
| 3817 | |
| 3818 | operation = psa_aead_operation_init( ); |
| 3819 | |
| 3820 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 3821 | PSA_ERROR_BAD_STATE ); |
| 3822 | |
| 3823 | psa_aead_abort( &operation ); |
| 3824 | |
| 3825 | operation = psa_aead_operation_init( ); |
| 3826 | |
| 3827 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 3828 | PSA_AEAD_NONCE_MAX_SIZE, |
| 3829 | &nonce_length ), |
| 3830 | PSA_ERROR_BAD_STATE ); |
| 3831 | |
| 3832 | psa_aead_abort( &operation ); |
| 3833 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 3834 | /* ------------------------------------------------------- */ |
| 3835 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 3836 | operation = psa_aead_operation_init( ); |
| 3837 | |
| 3838 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 3839 | input_data->len ), |
| 3840 | PSA_ERROR_BAD_STATE ); |
| 3841 | |
| 3842 | psa_aead_abort( &operation ); |
| 3843 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 3844 | /* ------------------------------------------------------- */ |
| 3845 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 3846 | operation = psa_aead_operation_init( ); |
| 3847 | |
| 3848 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 3849 | additional_data->len ), |
| 3850 | PSA_ERROR_BAD_STATE ); |
| 3851 | |
| 3852 | psa_aead_abort( &operation ); |
| 3853 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 3854 | /* ------------------------------------------------------- */ |
| 3855 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 3856 | operation = psa_aead_operation_init( ); |
| 3857 | |
| 3858 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 3859 | input_data->len, output_data, |
| 3860 | output_size, &output_length ), |
| 3861 | PSA_ERROR_BAD_STATE ); |
| 3862 | |
| 3863 | psa_aead_abort( &operation ); |
| 3864 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 3865 | /* ------------------------------------------------------- */ |
| 3866 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 3867 | operation = psa_aead_operation_init( ); |
| 3868 | |
| 3869 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 3870 | finish_output_size, |
| 3871 | &output_part_length, |
| 3872 | tag_buffer, tag_length, |
| 3873 | &tag_size ), |
| 3874 | PSA_ERROR_BAD_STATE ); |
| 3875 | |
| 3876 | psa_aead_abort( &operation ); |
| 3877 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 3878 | /* ------------------------------------------------------- */ |
| 3879 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 3880 | operation = psa_aead_operation_init( ); |
| 3881 | |
| 3882 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 3883 | finish_output_size, |
| 3884 | &output_part_length, |
| 3885 | tag_buffer, |
| 3886 | tag_length ), |
| 3887 | PSA_ERROR_BAD_STATE ); |
| 3888 | |
| 3889 | psa_aead_abort( &operation ); |
| 3890 | |
| 3891 | /* Test for double setups. */ |
| 3892 | |
| 3893 | operation = psa_aead_operation_init( ); |
| 3894 | |
| 3895 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 3896 | |
| 3897 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 3898 | PSA_ERROR_BAD_STATE ); |
| 3899 | |
| 3900 | psa_aead_abort( &operation ); |
| 3901 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 3902 | /* ------------------------------------------------------- */ |
| 3903 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 3904 | operation = psa_aead_operation_init( ); |
| 3905 | |
| 3906 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 3907 | |
| 3908 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 3909 | PSA_ERROR_BAD_STATE ); |
| 3910 | |
| 3911 | psa_aead_abort( &operation ); |
| 3912 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 3913 | /* ------------------------------------------------------- */ |
| 3914 | |
| 3915 | operation = psa_aead_operation_init( ); |
| 3916 | |
| 3917 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 3918 | |
| 3919 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 3920 | PSA_ERROR_BAD_STATE ); |
| 3921 | |
| 3922 | psa_aead_abort( &operation ); |
| 3923 | |
| 3924 | /* ------------------------------------------------------- */ |
| 3925 | |
| 3926 | operation = psa_aead_operation_init( ); |
| 3927 | |
| 3928 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 3929 | |
| 3930 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 3931 | PSA_ERROR_BAD_STATE ); |
| 3932 | |
| 3933 | psa_aead_abort( &operation ); |
| 3934 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 3935 | /* Test for not setting a nonce. */ |
| 3936 | |
| 3937 | operation = psa_aead_operation_init( ); |
| 3938 | |
| 3939 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 3940 | |
| 3941 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 3942 | additional_data->len ), |
| 3943 | PSA_ERROR_BAD_STATE ); |
| 3944 | |
| 3945 | psa_aead_abort( &operation ); |
| 3946 | |
| 3947 | /* Test for double setting nonce. */ |
| 3948 | |
| 3949 | operation = psa_aead_operation_init( ); |
| 3950 | |
| 3951 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 3952 | |
| 3953 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 3954 | |
| 3955 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 3956 | PSA_ERROR_BAD_STATE ); |
| 3957 | |
| 3958 | psa_aead_abort( &operation ); |
| 3959 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 3960 | /* Test for double generating nonce. */ |
| 3961 | |
| 3962 | operation = psa_aead_operation_init( ); |
| 3963 | |
| 3964 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 3965 | |
| 3966 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 3967 | PSA_AEAD_NONCE_MAX_SIZE, |
| 3968 | &nonce_length ) ); |
| 3969 | |
| 3970 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 3971 | PSA_AEAD_NONCE_MAX_SIZE, |
| 3972 | &nonce_length ), |
| 3973 | PSA_ERROR_BAD_STATE ); |
| 3974 | |
| 3975 | |
| 3976 | psa_aead_abort( &operation ); |
| 3977 | |
| 3978 | /* Test for generate nonce then set and vice versa */ |
| 3979 | |
| 3980 | operation = psa_aead_operation_init( ); |
| 3981 | |
| 3982 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 3983 | |
| 3984 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 3985 | PSA_AEAD_NONCE_MAX_SIZE, |
| 3986 | &nonce_length ) ); |
| 3987 | |
| 3988 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 3989 | PSA_ERROR_BAD_STATE ); |
| 3990 | |
| 3991 | psa_aead_abort( &operation ); |
| 3992 | |
| 3993 | /* ------------------------------------------------------- */ |
| 3994 | |
| 3995 | operation = psa_aead_operation_init( ); |
| 3996 | |
| 3997 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 3998 | |
| 3999 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4000 | |
| 4001 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4002 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4003 | &nonce_length ), |
| 4004 | PSA_ERROR_BAD_STATE ); |
| 4005 | |
| 4006 | psa_aead_abort( &operation ); |
| 4007 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 4008 | /* Test for generating nonce in decrypt setup. */ |
| 4009 | |
| 4010 | operation = psa_aead_operation_init( ); |
| 4011 | |
| 4012 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 4013 | |
| 4014 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4015 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4016 | &nonce_length ), |
| 4017 | PSA_ERROR_BAD_STATE ); |
| 4018 | |
| 4019 | psa_aead_abort( &operation ); |
| 4020 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4021 | /* Test for setting lengths twice. */ |
| 4022 | |
| 4023 | operation = psa_aead_operation_init( ); |
| 4024 | |
| 4025 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4026 | |
| 4027 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4028 | |
| 4029 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4030 | input_data->len ) ); |
| 4031 | |
| 4032 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 4033 | input_data->len ), |
| 4034 | PSA_ERROR_BAD_STATE ); |
| 4035 | |
| 4036 | psa_aead_abort( &operation ); |
| 4037 | |
| 4038 | /* Test for setting lengths after already starting data. */ |
| 4039 | |
| 4040 | operation = psa_aead_operation_init( ); |
| 4041 | |
| 4042 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4043 | |
| 4044 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4045 | |
| 4046 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 4047 | input_data->len, output_data, |
| 4048 | output_size, &output_length ) ); |
| 4049 | |
| 4050 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 4051 | input_data->len ), |
| 4052 | PSA_ERROR_BAD_STATE ); |
| 4053 | |
| 4054 | psa_aead_abort( &operation ); |
| 4055 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 4056 | /* Test for not sending any additional data or data after setting non zero |
| 4057 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4058 | |
| 4059 | operation = psa_aead_operation_init( ); |
| 4060 | |
| 4061 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4062 | |
| 4063 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4064 | |
| 4065 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4066 | input_data->len ) ); |
| 4067 | |
| 4068 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 4069 | finish_output_size, |
| 4070 | &output_part_length, |
| 4071 | tag_buffer, tag_length, |
| 4072 | &tag_size ), |
| 4073 | PSA_ERROR_INVALID_ARGUMENT ); |
| 4074 | |
| 4075 | psa_aead_abort( &operation ); |
| 4076 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 4077 | /* Test for not sending any additional data or data after setting non-zero |
| 4078 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4079 | |
| 4080 | operation = psa_aead_operation_init( ); |
| 4081 | |
| 4082 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 4083 | |
| 4084 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4085 | |
| 4086 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4087 | input_data->len ) ); |
| 4088 | |
| 4089 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 4090 | finish_output_size, |
| 4091 | &output_part_length, |
| 4092 | tag_buffer, |
| 4093 | tag_length ), |
| 4094 | PSA_ERROR_INVALID_ARGUMENT ); |
| 4095 | |
| 4096 | psa_aead_abort( &operation ); |
| 4097 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 4098 | /* Test for not sending any additional data after setting a non-zero length |
| 4099 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4100 | |
| 4101 | operation = psa_aead_operation_init( ); |
| 4102 | |
| 4103 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4104 | |
| 4105 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4106 | |
| 4107 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4108 | input_data->len ) ); |
| 4109 | |
| 4110 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 4111 | input_data->len, output_data, |
| 4112 | output_size, &output_length ), |
| 4113 | PSA_ERROR_INVALID_ARGUMENT ); |
| 4114 | |
| 4115 | psa_aead_abort( &operation ); |
| 4116 | |
| 4117 | /* Test sending additional data after data. */ |
| 4118 | |
| 4119 | operation = psa_aead_operation_init( ); |
| 4120 | |
| 4121 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4122 | |
| 4123 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4124 | |
| 4125 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 4126 | input_data->len, output_data, |
| 4127 | output_size, &output_length ) ); |
| 4128 | |
| 4129 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 4130 | additional_data->len ), |
| 4131 | PSA_ERROR_BAD_STATE ); |
| 4132 | |
| 4133 | psa_aead_abort( &operation ); |
| 4134 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 4135 | /* Test calling finish on decryption. */ |
| 4136 | |
| 4137 | operation = psa_aead_operation_init( ); |
| 4138 | |
| 4139 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 4140 | |
| 4141 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4142 | |
| 4143 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 4144 | finish_output_size, |
| 4145 | &output_part_length, |
| 4146 | tag_buffer, tag_length, |
| 4147 | &tag_size ), |
| 4148 | PSA_ERROR_BAD_STATE ); |
| 4149 | |
| 4150 | psa_aead_abort( &operation ); |
| 4151 | |
| 4152 | /* Test calling verify on encryption. */ |
| 4153 | |
| 4154 | operation = psa_aead_operation_init( ); |
| 4155 | |
| 4156 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4157 | |
| 4158 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4159 | |
| 4160 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 4161 | finish_output_size, |
| 4162 | &output_part_length, |
| 4163 | tag_buffer, |
| 4164 | tag_length ), |
Paul Elliott | 5b065cb | 2021-06-23 08:33:22 +0100 | [diff] [blame] | 4165 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 4166 | |
| 4167 | psa_aead_abort( &operation ); |
| 4168 | |
| 4169 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4170 | exit: |
| 4171 | psa_destroy_key( key ); |
| 4172 | psa_aead_abort( &operation ); |
| 4173 | mbedtls_free( output_data ); |
| 4174 | mbedtls_free( final_data ); |
| 4175 | PSA_DONE( ); |
| 4176 | } |
| 4177 | /* END_CASE */ |
| 4178 | |
| 4179 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4180 | void signature_size( int type_arg, |
| 4181 | int bits, |
| 4182 | int alg_arg, |
| 4183 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4184 | { |
| 4185 | psa_key_type_t type = type_arg; |
| 4186 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4187 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 4188 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4189 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 4190 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4191 | exit: |
| 4192 | ; |
| 4193 | } |
| 4194 | /* END_CASE */ |
| 4195 | |
| 4196 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 4197 | void sign_hash_deterministic( int key_type_arg, data_t *key_data, |
| 4198 | int alg_arg, data_t *input_data, |
| 4199 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4200 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4201 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4202 | psa_key_type_t key_type = key_type_arg; |
| 4203 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4204 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4205 | unsigned char *signature = NULL; |
| 4206 | size_t signature_size; |
| 4207 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4208 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4209 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4210 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4211 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4212 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4213 | psa_set_key_algorithm( &attributes, alg ); |
| 4214 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 4215 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4216 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4217 | &key ) ); |
| 4218 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4219 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4220 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4221 | /* Allocate a buffer which has the size advertized by the |
| 4222 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4223 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4224 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4225 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4226 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4227 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4228 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4229 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4230 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4231 | input_data->x, input_data->len, |
| 4232 | signature, signature_size, |
| 4233 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4234 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4235 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 4236 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4237 | |
| 4238 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4239 | /* |
| 4240 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4241 | * thus reset them as required. |
| 4242 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4243 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4244 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4245 | psa_destroy_key( key ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 4246 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4247 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4248 | } |
| 4249 | /* END_CASE */ |
| 4250 | |
| 4251 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 4252 | void sign_hash_fail( int key_type_arg, data_t *key_data, |
| 4253 | int alg_arg, data_t *input_data, |
| 4254 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4255 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4256 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4257 | psa_key_type_t key_type = key_type_arg; |
| 4258 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4259 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4260 | psa_status_t actual_status; |
| 4261 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 4262 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 4263 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4264 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4265 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4266 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4267 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4268 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4269 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4270 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4271 | psa_set_key_algorithm( &attributes, alg ); |
| 4272 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 4273 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4274 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4275 | &key ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4276 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4277 | actual_status = psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4278 | input_data->x, input_data->len, |
| 4279 | signature, signature_size, |
| 4280 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4281 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4282 | /* The value of *signature_length is unspecified on error, but |
| 4283 | * whatever it is, it should be less than signature_size, so that |
| 4284 | * if the caller tries to read *signature_length bytes without |
| 4285 | * checking the error code then they don't overflow a buffer. */ |
| 4286 | TEST_ASSERT( signature_length <= signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4287 | |
| 4288 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4289 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4290 | psa_destroy_key( key ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4291 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4292 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4293 | } |
| 4294 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 4295 | |
| 4296 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 4297 | void sign_verify_hash( int key_type_arg, data_t *key_data, |
| 4298 | int alg_arg, data_t *input_data ) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4299 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4300 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4301 | psa_key_type_t key_type = key_type_arg; |
| 4302 | psa_algorithm_t alg = alg_arg; |
| 4303 | size_t key_bits; |
| 4304 | unsigned char *signature = NULL; |
| 4305 | size_t signature_size; |
| 4306 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4307 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4308 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4309 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4310 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4311 | 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] | 4312 | psa_set_key_algorithm( &attributes, alg ); |
| 4313 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4314 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4315 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4316 | &key ) ); |
| 4317 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4318 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4319 | |
| 4320 | /* Allocate a buffer which has the size advertized by the |
| 4321 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4322 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4323 | key_bits, alg ); |
| 4324 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4325 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4326 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4327 | |
| 4328 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4329 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4330 | input_data->x, input_data->len, |
| 4331 | signature, signature_size, |
| 4332 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4333 | /* Check that the signature length looks sensible. */ |
| 4334 | TEST_ASSERT( signature_length <= signature_size ); |
| 4335 | TEST_ASSERT( signature_length > 0 ); |
| 4336 | |
| 4337 | /* Use the library to verify that the signature is correct. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4338 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4339 | input_data->x, input_data->len, |
| 4340 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4341 | |
| 4342 | if( input_data->len != 0 ) |
| 4343 | { |
| 4344 | /* Flip a bit in the input and verify that the signature is now |
| 4345 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 4346 | * because ECDSA may ignore the last few bits of the input. */ |
| 4347 | input_data->x[0] ^= 1; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4348 | TEST_EQUAL( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4349 | input_data->x, input_data->len, |
| 4350 | signature, signature_length ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4351 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4352 | } |
| 4353 | |
| 4354 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4355 | /* |
| 4356 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4357 | * thus reset them as required. |
| 4358 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4359 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4360 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4361 | psa_destroy_key( key ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4362 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4363 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4364 | } |
| 4365 | /* END_CASE */ |
| 4366 | |
| 4367 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 4368 | void verify_hash( int key_type_arg, data_t *key_data, |
| 4369 | int alg_arg, data_t *hash_data, |
| 4370 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4371 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4372 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4373 | psa_key_type_t key_type = key_type_arg; |
| 4374 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4375 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4376 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4377 | TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 4378 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4379 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4380 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4381 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4382 | psa_set_key_algorithm( &attributes, alg ); |
| 4383 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4384 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4385 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4386 | &key ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4387 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4388 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4389 | hash_data->x, hash_data->len, |
| 4390 | signature_data->x, signature_data->len ) ); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 4391 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4392 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4393 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4394 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4395 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4396 | } |
| 4397 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4398 | |
| 4399 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 4400 | void verify_hash_fail( int key_type_arg, data_t *key_data, |
| 4401 | int alg_arg, data_t *hash_data, |
| 4402 | data_t *signature_data, |
| 4403 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4404 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4405 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4406 | psa_key_type_t key_type = key_type_arg; |
| 4407 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4408 | psa_status_t actual_status; |
| 4409 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4410 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4411 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4412 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4413 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4414 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4415 | psa_set_key_algorithm( &attributes, alg ); |
| 4416 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4417 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4418 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4419 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4420 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4421 | actual_status = psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4422 | hash_data->x, hash_data->len, |
| 4423 | signature_data->x, signature_data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4424 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4425 | |
| 4426 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4427 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4428 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4429 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4430 | } |
| 4431 | /* END_CASE */ |
| 4432 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4433 | /* BEGIN_CASE */ |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 4434 | void sign_message_deterministic( int key_type_arg, |
| 4435 | data_t *key_data, |
| 4436 | int alg_arg, |
| 4437 | data_t *input_data, |
| 4438 | data_t *output_data ) |
| 4439 | { |
| 4440 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4441 | psa_key_type_t key_type = key_type_arg; |
| 4442 | psa_algorithm_t alg = alg_arg; |
| 4443 | size_t key_bits; |
| 4444 | unsigned char *signature = NULL; |
| 4445 | size_t signature_size; |
| 4446 | size_t signature_length = 0xdeadbeef; |
| 4447 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4448 | |
| 4449 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4450 | |
| 4451 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 4452 | psa_set_key_algorithm( &attributes, alg ); |
| 4453 | psa_set_key_type( &attributes, key_type ); |
| 4454 | |
| 4455 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4456 | &key ) ); |
| 4457 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4458 | key_bits = psa_get_key_bits( &attributes ); |
| 4459 | |
| 4460 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 4461 | TEST_ASSERT( signature_size != 0 ); |
| 4462 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
| 4463 | ASSERT_ALLOC( signature, signature_size ); |
| 4464 | |
| 4465 | PSA_ASSERT( psa_sign_message( key, alg, |
| 4466 | input_data->x, input_data->len, |
| 4467 | signature, signature_size, |
| 4468 | &signature_length ) ); |
| 4469 | |
| 4470 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 4471 | signature, signature_length ); |
| 4472 | |
| 4473 | exit: |
| 4474 | psa_reset_key_attributes( &attributes ); |
| 4475 | |
| 4476 | psa_destroy_key( key ); |
| 4477 | mbedtls_free( signature ); |
| 4478 | PSA_DONE( ); |
| 4479 | |
| 4480 | } |
| 4481 | /* END_CASE */ |
| 4482 | |
| 4483 | /* BEGIN_CASE */ |
| 4484 | void sign_message_fail( int key_type_arg, |
| 4485 | data_t *key_data, |
| 4486 | int alg_arg, |
| 4487 | data_t *input_data, |
| 4488 | int signature_size_arg, |
| 4489 | int expected_status_arg ) |
| 4490 | { |
| 4491 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4492 | psa_key_type_t key_type = key_type_arg; |
| 4493 | psa_algorithm_t alg = alg_arg; |
| 4494 | size_t signature_size = signature_size_arg; |
| 4495 | psa_status_t actual_status; |
| 4496 | psa_status_t expected_status = expected_status_arg; |
| 4497 | unsigned char *signature = NULL; |
| 4498 | size_t signature_length = 0xdeadbeef; |
| 4499 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4500 | |
| 4501 | ASSERT_ALLOC( signature, signature_size ); |
| 4502 | |
| 4503 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4504 | |
| 4505 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 4506 | psa_set_key_algorithm( &attributes, alg ); |
| 4507 | psa_set_key_type( &attributes, key_type ); |
| 4508 | |
| 4509 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4510 | &key ) ); |
| 4511 | |
| 4512 | actual_status = psa_sign_message( key, alg, |
| 4513 | input_data->x, input_data->len, |
| 4514 | signature, signature_size, |
| 4515 | &signature_length ); |
| 4516 | TEST_EQUAL( actual_status, expected_status ); |
| 4517 | /* The value of *signature_length is unspecified on error, but |
| 4518 | * whatever it is, it should be less than signature_size, so that |
| 4519 | * if the caller tries to read *signature_length bytes without |
| 4520 | * checking the error code then they don't overflow a buffer. */ |
| 4521 | TEST_ASSERT( signature_length <= signature_size ); |
| 4522 | |
| 4523 | exit: |
| 4524 | psa_reset_key_attributes( &attributes ); |
| 4525 | psa_destroy_key( key ); |
| 4526 | mbedtls_free( signature ); |
| 4527 | PSA_DONE( ); |
| 4528 | } |
| 4529 | /* END_CASE */ |
| 4530 | |
| 4531 | /* BEGIN_CASE */ |
| 4532 | void sign_verify_message( int key_type_arg, |
| 4533 | data_t *key_data, |
| 4534 | int alg_arg, |
| 4535 | data_t *input_data ) |
| 4536 | { |
| 4537 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4538 | psa_key_type_t key_type = key_type_arg; |
| 4539 | psa_algorithm_t alg = alg_arg; |
| 4540 | size_t key_bits; |
| 4541 | unsigned char *signature = NULL; |
| 4542 | size_t signature_size; |
| 4543 | size_t signature_length = 0xdeadbeef; |
| 4544 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4545 | |
| 4546 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4547 | |
| 4548 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 4549 | PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 4550 | psa_set_key_algorithm( &attributes, alg ); |
| 4551 | psa_set_key_type( &attributes, key_type ); |
| 4552 | |
| 4553 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4554 | &key ) ); |
| 4555 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4556 | key_bits = psa_get_key_bits( &attributes ); |
| 4557 | |
| 4558 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 4559 | TEST_ASSERT( signature_size != 0 ); |
| 4560 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
| 4561 | ASSERT_ALLOC( signature, signature_size ); |
| 4562 | |
| 4563 | PSA_ASSERT( psa_sign_message( key, alg, |
| 4564 | input_data->x, input_data->len, |
| 4565 | signature, signature_size, |
| 4566 | &signature_length ) ); |
| 4567 | TEST_ASSERT( signature_length <= signature_size ); |
| 4568 | TEST_ASSERT( signature_length > 0 ); |
| 4569 | |
| 4570 | PSA_ASSERT( psa_verify_message( key, alg, |
| 4571 | input_data->x, input_data->len, |
| 4572 | signature, signature_length ) ); |
| 4573 | |
| 4574 | if( input_data->len != 0 ) |
| 4575 | { |
| 4576 | /* Flip a bit in the input and verify that the signature is now |
| 4577 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 4578 | * because ECDSA may ignore the last few bits of the input. */ |
| 4579 | input_data->x[0] ^= 1; |
| 4580 | TEST_EQUAL( psa_verify_message( key, alg, |
| 4581 | input_data->x, input_data->len, |
| 4582 | signature, signature_length ), |
| 4583 | PSA_ERROR_INVALID_SIGNATURE ); |
| 4584 | } |
| 4585 | |
| 4586 | exit: |
| 4587 | psa_reset_key_attributes( &attributes ); |
| 4588 | |
| 4589 | psa_destroy_key( key ); |
| 4590 | mbedtls_free( signature ); |
| 4591 | PSA_DONE( ); |
| 4592 | } |
| 4593 | /* END_CASE */ |
| 4594 | |
| 4595 | /* BEGIN_CASE */ |
| 4596 | void verify_message( int key_type_arg, |
| 4597 | data_t *key_data, |
| 4598 | int alg_arg, |
| 4599 | data_t *input_data, |
| 4600 | data_t *signature_data ) |
| 4601 | { |
| 4602 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4603 | psa_key_type_t key_type = key_type_arg; |
| 4604 | psa_algorithm_t alg = alg_arg; |
| 4605 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4606 | |
| 4607 | TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE ); |
| 4608 | |
| 4609 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4610 | |
| 4611 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 4612 | psa_set_key_algorithm( &attributes, alg ); |
| 4613 | psa_set_key_type( &attributes, key_type ); |
| 4614 | |
| 4615 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4616 | &key ) ); |
| 4617 | |
| 4618 | PSA_ASSERT( psa_verify_message( key, alg, |
| 4619 | input_data->x, input_data->len, |
| 4620 | signature_data->x, signature_data->len ) ); |
| 4621 | |
| 4622 | exit: |
| 4623 | psa_reset_key_attributes( &attributes ); |
| 4624 | psa_destroy_key( key ); |
| 4625 | PSA_DONE( ); |
| 4626 | } |
| 4627 | /* END_CASE */ |
| 4628 | |
| 4629 | /* BEGIN_CASE */ |
| 4630 | void verify_message_fail( int key_type_arg, |
| 4631 | data_t *key_data, |
| 4632 | int alg_arg, |
| 4633 | data_t *hash_data, |
| 4634 | data_t *signature_data, |
| 4635 | int expected_status_arg ) |
| 4636 | { |
| 4637 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4638 | psa_key_type_t key_type = key_type_arg; |
| 4639 | psa_algorithm_t alg = alg_arg; |
| 4640 | psa_status_t actual_status; |
| 4641 | psa_status_t expected_status = expected_status_arg; |
| 4642 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4643 | |
| 4644 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4645 | |
| 4646 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 4647 | psa_set_key_algorithm( &attributes, alg ); |
| 4648 | psa_set_key_type( &attributes, key_type ); |
| 4649 | |
| 4650 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4651 | &key ) ); |
| 4652 | |
| 4653 | actual_status = psa_verify_message( key, alg, |
| 4654 | hash_data->x, hash_data->len, |
| 4655 | signature_data->x, |
| 4656 | signature_data->len ); |
| 4657 | TEST_EQUAL( actual_status, expected_status ); |
| 4658 | |
| 4659 | exit: |
| 4660 | psa_reset_key_attributes( &attributes ); |
| 4661 | psa_destroy_key( key ); |
| 4662 | PSA_DONE( ); |
| 4663 | } |
| 4664 | /* END_CASE */ |
| 4665 | |
| 4666 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4667 | void asymmetric_encrypt( int key_type_arg, |
| 4668 | data_t *key_data, |
| 4669 | int alg_arg, |
| 4670 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4671 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4672 | int expected_output_length_arg, |
| 4673 | int expected_status_arg ) |
| 4674 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4675 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4676 | psa_key_type_t key_type = key_type_arg; |
| 4677 | psa_algorithm_t alg = alg_arg; |
| 4678 | size_t expected_output_length = expected_output_length_arg; |
| 4679 | size_t key_bits; |
| 4680 | unsigned char *output = NULL; |
| 4681 | size_t output_size; |
| 4682 | size_t output_length = ~0; |
| 4683 | psa_status_t actual_status; |
| 4684 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4685 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4686 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4687 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4688 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4689 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4690 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4691 | psa_set_key_algorithm( &attributes, alg ); |
| 4692 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4693 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4694 | &key ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4695 | |
| 4696 | /* Determine the maximum output length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4697 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4698 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4699 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4700 | 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] | 4701 | TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4702 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4703 | |
| 4704 | /* Encrypt the input */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4705 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4706 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4707 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4708 | output, output_size, |
| 4709 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4710 | TEST_EQUAL( actual_status, expected_status ); |
| 4711 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4712 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4713 | /* If the label is empty, the test framework puts a non-null pointer |
| 4714 | * in label->x. Test that a null pointer works as well. */ |
| 4715 | if( label->len == 0 ) |
| 4716 | { |
| 4717 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4718 | if( output_size != 0 ) |
| 4719 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4720 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4721 | input_data->x, input_data->len, |
| 4722 | NULL, label->len, |
| 4723 | output, output_size, |
| 4724 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4725 | TEST_EQUAL( actual_status, expected_status ); |
| 4726 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4727 | } |
| 4728 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4729 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4730 | /* |
| 4731 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4732 | * thus reset them as required. |
| 4733 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4734 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4735 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4736 | psa_destroy_key( key ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4737 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4738 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4739 | } |
| 4740 | /* END_CASE */ |
| 4741 | |
| 4742 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4743 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 4744 | data_t *key_data, |
| 4745 | int alg_arg, |
| 4746 | data_t *input_data, |
| 4747 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4748 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4749 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4750 | psa_key_type_t key_type = key_type_arg; |
| 4751 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4752 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4753 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4754 | size_t output_size; |
| 4755 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4756 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4757 | size_t output2_size; |
| 4758 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4759 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4760 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4761 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4762 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4763 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4764 | psa_set_key_algorithm( &attributes, alg ); |
| 4765 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4766 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4767 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4768 | &key ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4769 | |
| 4770 | /* Determine the maximum ciphertext length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4771 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4772 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4773 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4774 | 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] | 4775 | TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4776 | ASSERT_ALLOC( output, output_size ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4777 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4778 | output2_size = input_data->len; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4779 | TEST_ASSERT( output2_size <= |
| 4780 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) ); |
| 4781 | TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4782 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4783 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 4784 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 4785 | * the original plaintext because of the non-optional random |
| 4786 | * part of encryption process which prevents using fixed vectors. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4787 | PSA_ASSERT( psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4788 | input_data->x, input_data->len, |
| 4789 | label->x, label->len, |
| 4790 | output, output_size, |
| 4791 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4792 | /* We don't know what ciphertext length to expect, but check that |
| 4793 | * it looks sensible. */ |
| 4794 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4795 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4796 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4797 | output, output_length, |
| 4798 | label->x, label->len, |
| 4799 | output2, output2_size, |
| 4800 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4801 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 4802 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4803 | |
| 4804 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4805 | /* |
| 4806 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4807 | * thus reset them as required. |
| 4808 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4809 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4810 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4811 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4812 | mbedtls_free( output ); |
| 4813 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4814 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4815 | } |
| 4816 | /* END_CASE */ |
| 4817 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4818 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4819 | void asymmetric_decrypt( int key_type_arg, |
| 4820 | data_t *key_data, |
| 4821 | int alg_arg, |
| 4822 | data_t *input_data, |
| 4823 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 4824 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4825 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4826 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4827 | psa_key_type_t key_type = key_type_arg; |
| 4828 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4829 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4830 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 4831 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4832 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4833 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4834 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4835 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4836 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4837 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4838 | psa_set_key_algorithm( &attributes, alg ); |
| 4839 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4840 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4841 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4842 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4843 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4844 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4845 | key_bits = psa_get_key_bits( &attributes ); |
| 4846 | |
| 4847 | /* Determine the maximum ciphertext length */ |
| 4848 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 4849 | TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
| 4850 | ASSERT_ALLOC( output, output_size ); |
| 4851 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4852 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4853 | input_data->x, input_data->len, |
| 4854 | label->x, label->len, |
| 4855 | output, |
| 4856 | output_size, |
| 4857 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4858 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4859 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4860 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4861 | /* If the label is empty, the test framework puts a non-null pointer |
| 4862 | * in label->x. Test that a null pointer works as well. */ |
| 4863 | if( label->len == 0 ) |
| 4864 | { |
| 4865 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4866 | if( output_size != 0 ) |
| 4867 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4868 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4869 | input_data->x, input_data->len, |
| 4870 | NULL, label->len, |
| 4871 | output, |
| 4872 | output_size, |
| 4873 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4874 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4875 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4876 | } |
| 4877 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4878 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4879 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4880 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4881 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4882 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4883 | } |
| 4884 | /* END_CASE */ |
| 4885 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4886 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4887 | void asymmetric_decrypt_fail( int key_type_arg, |
| 4888 | data_t *key_data, |
| 4889 | int alg_arg, |
| 4890 | data_t *input_data, |
| 4891 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4892 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4893 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4894 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4895 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4896 | psa_key_type_t key_type = key_type_arg; |
| 4897 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4898 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4899 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4900 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4901 | psa_status_t actual_status; |
| 4902 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4903 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4904 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4905 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4906 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4907 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4908 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4909 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4910 | psa_set_key_algorithm( &attributes, alg ); |
| 4911 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4912 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4913 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4914 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4915 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4916 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4917 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4918 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4919 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4920 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4921 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4922 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4923 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4924 | /* If the label is empty, the test framework puts a non-null pointer |
| 4925 | * in label->x. Test that a null pointer works as well. */ |
| 4926 | if( label->len == 0 ) |
| 4927 | { |
| 4928 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4929 | if( output_size != 0 ) |
| 4930 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4931 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4932 | input_data->x, input_data->len, |
| 4933 | NULL, label->len, |
| 4934 | output, output_size, |
| 4935 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4936 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4937 | TEST_ASSERT( output_length <= output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4938 | } |
| 4939 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4940 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4941 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4942 | psa_destroy_key( key ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4943 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4944 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4945 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4946 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4947 | |
| 4948 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4949 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4950 | { |
| 4951 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 4952 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 4953 | * though it's OK by the C standard. We could test for this, but we'd need |
| 4954 | * to supress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4955 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4956 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 4957 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4958 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4959 | |
| 4960 | memset( &zero, 0, sizeof( zero ) ); |
| 4961 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4962 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4963 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4964 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4965 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4966 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4967 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4968 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4969 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4970 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4971 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 4972 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 4973 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4974 | } |
| 4975 | /* END_CASE */ |
| 4976 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4977 | /* BEGIN_CASE */ |
| 4978 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4979 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4980 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4981 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4982 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4983 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4984 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4985 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4986 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4987 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4988 | |
| 4989 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4990 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4991 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4992 | } |
| 4993 | /* END_CASE */ |
| 4994 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4995 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4996 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 4997 | int expected_status_arg ) |
| 4998 | { |
| 4999 | psa_algorithm_t alg = alg_arg; |
| 5000 | size_t capacity = capacity_arg; |
| 5001 | psa_status_t expected_status = expected_status_arg; |
| 5002 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5003 | |
| 5004 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5005 | |
| 5006 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 5007 | |
| 5008 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 5009 | expected_status ); |
| 5010 | |
| 5011 | exit: |
| 5012 | psa_key_derivation_abort( &operation ); |
| 5013 | PSA_DONE( ); |
| 5014 | } |
| 5015 | /* END_CASE */ |
| 5016 | |
| 5017 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5018 | void derive_input( int alg_arg, |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 5019 | int step_arg1, int key_type_arg1, data_t *input1, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5020 | int expected_status_arg1, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 5021 | int step_arg2, int key_type_arg2, data_t *input2, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5022 | int expected_status_arg2, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 5023 | int step_arg3, int key_type_arg3, data_t *input3, |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 5024 | int expected_status_arg3, |
| 5025 | int output_key_type_arg, int expected_output_status_arg ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5026 | { |
| 5027 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 5028 | psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3}; |
| 5029 | 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] | 5030 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 5031 | expected_status_arg2, |
| 5032 | expected_status_arg3}; |
| 5033 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5034 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 5035 | MBEDTLS_SVC_KEY_ID_INIT, |
| 5036 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5037 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5038 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5039 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 5040 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5041 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 5042 | psa_status_t expected_output_status = expected_output_status_arg; |
| 5043 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5044 | |
| 5045 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5046 | |
| 5047 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5048 | psa_set_key_algorithm( &attributes, alg ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5049 | |
| 5050 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 5051 | |
| 5052 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 5053 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5054 | if( key_types[i] != PSA_KEY_TYPE_NONE ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5055 | { |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 5056 | psa_set_key_type( &attributes, key_types[i] ); |
| 5057 | PSA_ASSERT( psa_import_key( &attributes, |
| 5058 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5059 | &keys[i] ) ); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 5060 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) && |
| 5061 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5062 | { |
| 5063 | // When taking a private key as secret input, use key agreement |
| 5064 | // to add the shared secret to the derivation |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5065 | TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self( |
| 5066 | &operation, keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 5067 | expected_statuses[i] ); |
| 5068 | } |
| 5069 | else |
| 5070 | { |
| 5071 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5072 | keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 5073 | expected_statuses[i] ); |
| 5074 | } |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 5075 | } |
| 5076 | else |
| 5077 | { |
| 5078 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 5079 | &operation, steps[i], |
| 5080 | inputs[i]->x, inputs[i]->len ), |
| 5081 | expected_statuses[i] ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5082 | } |
| 5083 | } |
| 5084 | |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 5085 | if( output_key_type != PSA_KEY_TYPE_NONE ) |
| 5086 | { |
| 5087 | psa_reset_key_attributes( &attributes ); |
| 5088 | psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA ); |
| 5089 | psa_set_key_bits( &attributes, 8 ); |
| 5090 | actual_output_status = |
| 5091 | psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5092 | &output_key ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 5093 | } |
| 5094 | else |
| 5095 | { |
| 5096 | uint8_t buffer[1]; |
| 5097 | actual_output_status = |
| 5098 | psa_key_derivation_output_bytes( &operation, |
| 5099 | buffer, sizeof( buffer ) ); |
| 5100 | } |
| 5101 | TEST_EQUAL( actual_output_status, expected_output_status ); |
| 5102 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5103 | exit: |
| 5104 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5105 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 5106 | psa_destroy_key( keys[i] ); |
| 5107 | psa_destroy_key( output_key ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5108 | PSA_DONE( ); |
| 5109 | } |
| 5110 | /* END_CASE */ |
| 5111 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 5112 | /* BEGIN_CASE */ |
| 5113 | void test_derive_invalid_key_derivation_state( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 5114 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 5115 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5116 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 5117 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5118 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 5119 | unsigned char input1[] = "Input 1"; |
| 5120 | size_t input1_length = sizeof( input1 ); |
| 5121 | unsigned char input2[] = "Input 2"; |
| 5122 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5123 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 5124 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 5125 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 5126 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 5127 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5128 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 5129 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5130 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5131 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5132 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5133 | psa_set_key_algorithm( &attributes, alg ); |
| 5134 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5135 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 5136 | PSA_ASSERT( psa_import_key( &attributes, |
| 5137 | key_data, sizeof( key_data ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5138 | &key ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5139 | |
| 5140 | /* valid key derivation */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5141 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 5142 | input1, input1_length, |
| 5143 | input2, input2_length, |
| 5144 | capacity ) ) |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 5145 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5146 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5147 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 5148 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 5149 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5150 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5151 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5152 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5153 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5154 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5155 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5156 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5157 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5158 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5159 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5160 | } |
| 5161 | /* END_CASE */ |
| 5162 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5163 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5164 | void test_derive_invalid_key_derivation_tests( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5165 | { |
| 5166 | uint8_t output_buffer[16]; |
| 5167 | size_t buffer_size = 16; |
| 5168 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5169 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5170 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5171 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 5172 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5173 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5174 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5175 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5176 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5177 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5178 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5179 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5180 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 5181 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5182 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5183 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5184 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5185 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5186 | |
| 5187 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5188 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 5189 | } |
| 5190 | /* END_CASE */ |
| 5191 | |
| 5192 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5193 | void derive_output( int alg_arg, |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5194 | int step1_arg, data_t *input1, |
| 5195 | int step2_arg, data_t *input2, |
| 5196 | int step3_arg, data_t *input3, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5197 | int requested_capacity_arg, |
| 5198 | data_t *expected_output1, |
| 5199 | data_t *expected_output2 ) |
| 5200 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5201 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5202 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg}; |
| 5203 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5204 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 5205 | MBEDTLS_SVC_KEY_ID_INIT, |
| 5206 | MBEDTLS_SVC_KEY_ID_INIT }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5207 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5208 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5209 | uint8_t *expected_outputs[2] = |
| 5210 | {expected_output1->x, expected_output2->x}; |
| 5211 | size_t output_sizes[2] = |
| 5212 | {expected_output1->len, expected_output2->len}; |
| 5213 | size_t output_buffer_size = 0; |
| 5214 | uint8_t *output_buffer = NULL; |
| 5215 | size_t expected_capacity; |
| 5216 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5217 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5218 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5219 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5220 | |
| 5221 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 5222 | { |
| 5223 | if( output_sizes[i] > output_buffer_size ) |
| 5224 | output_buffer_size = output_sizes[i]; |
| 5225 | if( output_sizes[i] == 0 ) |
| 5226 | expected_outputs[i] = NULL; |
| 5227 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5228 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5229 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5230 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5231 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5232 | psa_set_key_algorithm( &attributes, alg ); |
| 5233 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5234 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5235 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5236 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 5237 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 5238 | requested_capacity ) ); |
| 5239 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5240 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5241 | switch( steps[i] ) |
| 5242 | { |
| 5243 | case 0: |
| 5244 | break; |
| 5245 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5246 | PSA_ASSERT( psa_import_key( &attributes, |
| 5247 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5248 | &keys[i] ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5249 | |
| 5250 | if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 5251 | { |
| 5252 | PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) ); |
| 5253 | TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <= |
| 5254 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ); |
| 5255 | } |
| 5256 | |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5257 | PSA_ASSERT( psa_key_derivation_input_key( |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5258 | &operation, steps[i], keys[i] ) ); |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5259 | break; |
| 5260 | default: |
| 5261 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 5262 | &operation, steps[i], |
| 5263 | inputs[i]->x, inputs[i]->len ) ); |
| 5264 | break; |
| 5265 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5266 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5267 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5268 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5269 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5270 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5271 | expected_capacity = requested_capacity; |
| 5272 | |
| 5273 | /* Expansion phase. */ |
| 5274 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 5275 | { |
| 5276 | /* Read some bytes. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5277 | status = psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5278 | output_buffer, output_sizes[i] ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5279 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 5280 | { |
| 5281 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 5282 | TEST_ASSERT( status == PSA_SUCCESS || |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5283 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5284 | continue; |
| 5285 | } |
| 5286 | else if( expected_capacity == 0 || |
| 5287 | output_sizes[i] > expected_capacity ) |
| 5288 | { |
| 5289 | /* Capacity exceeded. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5290 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5291 | expected_capacity = 0; |
| 5292 | continue; |
| 5293 | } |
| 5294 | /* Success. Check the read data. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5295 | PSA_ASSERT( status ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5296 | if( output_sizes[i] != 0 ) |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5297 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 5298 | expected_outputs[i], output_sizes[i] ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5299 | /* Check the operation status. */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5300 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5301 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5302 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5303 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5304 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5305 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5306 | |
| 5307 | exit: |
| 5308 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5309 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5310 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 5311 | psa_destroy_key( keys[i] ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5312 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5313 | } |
| 5314 | /* END_CASE */ |
| 5315 | |
| 5316 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5317 | void derive_full( int alg_arg, |
| 5318 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 5319 | data_t *input1, |
| 5320 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5321 | int requested_capacity_arg ) |
| 5322 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5323 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5324 | psa_algorithm_t alg = alg_arg; |
| 5325 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5326 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5327 | unsigned char output_buffer[16]; |
| 5328 | size_t expected_capacity = requested_capacity; |
| 5329 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5330 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5331 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5332 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5333 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5334 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5335 | psa_set_key_algorithm( &attributes, alg ); |
| 5336 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5337 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5338 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5339 | &key ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5340 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5341 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 5342 | input1->x, input1->len, |
| 5343 | input2->x, input2->len, |
| 5344 | requested_capacity ) ) |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 5345 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 5346 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5347 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5348 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5349 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5350 | |
| 5351 | /* Expansion phase. */ |
| 5352 | while( current_capacity > 0 ) |
| 5353 | { |
| 5354 | size_t read_size = sizeof( output_buffer ); |
| 5355 | if( read_size > current_capacity ) |
| 5356 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5357 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5358 | output_buffer, |
| 5359 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5360 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5361 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5362 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5363 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5364 | } |
| 5365 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5366 | /* Check that the operation refuses to go over capacity. */ |
| 5367 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5368 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5369 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5370 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5371 | |
| 5372 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5373 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5374 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5375 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5376 | } |
| 5377 | /* END_CASE */ |
| 5378 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 5379 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5380 | void derive_key_exercise( int alg_arg, |
| 5381 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 5382 | data_t *input1, |
| 5383 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5384 | int derived_type_arg, |
| 5385 | int derived_bits_arg, |
| 5386 | int derived_usage_arg, |
| 5387 | int derived_alg_arg ) |
| 5388 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5389 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5390 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5391 | psa_algorithm_t alg = alg_arg; |
| 5392 | psa_key_type_t derived_type = derived_type_arg; |
| 5393 | size_t derived_bits = derived_bits_arg; |
| 5394 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 5395 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 5396 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5397 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5398 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5399 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5400 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5401 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5402 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5403 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5404 | psa_set_key_algorithm( &attributes, alg ); |
| 5405 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5406 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5407 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5408 | |
| 5409 | /* Derive a key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5410 | if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 5411 | input1->x, input1->len, |
| 5412 | input2->x, input2->len, |
| 5413 | capacity ) ) |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 5414 | goto exit; |
| 5415 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5416 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 5417 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 5418 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5419 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5420 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5421 | &derived_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5422 | |
| 5423 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5424 | PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5425 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 5426 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5427 | |
| 5428 | /* Exercise the derived key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5429 | if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5430 | goto exit; |
| 5431 | |
| 5432 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5433 | /* |
| 5434 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5435 | * thus reset them as required. |
| 5436 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5437 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5438 | |
| 5439 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5440 | psa_destroy_key( base_key ); |
| 5441 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5442 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5443 | } |
| 5444 | /* END_CASE */ |
| 5445 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 5446 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5447 | void derive_key_export( int alg_arg, |
| 5448 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 5449 | data_t *input1, |
| 5450 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5451 | int bytes1_arg, |
| 5452 | int bytes2_arg ) |
| 5453 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5454 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5455 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5456 | psa_algorithm_t alg = alg_arg; |
| 5457 | size_t bytes1 = bytes1_arg; |
| 5458 | size_t bytes2 = bytes2_arg; |
| 5459 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5460 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5461 | uint8_t *output_buffer = NULL; |
| 5462 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5463 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5464 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5465 | size_t length; |
| 5466 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5467 | ASSERT_ALLOC( output_buffer, capacity ); |
| 5468 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5469 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5470 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5471 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 5472 | psa_set_key_algorithm( &base_attributes, alg ); |
| 5473 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5474 | 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] | 5475 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5476 | |
| 5477 | /* Derive some material and output it. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5478 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 5479 | input1->x, input1->len, |
| 5480 | input2->x, input2->len, |
| 5481 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 5482 | goto exit; |
| 5483 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5484 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5485 | output_buffer, |
| 5486 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5487 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5488 | |
| 5489 | /* 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] | 5490 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 5491 | input1->x, input1->len, |
| 5492 | input2->x, input2->len, |
| 5493 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 5494 | goto exit; |
| 5495 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5496 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 5497 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 5498 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5499 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5500 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5501 | &derived_key ) ); |
| 5502 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5503 | export_buffer, bytes1, |
| 5504 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5505 | TEST_EQUAL( length, bytes1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5506 | PSA_ASSERT( psa_destroy_key( derived_key ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5507 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5508 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5509 | &derived_key ) ); |
| 5510 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5511 | export_buffer + bytes1, bytes2, |
| 5512 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5513 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5514 | |
| 5515 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5516 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 5517 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5518 | |
| 5519 | exit: |
| 5520 | mbedtls_free( output_buffer ); |
| 5521 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5522 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5523 | psa_destroy_key( base_key ); |
| 5524 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5525 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5526 | } |
| 5527 | /* END_CASE */ |
| 5528 | |
| 5529 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 5530 | void derive_key( int alg_arg, |
| 5531 | data_t *key_data, data_t *input1, data_t *input2, |
| 5532 | int type_arg, int bits_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 5533 | int expected_status_arg, |
| 5534 | int is_large_output ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5535 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5536 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5537 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5538 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 5539 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5540 | size_t bits = bits_arg; |
| 5541 | psa_status_t expected_status = expected_status_arg; |
| 5542 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5543 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5544 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5545 | |
| 5546 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5547 | |
| 5548 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 5549 | psa_set_key_algorithm( &base_attributes, alg ); |
| 5550 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 5551 | 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] | 5552 | &base_key ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5553 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5554 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 5555 | input1->x, input1->len, |
| 5556 | input2->x, input2->len, |
| 5557 | SIZE_MAX ) ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5558 | goto exit; |
| 5559 | |
| 5560 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 5561 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 5562 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5563 | psa_set_key_bits( &derived_attributes, bits ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 5564 | |
| 5565 | psa_status_t status = |
| 5566 | psa_key_derivation_output_key( &derived_attributes, |
| 5567 | &operation, |
| 5568 | &derived_key ); |
| 5569 | if( is_large_output > 0 ) |
| 5570 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5571 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5572 | |
| 5573 | exit: |
| 5574 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5575 | psa_destroy_key( base_key ); |
| 5576 | psa_destroy_key( derived_key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5577 | PSA_DONE( ); |
| 5578 | } |
| 5579 | /* END_CASE */ |
| 5580 | |
| 5581 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5582 | void key_agreement_setup( int alg_arg, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 5583 | int our_key_type_arg, int our_key_alg_arg, |
| 5584 | data_t *our_key_data, data_t *peer_key_data, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5585 | int expected_status_arg ) |
| 5586 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5587 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5588 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5589 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5590 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5591 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5592 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5593 | psa_status_t expected_status = expected_status_arg; |
| 5594 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5595 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5596 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5597 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5598 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5599 | psa_set_key_algorithm( &attributes, our_key_alg ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5600 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5601 | PSA_ASSERT( psa_import_key( &attributes, |
| 5602 | our_key_data->x, our_key_data->len, |
| 5603 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5604 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5605 | /* The tests currently include inputs that should fail at either step. |
| 5606 | * Test cases that fail at the setup step should be changed to call |
| 5607 | * key_derivation_setup instead, and this function should be renamed |
| 5608 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5609 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5610 | if( status == PSA_SUCCESS ) |
| 5611 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5612 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 5613 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 5614 | our_key, |
| 5615 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5616 | expected_status ); |
| 5617 | } |
| 5618 | else |
| 5619 | { |
| 5620 | TEST_ASSERT( status == expected_status ); |
| 5621 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5622 | |
| 5623 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5624 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5625 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5626 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5627 | } |
| 5628 | /* END_CASE */ |
| 5629 | |
| 5630 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5631 | void raw_key_agreement( int alg_arg, |
| 5632 | int our_key_type_arg, data_t *our_key_data, |
| 5633 | data_t *peer_key_data, |
| 5634 | data_t *expected_output ) |
| 5635 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5636 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5637 | psa_algorithm_t alg = alg_arg; |
| 5638 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5639 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5640 | unsigned char *output = NULL; |
| 5641 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5642 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5643 | |
| 5644 | ASSERT_ALLOC( output, expected_output->len ); |
| 5645 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5646 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5647 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5648 | psa_set_key_algorithm( &attributes, alg ); |
| 5649 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5650 | PSA_ASSERT( psa_import_key( &attributes, |
| 5651 | our_key_data->x, our_key_data->len, |
| 5652 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5653 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5654 | PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) ); |
| 5655 | key_bits = psa_get_key_bits( &attributes ); |
| 5656 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5657 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 5658 | peer_key_data->x, peer_key_data->len, |
| 5659 | output, expected_output->len, |
| 5660 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5661 | ASSERT_COMPARE( output, output_length, |
| 5662 | expected_output->x, expected_output->len ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5663 | TEST_ASSERT( output_length <= |
| 5664 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) ); |
| 5665 | TEST_ASSERT( output_length <= |
| 5666 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5667 | |
| 5668 | exit: |
| 5669 | mbedtls_free( output ); |
| 5670 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5671 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5672 | } |
| 5673 | /* END_CASE */ |
| 5674 | |
| 5675 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5676 | void key_agreement_capacity( int alg_arg, |
| 5677 | int our_key_type_arg, data_t *our_key_data, |
| 5678 | data_t *peer_key_data, |
| 5679 | int expected_capacity_arg ) |
| 5680 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5681 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5682 | psa_algorithm_t alg = alg_arg; |
| 5683 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5684 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5685 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5686 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5687 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5688 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5689 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5690 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5691 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5692 | psa_set_key_algorithm( &attributes, alg ); |
| 5693 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5694 | PSA_ASSERT( psa_import_key( &attributes, |
| 5695 | our_key_data->x, our_key_data->len, |
| 5696 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5697 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5698 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5699 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 5700 | &operation, |
| 5701 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 5702 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5703 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 5704 | { |
| 5705 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5706 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5707 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5708 | NULL, 0 ) ); |
| 5709 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5710 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5711 | /* Test the advertized capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5712 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5713 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5714 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5715 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5716 | /* Test the actual capacity by reading the output. */ |
| 5717 | while( actual_capacity > sizeof( output ) ) |
| 5718 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5719 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5720 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5721 | actual_capacity -= sizeof( output ); |
| 5722 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5723 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5724 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5725 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5726 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5727 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5728 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5729 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5730 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5731 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5732 | } |
| 5733 | /* END_CASE */ |
| 5734 | |
| 5735 | /* BEGIN_CASE */ |
| 5736 | void key_agreement_output( int alg_arg, |
| 5737 | int our_key_type_arg, data_t *our_key_data, |
| 5738 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5739 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5740 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5741 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5742 | psa_algorithm_t alg = alg_arg; |
| 5743 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5744 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5745 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5746 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5747 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5748 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 5749 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5750 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5751 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5752 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5753 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5754 | psa_set_key_algorithm( &attributes, alg ); |
| 5755 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5756 | PSA_ASSERT( psa_import_key( &attributes, |
| 5757 | our_key_data->x, our_key_data->len, |
| 5758 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5759 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5760 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5761 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 5762 | &operation, |
| 5763 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 5764 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5765 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 5766 | { |
| 5767 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5768 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5769 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5770 | NULL, 0 ) ); |
| 5771 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5772 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5773 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5774 | actual_output, |
| 5775 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5776 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 5777 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5778 | if( expected_output2->len != 0 ) |
| 5779 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5780 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5781 | actual_output, |
| 5782 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5783 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 5784 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5785 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5786 | |
| 5787 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5788 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5789 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5790 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5791 | mbedtls_free( actual_output ); |
| 5792 | } |
| 5793 | /* END_CASE */ |
| 5794 | |
| 5795 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5796 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5797 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5798 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5799 | unsigned char *output = NULL; |
| 5800 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5801 | size_t i; |
| 5802 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5803 | |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 5804 | TEST_ASSERT( bytes_arg >= 0 ); |
| 5805 | |
Gilles Peskine | 9189202 | 2021-02-08 19:50:26 +0100 | [diff] [blame] | 5806 | ASSERT_ALLOC( output, bytes ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5807 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5808 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5809 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5810 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5811 | /* Run several times, to ensure that every output byte will be |
| 5812 | * nonzero at least once with overwhelming probability |
| 5813 | * (2^(-8*number_of_runs)). */ |
| 5814 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5815 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 5816 | if( bytes != 0 ) |
| 5817 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5818 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5819 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5820 | for( i = 0; i < bytes; i++ ) |
| 5821 | { |
| 5822 | if( output[i] != 0 ) |
| 5823 | ++changed[i]; |
| 5824 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5825 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5826 | |
| 5827 | /* Check that every byte was changed to nonzero at least once. This |
| 5828 | * validates that psa_generate_random is overwriting every byte of |
| 5829 | * the output buffer. */ |
| 5830 | for( i = 0; i < bytes; i++ ) |
| 5831 | { |
| 5832 | TEST_ASSERT( changed[i] != 0 ); |
| 5833 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5834 | |
| 5835 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5836 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5837 | mbedtls_free( output ); |
| 5838 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5839 | } |
| 5840 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5841 | |
| 5842 | /* BEGIN_CASE */ |
| 5843 | void generate_key( int type_arg, |
| 5844 | int bits_arg, |
| 5845 | int usage_arg, |
| 5846 | int alg_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 5847 | int expected_status_arg, |
| 5848 | int is_large_key ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5849 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5850 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5851 | psa_key_type_t type = type_arg; |
| 5852 | psa_key_usage_t usage = usage_arg; |
| 5853 | size_t bits = bits_arg; |
| 5854 | psa_algorithm_t alg = alg_arg; |
| 5855 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5856 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5857 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5858 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5859 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5860 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5861 | psa_set_key_usage_flags( &attributes, usage ); |
| 5862 | psa_set_key_algorithm( &attributes, alg ); |
| 5863 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5864 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5865 | |
| 5866 | /* Generate a key */ |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 5867 | psa_status_t status = psa_generate_key( &attributes, &key ); |
| 5868 | |
| 5869 | if( is_large_key > 0 ) |
| 5870 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5871 | TEST_EQUAL( status , expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5872 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5873 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5874 | |
| 5875 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5876 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5877 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 5878 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5879 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 5880 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5881 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 5882 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5883 | |
| 5884 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5885 | /* |
| 5886 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5887 | * thus reset them as required. |
| 5888 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5889 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5890 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5891 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5892 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5893 | } |
| 5894 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5895 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 5896 | /* 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] | 5897 | void generate_key_rsa( int bits_arg, |
| 5898 | data_t *e_arg, |
| 5899 | int expected_status_arg ) |
| 5900 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5901 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5902 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5903 | size_t bits = bits_arg; |
| 5904 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 5905 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 5906 | psa_status_t expected_status = expected_status_arg; |
| 5907 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5908 | uint8_t *exported = NULL; |
| 5909 | size_t exported_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5910 | PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5911 | size_t exported_length = SIZE_MAX; |
| 5912 | uint8_t *e_read_buffer = NULL; |
| 5913 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 5914 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5915 | size_t e_read_length = SIZE_MAX; |
| 5916 | |
| 5917 | if( e_arg->len == 0 || |
| 5918 | ( e_arg->len == 3 && |
| 5919 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 5920 | { |
| 5921 | is_default_public_exponent = 1; |
| 5922 | e_read_size = 0; |
| 5923 | } |
| 5924 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 5925 | ASSERT_ALLOC( exported, exported_size ); |
| 5926 | |
| 5927 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5928 | |
| 5929 | psa_set_key_usage_flags( &attributes, usage ); |
| 5930 | psa_set_key_algorithm( &attributes, alg ); |
| 5931 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 5932 | e_arg->x, e_arg->len ) ); |
| 5933 | psa_set_key_bits( &attributes, bits ); |
| 5934 | |
| 5935 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5936 | TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5937 | if( expected_status != PSA_SUCCESS ) |
| 5938 | goto exit; |
| 5939 | |
| 5940 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5941 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5942 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 5943 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 5944 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 5945 | e_read_buffer, e_read_size, |
| 5946 | &e_read_length ) ); |
| 5947 | if( is_default_public_exponent ) |
| 5948 | TEST_EQUAL( e_read_length, 0 ); |
| 5949 | else |
| 5950 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 5951 | |
| 5952 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5953 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5954 | goto exit; |
| 5955 | |
| 5956 | /* Export the key and check the public exponent. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5957 | PSA_ASSERT( psa_export_public_key( key, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5958 | exported, exported_size, |
| 5959 | &exported_length ) ); |
| 5960 | { |
| 5961 | uint8_t *p = exported; |
| 5962 | uint8_t *end = exported + exported_length; |
| 5963 | size_t len; |
| 5964 | /* RSAPublicKey ::= SEQUENCE { |
| 5965 | * modulus INTEGER, -- n |
| 5966 | * publicExponent INTEGER } -- e |
| 5967 | */ |
| 5968 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5969 | MBEDTLS_ASN1_SEQUENCE | |
| 5970 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 5971 | TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5972 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 5973 | MBEDTLS_ASN1_INTEGER ) ); |
| 5974 | if( len >= 1 && p[0] == 0 ) |
| 5975 | { |
| 5976 | ++p; |
| 5977 | --len; |
| 5978 | } |
| 5979 | if( e_arg->len == 0 ) |
| 5980 | { |
| 5981 | TEST_EQUAL( len, 3 ); |
| 5982 | TEST_EQUAL( p[0], 1 ); |
| 5983 | TEST_EQUAL( p[1], 0 ); |
| 5984 | TEST_EQUAL( p[2], 1 ); |
| 5985 | } |
| 5986 | else |
| 5987 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 5988 | } |
| 5989 | |
| 5990 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5991 | /* |
| 5992 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 5993 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 5994 | */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5995 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5996 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5997 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5998 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5999 | mbedtls_free( e_read_buffer ); |
| 6000 | mbedtls_free( exported ); |
| 6001 | } |
| 6002 | /* END_CASE */ |
| 6003 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6004 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6005 | void persistent_key_load_key_from_storage( data_t *data, |
| 6006 | int type_arg, int bits_arg, |
| 6007 | int usage_flags_arg, int alg_arg, |
| 6008 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6009 | { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 6010 | 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] | 6011 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6012 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6013 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6014 | psa_key_type_t type = type_arg; |
| 6015 | size_t bits = bits_arg; |
| 6016 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 6017 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6018 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6019 | unsigned char *first_export = NULL; |
| 6020 | unsigned char *second_export = NULL; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 6021 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6022 | size_t first_exported_length; |
| 6023 | size_t second_exported_length; |
| 6024 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6025 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 6026 | { |
| 6027 | ASSERT_ALLOC( first_export, export_size ); |
| 6028 | ASSERT_ALLOC( second_export, export_size ); |
| 6029 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6030 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6031 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6032 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 6033 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6034 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 6035 | psa_set_key_algorithm( &attributes, alg ); |
| 6036 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 6037 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6038 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6039 | switch( generation_method ) |
| 6040 | { |
| 6041 | case IMPORT_KEY: |
| 6042 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6043 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6044 | &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6045 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6046 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6047 | case GENERATE_KEY: |
| 6048 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6049 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6050 | break; |
| 6051 | |
| 6052 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 6053 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6054 | { |
| 6055 | /* Create base key */ |
| 6056 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 6057 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6058 | psa_set_key_usage_flags( &base_attributes, |
| 6059 | PSA_KEY_USAGE_DERIVE ); |
| 6060 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 6061 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6062 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 6063 | data->x, data->len, |
| 6064 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6065 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6066 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6067 | PSA_ASSERT( psa_key_derivation_input_key( |
| 6068 | &operation, |
| 6069 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6070 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6071 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6072 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6073 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 6074 | &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6075 | &key ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6076 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6077 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6078 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6079 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 6080 | #else |
| 6081 | TEST_ASSUME( ! "KDF not supported in this configuration" ); |
| 6082 | #endif |
| 6083 | break; |
| 6084 | |
| 6085 | default: |
| 6086 | TEST_ASSERT( ! "generation_method not implemented in test" ); |
| 6087 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6088 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6089 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6090 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6091 | /* Export the key if permitted by the key policy. */ |
| 6092 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 6093 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6094 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6095 | first_export, export_size, |
| 6096 | &first_exported_length ) ); |
| 6097 | if( generation_method == IMPORT_KEY ) |
| 6098 | ASSERT_COMPARE( data->x, data->len, |
| 6099 | first_export, first_exported_length ); |
| 6100 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6101 | |
| 6102 | /* Shutdown and restart */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6103 | PSA_ASSERT( psa_purge_key( key ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6104 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6105 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6106 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6107 | /* Check key slot still contains key data */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6108 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 6109 | TEST_ASSERT( mbedtls_svc_key_id_equal( |
| 6110 | psa_get_key_id( &attributes ), key_id ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6111 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 6112 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 6113 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 6114 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 6115 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags ); |
| 6116 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6117 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6118 | /* Export the key again if permitted by the key policy. */ |
| 6119 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6120 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6121 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6122 | second_export, export_size, |
| 6123 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6124 | ASSERT_COMPARE( first_export, first_exported_length, |
| 6125 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6126 | } |
| 6127 | |
| 6128 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 6129 | if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6130 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6131 | |
| 6132 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6133 | /* |
| 6134 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6135 | * thus reset them as required. |
| 6136 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6137 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6138 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6139 | mbedtls_free( first_export ); |
| 6140 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6141 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6142 | psa_destroy_key( base_key ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6143 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6144 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6145 | } |
| 6146 | /* END_CASE */ |