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 | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 267 | typedef enum |
| 268 | { |
| 269 | DO_NOT_SET_LENGTHS = 0, |
| 270 | SET_LENGTHS_BEFORE_NONCE = 1, |
| 271 | SET_LENGTHS_AFTER_NONCE = 2 |
| 272 | } setlengths_method; |
| 273 | |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 274 | typedef enum |
| 275 | { |
| 276 | USE_NULL_TAG = 0, |
| 277 | USE_GIVEN_TAG = 1, |
| 278 | } tagusage_method; |
| 279 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 280 | /*! |
| 281 | * \brief Internal Function for AEAD multipart tests. |
| 282 | * |
| 283 | * \param key_type_arg Type of key passed in |
| 284 | * \param key_data The encryption / decryption key data |
| 285 | * \param alg_arg The type of algorithm used |
| 286 | * \param nonce Nonce data |
| 287 | * \param additional_data Additional data |
| 288 | * \param ad_part_len If not -1, the length of chunks to |
| 289 | * feed additional data in to be encrypted / |
| 290 | * decrypted. If -1, no chunking. |
| 291 | * \param input_data Data to encrypt / decrypt |
| 292 | * \param data_part_len If not -1, the length of chunks to feed the |
| 293 | * data in to be encrypted / decrypted. If -1, |
| 294 | * no chunking |
| 295 | * \param do_set_lengths If non-zero, then set lengths prior to |
| 296 | * calling encryption / decryption. |
| 297 | * \param expected_output Expected output |
Paul Elliott | 41ffae1 | 2021-07-22 21:52:01 +0100 | [diff] [blame] | 298 | * \param expect_valid_signature If non zero, we expect the signature to be |
| 299 | * valid |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 300 | * \param is_encrypt If non-zero this is an encryption operation. |
Paul Elliott | 41ffae1 | 2021-07-22 21:52:01 +0100 | [diff] [blame] | 301 | * \param do_zero_parts If non-zero, interleave zero length chunks |
| 302 | * with normal length chunks |
| 303 | * \param swap_set_functions If non-zero, swap the order of set lengths |
| 304 | * and set nonce. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 305 | * |
| 306 | * \return int Zero on failure, non-zero on success. |
| 307 | * |
| 308 | */ |
| 309 | static int aead_multipart_internal_func( int key_type_arg, data_t *key_data, |
| 310 | int alg_arg, |
| 311 | data_t *nonce, |
| 312 | data_t *additional_data, |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 313 | int ad_part_len_arg, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 314 | data_t *input_data, |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 315 | int data_part_len_arg, |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 316 | setlengths_method set_lengths_method, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 317 | data_t *expected_output, |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 318 | int is_encrypt, |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 319 | int do_zero_parts ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 320 | { |
| 321 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 322 | psa_key_type_t key_type = key_type_arg; |
| 323 | psa_algorithm_t alg = alg_arg; |
| 324 | psa_aead_operation_t operation; |
| 325 | unsigned char *output_data = NULL; |
| 326 | unsigned char *part_data = NULL; |
| 327 | unsigned char *final_data = NULL; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 328 | size_t data_true_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 329 | size_t part_data_size = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 330 | size_t output_size = 0; |
| 331 | size_t final_output_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 332 | size_t output_length = 0; |
| 333 | size_t key_bits = 0; |
| 334 | size_t tag_length = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 335 | size_t part_offset = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 336 | size_t part_length = 0; |
| 337 | size_t output_part_length = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 338 | size_t tag_size = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 339 | size_t ad_part_len = 0; |
| 340 | size_t data_part_len = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 341 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 342 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 343 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 344 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 345 | int test_ok = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 346 | size_t part_count = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 347 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 348 | PSA_ASSERT( psa_crypto_init( ) ); |
| 349 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 350 | if( is_encrypt ) |
| 351 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 352 | else |
| 353 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 354 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 355 | psa_set_key_algorithm( &attributes, alg ); |
| 356 | psa_set_key_type( &attributes, key_type ); |
| 357 | |
| 358 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 359 | &key ) ); |
| 360 | |
| 361 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 362 | key_bits = psa_get_key_bits( &attributes ); |
| 363 | |
| 364 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 365 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 366 | if( is_encrypt ) |
| 367 | { |
| 368 | /* Tag gets written at end of buffer. */ |
| 369 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 370 | ( input_data->len + |
| 371 | tag_length ) ); |
| 372 | data_true_size = input_data->len; |
| 373 | } |
| 374 | else |
| 375 | { |
| 376 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 377 | ( input_data->len - |
| 378 | tag_length ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 379 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 380 | /* Do not want to attempt to decrypt tag. */ |
| 381 | data_true_size = input_data->len - tag_length; |
| 382 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 383 | |
| 384 | ASSERT_ALLOC( output_data, output_size ); |
| 385 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 386 | if( is_encrypt ) |
| 387 | { |
Paul Elliott | 5a9642f | 2021-09-13 19:13:22 +0100 | [diff] [blame] | 388 | final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 389 | TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 390 | } |
| 391 | else |
| 392 | { |
Paul Elliott | 5a9642f | 2021-09-13 19:13:22 +0100 | [diff] [blame] | 393 | final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg ); |
| 394 | TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 395 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 396 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 397 | ASSERT_ALLOC( final_data, final_output_size ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 398 | |
| 399 | operation = psa_aead_operation_init( ); |
| 400 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 401 | |
| 402 | if( is_encrypt ) |
| 403 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 404 | else |
| 405 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 406 | |
| 407 | /* If the operation is not supported, just skip and not fail in case the |
| 408 | * encryption involves a common limitation of cryptography hardwares and |
| 409 | * an alternative implementation. */ |
| 410 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 411 | { |
| 412 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 413 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 414 | } |
| 415 | |
| 416 | PSA_ASSERT( status ); |
| 417 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 418 | if( set_lengths_method == DO_NOT_SET_LENGTHS ) |
| 419 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 420 | else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 421 | { |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 422 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 423 | data_true_size ) ); |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 424 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 425 | } |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 426 | else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE ) |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 427 | { |
| 428 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 429 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 430 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 431 | data_true_size ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 432 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 433 | |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 434 | if( ad_part_len_arg != -1 ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 435 | { |
| 436 | /* Pass additional data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 437 | ad_part_len = (size_t) ad_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 438 | |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 439 | for( part_offset = 0, part_count = 0; |
| 440 | part_offset < additional_data->len; |
| 441 | part_offset += part_length, part_count++ ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 442 | { |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 443 | if( do_zero_parts && ( part_count & 0x01 ) ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 444 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 445 | part_length = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 446 | } |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 447 | else if( additional_data->len - part_offset < ad_part_len ) |
| 448 | { |
| 449 | part_length = additional_data->len - part_offset; |
| 450 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 451 | else |
| 452 | { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 453 | part_length = ad_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | PSA_ASSERT( psa_aead_update_ad( &operation, |
| 457 | additional_data->x + part_offset, |
| 458 | part_length ) ); |
| 459 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | else |
| 463 | { |
| 464 | /* Pass additional data in one go. */ |
| 465 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 466 | additional_data->len ) ); |
| 467 | } |
| 468 | |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 469 | if( data_part_len_arg != -1 ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 470 | { |
| 471 | /* Pass data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 472 | data_part_len = ( size_t ) data_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 473 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 474 | ( size_t ) data_part_len ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 475 | |
| 476 | ASSERT_ALLOC( part_data, part_data_size ); |
| 477 | |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 478 | for( part_offset = 0, part_count = 0; |
| 479 | part_offset < data_true_size; |
| 480 | part_offset += part_length, part_count++ ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 481 | { |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 482 | if( do_zero_parts && ( part_count & 0x01 ) ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 483 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 484 | part_length = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 485 | } |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 486 | else if( ( data_true_size - part_offset ) < data_part_len ) |
| 487 | { |
| 488 | part_length = ( data_true_size - part_offset ); |
| 489 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 490 | else |
| 491 | { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 492 | part_length = data_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | PSA_ASSERT( psa_aead_update( &operation, |
| 496 | ( input_data->x + part_offset ), |
| 497 | part_length, part_data, |
| 498 | part_data_size, |
| 499 | &output_part_length ) ); |
| 500 | |
| 501 | if( output_data && output_part_length ) |
| 502 | { |
| 503 | memcpy( ( output_data + part_offset ), part_data, |
| 504 | output_part_length ); |
| 505 | } |
| 506 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 507 | output_length += output_part_length; |
| 508 | } |
| 509 | } |
| 510 | else |
| 511 | { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 512 | /* Pass all data in one go. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 513 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 514 | data_true_size, output_data, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 515 | output_size, &output_length ) ); |
| 516 | } |
| 517 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 518 | if( is_encrypt ) |
| 519 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 520 | final_output_size, |
| 521 | &output_part_length, |
| 522 | tag_buffer, tag_length, |
| 523 | &tag_size ) ); |
| 524 | else |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 525 | { |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 526 | PSA_ASSERT( psa_aead_verify( &operation, final_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 527 | final_output_size, |
| 528 | &output_part_length, |
| 529 | ( input_data->x + data_true_size ), |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 530 | tag_length ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 531 | } |
| 532 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 533 | if( output_data && output_part_length ) |
| 534 | memcpy( ( output_data + output_length ), final_data, |
| 535 | output_part_length ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 536 | |
| 537 | output_length += output_part_length; |
| 538 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 539 | |
| 540 | /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE |
| 541 | * should be exact.*/ |
| 542 | if( is_encrypt ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 543 | { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 544 | TEST_EQUAL( tag_length, tag_size ); |
| 545 | |
| 546 | if( output_data && tag_length ) |
| 547 | memcpy( ( output_data + output_length ), tag_buffer, |
| 548 | tag_length ); |
| 549 | |
| 550 | output_length += tag_length; |
| 551 | |
| 552 | TEST_EQUAL( output_length, |
| 553 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 554 | input_data->len ) ); |
| 555 | TEST_ASSERT( output_length <= |
| 556 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 557 | } |
| 558 | else |
| 559 | { |
| 560 | TEST_EQUAL( output_length, |
| 561 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, |
| 562 | input_data->len ) ); |
| 563 | TEST_ASSERT( output_length <= |
| 564 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 565 | } |
| 566 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 567 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 568 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 569 | output_data, output_length ); |
| 570 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 571 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 572 | test_ok = 1; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 573 | |
| 574 | exit: |
| 575 | psa_destroy_key( key ); |
| 576 | psa_aead_abort( &operation ); |
| 577 | mbedtls_free( output_data ); |
| 578 | mbedtls_free( part_data ); |
| 579 | mbedtls_free( final_data ); |
| 580 | PSA_DONE( ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 581 | |
| 582 | return( test_ok ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 583 | } |
| 584 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 585 | /* END_HEADER */ |
| 586 | |
| 587 | /* BEGIN_DEPENDENCIES |
| 588 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 589 | * END_DEPENDENCIES |
| 590 | */ |
| 591 | |
| 592 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 593 | void static_checks( ) |
| 594 | { |
| 595 | size_t max_truncated_mac_size = |
| 596 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 597 | |
| 598 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 599 | * encoding. The shifted mask is the maximum truncated value. The |
| 600 | * untruncated algorithm may be one byte larger. */ |
| 601 | TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size ); |
| 602 | } |
| 603 | /* END_CASE */ |
| 604 | |
| 605 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 606 | void import_with_policy( int type_arg, |
| 607 | int usage_arg, int alg_arg, |
| 608 | int expected_status_arg ) |
| 609 | { |
| 610 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 611 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 612 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 613 | psa_key_type_t type = type_arg; |
| 614 | psa_key_usage_t usage = usage_arg; |
| 615 | psa_algorithm_t alg = alg_arg; |
| 616 | psa_status_t expected_status = expected_status_arg; |
| 617 | const uint8_t key_material[16] = {0}; |
| 618 | psa_status_t status; |
| 619 | |
| 620 | PSA_ASSERT( psa_crypto_init( ) ); |
| 621 | |
| 622 | psa_set_key_type( &attributes, type ); |
| 623 | psa_set_key_usage_flags( &attributes, usage ); |
| 624 | psa_set_key_algorithm( &attributes, alg ); |
| 625 | |
| 626 | status = psa_import_key( &attributes, |
| 627 | key_material, sizeof( key_material ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 628 | &key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 629 | TEST_EQUAL( status, expected_status ); |
| 630 | if( status != PSA_SUCCESS ) |
| 631 | goto exit; |
| 632 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 633 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 634 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 635 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 636 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 637 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 638 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 639 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 640 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 641 | |
| 642 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 643 | /* |
| 644 | * Key attributes may have been returned by psa_get_key_attributes() |
| 645 | * thus reset them as required. |
| 646 | */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 647 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 648 | |
| 649 | psa_destroy_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 650 | PSA_DONE( ); |
| 651 | } |
| 652 | /* END_CASE */ |
| 653 | |
| 654 | /* BEGIN_CASE */ |
| 655 | void import_with_data( data_t *data, int type_arg, |
| 656 | int attr_bits_arg, |
| 657 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 658 | { |
| 659 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 660 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 661 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 662 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 663 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 664 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 665 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 666 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 667 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 668 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 669 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 670 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 671 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 672 | status = psa_import_key( &attributes, data->x, data->len, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 673 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 674 | if( status != PSA_SUCCESS ) |
| 675 | goto exit; |
| 676 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 677 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 678 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 679 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 680 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 681 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 682 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 683 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 684 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 685 | |
| 686 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 687 | /* |
| 688 | * Key attributes may have been returned by psa_get_key_attributes() |
| 689 | * thus reset them as required. |
| 690 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 691 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 692 | |
| 693 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 694 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 695 | } |
| 696 | /* END_CASE */ |
| 697 | |
| 698 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 699 | void import_large_key( int type_arg, int byte_size_arg, |
| 700 | int expected_status_arg ) |
| 701 | { |
| 702 | psa_key_type_t type = type_arg; |
| 703 | size_t byte_size = byte_size_arg; |
| 704 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 705 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 706 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 707 | psa_status_t status; |
| 708 | uint8_t *buffer = NULL; |
| 709 | size_t buffer_size = byte_size + 1; |
| 710 | size_t n; |
| 711 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 712 | /* Skip the test case if the target running the test cannot |
| 713 | * accomodate large keys due to heap size constraints */ |
| 714 | ASSERT_ALLOC_WEAK( buffer, buffer_size ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 715 | memset( buffer, 'K', byte_size ); |
| 716 | |
| 717 | PSA_ASSERT( psa_crypto_init( ) ); |
| 718 | |
| 719 | /* Try importing the key */ |
| 720 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 721 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 722 | status = psa_import_key( &attributes, buffer, byte_size, &key ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 723 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 724 | TEST_EQUAL( status, expected_status ); |
| 725 | |
| 726 | if( status == PSA_SUCCESS ) |
| 727 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 728 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 729 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 730 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 731 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 732 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 733 | memset( buffer, 0, byte_size + 1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 734 | PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 735 | for( n = 0; n < byte_size; n++ ) |
| 736 | TEST_EQUAL( buffer[n], 'K' ); |
| 737 | for( n = byte_size; n < buffer_size; n++ ) |
| 738 | TEST_EQUAL( buffer[n], 0 ); |
| 739 | } |
| 740 | |
| 741 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 742 | /* |
| 743 | * Key attributes may have been returned by psa_get_key_attributes() |
| 744 | * thus reset them as required. |
| 745 | */ |
| 746 | psa_reset_key_attributes( &attributes ); |
| 747 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 748 | psa_destroy_key( key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 749 | PSA_DONE( ); |
| 750 | mbedtls_free( buffer ); |
| 751 | } |
| 752 | /* END_CASE */ |
| 753 | |
| 754 | /* BEGIN_CASE */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 755 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 756 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 757 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 758 | size_t bits = bits_arg; |
| 759 | psa_status_t expected_status = expected_status_arg; |
| 760 | psa_status_t status; |
| 761 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 762 | 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] | 763 | size_t buffer_size = /* Slight overapproximations */ |
| 764 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 765 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 766 | unsigned char *p; |
| 767 | int ret; |
| 768 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 769 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 770 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 771 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 772 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 773 | |
| 774 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 775 | bits, keypair ) ) >= 0 ); |
| 776 | length = ret; |
| 777 | |
| 778 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 779 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 780 | status = psa_import_key( &attributes, p, length, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 781 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 782 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 783 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 784 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 785 | |
| 786 | exit: |
| 787 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 788 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 789 | } |
| 790 | /* END_CASE */ |
| 791 | |
| 792 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 793 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 794 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 795 | int usage_arg, int alg_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 796 | int expected_bits, |
| 797 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 798 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 799 | int canonical_input ) |
| 800 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 801 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 802 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 803 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 804 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 805 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 806 | unsigned char *exported = NULL; |
| 807 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 808 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 809 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 810 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 811 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 812 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 813 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 814 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 815 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 816 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 817 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 818 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 819 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 820 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 821 | psa_set_key_algorithm( &attributes, alg ); |
| 822 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 823 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 824 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 825 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 826 | |
| 827 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 828 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 829 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 830 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 831 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 832 | |
| 833 | /* Export the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 834 | status = psa_export_key( key, exported, export_size, &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 835 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 836 | |
| 837 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 838 | * and export_size. On errors, the exported length must be 0. */ |
| 839 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 840 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
| 841 | TEST_ASSERT( exported_length <= export_size ); |
| 842 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 843 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 844 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 845 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 846 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 847 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 848 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 849 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 850 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 851 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 852 | * this validates the canonical representations. For canonical inputs, |
| 853 | * this doesn't directly validate the implementation, but it still helps |
| 854 | * by cross-validating the test data with the sanity check code. */ |
| 855 | if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) ) |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 856 | goto exit; |
| 857 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 858 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 859 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 860 | else |
| 861 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 862 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 863 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 864 | &key2 ) ); |
| 865 | PSA_ASSERT( psa_export_key( key2, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 866 | reexported, |
| 867 | export_size, |
| 868 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 869 | ASSERT_COMPARE( exported, exported_length, |
| 870 | reexported, reexported_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 871 | PSA_ASSERT( psa_destroy_key( key2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 872 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 873 | TEST_ASSERT( exported_length <= |
| 874 | PSA_EXPORT_KEY_OUTPUT_SIZE( type, |
| 875 | psa_get_key_bits( &got_attributes ) ) ); |
| 876 | TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 877 | |
| 878 | destroy: |
| 879 | /* Destroy the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 880 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 881 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 882 | |
| 883 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 884 | /* |
| 885 | * Key attributes may have been returned by psa_get_key_attributes() |
| 886 | * thus reset them as required. |
| 887 | */ |
| 888 | psa_reset_key_attributes( &got_attributes ); |
| 889 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 890 | mbedtls_free( exported ); |
| 891 | mbedtls_free( reexported ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 892 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 893 | } |
| 894 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 895 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 896 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 897 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 898 | int type_arg, |
| 899 | int alg_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 900 | int export_size_delta, |
| 901 | int expected_export_status_arg, |
| 902 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 903 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 904 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 905 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 906 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 907 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 908 | psa_status_t status; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 909 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 910 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 911 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 912 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 913 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 914 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 915 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 916 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 917 | psa_set_key_algorithm( &attributes, alg ); |
| 918 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 919 | |
| 920 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 921 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 922 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 923 | /* Export the public key */ |
| 924 | ASSERT_ALLOC( exported, export_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 925 | status = psa_export_public_key( key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 926 | exported, export_size, |
| 927 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 928 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 929 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 930 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 931 | 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] | 932 | size_t bits; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 933 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 934 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 935 | TEST_ASSERT( expected_public_key->len <= |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 936 | PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 937 | TEST_ASSERT( expected_public_key->len <= |
| 938 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 939 | TEST_ASSERT( expected_public_key->len <= |
| 940 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 941 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 942 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 943 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 944 | |
| 945 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 946 | /* |
| 947 | * Key attributes may have been returned by psa_get_key_attributes() |
| 948 | * thus reset them as required. |
| 949 | */ |
| 950 | psa_reset_key_attributes( &attributes ); |
| 951 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 952 | mbedtls_free( exported ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 953 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 954 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 955 | } |
| 956 | /* END_CASE */ |
| 957 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 958 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 959 | void import_and_exercise_key( data_t *data, |
| 960 | int type_arg, |
| 961 | int bits_arg, |
| 962 | int alg_arg ) |
| 963 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 964 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 965 | psa_key_type_t type = type_arg; |
| 966 | size_t bits = bits_arg; |
| 967 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 968 | 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] | 969 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 970 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 971 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 972 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 973 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 974 | psa_set_key_usage_flags( &attributes, usage ); |
| 975 | psa_set_key_algorithm( &attributes, alg ); |
| 976 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 977 | |
| 978 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 979 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 980 | |
| 981 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 982 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 983 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 984 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 985 | |
| 986 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 987 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 988 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 989 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 990 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 991 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 992 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 993 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 994 | /* |
| 995 | * Key attributes may have been returned by psa_get_key_attributes() |
| 996 | * thus reset them as required. |
| 997 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 998 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 999 | |
| 1000 | psa_reset_key_attributes( &attributes ); |
| 1001 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1002 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1003 | } |
| 1004 | /* END_CASE */ |
| 1005 | |
| 1006 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1007 | void effective_key_attributes( int type_arg, int expected_type_arg, |
| 1008 | int bits_arg, int expected_bits_arg, |
| 1009 | int usage_arg, int expected_usage_arg, |
| 1010 | int alg_arg, int expected_alg_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1011 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1012 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1013 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1014 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1015 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1016 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1017 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1018 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1019 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1020 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1021 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1022 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1023 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1024 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1025 | psa_set_key_usage_flags( &attributes, usage ); |
| 1026 | psa_set_key_algorithm( &attributes, alg ); |
| 1027 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1028 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1029 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1030 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1031 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1032 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1033 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1034 | TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type ); |
| 1035 | TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits ); |
| 1036 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1037 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1038 | |
| 1039 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1040 | /* |
| 1041 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1042 | * thus reset them as required. |
| 1043 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1044 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1045 | |
| 1046 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1047 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1048 | } |
| 1049 | /* END_CASE */ |
| 1050 | |
| 1051 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1052 | void check_key_policy( int type_arg, int bits_arg, |
| 1053 | int usage_arg, int alg_arg ) |
| 1054 | { |
| 1055 | test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg, |
| 1056 | usage_arg, usage_arg, alg_arg, alg_arg ); |
| 1057 | goto exit; |
| 1058 | } |
| 1059 | /* END_CASE */ |
| 1060 | |
| 1061 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1062 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1063 | { |
| 1064 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1065 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1066 | * though it's OK by the C standard. We could test for this, but we'd need |
| 1067 | * to supress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1068 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 1069 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1070 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1071 | |
| 1072 | memset( &zero, 0, sizeof( zero ) ); |
| 1073 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1074 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1075 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1076 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1077 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1078 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 1079 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 1080 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 1081 | |
| 1082 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 1083 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 1084 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 1085 | |
| 1086 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 1087 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 1088 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 1089 | |
| 1090 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 1091 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 1092 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1093 | } |
| 1094 | /* END_CASE */ |
| 1095 | |
| 1096 | /* BEGIN_CASE */ |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1097 | void mac_key_policy( int policy_usage, |
| 1098 | int policy_alg, |
| 1099 | int key_type, |
| 1100 | data_t *key_data, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1101 | int exercise_alg, |
| 1102 | int expected_status_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1103 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1104 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1105 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1106 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1107 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1108 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1109 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1110 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1111 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1112 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1113 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1114 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1115 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1116 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1117 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1118 | &key ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1119 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1120 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1121 | if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1122 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1123 | else |
| 1124 | TEST_EQUAL( status, expected_status ); |
| 1125 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1126 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1127 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1128 | memset( mac, 0, sizeof( mac ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1129 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1130 | if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1131 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1132 | else |
| 1133 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1134 | |
| 1135 | exit: |
| 1136 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1137 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1138 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1139 | } |
| 1140 | /* END_CASE */ |
| 1141 | |
| 1142 | /* BEGIN_CASE */ |
| 1143 | void cipher_key_policy( int policy_usage, |
| 1144 | int policy_alg, |
| 1145 | int key_type, |
| 1146 | data_t *key_data, |
| 1147 | int exercise_alg ) |
| 1148 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1149 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1150 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1151 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1152 | psa_status_t status; |
| 1153 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1154 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1155 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1156 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1157 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1158 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1159 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1160 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1161 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1162 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1163 | status = psa_cipher_encrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1164 | if( policy_alg == exercise_alg && |
| 1165 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1166 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1167 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1168 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1169 | psa_cipher_abort( &operation ); |
| 1170 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1171 | status = psa_cipher_decrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1172 | if( policy_alg == exercise_alg && |
| 1173 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1174 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1175 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1176 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1177 | |
| 1178 | exit: |
| 1179 | psa_cipher_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1180 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1181 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1182 | } |
| 1183 | /* END_CASE */ |
| 1184 | |
| 1185 | /* BEGIN_CASE */ |
| 1186 | void aead_key_policy( int policy_usage, |
| 1187 | int policy_alg, |
| 1188 | int key_type, |
| 1189 | data_t *key_data, |
| 1190 | int nonce_length_arg, |
| 1191 | int tag_length_arg, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1192 | int exercise_alg, |
| 1193 | int expected_status_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1194 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1195 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1196 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1197 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1198 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1199 | unsigned char nonce[16] = {0}; |
| 1200 | size_t nonce_length = nonce_length_arg; |
| 1201 | unsigned char tag[16]; |
| 1202 | size_t tag_length = tag_length_arg; |
| 1203 | size_t output_length; |
| 1204 | |
| 1205 | TEST_ASSERT( nonce_length <= sizeof( nonce ) ); |
| 1206 | TEST_ASSERT( tag_length <= sizeof( tag ) ); |
| 1207 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1208 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1209 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1210 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1211 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1212 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1213 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1214 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1215 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1216 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1217 | status = psa_aead_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1218 | nonce, nonce_length, |
| 1219 | NULL, 0, |
| 1220 | NULL, 0, |
| 1221 | tag, tag_length, |
| 1222 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1223 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1224 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1225 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1226 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1227 | |
| 1228 | memset( tag, 0, sizeof( tag ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1229 | status = psa_aead_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1230 | nonce, nonce_length, |
| 1231 | NULL, 0, |
| 1232 | tag, tag_length, |
| 1233 | NULL, 0, |
| 1234 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1235 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 1236 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1237 | else if( expected_status == PSA_SUCCESS ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1238 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1239 | else |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1240 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1241 | |
| 1242 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1243 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1244 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1245 | } |
| 1246 | /* END_CASE */ |
| 1247 | |
| 1248 | /* BEGIN_CASE */ |
| 1249 | void asymmetric_encryption_key_policy( int policy_usage, |
| 1250 | int policy_alg, |
| 1251 | int key_type, |
| 1252 | data_t *key_data, |
| 1253 | int exercise_alg ) |
| 1254 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1255 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1256 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1257 | psa_status_t status; |
| 1258 | size_t key_bits; |
| 1259 | size_t buffer_length; |
| 1260 | unsigned char *buffer = NULL; |
| 1261 | size_t output_length; |
| 1262 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1263 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1264 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1265 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1266 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1267 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1268 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1269 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1270 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1271 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1272 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1273 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1274 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 1275 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1276 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1277 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1278 | status = psa_asymmetric_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1279 | NULL, 0, |
| 1280 | NULL, 0, |
| 1281 | buffer, buffer_length, |
| 1282 | &output_length ); |
| 1283 | if( policy_alg == exercise_alg && |
| 1284 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1285 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1286 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1287 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1288 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 1289 | if( buffer_length != 0 ) |
| 1290 | memset( buffer, 0, buffer_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1291 | status = psa_asymmetric_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1292 | buffer, buffer_length, |
| 1293 | NULL, 0, |
| 1294 | buffer, buffer_length, |
| 1295 | &output_length ); |
| 1296 | if( policy_alg == exercise_alg && |
| 1297 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1298 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1299 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1300 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1301 | |
| 1302 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1303 | /* |
| 1304 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1305 | * thus reset them as required. |
| 1306 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1307 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1308 | |
| 1309 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1310 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1311 | mbedtls_free( buffer ); |
| 1312 | } |
| 1313 | /* END_CASE */ |
| 1314 | |
| 1315 | /* BEGIN_CASE */ |
| 1316 | void asymmetric_signature_key_policy( int policy_usage, |
| 1317 | int policy_alg, |
| 1318 | int key_type, |
| 1319 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1320 | int exercise_alg, |
| 1321 | int payload_length_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1322 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1323 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1324 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1325 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1326 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 1327 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 1328 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 1329 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 1330 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 1331 | int compatible_alg = payload_length_arg > 0; |
| 1332 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1333 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1334 | size_t signature_length; |
| 1335 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1336 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1337 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1338 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1339 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1340 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1341 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1342 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1343 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1344 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1345 | status = psa_sign_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1346 | payload, payload_length, |
| 1347 | signature, sizeof( signature ), |
| 1348 | &signature_length ); |
| 1349 | if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1350 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1351 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1352 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1353 | |
| 1354 | memset( signature, 0, sizeof( signature ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1355 | status = psa_verify_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1356 | payload, payload_length, |
| 1357 | signature, sizeof( signature ) ); |
| 1358 | if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1359 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1360 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1361 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1362 | |
| 1363 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1364 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1365 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1366 | } |
| 1367 | /* END_CASE */ |
| 1368 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1369 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1370 | void derive_key_policy( int policy_usage, |
| 1371 | int policy_alg, |
| 1372 | int key_type, |
| 1373 | data_t *key_data, |
| 1374 | int exercise_alg ) |
| 1375 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1376 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1377 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1378 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1379 | psa_status_t status; |
| 1380 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1381 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1382 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1383 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1384 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1385 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1386 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1387 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1388 | &key ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1389 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1390 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 1391 | |
| 1392 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 1393 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1394 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1395 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 1396 | &operation, |
| 1397 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 1398 | (const uint8_t*) "", 0) ); |
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 | |
| 1401 | status = psa_key_derivation_input_key( &operation, |
| 1402 | PSA_KEY_DERIVATION_INPUT_SECRET, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1403 | key ); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1404 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1405 | if( policy_alg == exercise_alg && |
| 1406 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1407 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1408 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1409 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1410 | |
| 1411 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1412 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1413 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1414 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1415 | } |
| 1416 | /* END_CASE */ |
| 1417 | |
| 1418 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1419 | void agreement_key_policy( int policy_usage, |
| 1420 | int policy_alg, |
| 1421 | int key_type_arg, |
| 1422 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1423 | int exercise_alg, |
| 1424 | int expected_status_arg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1425 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1426 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1427 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1428 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1429 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1430 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1431 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1432 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1433 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1434 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1435 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1436 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1437 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1438 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1439 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1440 | &key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1441 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1442 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1443 | status = mbedtls_test_psa_key_agreement_with_self( &operation, key ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1444 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1445 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1446 | |
| 1447 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1448 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1449 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1450 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1451 | } |
| 1452 | /* END_CASE */ |
| 1453 | |
| 1454 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1455 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 1456 | int usage_arg, int alg_arg, int alg2_arg ) |
| 1457 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1458 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1459 | psa_key_type_t key_type = key_type_arg; |
| 1460 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1461 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1462 | psa_key_usage_t usage = usage_arg; |
| 1463 | psa_algorithm_t alg = alg_arg; |
| 1464 | psa_algorithm_t alg2 = alg2_arg; |
| 1465 | |
| 1466 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1467 | |
| 1468 | psa_set_key_usage_flags( &attributes, usage ); |
| 1469 | psa_set_key_algorithm( &attributes, alg ); |
| 1470 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 1471 | psa_set_key_type( &attributes, key_type ); |
| 1472 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1473 | &key ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1474 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1475 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1476 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 1477 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 1478 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 1479 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1480 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1481 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1482 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1483 | goto exit; |
| 1484 | |
| 1485 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1486 | /* |
| 1487 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1488 | * thus reset them as required. |
| 1489 | */ |
| 1490 | psa_reset_key_attributes( &got_attributes ); |
| 1491 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1492 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1493 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1494 | } |
| 1495 | /* END_CASE */ |
| 1496 | |
| 1497 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1498 | void raw_agreement_key_policy( int policy_usage, |
| 1499 | int policy_alg, |
| 1500 | int key_type_arg, |
| 1501 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1502 | int exercise_alg, |
| 1503 | int expected_status_arg ) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1504 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1505 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1506 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1507 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1508 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1509 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1510 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1511 | |
| 1512 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1513 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1514 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1515 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1516 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1517 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1518 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1519 | &key ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1520 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1521 | status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1522 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1523 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1524 | |
| 1525 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1526 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1527 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1528 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1529 | } |
| 1530 | /* END_CASE */ |
| 1531 | |
| 1532 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1533 | void copy_success( int source_usage_arg, |
| 1534 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1535 | int type_arg, data_t *material, |
| 1536 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1537 | int target_usage_arg, |
| 1538 | int target_alg_arg, int target_alg2_arg, |
| 1539 | int expected_usage_arg, |
| 1540 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1541 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1542 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1543 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1544 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 1545 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1546 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1547 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1548 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1549 | uint8_t *export_buffer = NULL; |
| 1550 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1551 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1552 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1553 | /* Prepare the source key. */ |
| 1554 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 1555 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1556 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1557 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1558 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 1559 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1560 | &source_key ) ); |
| 1561 | PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1562 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1563 | /* Prepare the target attributes. */ |
| 1564 | if( copy_attributes ) |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1565 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1566 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1567 | /* Set volatile lifetime to reset the key identifier to 0. */ |
| 1568 | psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE ); |
| 1569 | } |
| 1570 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1571 | if( target_usage_arg != -1 ) |
| 1572 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 1573 | if( target_alg_arg != -1 ) |
| 1574 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1575 | if( target_alg2_arg != -1 ) |
| 1576 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1577 | |
| 1578 | /* Copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1579 | PSA_ASSERT( psa_copy_key( source_key, |
| 1580 | &target_attributes, &target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1581 | |
| 1582 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1583 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1584 | |
| 1585 | /* Test that the target slot has the expected content and policy. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1586 | PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1587 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 1588 | psa_get_key_type( &target_attributes ) ); |
| 1589 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 1590 | psa_get_key_bits( &target_attributes ) ); |
| 1591 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 1592 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1593 | TEST_EQUAL( expected_alg2, |
| 1594 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1595 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 1596 | { |
| 1597 | size_t length; |
| 1598 | ASSERT_ALLOC( export_buffer, material->len ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1599 | PSA_ASSERT( psa_export_key( target_key, export_buffer, |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1600 | material->len, &length ) ); |
| 1601 | ASSERT_COMPARE( material->x, material->len, |
| 1602 | export_buffer, length ); |
| 1603 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1604 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1605 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1606 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1607 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) ) |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1608 | goto exit; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1609 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1610 | PSA_ASSERT( psa_destroy_key( target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1611 | |
| 1612 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1613 | /* |
| 1614 | * Source and target key attributes may have been returned by |
| 1615 | * psa_get_key_attributes() thus reset them as required. |
| 1616 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1617 | psa_reset_key_attributes( &source_attributes ); |
| 1618 | psa_reset_key_attributes( &target_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1619 | |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1620 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1621 | mbedtls_free( export_buffer ); |
| 1622 | } |
| 1623 | /* END_CASE */ |
| 1624 | |
| 1625 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1626 | void copy_fail( int source_usage_arg, |
| 1627 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1628 | int type_arg, data_t *material, |
| 1629 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1630 | int target_usage_arg, |
| 1631 | int target_alg_arg, int target_alg2_arg, |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 1632 | int target_id_arg, int target_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1633 | int expected_status_arg ) |
| 1634 | { |
| 1635 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1636 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1637 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1638 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 1639 | 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] | 1640 | |
| 1641 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1642 | |
| 1643 | /* Prepare the source key. */ |
| 1644 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 1645 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1646 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1647 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1648 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 1649 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1650 | &source_key ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1651 | |
| 1652 | /* Prepare the target attributes. */ |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 1653 | psa_set_key_id( &target_attributes, key_id ); |
| 1654 | psa_set_key_lifetime( &target_attributes, target_lifetime_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1655 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 1656 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 1657 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 1658 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1659 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1660 | |
| 1661 | /* Try to copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1662 | TEST_EQUAL( psa_copy_key( source_key, |
| 1663 | &target_attributes, &target_key ), |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1664 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1665 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1666 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1667 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1668 | exit: |
| 1669 | psa_reset_key_attributes( &source_attributes ); |
| 1670 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1671 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1672 | } |
| 1673 | /* END_CASE */ |
| 1674 | |
| 1675 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1676 | void hash_operation_init( ) |
| 1677 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1678 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1679 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1680 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1681 | * though it's OK by the C standard. We could test for this, but we'd need |
| 1682 | * to supress the Clang warning for the test. */ |
| 1683 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 1684 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 1685 | psa_hash_operation_t zero; |
| 1686 | |
| 1687 | memset( &zero, 0, sizeof( zero ) ); |
| 1688 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1689 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1690 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 1691 | PSA_ERROR_BAD_STATE ); |
| 1692 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 1693 | PSA_ERROR_BAD_STATE ); |
| 1694 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 1695 | PSA_ERROR_BAD_STATE ); |
| 1696 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1697 | /* A default hash operation should be abortable without error. */ |
| 1698 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 1699 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 1700 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1701 | } |
| 1702 | /* END_CASE */ |
| 1703 | |
| 1704 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1705 | void hash_setup( int alg_arg, |
| 1706 | int expected_status_arg ) |
| 1707 | { |
| 1708 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1709 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1710 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1711 | psa_status_t status; |
| 1712 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1713 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1714 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1715 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1716 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1717 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 1718 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 1719 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1720 | |
| 1721 | /* If setup failed, reproduce the failure, so as to |
| 1722 | * test the resulting state of the operation object. */ |
| 1723 | if( status != PSA_SUCCESS ) |
| 1724 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 1725 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1726 | /* Now the operation object should be reusable. */ |
| 1727 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 1728 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 1729 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1730 | #endif |
| 1731 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1732 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1733 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1734 | } |
| 1735 | /* END_CASE */ |
| 1736 | |
| 1737 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1738 | void hash_compute_fail( int alg_arg, data_t *input, |
| 1739 | int output_size_arg, int expected_status_arg ) |
| 1740 | { |
| 1741 | psa_algorithm_t alg = alg_arg; |
| 1742 | uint8_t *output = NULL; |
| 1743 | size_t output_size = output_size_arg; |
| 1744 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 1745 | psa_status_t expected_status = expected_status_arg; |
| 1746 | psa_status_t status; |
| 1747 | |
| 1748 | ASSERT_ALLOC( output, output_size ); |
| 1749 | |
| 1750 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1751 | |
| 1752 | status = psa_hash_compute( alg, input->x, input->len, |
| 1753 | output, output_size, &output_length ); |
| 1754 | TEST_EQUAL( status, expected_status ); |
| 1755 | TEST_ASSERT( output_length <= output_size ); |
| 1756 | |
| 1757 | exit: |
| 1758 | mbedtls_free( output ); |
| 1759 | PSA_DONE( ); |
| 1760 | } |
| 1761 | /* END_CASE */ |
| 1762 | |
| 1763 | /* BEGIN_CASE */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 1764 | void hash_compare_fail( int alg_arg, data_t *input, |
| 1765 | data_t *reference_hash, |
| 1766 | int expected_status_arg ) |
| 1767 | { |
| 1768 | psa_algorithm_t alg = alg_arg; |
| 1769 | psa_status_t expected_status = expected_status_arg; |
| 1770 | psa_status_t status; |
| 1771 | |
| 1772 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1773 | |
| 1774 | status = psa_hash_compare( alg, input->x, input->len, |
| 1775 | reference_hash->x, reference_hash->len ); |
| 1776 | TEST_EQUAL( status, expected_status ); |
| 1777 | |
| 1778 | exit: |
| 1779 | PSA_DONE( ); |
| 1780 | } |
| 1781 | /* END_CASE */ |
| 1782 | |
| 1783 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1784 | void hash_compute_compare( int alg_arg, data_t *input, |
| 1785 | data_t *expected_output ) |
| 1786 | { |
| 1787 | psa_algorithm_t alg = alg_arg; |
| 1788 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 1789 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 1790 | size_t i; |
| 1791 | |
| 1792 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1793 | |
| 1794 | /* Compute with tight buffer */ |
| 1795 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1796 | output, PSA_HASH_LENGTH( alg ), |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1797 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1798 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1799 | ASSERT_COMPARE( output, output_length, |
| 1800 | expected_output->x, expected_output->len ); |
| 1801 | |
| 1802 | /* Compute with larger buffer */ |
| 1803 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
| 1804 | output, sizeof( output ), |
| 1805 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1806 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1807 | ASSERT_COMPARE( output, output_length, |
| 1808 | expected_output->x, expected_output->len ); |
| 1809 | |
| 1810 | /* Compare with correct hash */ |
| 1811 | PSA_ASSERT( psa_hash_compare( alg, input->x, input->len, |
| 1812 | output, output_length ) ); |
| 1813 | |
| 1814 | /* Compare with trailing garbage */ |
| 1815 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 1816 | output, output_length + 1 ), |
| 1817 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1818 | |
| 1819 | /* Compare with truncated hash */ |
| 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 corrupted value */ |
| 1825 | for( i = 0; i < output_length; i++ ) |
| 1826 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 1827 | mbedtls_test_set_step( i ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1828 | output[i] ^= 1; |
| 1829 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 1830 | output, output_length ), |
| 1831 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1832 | output[i] ^= 1; |
| 1833 | } |
| 1834 | |
| 1835 | exit: |
| 1836 | PSA_DONE( ); |
| 1837 | } |
| 1838 | /* END_CASE */ |
| 1839 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 1840 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1841 | void hash_bad_order( ) |
| 1842 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1843 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1844 | unsigned char input[] = ""; |
| 1845 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1846 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1847 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 1848 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 1849 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1850 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1851 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1852 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1853 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1854 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1855 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1856 | /* Call setup twice in a row. */ |
| 1857 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1858 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 1859 | PSA_ERROR_BAD_STATE ); |
| 1860 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1861 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1862 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1863 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1864 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1865 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1866 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1867 | /* Call update after finish. */ |
| 1868 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1869 | PSA_ASSERT( psa_hash_finish( &operation, |
| 1870 | hash, sizeof( hash ), &hash_len ) ); |
| 1871 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1872 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1873 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1874 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1875 | /* Call verify without calling setup beforehand. */ |
| 1876 | TEST_EQUAL( psa_hash_verify( &operation, |
| 1877 | valid_hash, sizeof( valid_hash ) ), |
| 1878 | PSA_ERROR_BAD_STATE ); |
| 1879 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1880 | |
| 1881 | /* Call verify after finish. */ |
| 1882 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1883 | PSA_ASSERT( psa_hash_finish( &operation, |
| 1884 | hash, sizeof( hash ), &hash_len ) ); |
| 1885 | TEST_EQUAL( psa_hash_verify( &operation, |
| 1886 | valid_hash, sizeof( valid_hash ) ), |
| 1887 | PSA_ERROR_BAD_STATE ); |
| 1888 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1889 | |
| 1890 | /* Call verify twice in a row. */ |
| 1891 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1892 | PSA_ASSERT( psa_hash_verify( &operation, |
| 1893 | valid_hash, sizeof( valid_hash ) ) ); |
| 1894 | TEST_EQUAL( psa_hash_verify( &operation, |
| 1895 | valid_hash, sizeof( valid_hash ) ), |
| 1896 | PSA_ERROR_BAD_STATE ); |
| 1897 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1898 | |
| 1899 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1900 | TEST_EQUAL( psa_hash_finish( &operation, |
| 1901 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1902 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1903 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1904 | |
| 1905 | /* Call finish twice in a row. */ |
| 1906 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1907 | PSA_ASSERT( psa_hash_finish( &operation, |
| 1908 | hash, sizeof( hash ), &hash_len ) ); |
| 1909 | TEST_EQUAL( psa_hash_finish( &operation, |
| 1910 | hash, sizeof( hash ), &hash_len ), |
| 1911 | PSA_ERROR_BAD_STATE ); |
| 1912 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1913 | |
| 1914 | /* Call finish after calling verify. */ |
| 1915 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1916 | PSA_ASSERT( psa_hash_verify( &operation, |
| 1917 | valid_hash, sizeof( valid_hash ) ) ); |
| 1918 | TEST_EQUAL( psa_hash_finish( &operation, |
| 1919 | hash, sizeof( hash ), &hash_len ), |
| 1920 | PSA_ERROR_BAD_STATE ); |
| 1921 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1922 | |
| 1923 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1924 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1925 | } |
| 1926 | /* END_CASE */ |
| 1927 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 1928 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1929 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1930 | { |
| 1931 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1932 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 1933 | * appended to it */ |
| 1934 | unsigned char hash[] = { |
| 1935 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 1936 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 1937 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1938 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1939 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1940 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1941 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1942 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1943 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1944 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1945 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1946 | PSA_ERROR_INVALID_SIGNATURE ); |
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 non-matching hash */ |
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 + 1, expected_size ), |
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 hash longer than expected */ |
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, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1956 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 1957 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1958 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1959 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1960 | } |
| 1961 | /* END_CASE */ |
| 1962 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 1963 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 1964 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1965 | { |
| 1966 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 1967 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1968 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1969 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1970 | size_t hash_len; |
| 1971 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1972 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1973 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1974 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1975 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1976 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1977 | hash, expected_size - 1, &hash_len ), |
| 1978 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1979 | |
| 1980 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1981 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1982 | } |
| 1983 | /* END_CASE */ |
| 1984 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 1985 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1986 | void hash_clone_source_state( ) |
| 1987 | { |
| 1988 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 1989 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 1990 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 1991 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 1992 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 1993 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 1994 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 1995 | size_t hash_len; |
| 1996 | |
| 1997 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1998 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 1999 | |
| 2000 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2001 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2002 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2003 | hash, sizeof( hash ), &hash_len ) ); |
| 2004 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2005 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2006 | |
| 2007 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 2008 | PSA_ERROR_BAD_STATE ); |
| 2009 | |
| 2010 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 2011 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 2012 | hash, sizeof( hash ), &hash_len ) ); |
| 2013 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 2014 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2015 | hash, sizeof( hash ), &hash_len ) ); |
| 2016 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 2017 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 2018 | hash, sizeof( hash ), &hash_len ) ); |
| 2019 | |
| 2020 | exit: |
| 2021 | psa_hash_abort( &op_source ); |
| 2022 | psa_hash_abort( &op_init ); |
| 2023 | psa_hash_abort( &op_setup ); |
| 2024 | psa_hash_abort( &op_finished ); |
| 2025 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2026 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2027 | } |
| 2028 | /* END_CASE */ |
| 2029 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2030 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2031 | void hash_clone_target_state( ) |
| 2032 | { |
| 2033 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2034 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2035 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2036 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2037 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2038 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2039 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 2040 | size_t hash_len; |
| 2041 | |
| 2042 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2043 | |
| 2044 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2045 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2046 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2047 | hash, sizeof( hash ), &hash_len ) ); |
| 2048 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2049 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2050 | |
| 2051 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 2052 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 2053 | hash, sizeof( hash ), &hash_len ) ); |
| 2054 | |
| 2055 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 2056 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 2057 | PSA_ERROR_BAD_STATE ); |
| 2058 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 2059 | PSA_ERROR_BAD_STATE ); |
| 2060 | |
| 2061 | exit: |
| 2062 | psa_hash_abort( &op_target ); |
| 2063 | psa_hash_abort( &op_init ); |
| 2064 | psa_hash_abort( &op_setup ); |
| 2065 | psa_hash_abort( &op_finished ); |
| 2066 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2067 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2068 | } |
| 2069 | /* END_CASE */ |
| 2070 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2071 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2072 | void mac_operation_init( ) |
| 2073 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2074 | const uint8_t input[1] = { 0 }; |
| 2075 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2076 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2077 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2078 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2079 | * to supress the Clang warning for the test. */ |
| 2080 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 2081 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 2082 | psa_mac_operation_t zero; |
| 2083 | |
| 2084 | memset( &zero, 0, sizeof( zero ) ); |
| 2085 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2086 | /* A freshly-initialized MAC operation should not be usable. */ |
| 2087 | TEST_EQUAL( psa_mac_update( &func, |
| 2088 | input, sizeof( input ) ), |
| 2089 | PSA_ERROR_BAD_STATE ); |
| 2090 | TEST_EQUAL( psa_mac_update( &init, |
| 2091 | input, sizeof( input ) ), |
| 2092 | PSA_ERROR_BAD_STATE ); |
| 2093 | TEST_EQUAL( psa_mac_update( &zero, |
| 2094 | input, sizeof( input ) ), |
| 2095 | PSA_ERROR_BAD_STATE ); |
| 2096 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2097 | /* A default MAC operation should be abortable without error. */ |
| 2098 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 2099 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 2100 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2101 | } |
| 2102 | /* END_CASE */ |
| 2103 | |
| 2104 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2105 | void mac_setup( int key_type_arg, |
| 2106 | data_t *key, |
| 2107 | int alg_arg, |
| 2108 | int expected_status_arg ) |
| 2109 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2110 | psa_key_type_t key_type = key_type_arg; |
| 2111 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2112 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2113 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2114 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 2115 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2116 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2117 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2118 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2119 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2120 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2121 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 2122 | &operation, &status ) ) |
| 2123 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2124 | TEST_EQUAL( status, expected_status ); |
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 | /* The operation object should be reusable. */ |
| 2127 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2128 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 2129 | smoke_test_key_data, |
| 2130 | sizeof( smoke_test_key_data ), |
| 2131 | KNOWN_SUPPORTED_MAC_ALG, |
| 2132 | &operation, &status ) ) |
| 2133 | goto exit; |
| 2134 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2135 | #endif |
| 2136 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2137 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2138 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2139 | } |
| 2140 | /* END_CASE */ |
| 2141 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2142 | /* 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] | 2143 | void mac_bad_order( ) |
| 2144 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2145 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2146 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 2147 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2148 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2149 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2150 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2151 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2152 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2153 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 2154 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 2155 | size_t sign_mac_length = 0; |
| 2156 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 2157 | const uint8_t verify_mac[] = { |
| 2158 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 2159 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 2160 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 2161 | |
| 2162 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2163 | 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] | 2164 | psa_set_key_algorithm( &attributes, alg ); |
| 2165 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2166 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2167 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 2168 | &key ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2169 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2170 | /* Call update without calling setup beforehand. */ |
| 2171 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2172 | PSA_ERROR_BAD_STATE ); |
| 2173 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2174 | |
| 2175 | /* Call sign finish without calling setup beforehand. */ |
| 2176 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 2177 | &sign_mac_length), |
| 2178 | PSA_ERROR_BAD_STATE ); |
| 2179 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2180 | |
| 2181 | /* Call verify finish without calling setup beforehand. */ |
| 2182 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2183 | verify_mac, sizeof( verify_mac ) ), |
| 2184 | PSA_ERROR_BAD_STATE ); |
| 2185 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2186 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2187 | /* Call setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2188 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
| 2189 | TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2190 | PSA_ERROR_BAD_STATE ); |
| 2191 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2192 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2193 | /* Call update after sign finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2194 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2195 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2196 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2197 | sign_mac, sizeof( sign_mac ), |
| 2198 | &sign_mac_length ) ); |
| 2199 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2200 | PSA_ERROR_BAD_STATE ); |
| 2201 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2202 | |
| 2203 | /* Call update after verify finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2204 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2205 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2206 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2207 | verify_mac, sizeof( verify_mac ) ) ); |
| 2208 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2209 | PSA_ERROR_BAD_STATE ); |
| 2210 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2211 | |
| 2212 | /* Call sign finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2213 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2214 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2215 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2216 | sign_mac, sizeof( sign_mac ), |
| 2217 | &sign_mac_length ) ); |
| 2218 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2219 | sign_mac, sizeof( sign_mac ), |
| 2220 | &sign_mac_length ), |
| 2221 | PSA_ERROR_BAD_STATE ); |
| 2222 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2223 | |
| 2224 | /* Call verify finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2225 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2226 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2227 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2228 | verify_mac, sizeof( verify_mac ) ) ); |
| 2229 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2230 | verify_mac, sizeof( verify_mac ) ), |
| 2231 | PSA_ERROR_BAD_STATE ); |
| 2232 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2233 | |
| 2234 | /* Setup sign but try verify. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2235 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2236 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2237 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2238 | verify_mac, sizeof( verify_mac ) ), |
| 2239 | PSA_ERROR_BAD_STATE ); |
| 2240 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2241 | |
| 2242 | /* Setup verify but try sign. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2243 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2244 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2245 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2246 | sign_mac, sizeof( sign_mac ), |
| 2247 | &sign_mac_length ), |
| 2248 | PSA_ERROR_BAD_STATE ); |
| 2249 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2250 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2251 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2252 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2253 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2254 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2255 | } |
| 2256 | /* END_CASE */ |
| 2257 | |
| 2258 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2259 | void mac_sign( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2260 | data_t *key_data, |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2261 | int alg_arg, |
| 2262 | data_t *input, |
| 2263 | data_t *expected_mac ) |
| 2264 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2265 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2266 | psa_key_type_t key_type = key_type_arg; |
| 2267 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2268 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2269 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2270 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2271 | size_t mac_buffer_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2272 | 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] | 2273 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2274 | const size_t output_sizes_to_test[] = { |
| 2275 | 0, |
| 2276 | 1, |
| 2277 | expected_mac->len - 1, |
| 2278 | expected_mac->len, |
| 2279 | expected_mac->len + 1, |
| 2280 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2281 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2282 | TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2283 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 3d404d6 | 2020-08-25 23:47:36 +0200 | [diff] [blame] | 2284 | TEST_ASSERT( expected_mac->len == mac_buffer_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2285 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2286 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2287 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2288 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2289 | psa_set_key_algorithm( &attributes, alg ); |
| 2290 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2291 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2292 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2293 | &key ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2294 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2295 | for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ ) |
| 2296 | { |
| 2297 | const size_t output_size = output_sizes_to_test[i]; |
| 2298 | psa_status_t expected_status = |
| 2299 | ( output_size >= expected_mac->len ? PSA_SUCCESS : |
| 2300 | PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2301 | |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2302 | mbedtls_test_set_step( output_size ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2303 | ASSERT_ALLOC( actual_mac, output_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2304 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2305 | /* Calculate the MAC. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2306 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2307 | PSA_ASSERT( psa_mac_update( &operation, |
| 2308 | input->x, input->len ) ); |
| 2309 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2310 | actual_mac, output_size, |
| 2311 | &mac_length ), |
| 2312 | expected_status ); |
| 2313 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2314 | |
| 2315 | if( expected_status == PSA_SUCCESS ) |
| 2316 | { |
| 2317 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 2318 | actual_mac, mac_length ); |
| 2319 | } |
| 2320 | mbedtls_free( actual_mac ); |
| 2321 | actual_mac = NULL; |
| 2322 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2323 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2324 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2325 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2326 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2327 | PSA_DONE( ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2328 | mbedtls_free( actual_mac ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2329 | } |
| 2330 | /* END_CASE */ |
| 2331 | |
| 2332 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2333 | void mac_verify( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2334 | data_t *key_data, |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2335 | int alg_arg, |
| 2336 | data_t *input, |
| 2337 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2338 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2339 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2340 | psa_key_type_t key_type = key_type_arg; |
| 2341 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2342 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2343 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2344 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2345 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 2346 | TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE ); |
| 2347 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2348 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2349 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2350 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2351 | psa_set_key_algorithm( &attributes, alg ); |
| 2352 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2353 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2354 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2355 | &key ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2356 | |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2357 | /* Test the correct MAC. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2358 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2359 | PSA_ASSERT( psa_mac_update( &operation, |
| 2360 | input->x, input->len ) ); |
| 2361 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2362 | expected_mac->x, |
| 2363 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2364 | |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2365 | /* Test a MAC that's too short. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2366 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2367 | PSA_ASSERT( psa_mac_update( &operation, |
| 2368 | input->x, input->len ) ); |
| 2369 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2370 | expected_mac->x, |
| 2371 | expected_mac->len - 1 ), |
| 2372 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2373 | |
| 2374 | /* Test a MAC that's too long. */ |
| 2375 | ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 ); |
| 2376 | memcpy( perturbed_mac, expected_mac->x, expected_mac->len ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2377 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2378 | PSA_ASSERT( psa_mac_update( &operation, |
| 2379 | input->x, input->len ) ); |
| 2380 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2381 | perturbed_mac, |
| 2382 | expected_mac->len + 1 ), |
| 2383 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2384 | |
| 2385 | /* Test changing one byte. */ |
| 2386 | for( size_t i = 0; i < expected_mac->len; i++ ) |
| 2387 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2388 | mbedtls_test_set_step( i ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2389 | perturbed_mac[i] ^= 1; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2390 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2391 | PSA_ASSERT( psa_mac_update( &operation, |
| 2392 | input->x, input->len ) ); |
| 2393 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2394 | perturbed_mac, |
| 2395 | expected_mac->len ), |
| 2396 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2397 | perturbed_mac[i] ^= 1; |
| 2398 | } |
| 2399 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2400 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2401 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2402 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2403 | PSA_DONE( ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2404 | mbedtls_free( perturbed_mac ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2405 | } |
| 2406 | /* END_CASE */ |
| 2407 | |
| 2408 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2409 | void cipher_operation_init( ) |
| 2410 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2411 | const uint8_t input[1] = { 0 }; |
| 2412 | unsigned char output[1] = { 0 }; |
| 2413 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2414 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2415 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2416 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2417 | * to supress the Clang warning for the test. */ |
| 2418 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 2419 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 2420 | psa_cipher_operation_t zero; |
| 2421 | |
| 2422 | memset( &zero, 0, sizeof( zero ) ); |
| 2423 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2424 | /* A freshly-initialized cipher operation should not be usable. */ |
| 2425 | TEST_EQUAL( psa_cipher_update( &func, |
| 2426 | input, sizeof( input ), |
| 2427 | output, sizeof( output ), |
| 2428 | &output_length ), |
| 2429 | PSA_ERROR_BAD_STATE ); |
| 2430 | TEST_EQUAL( psa_cipher_update( &init, |
| 2431 | input, sizeof( input ), |
| 2432 | output, sizeof( output ), |
| 2433 | &output_length ), |
| 2434 | PSA_ERROR_BAD_STATE ); |
| 2435 | TEST_EQUAL( psa_cipher_update( &zero, |
| 2436 | input, sizeof( input ), |
| 2437 | output, sizeof( output ), |
| 2438 | &output_length ), |
| 2439 | PSA_ERROR_BAD_STATE ); |
| 2440 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2441 | /* A default cipher operation should be abortable without error. */ |
| 2442 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 2443 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 2444 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2445 | } |
| 2446 | /* END_CASE */ |
| 2447 | |
| 2448 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2449 | void cipher_setup( int key_type_arg, |
| 2450 | data_t *key, |
| 2451 | int alg_arg, |
| 2452 | int expected_status_arg ) |
| 2453 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2454 | psa_key_type_t key_type = key_type_arg; |
| 2455 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2456 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2457 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2458 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 2459 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2460 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2461 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2462 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2463 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2464 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2465 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 2466 | &operation, &status ) ) |
| 2467 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2468 | TEST_EQUAL( status, expected_status ); |
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 | /* The operation object should be reusable. */ |
| 2471 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 2472 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 2473 | smoke_test_key_data, |
| 2474 | sizeof( smoke_test_key_data ), |
| 2475 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 2476 | &operation, &status ) ) |
| 2477 | goto exit; |
| 2478 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2479 | #endif |
| 2480 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2481 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2482 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2483 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2484 | } |
| 2485 | /* END_CASE */ |
| 2486 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2487 | /* 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] | 2488 | void cipher_bad_order( ) |
| 2489 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2490 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2491 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 2492 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2493 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2494 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2495 | 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] | 2496 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2497 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2498 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 2499 | const uint8_t text[] = { |
| 2500 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 2501 | 0xbb, 0xbb, 0xbb, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2502 | 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] | 2503 | size_t length = 0; |
| 2504 | |
| 2505 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2506 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 2507 | psa_set_key_algorithm( &attributes, alg ); |
| 2508 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2509 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 2510 | &key ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2511 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2512 | /* Call encrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2513 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 2514 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2515 | PSA_ERROR_BAD_STATE ); |
| 2516 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2517 | |
| 2518 | /* Call decrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2519 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 2520 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2521 | PSA_ERROR_BAD_STATE ); |
| 2522 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2523 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2524 | /* Generate an IV without calling setup beforehand. */ |
| 2525 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2526 | buffer, sizeof( buffer ), |
| 2527 | &length ), |
| 2528 | PSA_ERROR_BAD_STATE ); |
| 2529 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2530 | |
| 2531 | /* Generate an IV twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2532 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2533 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 2534 | buffer, sizeof( buffer ), |
| 2535 | &length ) ); |
| 2536 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2537 | buffer, sizeof( buffer ), |
| 2538 | &length ), |
| 2539 | PSA_ERROR_BAD_STATE ); |
| 2540 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2541 | |
| 2542 | /* Generate an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2543 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2544 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2545 | iv, sizeof( iv ) ) ); |
| 2546 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2547 | buffer, sizeof( buffer ), |
| 2548 | &length ), |
| 2549 | PSA_ERROR_BAD_STATE ); |
| 2550 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2551 | |
| 2552 | /* Set an IV without calling setup beforehand. */ |
| 2553 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2554 | iv, sizeof( iv ) ), |
| 2555 | PSA_ERROR_BAD_STATE ); |
| 2556 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2557 | |
| 2558 | /* Set an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2559 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2560 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2561 | iv, sizeof( iv ) ) ); |
| 2562 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2563 | iv, sizeof( iv ) ), |
| 2564 | PSA_ERROR_BAD_STATE ); |
| 2565 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2566 | |
| 2567 | /* Set an IV after it's already generated. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2568 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2569 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 2570 | buffer, sizeof( buffer ), |
| 2571 | &length ) ); |
| 2572 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2573 | iv, sizeof( iv ) ), |
| 2574 | PSA_ERROR_BAD_STATE ); |
| 2575 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2576 | |
| 2577 | /* Call update without calling setup beforehand. */ |
| 2578 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2579 | text, sizeof( text ), |
| 2580 | buffer, sizeof( buffer ), |
| 2581 | &length ), |
| 2582 | PSA_ERROR_BAD_STATE ); |
| 2583 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2584 | |
| 2585 | /* Call update without an IV where an IV is required. */ |
| 2586 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2587 | text, sizeof( text ), |
| 2588 | buffer, sizeof( buffer ), |
| 2589 | &length ), |
| 2590 | PSA_ERROR_BAD_STATE ); |
| 2591 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2592 | |
| 2593 | /* Call update after finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2594 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2595 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2596 | iv, sizeof( iv ) ) ); |
| 2597 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2598 | buffer, sizeof( buffer ), &length ) ); |
| 2599 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2600 | text, sizeof( text ), |
| 2601 | buffer, sizeof( buffer ), |
| 2602 | &length ), |
| 2603 | PSA_ERROR_BAD_STATE ); |
| 2604 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2605 | |
| 2606 | /* Call finish without calling setup beforehand. */ |
| 2607 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2608 | buffer, sizeof( buffer ), &length ), |
| 2609 | PSA_ERROR_BAD_STATE ); |
| 2610 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2611 | |
| 2612 | /* Call finish without an IV where an IV is required. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2613 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2614 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 2615 | * for cipher modes with padding. */ |
| 2616 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2617 | buffer, sizeof( buffer ), &length ), |
| 2618 | PSA_ERROR_BAD_STATE ); |
| 2619 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2620 | |
| 2621 | /* Call finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2622 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2623 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2624 | iv, sizeof( iv ) ) ); |
| 2625 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2626 | buffer, sizeof( buffer ), &length ) ); |
| 2627 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2628 | buffer, sizeof( buffer ), &length ), |
| 2629 | PSA_ERROR_BAD_STATE ); |
| 2630 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2631 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2632 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2633 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2634 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2635 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2636 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2637 | } |
| 2638 | /* END_CASE */ |
| 2639 | |
| 2640 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2641 | void cipher_encrypt( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2642 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2643 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2644 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2645 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2646 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2647 | psa_status_t status; |
| 2648 | psa_key_type_t key_type = key_type_arg; |
| 2649 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2650 | psa_status_t expected_status = expected_status_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2651 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2652 | size_t output_buffer_size = 0; |
| 2653 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2654 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2655 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2656 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2657 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2658 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2659 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2660 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2661 | psa_set_key_algorithm( &attributes, alg ); |
| 2662 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2663 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2664 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2665 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2666 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2667 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2668 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2669 | if( iv->len > 0 ) |
| 2670 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 2671 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2672 | } |
| 2673 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2674 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 2675 | TEST_ASSERT( output_buffer_size <= |
| 2676 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2677 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2678 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2679 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2680 | input->x, input->len, |
| 2681 | output, output_buffer_size, |
| 2682 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2683 | TEST_ASSERT( function_output_length <= |
| 2684 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 2685 | TEST_ASSERT( function_output_length <= |
| 2686 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2687 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2688 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2689 | status = psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 2690 | ( output_buffer_size == 0 ? NULL : |
| 2691 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2692 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2693 | &function_output_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2694 | TEST_ASSERT( function_output_length <= |
| 2695 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 2696 | TEST_ASSERT( function_output_length <= |
| 2697 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2698 | total_output_length += function_output_length; |
| 2699 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2700 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2701 | if( expected_status == PSA_SUCCESS ) |
| 2702 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2703 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2704 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2705 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2706 | } |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2707 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2708 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2709 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2710 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2711 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2712 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2713 | } |
| 2714 | /* END_CASE */ |
| 2715 | |
| 2716 | /* BEGIN_CASE */ |
| 2717 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2718 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2719 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2720 | int first_part_size_arg, |
| 2721 | int output1_length_arg, int output2_length_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2722 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2723 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2724 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2725 | psa_key_type_t key_type = key_type_arg; |
| 2726 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2727 | size_t first_part_size = first_part_size_arg; |
| 2728 | size_t output1_length = output1_length_arg; |
| 2729 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2730 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2731 | size_t output_buffer_size = 0; |
| 2732 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2733 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2734 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2735 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2736 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2737 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2738 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2739 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2740 | psa_set_key_algorithm( &attributes, alg ); |
| 2741 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2742 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2743 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2744 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2745 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2746 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2747 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2748 | if( iv->len > 0 ) |
| 2749 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 2750 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2751 | } |
| 2752 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2753 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 2754 | TEST_ASSERT( output_buffer_size <= |
| 2755 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2756 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2757 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2758 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2759 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 2760 | output, output_buffer_size, |
| 2761 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2762 | TEST_ASSERT( function_output_length == output1_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2763 | TEST_ASSERT( function_output_length <= |
| 2764 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 2765 | TEST_ASSERT( function_output_length <= |
| 2766 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2767 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2768 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2769 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2770 | input->x + first_part_size, |
| 2771 | input->len - first_part_size, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 2772 | ( output_buffer_size == 0 ? NULL : |
| 2773 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2774 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2775 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2776 | TEST_ASSERT( function_output_length == output2_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2777 | TEST_ASSERT( function_output_length <= |
| 2778 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 2779 | alg, |
| 2780 | input->len - first_part_size ) ); |
| 2781 | TEST_ASSERT( function_output_length <= |
| 2782 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2783 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2784 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2785 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 2786 | ( output_buffer_size == 0 ? NULL : |
| 2787 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2788 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2789 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2790 | TEST_ASSERT( function_output_length <= |
| 2791 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 2792 | TEST_ASSERT( function_output_length <= |
| 2793 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2794 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2795 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2796 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2797 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2798 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2799 | |
| 2800 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2801 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2802 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2803 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2804 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2805 | } |
| 2806 | /* END_CASE */ |
| 2807 | |
| 2808 | /* BEGIN_CASE */ |
| 2809 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2810 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2811 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2812 | int first_part_size_arg, |
| 2813 | int output1_length_arg, int output2_length_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2814 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2815 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2816 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2817 | psa_key_type_t key_type = key_type_arg; |
| 2818 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2819 | size_t first_part_size = first_part_size_arg; |
| 2820 | size_t output1_length = output1_length_arg; |
| 2821 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2822 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2823 | size_t output_buffer_size = 0; |
| 2824 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2825 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2826 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2827 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2828 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2829 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2830 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2831 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 2832 | psa_set_key_algorithm( &attributes, alg ); |
| 2833 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2834 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2835 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2836 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2837 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2838 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2839 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 2840 | if( iv->len > 0 ) |
| 2841 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 2842 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2843 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2844 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2845 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 2846 | TEST_ASSERT( output_buffer_size <= |
| 2847 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2848 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2849 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2850 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2851 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2852 | input->x, first_part_size, |
| 2853 | output, output_buffer_size, |
| 2854 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2855 | TEST_ASSERT( function_output_length == output1_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2856 | TEST_ASSERT( function_output_length <= |
| 2857 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 2858 | TEST_ASSERT( function_output_length <= |
| 2859 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2860 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2861 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2862 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2863 | input->x + first_part_size, |
| 2864 | input->len - first_part_size, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 2865 | ( output_buffer_size == 0 ? NULL : |
| 2866 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2867 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2868 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2869 | TEST_ASSERT( function_output_length == output2_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2870 | TEST_ASSERT( function_output_length <= |
| 2871 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 2872 | alg, |
| 2873 | input->len - first_part_size ) ); |
| 2874 | TEST_ASSERT( function_output_length <= |
| 2875 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2876 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2877 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2878 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 2879 | ( output_buffer_size == 0 ? NULL : |
| 2880 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2881 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2882 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2883 | TEST_ASSERT( function_output_length <= |
| 2884 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 2885 | TEST_ASSERT( function_output_length <= |
| 2886 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2887 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2888 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2889 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2890 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2891 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2892 | |
| 2893 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2894 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2895 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2896 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2897 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2898 | } |
| 2899 | /* END_CASE */ |
| 2900 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2901 | /* BEGIN_CASE */ |
| 2902 | void cipher_decrypt( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2903 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2904 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2905 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2906 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2907 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2908 | psa_status_t status; |
| 2909 | psa_key_type_t key_type = key_type_arg; |
| 2910 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2911 | psa_status_t expected_status = expected_status_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2912 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2913 | size_t output_buffer_size = 0; |
| 2914 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2915 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2916 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2917 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2918 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2919 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2920 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2921 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 2922 | psa_set_key_algorithm( &attributes, alg ); |
| 2923 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2924 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2925 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2926 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2927 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2928 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2929 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 2930 | if( iv->len > 0 ) |
| 2931 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 2932 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2933 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2934 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2935 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 2936 | TEST_ASSERT( output_buffer_size <= |
| 2937 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2938 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2939 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2940 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2941 | input->x, input->len, |
| 2942 | output, output_buffer_size, |
| 2943 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2944 | TEST_ASSERT( function_output_length <= |
| 2945 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 2946 | TEST_ASSERT( function_output_length <= |
| 2947 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2948 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2949 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2950 | status = psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 2951 | ( output_buffer_size == 0 ? NULL : |
| 2952 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2953 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2954 | &function_output_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2955 | TEST_ASSERT( function_output_length <= |
| 2956 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 2957 | TEST_ASSERT( function_output_length <= |
| 2958 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2959 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2960 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2961 | |
| 2962 | if( expected_status == PSA_SUCCESS ) |
| 2963 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2964 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2965 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2966 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2967 | } |
| 2968 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2969 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2970 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2971 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2972 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2973 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2974 | } |
| 2975 | /* END_CASE */ |
| 2976 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2977 | /* BEGIN_CASE */ |
| 2978 | void cipher_verify_output( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2979 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2980 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2981 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2982 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2983 | psa_key_type_t key_type = key_type_arg; |
| 2984 | psa_algorithm_t alg = alg_arg; |
mohammad1603 | e6b67a1 | 2018-03-12 10:38:49 -0700 | [diff] [blame] | 2985 | unsigned char iv[16] = {0}; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2986 | size_t iv_size = 16; |
| 2987 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2988 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2989 | size_t output1_size = 0; |
| 2990 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2991 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2992 | size_t output2_size = 0; |
| 2993 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2994 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2995 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 2996 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2997 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2998 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2999 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3000 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3001 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3002 | psa_set_key_algorithm( &attributes, alg ); |
| 3003 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3004 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3005 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3006 | &key ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3007 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3008 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 3009 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3010 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3011 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3012 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3013 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3014 | iv, iv_size, |
| 3015 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3016 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3017 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3018 | TEST_ASSERT( output1_size <= |
| 3019 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3020 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3021 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3022 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len, |
| 3023 | output1, output1_size, |
| 3024 | &output1_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3025 | TEST_ASSERT( output1_length <= |
| 3026 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 3027 | TEST_ASSERT( output1_length <= |
| 3028 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
| 3029 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3030 | PSA_ASSERT( psa_cipher_finish( &operation1, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3031 | output1 + output1_length, |
| 3032 | output1_size - output1_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3033 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3034 | TEST_ASSERT( function_output_length <= |
| 3035 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3036 | TEST_ASSERT( function_output_length <= |
| 3037 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3038 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3039 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3040 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3041 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3042 | |
| 3043 | output2_size = output1_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3044 | TEST_ASSERT( output2_size <= |
| 3045 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 3046 | TEST_ASSERT( output2_size <= |
| 3047 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3048 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3049 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3050 | if( iv_length > 0 ) |
| 3051 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3052 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3053 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3054 | } |
| 3055 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3056 | PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length, |
| 3057 | output2, output2_size, |
| 3058 | &output2_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3059 | TEST_ASSERT( output2_length <= |
| 3060 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 3061 | TEST_ASSERT( output2_length <= |
| 3062 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) ); |
| 3063 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3064 | function_output_length = 0; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3065 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3066 | output2 + output2_length, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3067 | output2_size - output2_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3068 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3069 | TEST_ASSERT( function_output_length <= |
| 3070 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3071 | TEST_ASSERT( function_output_length <= |
| 3072 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3073 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3074 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3075 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3076 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3077 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3078 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3079 | |
| 3080 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3081 | psa_cipher_abort( &operation1 ); |
| 3082 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3083 | mbedtls_free( output1 ); |
| 3084 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3085 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3086 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3087 | } |
| 3088 | /* END_CASE */ |
| 3089 | |
| 3090 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3091 | void cipher_verify_output_multipart( int alg_arg, |
| 3092 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3093 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3094 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3095 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3096 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3097 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3098 | psa_key_type_t key_type = key_type_arg; |
| 3099 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3100 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3101 | unsigned char iv[16] = {0}; |
| 3102 | size_t iv_size = 16; |
| 3103 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3104 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3105 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3106 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3107 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3108 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3109 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3110 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3111 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3112 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3113 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3114 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3115 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3116 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3117 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3118 | psa_set_key_algorithm( &attributes, alg ); |
| 3119 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3120 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3121 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3122 | &key ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3123 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3124 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 3125 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3126 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3127 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3128 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3129 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3130 | iv, iv_size, |
| 3131 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3132 | } |
| 3133 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3134 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3135 | TEST_ASSERT( output1_buffer_size <= |
| 3136 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3137 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3138 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3139 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3140 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3141 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 3142 | output1, output1_buffer_size, |
| 3143 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3144 | TEST_ASSERT( function_output_length <= |
| 3145 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3146 | TEST_ASSERT( function_output_length <= |
| 3147 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3148 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3149 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3150 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 3151 | input->x + first_part_size, |
| 3152 | input->len - first_part_size, |
| 3153 | output1, output1_buffer_size, |
| 3154 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3155 | TEST_ASSERT( function_output_length <= |
| 3156 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3157 | alg, |
| 3158 | input->len - first_part_size ) ); |
| 3159 | TEST_ASSERT( function_output_length <= |
| 3160 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3161 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3162 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3163 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 3164 | output1 + output1_length, |
| 3165 | output1_buffer_size - output1_length, |
| 3166 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3167 | TEST_ASSERT( function_output_length <= |
| 3168 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3169 | TEST_ASSERT( function_output_length <= |
| 3170 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3171 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3172 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3173 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3174 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3175 | output2_buffer_size = output1_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3176 | TEST_ASSERT( output2_buffer_size <= |
| 3177 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 3178 | TEST_ASSERT( output2_buffer_size <= |
| 3179 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3180 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3181 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3182 | if( iv_length > 0 ) |
| 3183 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3184 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3185 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3186 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3187 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3188 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 3189 | output2, output2_buffer_size, |
| 3190 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3191 | TEST_ASSERT( function_output_length <= |
| 3192 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3193 | TEST_ASSERT( function_output_length <= |
| 3194 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3195 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3196 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3197 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 3198 | output1 + first_part_size, |
| 3199 | output1_length - first_part_size, |
| 3200 | output2, output2_buffer_size, |
| 3201 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3202 | TEST_ASSERT( function_output_length <= |
| 3203 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3204 | alg, |
| 3205 | output1_length - first_part_size ) ); |
| 3206 | TEST_ASSERT( function_output_length <= |
| 3207 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3208 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3209 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3210 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3211 | output2 + output2_length, |
| 3212 | output2_buffer_size - output2_length, |
| 3213 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3214 | TEST_ASSERT( function_output_length <= |
| 3215 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3216 | TEST_ASSERT( function_output_length <= |
| 3217 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3218 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3219 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3220 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3221 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3222 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3223 | |
| 3224 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3225 | psa_cipher_abort( &operation1 ); |
| 3226 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3227 | mbedtls_free( output1 ); |
| 3228 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3229 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3230 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3231 | } |
| 3232 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 3233 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3234 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3235 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3236 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3237 | data_t *nonce, |
| 3238 | data_t *additional_data, |
| 3239 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3240 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3241 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3242 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3243 | psa_key_type_t key_type = key_type_arg; |
| 3244 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3245 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3246 | unsigned char *output_data = NULL; |
| 3247 | size_t output_size = 0; |
| 3248 | size_t output_length = 0; |
| 3249 | unsigned char *output_data2 = NULL; |
| 3250 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 3251 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3252 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3253 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3254 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3255 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3256 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3257 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3258 | psa_set_key_algorithm( &attributes, alg ); |
| 3259 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3260 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3261 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3262 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3263 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3264 | key_bits = psa_get_key_bits( &attributes ); |
| 3265 | |
| 3266 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3267 | alg ); |
| 3268 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3269 | * should be exact. */ |
| 3270 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 3271 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 3272 | { |
| 3273 | TEST_EQUAL( output_size, |
| 3274 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3275 | TEST_ASSERT( output_size <= |
| 3276 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3277 | } |
| 3278 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3279 | |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 3280 | status = psa_aead_encrypt( key, alg, |
| 3281 | nonce->x, nonce->len, |
| 3282 | additional_data->x, |
| 3283 | additional_data->len, |
| 3284 | input_data->x, input_data->len, |
| 3285 | output_data, output_size, |
| 3286 | &output_length ); |
| 3287 | |
| 3288 | /* If the operation is not supported, just skip and not fail in case the |
| 3289 | * encryption involves a common limitation of cryptography hardwares and |
| 3290 | * an alternative implementation. */ |
| 3291 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 3292 | { |
| 3293 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3294 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 3295 | } |
| 3296 | |
| 3297 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3298 | |
| 3299 | if( PSA_SUCCESS == expected_result ) |
| 3300 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3301 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3302 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3303 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3304 | * should be exact. */ |
| 3305 | TEST_EQUAL( input_data->len, |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3306 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) ); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3307 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3308 | TEST_ASSERT( input_data->len <= |
| 3309 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) ); |
| 3310 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3311 | TEST_EQUAL( psa_aead_decrypt( key, alg, |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3312 | nonce->x, nonce->len, |
| 3313 | additional_data->x, |
| 3314 | additional_data->len, |
| 3315 | output_data, output_length, |
| 3316 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3317 | &output_length2 ), |
| 3318 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3319 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3320 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 3321 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3322 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3323 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3324 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3325 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3326 | mbedtls_free( output_data ); |
| 3327 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3328 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3329 | } |
| 3330 | /* END_CASE */ |
| 3331 | |
| 3332 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3333 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 3334 | int alg_arg, |
| 3335 | data_t *nonce, |
| 3336 | data_t *additional_data, |
| 3337 | data_t *input_data, |
| 3338 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3339 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3340 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3341 | psa_key_type_t key_type = key_type_arg; |
| 3342 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3343 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3344 | unsigned char *output_data = NULL; |
| 3345 | size_t output_size = 0; |
| 3346 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3347 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3348 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3349 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3350 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3351 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3352 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3353 | psa_set_key_algorithm( &attributes, alg ); |
| 3354 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3355 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3356 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3357 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3358 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3359 | key_bits = psa_get_key_bits( &attributes ); |
| 3360 | |
| 3361 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3362 | alg ); |
| 3363 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3364 | * should be exact. */ |
| 3365 | TEST_EQUAL( output_size, |
| 3366 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3367 | TEST_ASSERT( output_size <= |
| 3368 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3369 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3370 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3371 | status = psa_aead_encrypt( key, alg, |
| 3372 | nonce->x, nonce->len, |
| 3373 | additional_data->x, additional_data->len, |
| 3374 | input_data->x, input_data->len, |
| 3375 | output_data, output_size, |
| 3376 | &output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3377 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3378 | /* If the operation is not supported, just skip and not fail in case the |
| 3379 | * encryption involves a common limitation of cryptography hardwares and |
| 3380 | * an alternative implementation. */ |
| 3381 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3382 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3383 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3384 | 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] | 3385 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3386 | |
| 3387 | PSA_ASSERT( status ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3388 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 3389 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3390 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3391 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3392 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3393 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3394 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3395 | } |
| 3396 | /* END_CASE */ |
| 3397 | |
| 3398 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3399 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 3400 | int alg_arg, |
| 3401 | data_t *nonce, |
| 3402 | data_t *additional_data, |
| 3403 | data_t *input_data, |
| 3404 | data_t *expected_data, |
| 3405 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3406 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3407 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3408 | psa_key_type_t key_type = key_type_arg; |
| 3409 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3410 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3411 | unsigned char *output_data = NULL; |
| 3412 | size_t output_size = 0; |
| 3413 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3414 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3415 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3416 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3417 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3418 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3419 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3420 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3421 | psa_set_key_algorithm( &attributes, alg ); |
| 3422 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3423 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3424 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3425 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3426 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3427 | key_bits = psa_get_key_bits( &attributes ); |
| 3428 | |
| 3429 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3430 | alg ); |
| 3431 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 3432 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 3433 | { |
| 3434 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3435 | * should be exact. */ |
| 3436 | TEST_EQUAL( output_size, |
| 3437 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3438 | TEST_ASSERT( output_size <= |
| 3439 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3440 | } |
| 3441 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3442 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3443 | status = psa_aead_decrypt( key, alg, |
| 3444 | nonce->x, nonce->len, |
| 3445 | additional_data->x, |
| 3446 | additional_data->len, |
| 3447 | input_data->x, input_data->len, |
| 3448 | output_data, output_size, |
| 3449 | &output_length ); |
| 3450 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3451 | /* If the operation is not supported, just skip and not fail in case the |
| 3452 | * decryption involves a common limitation of cryptography hardwares and |
| 3453 | * an alternative implementation. */ |
| 3454 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3455 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3456 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3457 | 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] | 3458 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3459 | |
| 3460 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3461 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3462 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3463 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 3464 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3465 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3466 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3467 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3468 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3469 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3470 | } |
| 3471 | /* END_CASE */ |
| 3472 | |
| 3473 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3474 | void aead_multipart_encrypt( int key_type_arg, data_t *key_data, |
| 3475 | int alg_arg, |
| 3476 | data_t *nonce, |
| 3477 | data_t *additional_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3478 | int do_test_ad_chunked, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3479 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3480 | int do_test_data_chunked, |
| 3481 | int do_set_lengths, |
| 3482 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3483 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3484 | size_t ad_part_len = 0; |
| 3485 | size_t data_part_len = 0; |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 3486 | setlengths_method set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3487 | |
Paul Elliott | e64deda | 2021-09-09 14:07:23 +0100 | [diff] [blame] | 3488 | /* Ensure that either one part of the test or the other is done, i.e this |
| 3489 | * test does something. */ |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3490 | TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked ); |
| 3491 | |
| 3492 | /* Temporary whilst we have algorithms that cannot support chunking */ |
| 3493 | if( do_test_ad_chunked == 1 ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3494 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3495 | for( ad_part_len = 1; ad_part_len <= additional_data->len; |
| 3496 | ad_part_len++ ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3497 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3498 | mbedtls_test_set_step( ad_part_len ); |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3499 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 3500 | if( do_set_lengths ) |
| 3501 | { |
| 3502 | if( ad_part_len & 0x01 ) |
| 3503 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 3504 | else |
| 3505 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
| 3506 | } |
| 3507 | |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 3508 | /* Split ad into length(ad_part_len) parts. */ |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3509 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3510 | alg_arg, nonce, |
| 3511 | additional_data, |
| 3512 | ad_part_len, |
| 3513 | input_data, -1, |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 3514 | set_lengths_method, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3515 | expected_output, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 3516 | 1, 0 ) ) |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 3517 | break; |
| 3518 | |
| 3519 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 3520 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 3521 | |
| 3522 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3523 | alg_arg, nonce, |
| 3524 | additional_data, |
| 3525 | ad_part_len, |
| 3526 | input_data, -1, |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 3527 | set_lengths_method, |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 3528 | expected_output, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 3529 | 1, 1 ) ) |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3530 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3531 | } |
| 3532 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3533 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3534 | /* Temporary whilst we have algorithms that cannot support chunking */ |
| 3535 | if( do_test_data_chunked == 1 ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3536 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3537 | for( data_part_len = 1; data_part_len <= input_data->len; |
| 3538 | data_part_len++ ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3539 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 3540 | /* Split data into length(data_part_len) parts. */ |
| 3541 | mbedtls_test_set_step( 2000 + data_part_len ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3542 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 3543 | if( do_set_lengths ) |
| 3544 | { |
| 3545 | if( data_part_len & 0x01 ) |
| 3546 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 3547 | else |
| 3548 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
| 3549 | } |
| 3550 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3551 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3552 | alg_arg, nonce, |
| 3553 | additional_data, -1, |
| 3554 | input_data, data_part_len, |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 3555 | set_lengths_method, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3556 | expected_output, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 3557 | 1, 0 ) ) |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 3558 | break; |
| 3559 | |
| 3560 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 3561 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 3562 | |
| 3563 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3564 | alg_arg, nonce, |
| 3565 | additional_data, -1, |
| 3566 | input_data, data_part_len, |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 3567 | set_lengths_method, |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 3568 | expected_output, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 3569 | 1, 1 ) ) |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3570 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3571 | } |
| 3572 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3573 | |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3574 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 3575 | /* Goto is required to silence warnings about unused labels, as we |
| 3576 | * don't actually do any test assertions in this function. */ |
| 3577 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3578 | } |
| 3579 | /* END_CASE */ |
| 3580 | |
| 3581 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3582 | void aead_multipart_decrypt( int key_type_arg, data_t *key_data, |
| 3583 | int alg_arg, |
| 3584 | data_t *nonce, |
| 3585 | data_t *additional_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3586 | int do_test_ad_chunked, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3587 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3588 | int do_test_data_chunked, |
| 3589 | int do_set_lengths, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 3590 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3591 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3592 | size_t ad_part_len = 0; |
| 3593 | size_t data_part_len = 0; |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 3594 | setlengths_method set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3595 | |
Paul Elliott | e64deda | 2021-09-09 14:07:23 +0100 | [diff] [blame] | 3596 | /* Ensure that either one part of the test or the other is done, i.e this |
| 3597 | * test does something. */ |
| 3598 | TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked ); |
| 3599 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3600 | /* Temporary whilst we have algorithms that cannot support chunking */ |
| 3601 | if( do_test_ad_chunked == 1 ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3602 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3603 | for( ad_part_len = 1; ad_part_len <= additional_data->len; |
| 3604 | ad_part_len++ ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3605 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 3606 | /* Split ad into length(ad_part_len) parts. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3607 | mbedtls_test_set_step( ad_part_len ); |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3608 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 3609 | if( do_set_lengths ) |
| 3610 | { |
| 3611 | if( ad_part_len & 0x01 ) |
| 3612 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 3613 | else |
| 3614 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
| 3615 | } |
| 3616 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3617 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3618 | alg_arg, nonce, |
| 3619 | additional_data, |
| 3620 | ad_part_len, |
| 3621 | input_data, -1, |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 3622 | set_lengths_method, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3623 | expected_output, |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 3624 | 0, 0 ) ) |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 3625 | break; |
| 3626 | |
| 3627 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 3628 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 3629 | |
| 3630 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3631 | alg_arg, nonce, |
| 3632 | additional_data, |
| 3633 | ad_part_len, |
| 3634 | input_data, -1, |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 3635 | set_lengths_method, |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 3636 | expected_output, |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 3637 | 0, 1 ) ) |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3638 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3639 | } |
| 3640 | } |
| 3641 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3642 | /* Temporary whilst we have algorithms that cannot support chunking */ |
| 3643 | if( do_test_data_chunked == 1 ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3644 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3645 | for( data_part_len = 1; data_part_len <= input_data->len; |
| 3646 | data_part_len++ ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3647 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 3648 | /* Split data into length(data_part_len) parts. */ |
| 3649 | mbedtls_test_set_step( 2000 + data_part_len ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3650 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 3651 | if( do_set_lengths ) |
| 3652 | { |
| 3653 | if( data_part_len & 0x01 ) |
| 3654 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 3655 | else |
| 3656 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
| 3657 | } |
| 3658 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3659 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3660 | alg_arg, nonce, |
| 3661 | additional_data, -1, |
| 3662 | input_data, data_part_len, |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 3663 | set_lengths_method, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3664 | expected_output, |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 3665 | 0, 0 ) ) |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 3666 | break; |
| 3667 | |
| 3668 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 3669 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 3670 | |
| 3671 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3672 | alg_arg, nonce, |
| 3673 | additional_data, -1, |
| 3674 | input_data, data_part_len, |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 3675 | set_lengths_method, |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 3676 | expected_output, |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 3677 | 0, 1 ) ) |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3678 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3679 | } |
| 3680 | } |
| 3681 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 3682 | /* Goto is required to silence warnings about unused labels, as we |
| 3683 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3684 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3685 | } |
| 3686 | /* END_CASE */ |
| 3687 | |
| 3688 | /* BEGIN_CASE */ |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3689 | void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data, |
| 3690 | int alg_arg, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 3691 | int nonce_length, |
| 3692 | int expected_nonce_length_arg, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 3693 | data_t *additional_data, |
| 3694 | data_t *input_data, |
| 3695 | int expected_status_arg ) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3696 | { |
| 3697 | |
| 3698 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3699 | psa_key_type_t key_type = key_type_arg; |
| 3700 | psa_algorithm_t alg = alg_arg; |
| 3701 | psa_aead_operation_t operation; |
| 3702 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 3703 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3704 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 3705 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 3706 | size_t actual_nonce_length = 0; |
| 3707 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 3708 | unsigned char *output = NULL; |
| 3709 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 3710 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 3711 | size_t ciphertext_size = 0; |
| 3712 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 3713 | size_t tag_length = 0; |
| 3714 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3715 | |
| 3716 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3717 | |
| 3718 | psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3719 | psa_set_key_algorithm( & attributes, alg ); |
| 3720 | psa_set_key_type( & attributes, key_type ); |
| 3721 | |
| 3722 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3723 | &key ) ); |
| 3724 | |
| 3725 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3726 | |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 3727 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 3728 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 3729 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 3730 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 3731 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 3732 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 3733 | TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 3734 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 3735 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 3736 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3737 | operation = psa_aead_operation_init( ); |
| 3738 | |
| 3739 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 3740 | |
| 3741 | /* If the operation is not supported, just skip and not fail in case the |
| 3742 | * encryption involves a common limitation of cryptography hardwares and |
| 3743 | * an alternative implementation. */ |
| 3744 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 3745 | { |
| 3746 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 3747 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3748 | } |
| 3749 | |
| 3750 | PSA_ASSERT( status ); |
| 3751 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3752 | status = psa_aead_generate_nonce( &operation, nonce_buffer, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 3753 | nonce_length, |
| 3754 | &actual_nonce_length ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3755 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 3756 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 3757 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 3758 | TEST_EQUAL( actual_nonce_length, expected_nonce_length ); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 3759 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 3760 | TEST_ASSERT( actual_nonce_length < PSA_AEAD_NONCE_MAX_SIZE ); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 3761 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 3762 | if( expected_status == PSA_SUCCESS ) |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 3763 | { |
| 3764 | |
| 3765 | /* Ensure we can still complete operation. */ |
| 3766 | |
| 3767 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 3768 | additional_data->len ) ); |
| 3769 | |
| 3770 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 3771 | output, output_size, |
| 3772 | &ciphertext_length ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 3773 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 3774 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 3775 | &ciphertext_length, tag_buffer, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 3776 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 3777 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3778 | |
| 3779 | exit: |
| 3780 | psa_destroy_key( key ); |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 3781 | mbedtls_free( output ); |
| 3782 | mbedtls_free( ciphertext ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3783 | psa_aead_abort( &operation ); |
| 3784 | PSA_DONE( ); |
| 3785 | } |
| 3786 | /* END_CASE */ |
| 3787 | |
| 3788 | /* BEGIN_CASE */ |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 3789 | void aead_multipart_set_nonce( int key_type_arg, data_t *key_data, |
| 3790 | int alg_arg, |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 3791 | int nonce_length_arg, |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 3792 | data_t *additional_data, |
| 3793 | data_t *input_data, |
| 3794 | int expected_status_arg ) |
| 3795 | { |
| 3796 | |
| 3797 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3798 | psa_key_type_t key_type = key_type_arg; |
| 3799 | psa_algorithm_t alg = alg_arg; |
| 3800 | psa_aead_operation_t operation; |
| 3801 | uint8_t *nonce_buffer = NULL; |
| 3802 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3803 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3804 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 3805 | unsigned char *output = NULL; |
| 3806 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 3807 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 3808 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 3809 | size_t ciphertext_size = 0; |
| 3810 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 3811 | size_t tag_length = 0; |
| 3812 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 3813 | size_t index = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 3814 | |
| 3815 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3816 | |
| 3817 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3818 | psa_set_key_algorithm( &attributes, alg ); |
| 3819 | psa_set_key_type( &attributes, key_type ); |
| 3820 | |
| 3821 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3822 | &key ) ); |
| 3823 | |
| 3824 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3825 | |
| 3826 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 3827 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 3828 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 3829 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 3830 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 3831 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 3832 | TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 3833 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 3834 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 3835 | |
| 3836 | operation = psa_aead_operation_init( ); |
| 3837 | |
| 3838 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 3839 | |
| 3840 | /* If the operation is not supported, just skip and not fail in case the |
| 3841 | * encryption involves a common limitation of cryptography hardwares and |
| 3842 | * an alternative implementation. */ |
| 3843 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 3844 | { |
| 3845 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 3846 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 3847 | } |
| 3848 | |
| 3849 | PSA_ASSERT( status ); |
| 3850 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 3851 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
| 3852 | if( nonce_length_arg == -1 ) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 3853 | { |
Paul Elliott | 5e69aa5 | 2021-08-25 17:24:37 +0100 | [diff] [blame] | 3854 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
| 3855 | ASSERT_ALLOC( nonce_buffer, 4 ); |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 3856 | nonce_length = 0; |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 3857 | } |
| 3858 | else |
| 3859 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 3860 | /* If length is zero, then this will return NULL. */ |
| 3861 | nonce_length = ( size_t ) nonce_length_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 3862 | ASSERT_ALLOC( nonce_buffer, nonce_length ); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 3863 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 3864 | if( nonce_buffer ) |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 3865 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 3866 | for( index = 0; index < nonce_length - 1; ++index ) |
| 3867 | { |
| 3868 | nonce_buffer[index] = 'a' + index; |
| 3869 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 3870 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 3871 | } |
| 3872 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 3873 | status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 3874 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 3875 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 3876 | |
| 3877 | if( expected_status == PSA_SUCCESS ) |
| 3878 | { |
| 3879 | /* Ensure we can still complete operation. */ |
| 3880 | |
| 3881 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 3882 | additional_data->len ) ); |
| 3883 | |
| 3884 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 3885 | output, output_size, |
| 3886 | &ciphertext_length ) ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 3887 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 3888 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 3889 | &ciphertext_length, tag_buffer, |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 3890 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 3891 | } |
| 3892 | |
| 3893 | exit: |
| 3894 | psa_destroy_key( key ); |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 3895 | mbedtls_free( output ); |
| 3896 | mbedtls_free( ciphertext ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 3897 | mbedtls_free( nonce_buffer ); |
| 3898 | psa_aead_abort( &operation ); |
| 3899 | PSA_DONE( ); |
| 3900 | } |
| 3901 | /* END_CASE */ |
| 3902 | |
| 3903 | /* BEGIN_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 3904 | void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data, |
| 3905 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 3906 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 3907 | data_t *nonce, |
| 3908 | data_t *additional_data, |
| 3909 | data_t *input_data, |
| 3910 | int expected_status_arg ) |
| 3911 | { |
| 3912 | |
| 3913 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3914 | psa_key_type_t key_type = key_type_arg; |
| 3915 | psa_algorithm_t alg = alg_arg; |
| 3916 | psa_aead_operation_t operation; |
| 3917 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3918 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3919 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 3920 | unsigned char *output = NULL; |
| 3921 | unsigned char *ciphertext = NULL; |
| 3922 | size_t output_size = output_size_arg; |
| 3923 | size_t ciphertext_size = 0; |
| 3924 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 3925 | size_t tag_length = 0; |
| 3926 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 3927 | |
| 3928 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3929 | |
| 3930 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3931 | psa_set_key_algorithm( &attributes, alg ); |
| 3932 | psa_set_key_type( &attributes, key_type ); |
| 3933 | |
| 3934 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3935 | &key ) ); |
| 3936 | |
| 3937 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3938 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 3939 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 3940 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 3941 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 3942 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 3943 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 3944 | |
| 3945 | operation = psa_aead_operation_init( ); |
| 3946 | |
| 3947 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 3948 | |
| 3949 | /* If the operation is not supported, just skip and not fail in case the |
| 3950 | * encryption involves a common limitation of cryptography hardwares and |
| 3951 | * an alternative implementation. */ |
| 3952 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 3953 | { |
| 3954 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3955 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 3956 | } |
| 3957 | |
| 3958 | PSA_ASSERT( status ); |
| 3959 | |
| 3960 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 3961 | |
| 3962 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 3963 | additional_data->len ) ); |
| 3964 | |
| 3965 | status = psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 3966 | output, output_size, &ciphertext_length ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 3967 | |
| 3968 | TEST_EQUAL( status, expected_status ); |
| 3969 | |
| 3970 | if( expected_status == PSA_SUCCESS ) |
| 3971 | { |
| 3972 | /* Ensure we can still complete operation. */ |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 3973 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 3974 | &ciphertext_length, tag_buffer, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 3975 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 3976 | } |
| 3977 | |
| 3978 | exit: |
| 3979 | psa_destroy_key( key ); |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 3980 | mbedtls_free( output ); |
| 3981 | mbedtls_free( ciphertext ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 3982 | psa_aead_abort( &operation ); |
| 3983 | PSA_DONE( ); |
| 3984 | } |
| 3985 | /* END_CASE */ |
| 3986 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 3987 | /* BEGIN_CASE */ |
| 3988 | void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data, |
| 3989 | int alg_arg, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 3990 | int finish_ciphertext_size_arg, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 3991 | int tag_size_arg, |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 3992 | data_t *nonce, |
| 3993 | data_t *additional_data, |
| 3994 | data_t *input_data, |
| 3995 | int expected_status_arg ) |
| 3996 | { |
| 3997 | |
| 3998 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3999 | psa_key_type_t key_type = key_type_arg; |
| 4000 | psa_algorithm_t alg = alg_arg; |
| 4001 | psa_aead_operation_t operation; |
| 4002 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4003 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 4004 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 4005 | unsigned char *ciphertext = NULL; |
| 4006 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 4007 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 4008 | size_t ciphertext_size = 0; |
| 4009 | size_t ciphertext_length = 0; |
| 4010 | size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 4011 | size_t tag_size = ( size_t ) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4012 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4013 | |
| 4014 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4015 | |
| 4016 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4017 | psa_set_key_algorithm( &attributes, alg ); |
| 4018 | psa_set_key_type( &attributes, key_type ); |
| 4019 | |
| 4020 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4021 | &key ) ); |
| 4022 | |
| 4023 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4024 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 4025 | ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4026 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 4027 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4028 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 4029 | ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4030 | |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 4031 | ASSERT_ALLOC( tag_buffer, tag_size ); |
| 4032 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4033 | operation = psa_aead_operation_init( ); |
| 4034 | |
| 4035 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 4036 | |
| 4037 | /* If the operation is not supported, just skip and not fail in case the |
| 4038 | * encryption involves a common limitation of cryptography hardwares and |
| 4039 | * an alternative implementation. */ |
| 4040 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4041 | { |
| 4042 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4043 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 4044 | } |
| 4045 | |
| 4046 | PSA_ASSERT( status ); |
| 4047 | |
| 4048 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4049 | |
| 4050 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 4051 | additional_data->len ) ); |
| 4052 | |
| 4053 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 4054 | ciphertext, ciphertext_size, &ciphertext_length ) ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4055 | |
| 4056 | /* Ensure we can still complete operation. */ |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 4057 | status = psa_aead_finish( &operation, finish_ciphertext, |
| 4058 | finish_ciphertext_size, |
| 4059 | &ciphertext_length, tag_buffer, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 4060 | tag_size, &tag_length ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4061 | |
| 4062 | TEST_EQUAL( status, expected_status ); |
| 4063 | |
| 4064 | exit: |
| 4065 | psa_destroy_key( key ); |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 4066 | mbedtls_free( ciphertext ); |
| 4067 | mbedtls_free( finish_ciphertext ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4068 | psa_aead_abort( &operation ); |
| 4069 | PSA_DONE( ); |
| 4070 | } |
| 4071 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 4072 | |
| 4073 | /* BEGIN_CASE */ |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4074 | void aead_multipart_verify( int key_type_arg, data_t *key_data, |
| 4075 | int alg_arg, |
| 4076 | data_t *nonce, |
| 4077 | data_t *additional_data, |
| 4078 | data_t *input_data, |
| 4079 | data_t *tag, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 4080 | int tag_usage_arg, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4081 | int expected_status_arg ) |
| 4082 | { |
| 4083 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4084 | psa_key_type_t key_type = key_type_arg; |
| 4085 | psa_algorithm_t alg = alg_arg; |
| 4086 | psa_aead_operation_t operation; |
| 4087 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4088 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 4089 | psa_status_t expected_status = expected_status_arg; |
| 4090 | unsigned char *plaintext = NULL; |
| 4091 | unsigned char *finish_plaintext = NULL; |
| 4092 | size_t plaintext_size = 0; |
| 4093 | size_t plaintext_length = 0; |
| 4094 | size_t verify_plaintext_size = 0; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 4095 | tagusage_method tag_usage = tag_usage_arg; |
| 4096 | unsigned char *tag_buffer = NULL; |
| 4097 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4098 | |
| 4099 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4100 | |
| 4101 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4102 | psa_set_key_algorithm( &attributes, alg ); |
| 4103 | psa_set_key_type( &attributes, key_type ); |
| 4104 | |
| 4105 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4106 | &key ) ); |
| 4107 | |
| 4108 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4109 | |
| 4110 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 4111 | input_data->len ); |
| 4112 | |
| 4113 | ASSERT_ALLOC( plaintext, plaintext_size ); |
| 4114 | |
| 4115 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg ); |
| 4116 | |
| 4117 | ASSERT_ALLOC( finish_plaintext, verify_plaintext_size ); |
| 4118 | |
| 4119 | operation = psa_aead_operation_init( ); |
| 4120 | |
| 4121 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 4122 | |
| 4123 | /* If the operation is not supported, just skip and not fail in case the |
| 4124 | * encryption involves a common limitation of cryptography hardwares and |
| 4125 | * an alternative implementation. */ |
| 4126 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4127 | { |
| 4128 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4129 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 4130 | } |
| 4131 | |
| 4132 | PSA_ASSERT( status ); |
| 4133 | |
| 4134 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4135 | |
| 4136 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 4137 | additional_data->len ) ); |
| 4138 | |
| 4139 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 4140 | input_data->len, |
| 4141 | plaintext, plaintext_size, |
| 4142 | &plaintext_length ) ); |
| 4143 | |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 4144 | if( tag_usage == USE_GIVEN_TAG ) |
| 4145 | { |
| 4146 | tag_buffer = tag->x; |
| 4147 | tag_size = tag->len; |
| 4148 | } |
| 4149 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4150 | status = psa_aead_verify( &operation, finish_plaintext, |
| 4151 | verify_plaintext_size, |
| 4152 | &plaintext_length, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 4153 | tag_buffer, tag_size ); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4154 | |
| 4155 | TEST_EQUAL( status, expected_status ); |
| 4156 | |
| 4157 | exit: |
| 4158 | psa_destroy_key( key ); |
| 4159 | mbedtls_free( plaintext ); |
| 4160 | mbedtls_free( finish_plaintext ); |
| 4161 | psa_aead_abort( &operation ); |
| 4162 | PSA_DONE( ); |
| 4163 | } |
| 4164 | /* END_CASE */ |
| 4165 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4166 | /* BEGIN_CASE */ |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame^] | 4167 | void aead_multipart_setup( int key_type_arg, data_t *key_data, |
| 4168 | int alg_arg, int expected_status_arg ) |
| 4169 | { |
| 4170 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4171 | psa_key_type_t key_type = key_type_arg; |
| 4172 | psa_algorithm_t alg = alg_arg; |
| 4173 | psa_aead_operation_t operation; |
| 4174 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4175 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 4176 | psa_status_t expected_status = expected_status_arg; |
| 4177 | |
| 4178 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4179 | |
| 4180 | psa_set_key_usage_flags( &attributes, |
| 4181 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4182 | psa_set_key_algorithm( &attributes, alg ); |
| 4183 | psa_set_key_type( &attributes, key_type ); |
| 4184 | |
| 4185 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4186 | &key ) ); |
| 4187 | |
| 4188 | mbedtls_test_set_step( 0 ); |
| 4189 | |
| 4190 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 4191 | |
| 4192 | TEST_EQUAL( status, expected_status ); |
| 4193 | |
| 4194 | psa_aead_abort( &operation ); |
| 4195 | |
| 4196 | operation = psa_aead_operation_init( ); |
| 4197 | |
| 4198 | mbedtls_test_set_step( 1 ); |
| 4199 | |
| 4200 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 4201 | |
| 4202 | TEST_EQUAL(status, expected_status ); |
| 4203 | |
| 4204 | exit: |
| 4205 | psa_destroy_key( key ); |
| 4206 | psa_aead_abort( &operation ); |
| 4207 | PSA_DONE( ); |
| 4208 | } |
| 4209 | /* END_CASE */ |
| 4210 | |
| 4211 | /* BEGIN_CASE */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4212 | void aead_multipart_state_test( int key_type_arg, data_t *key_data, |
| 4213 | int alg_arg, |
| 4214 | data_t *nonce, |
| 4215 | data_t *additional_data, |
| 4216 | data_t *input_data ) |
| 4217 | { |
| 4218 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4219 | psa_key_type_t key_type = key_type_arg; |
| 4220 | psa_algorithm_t alg = alg_arg; |
| 4221 | psa_aead_operation_t operation; |
| 4222 | unsigned char *output_data = NULL; |
| 4223 | unsigned char *final_data = NULL; |
| 4224 | size_t output_size = 0; |
| 4225 | size_t finish_output_size = 0; |
| 4226 | size_t output_length = 0; |
| 4227 | size_t key_bits = 0; |
| 4228 | size_t tag_length = 0; |
| 4229 | size_t tag_size = 0; |
| 4230 | size_t nonce_length = 0; |
| 4231 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 4232 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 4233 | size_t output_part_length = 0; |
| 4234 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4235 | |
| 4236 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4237 | |
| 4238 | psa_set_key_usage_flags( & attributes, |
| 4239 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4240 | psa_set_key_algorithm( & attributes, alg ); |
| 4241 | psa_set_key_type( & attributes, key_type ); |
| 4242 | |
| 4243 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4244 | &key ) ); |
| 4245 | |
| 4246 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4247 | key_bits = psa_get_key_bits( &attributes ); |
| 4248 | |
| 4249 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 4250 | |
| 4251 | TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE ); |
| 4252 | |
| 4253 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 4254 | |
| 4255 | ASSERT_ALLOC( output_data, output_size ); |
| 4256 | |
| 4257 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 4258 | |
| 4259 | TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
| 4260 | |
| 4261 | ASSERT_ALLOC( final_data, finish_output_size ); |
| 4262 | |
| 4263 | /* Test all operations error without calling setup first. */ |
| 4264 | |
| 4265 | operation = psa_aead_operation_init( ); |
| 4266 | |
| 4267 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 4268 | PSA_ERROR_BAD_STATE ); |
| 4269 | |
| 4270 | psa_aead_abort( &operation ); |
| 4271 | |
| 4272 | operation = psa_aead_operation_init( ); |
| 4273 | |
| 4274 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4275 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4276 | &nonce_length ), |
| 4277 | PSA_ERROR_BAD_STATE ); |
| 4278 | |
| 4279 | psa_aead_abort( &operation ); |
| 4280 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 4281 | /* ------------------------------------------------------- */ |
| 4282 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4283 | operation = psa_aead_operation_init( ); |
| 4284 | |
| 4285 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 4286 | input_data->len ), |
| 4287 | PSA_ERROR_BAD_STATE ); |
| 4288 | |
| 4289 | psa_aead_abort( &operation ); |
| 4290 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 4291 | /* ------------------------------------------------------- */ |
| 4292 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4293 | operation = psa_aead_operation_init( ); |
| 4294 | |
| 4295 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 4296 | additional_data->len ), |
| 4297 | PSA_ERROR_BAD_STATE ); |
| 4298 | |
| 4299 | psa_aead_abort( &operation ); |
| 4300 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 4301 | /* ------------------------------------------------------- */ |
| 4302 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4303 | operation = psa_aead_operation_init( ); |
| 4304 | |
| 4305 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 4306 | input_data->len, output_data, |
| 4307 | output_size, &output_length ), |
| 4308 | PSA_ERROR_BAD_STATE ); |
| 4309 | |
| 4310 | psa_aead_abort( &operation ); |
| 4311 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 4312 | /* ------------------------------------------------------- */ |
| 4313 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4314 | operation = psa_aead_operation_init( ); |
| 4315 | |
| 4316 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 4317 | finish_output_size, |
| 4318 | &output_part_length, |
| 4319 | tag_buffer, tag_length, |
| 4320 | &tag_size ), |
| 4321 | PSA_ERROR_BAD_STATE ); |
| 4322 | |
| 4323 | psa_aead_abort( &operation ); |
| 4324 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 4325 | /* ------------------------------------------------------- */ |
| 4326 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4327 | operation = psa_aead_operation_init( ); |
| 4328 | |
| 4329 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 4330 | finish_output_size, |
| 4331 | &output_part_length, |
| 4332 | tag_buffer, |
| 4333 | tag_length ), |
| 4334 | PSA_ERROR_BAD_STATE ); |
| 4335 | |
| 4336 | psa_aead_abort( &operation ); |
| 4337 | |
| 4338 | /* Test for double setups. */ |
| 4339 | |
| 4340 | operation = psa_aead_operation_init( ); |
| 4341 | |
| 4342 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4343 | |
| 4344 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 4345 | PSA_ERROR_BAD_STATE ); |
| 4346 | |
| 4347 | psa_aead_abort( &operation ); |
| 4348 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 4349 | /* ------------------------------------------------------- */ |
| 4350 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4351 | operation = psa_aead_operation_init( ); |
| 4352 | |
| 4353 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 4354 | |
| 4355 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 4356 | PSA_ERROR_BAD_STATE ); |
| 4357 | |
| 4358 | psa_aead_abort( &operation ); |
| 4359 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 4360 | /* ------------------------------------------------------- */ |
| 4361 | |
| 4362 | operation = psa_aead_operation_init( ); |
| 4363 | |
| 4364 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4365 | |
| 4366 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 4367 | PSA_ERROR_BAD_STATE ); |
| 4368 | |
| 4369 | psa_aead_abort( &operation ); |
| 4370 | |
| 4371 | /* ------------------------------------------------------- */ |
| 4372 | |
| 4373 | operation = psa_aead_operation_init( ); |
| 4374 | |
| 4375 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 4376 | |
| 4377 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 4378 | PSA_ERROR_BAD_STATE ); |
| 4379 | |
| 4380 | psa_aead_abort( &operation ); |
| 4381 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4382 | /* Test for not setting a nonce. */ |
| 4383 | |
| 4384 | operation = psa_aead_operation_init( ); |
| 4385 | |
| 4386 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4387 | |
| 4388 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 4389 | additional_data->len ), |
| 4390 | PSA_ERROR_BAD_STATE ); |
| 4391 | |
| 4392 | psa_aead_abort( &operation ); |
| 4393 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 4394 | /* ------------------------------------------------------- */ |
| 4395 | |
| 4396 | operation = psa_aead_operation_init( ); |
| 4397 | |
| 4398 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4399 | |
| 4400 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 4401 | input_data->len, output_data, |
| 4402 | output_size, &output_length ), |
| 4403 | PSA_ERROR_BAD_STATE ); |
| 4404 | |
| 4405 | psa_aead_abort( &operation ); |
| 4406 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4407 | /* Test for double setting nonce. */ |
| 4408 | |
| 4409 | operation = psa_aead_operation_init( ); |
| 4410 | |
| 4411 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4412 | |
| 4413 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4414 | |
| 4415 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 4416 | PSA_ERROR_BAD_STATE ); |
| 4417 | |
| 4418 | psa_aead_abort( &operation ); |
| 4419 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 4420 | /* Test for double generating nonce. */ |
| 4421 | |
| 4422 | operation = psa_aead_operation_init( ); |
| 4423 | |
| 4424 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4425 | |
| 4426 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4427 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4428 | &nonce_length ) ); |
| 4429 | |
| 4430 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4431 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4432 | &nonce_length ), |
| 4433 | PSA_ERROR_BAD_STATE ); |
| 4434 | |
| 4435 | |
| 4436 | psa_aead_abort( &operation ); |
| 4437 | |
| 4438 | /* Test for generate nonce then set and vice versa */ |
| 4439 | |
| 4440 | operation = psa_aead_operation_init( ); |
| 4441 | |
| 4442 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4443 | |
| 4444 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4445 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4446 | &nonce_length ) ); |
| 4447 | |
| 4448 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 4449 | PSA_ERROR_BAD_STATE ); |
| 4450 | |
| 4451 | psa_aead_abort( &operation ); |
| 4452 | |
| 4453 | /* ------------------------------------------------------- */ |
| 4454 | |
| 4455 | operation = psa_aead_operation_init( ); |
| 4456 | |
| 4457 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4458 | |
| 4459 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4460 | |
| 4461 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4462 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4463 | &nonce_length ), |
| 4464 | PSA_ERROR_BAD_STATE ); |
| 4465 | |
| 4466 | psa_aead_abort( &operation ); |
| 4467 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 4468 | /* Test for generating nonce in decrypt setup. */ |
| 4469 | |
| 4470 | operation = psa_aead_operation_init( ); |
| 4471 | |
| 4472 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 4473 | |
| 4474 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4475 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4476 | &nonce_length ), |
| 4477 | PSA_ERROR_BAD_STATE ); |
| 4478 | |
| 4479 | psa_aead_abort( &operation ); |
| 4480 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4481 | /* Test for setting lengths twice. */ |
| 4482 | |
| 4483 | operation = psa_aead_operation_init( ); |
| 4484 | |
| 4485 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4486 | |
| 4487 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4488 | |
| 4489 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4490 | input_data->len ) ); |
| 4491 | |
| 4492 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 4493 | input_data->len ), |
| 4494 | PSA_ERROR_BAD_STATE ); |
| 4495 | |
| 4496 | psa_aead_abort( &operation ); |
| 4497 | |
| 4498 | /* Test for setting lengths after already starting data. */ |
| 4499 | |
| 4500 | operation = psa_aead_operation_init( ); |
| 4501 | |
| 4502 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4503 | |
| 4504 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4505 | |
| 4506 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 4507 | input_data->len, output_data, |
| 4508 | output_size, &output_length ) ); |
| 4509 | |
| 4510 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 4511 | input_data->len ), |
| 4512 | PSA_ERROR_BAD_STATE ); |
| 4513 | |
| 4514 | psa_aead_abort( &operation ); |
| 4515 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 4516 | /* Test for not sending any additional data or data after setting non zero |
| 4517 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4518 | |
| 4519 | operation = psa_aead_operation_init( ); |
| 4520 | |
| 4521 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4522 | |
| 4523 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4524 | |
| 4525 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4526 | input_data->len ) ); |
| 4527 | |
| 4528 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 4529 | finish_output_size, |
| 4530 | &output_part_length, |
| 4531 | tag_buffer, tag_length, |
| 4532 | &tag_size ), |
| 4533 | PSA_ERROR_INVALID_ARGUMENT ); |
| 4534 | |
| 4535 | psa_aead_abort( &operation ); |
| 4536 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 4537 | /* Test for not sending any additional data or data after setting non-zero |
| 4538 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4539 | |
| 4540 | operation = psa_aead_operation_init( ); |
| 4541 | |
| 4542 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 4543 | |
| 4544 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4545 | |
| 4546 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4547 | input_data->len ) ); |
| 4548 | |
| 4549 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 4550 | finish_output_size, |
| 4551 | &output_part_length, |
| 4552 | tag_buffer, |
| 4553 | tag_length ), |
| 4554 | PSA_ERROR_INVALID_ARGUMENT ); |
| 4555 | |
| 4556 | psa_aead_abort( &operation ); |
| 4557 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 4558 | /* Test for not sending any additional data after setting a non-zero length |
| 4559 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4560 | |
| 4561 | operation = psa_aead_operation_init( ); |
| 4562 | |
| 4563 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4564 | |
| 4565 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4566 | |
| 4567 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4568 | input_data->len ) ); |
| 4569 | |
| 4570 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 4571 | input_data->len, output_data, |
| 4572 | output_size, &output_length ), |
| 4573 | PSA_ERROR_INVALID_ARGUMENT ); |
| 4574 | |
| 4575 | psa_aead_abort( &operation ); |
| 4576 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 4577 | /* Test for sending too much additional data after setting lengths. */ |
| 4578 | |
| 4579 | operation = psa_aead_operation_init( ); |
| 4580 | |
| 4581 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4582 | |
| 4583 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4584 | |
| 4585 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 4586 | |
| 4587 | |
| 4588 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 4589 | additional_data->len ), |
| 4590 | PSA_ERROR_INVALID_ARGUMENT ); |
| 4591 | |
| 4592 | psa_aead_abort( &operation ); |
| 4593 | |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 4594 | operation = psa_aead_operation_init( ); |
| 4595 | |
| 4596 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4597 | |
| 4598 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4599 | |
| 4600 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4601 | input_data->len ) ); |
| 4602 | |
| 4603 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 4604 | additional_data->len ) ); |
| 4605 | |
| 4606 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 4607 | 1 ), |
| 4608 | PSA_ERROR_INVALID_ARGUMENT ); |
| 4609 | |
| 4610 | psa_aead_abort( &operation ); |
| 4611 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 4612 | /* Test for sending too much data after setting lengths. */ |
| 4613 | |
| 4614 | operation = psa_aead_operation_init( ); |
| 4615 | |
| 4616 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4617 | |
| 4618 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4619 | |
| 4620 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 4621 | |
| 4622 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 4623 | input_data->len, output_data, |
| 4624 | output_size, &output_length ), |
| 4625 | PSA_ERROR_INVALID_ARGUMENT ); |
| 4626 | |
| 4627 | psa_aead_abort( &operation ); |
| 4628 | |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 4629 | operation = psa_aead_operation_init( ); |
| 4630 | |
| 4631 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4632 | |
| 4633 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4634 | |
| 4635 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4636 | input_data->len ) ); |
| 4637 | |
| 4638 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 4639 | additional_data->len ) ); |
| 4640 | |
| 4641 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 4642 | input_data->len, output_data, |
| 4643 | output_size, &output_length ) ); |
| 4644 | |
| 4645 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 4646 | 1, output_data, |
| 4647 | output_size, &output_length ), |
| 4648 | PSA_ERROR_INVALID_ARGUMENT ); |
| 4649 | |
| 4650 | psa_aead_abort( &operation ); |
| 4651 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4652 | /* Test sending additional data after data. */ |
| 4653 | |
| 4654 | operation = psa_aead_operation_init( ); |
| 4655 | |
| 4656 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4657 | |
| 4658 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4659 | |
| 4660 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 4661 | input_data->len, output_data, |
| 4662 | output_size, &output_length ) ); |
| 4663 | |
| 4664 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 4665 | additional_data->len ), |
| 4666 | PSA_ERROR_BAD_STATE ); |
| 4667 | |
| 4668 | psa_aead_abort( &operation ); |
| 4669 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 4670 | /* Test calling finish on decryption. */ |
| 4671 | |
| 4672 | operation = psa_aead_operation_init( ); |
| 4673 | |
| 4674 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 4675 | |
| 4676 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4677 | |
| 4678 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 4679 | finish_output_size, |
| 4680 | &output_part_length, |
| 4681 | tag_buffer, tag_length, |
| 4682 | &tag_size ), |
| 4683 | PSA_ERROR_BAD_STATE ); |
| 4684 | |
| 4685 | psa_aead_abort( &operation ); |
| 4686 | |
| 4687 | /* Test calling verify on encryption. */ |
| 4688 | |
| 4689 | operation = psa_aead_operation_init( ); |
| 4690 | |
| 4691 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4692 | |
| 4693 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4694 | |
| 4695 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 4696 | finish_output_size, |
| 4697 | &output_part_length, |
| 4698 | tag_buffer, |
| 4699 | tag_length ), |
Paul Elliott | 5b065cb | 2021-06-23 08:33:22 +0100 | [diff] [blame] | 4700 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 4701 | |
| 4702 | psa_aead_abort( &operation ); |
| 4703 | |
| 4704 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4705 | exit: |
| 4706 | psa_destroy_key( key ); |
| 4707 | psa_aead_abort( &operation ); |
| 4708 | mbedtls_free( output_data ); |
| 4709 | mbedtls_free( final_data ); |
| 4710 | PSA_DONE( ); |
| 4711 | } |
| 4712 | /* END_CASE */ |
| 4713 | |
| 4714 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4715 | void signature_size( int type_arg, |
| 4716 | int bits, |
| 4717 | int alg_arg, |
| 4718 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4719 | { |
| 4720 | psa_key_type_t type = type_arg; |
| 4721 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4722 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 4723 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4724 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 4725 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4726 | exit: |
| 4727 | ; |
| 4728 | } |
| 4729 | /* END_CASE */ |
| 4730 | |
| 4731 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 4732 | void sign_hash_deterministic( int key_type_arg, data_t *key_data, |
| 4733 | int alg_arg, data_t *input_data, |
| 4734 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4735 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4736 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4737 | psa_key_type_t key_type = key_type_arg; |
| 4738 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4739 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4740 | unsigned char *signature = NULL; |
| 4741 | size_t signature_size; |
| 4742 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4743 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4744 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4745 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4746 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4747 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4748 | psa_set_key_algorithm( &attributes, alg ); |
| 4749 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 4750 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4751 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4752 | &key ) ); |
| 4753 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4754 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4755 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4756 | /* Allocate a buffer which has the size advertized by the |
| 4757 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4758 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4759 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4760 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4761 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4762 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4763 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4764 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4765 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4766 | input_data->x, input_data->len, |
| 4767 | signature, signature_size, |
| 4768 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4769 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4770 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 4771 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4772 | |
| 4773 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4774 | /* |
| 4775 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4776 | * thus reset them as required. |
| 4777 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4778 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4779 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4780 | psa_destroy_key( key ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 4781 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4782 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4783 | } |
| 4784 | /* END_CASE */ |
| 4785 | |
| 4786 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 4787 | void sign_hash_fail( int key_type_arg, data_t *key_data, |
| 4788 | int alg_arg, data_t *input_data, |
| 4789 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4790 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4791 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4792 | psa_key_type_t key_type = key_type_arg; |
| 4793 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4794 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4795 | psa_status_t actual_status; |
| 4796 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 4797 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 4798 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4799 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4800 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4801 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4802 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4803 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4804 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4805 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4806 | psa_set_key_algorithm( &attributes, alg ); |
| 4807 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 4808 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4809 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4810 | &key ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4811 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4812 | actual_status = psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4813 | input_data->x, input_data->len, |
| 4814 | signature, signature_size, |
| 4815 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4816 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4817 | /* The value of *signature_length is unspecified on error, but |
| 4818 | * whatever it is, it should be less than signature_size, so that |
| 4819 | * if the caller tries to read *signature_length bytes without |
| 4820 | * checking the error code then they don't overflow a buffer. */ |
| 4821 | TEST_ASSERT( signature_length <= signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4822 | |
| 4823 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4824 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4825 | psa_destroy_key( key ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4826 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4827 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4828 | } |
| 4829 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 4830 | |
| 4831 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 4832 | void sign_verify_hash( int key_type_arg, data_t *key_data, |
| 4833 | int alg_arg, data_t *input_data ) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4834 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4835 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4836 | psa_key_type_t key_type = key_type_arg; |
| 4837 | psa_algorithm_t alg = alg_arg; |
| 4838 | size_t key_bits; |
| 4839 | unsigned char *signature = NULL; |
| 4840 | size_t signature_size; |
| 4841 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4842 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4843 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4844 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4845 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4846 | 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] | 4847 | psa_set_key_algorithm( &attributes, alg ); |
| 4848 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4849 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4850 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4851 | &key ) ); |
| 4852 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4853 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4854 | |
| 4855 | /* Allocate a buffer which has the size advertized by the |
| 4856 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4857 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4858 | key_bits, alg ); |
| 4859 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4860 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4861 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4862 | |
| 4863 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4864 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4865 | input_data->x, input_data->len, |
| 4866 | signature, signature_size, |
| 4867 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4868 | /* Check that the signature length looks sensible. */ |
| 4869 | TEST_ASSERT( signature_length <= signature_size ); |
| 4870 | TEST_ASSERT( signature_length > 0 ); |
| 4871 | |
| 4872 | /* Use the library to verify that the signature is correct. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4873 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4874 | input_data->x, input_data->len, |
| 4875 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4876 | |
| 4877 | if( input_data->len != 0 ) |
| 4878 | { |
| 4879 | /* Flip a bit in the input and verify that the signature is now |
| 4880 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 4881 | * because ECDSA may ignore the last few bits of the input. */ |
| 4882 | input_data->x[0] ^= 1; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4883 | TEST_EQUAL( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4884 | input_data->x, input_data->len, |
| 4885 | signature, signature_length ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4886 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4887 | } |
| 4888 | |
| 4889 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4890 | /* |
| 4891 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4892 | * thus reset them as required. |
| 4893 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4894 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4895 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4896 | psa_destroy_key( key ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4897 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4898 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4899 | } |
| 4900 | /* END_CASE */ |
| 4901 | |
| 4902 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 4903 | void verify_hash( int key_type_arg, data_t *key_data, |
| 4904 | int alg_arg, data_t *hash_data, |
| 4905 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4906 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4907 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4908 | psa_key_type_t key_type = key_type_arg; |
| 4909 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4910 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4911 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4912 | TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 4913 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4914 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4915 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4916 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4917 | psa_set_key_algorithm( &attributes, alg ); |
| 4918 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4919 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4920 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4921 | &key ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4922 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4923 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4924 | hash_data->x, hash_data->len, |
| 4925 | signature_data->x, signature_data->len ) ); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 4926 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4927 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4928 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4929 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4930 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4931 | } |
| 4932 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4933 | |
| 4934 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 4935 | void verify_hash_fail( int key_type_arg, data_t *key_data, |
| 4936 | int alg_arg, data_t *hash_data, |
| 4937 | data_t *signature_data, |
| 4938 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4939 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4940 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4941 | psa_key_type_t key_type = key_type_arg; |
| 4942 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4943 | psa_status_t actual_status; |
| 4944 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4945 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4946 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4947 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4948 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4949 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4950 | psa_set_key_algorithm( &attributes, alg ); |
| 4951 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4952 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4953 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4954 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4955 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4956 | actual_status = psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4957 | hash_data->x, hash_data->len, |
| 4958 | signature_data->x, signature_data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4959 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4960 | |
| 4961 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4962 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4963 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4964 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4965 | } |
| 4966 | /* END_CASE */ |
| 4967 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4968 | /* BEGIN_CASE */ |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 4969 | void sign_message_deterministic( int key_type_arg, |
| 4970 | data_t *key_data, |
| 4971 | int alg_arg, |
| 4972 | data_t *input_data, |
| 4973 | data_t *output_data ) |
| 4974 | { |
| 4975 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4976 | psa_key_type_t key_type = key_type_arg; |
| 4977 | psa_algorithm_t alg = alg_arg; |
| 4978 | size_t key_bits; |
| 4979 | unsigned char *signature = NULL; |
| 4980 | size_t signature_size; |
| 4981 | size_t signature_length = 0xdeadbeef; |
| 4982 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4983 | |
| 4984 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4985 | |
| 4986 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 4987 | psa_set_key_algorithm( &attributes, alg ); |
| 4988 | psa_set_key_type( &attributes, key_type ); |
| 4989 | |
| 4990 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4991 | &key ) ); |
| 4992 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4993 | key_bits = psa_get_key_bits( &attributes ); |
| 4994 | |
| 4995 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 4996 | TEST_ASSERT( signature_size != 0 ); |
| 4997 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
| 4998 | ASSERT_ALLOC( signature, signature_size ); |
| 4999 | |
| 5000 | PSA_ASSERT( psa_sign_message( key, alg, |
| 5001 | input_data->x, input_data->len, |
| 5002 | signature, signature_size, |
| 5003 | &signature_length ) ); |
| 5004 | |
| 5005 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 5006 | signature, signature_length ); |
| 5007 | |
| 5008 | exit: |
| 5009 | psa_reset_key_attributes( &attributes ); |
| 5010 | |
| 5011 | psa_destroy_key( key ); |
| 5012 | mbedtls_free( signature ); |
| 5013 | PSA_DONE( ); |
| 5014 | |
| 5015 | } |
| 5016 | /* END_CASE */ |
| 5017 | |
| 5018 | /* BEGIN_CASE */ |
| 5019 | void sign_message_fail( int key_type_arg, |
| 5020 | data_t *key_data, |
| 5021 | int alg_arg, |
| 5022 | data_t *input_data, |
| 5023 | int signature_size_arg, |
| 5024 | int expected_status_arg ) |
| 5025 | { |
| 5026 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5027 | psa_key_type_t key_type = key_type_arg; |
| 5028 | psa_algorithm_t alg = alg_arg; |
| 5029 | size_t signature_size = signature_size_arg; |
| 5030 | psa_status_t actual_status; |
| 5031 | psa_status_t expected_status = expected_status_arg; |
| 5032 | unsigned char *signature = NULL; |
| 5033 | size_t signature_length = 0xdeadbeef; |
| 5034 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5035 | |
| 5036 | ASSERT_ALLOC( signature, signature_size ); |
| 5037 | |
| 5038 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5039 | |
| 5040 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 5041 | psa_set_key_algorithm( &attributes, alg ); |
| 5042 | psa_set_key_type( &attributes, key_type ); |
| 5043 | |
| 5044 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5045 | &key ) ); |
| 5046 | |
| 5047 | actual_status = psa_sign_message( key, alg, |
| 5048 | input_data->x, input_data->len, |
| 5049 | signature, signature_size, |
| 5050 | &signature_length ); |
| 5051 | TEST_EQUAL( actual_status, expected_status ); |
| 5052 | /* The value of *signature_length is unspecified on error, but |
| 5053 | * whatever it is, it should be less than signature_size, so that |
| 5054 | * if the caller tries to read *signature_length bytes without |
| 5055 | * checking the error code then they don't overflow a buffer. */ |
| 5056 | TEST_ASSERT( signature_length <= signature_size ); |
| 5057 | |
| 5058 | exit: |
| 5059 | psa_reset_key_attributes( &attributes ); |
| 5060 | psa_destroy_key( key ); |
| 5061 | mbedtls_free( signature ); |
| 5062 | PSA_DONE( ); |
| 5063 | } |
| 5064 | /* END_CASE */ |
| 5065 | |
| 5066 | /* BEGIN_CASE */ |
| 5067 | void sign_verify_message( int key_type_arg, |
| 5068 | data_t *key_data, |
| 5069 | int alg_arg, |
| 5070 | data_t *input_data ) |
| 5071 | { |
| 5072 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5073 | psa_key_type_t key_type = key_type_arg; |
| 5074 | psa_algorithm_t alg = alg_arg; |
| 5075 | size_t key_bits; |
| 5076 | unsigned char *signature = NULL; |
| 5077 | size_t signature_size; |
| 5078 | size_t signature_length = 0xdeadbeef; |
| 5079 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5080 | |
| 5081 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5082 | |
| 5083 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 5084 | PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 5085 | psa_set_key_algorithm( &attributes, alg ); |
| 5086 | psa_set_key_type( &attributes, key_type ); |
| 5087 | |
| 5088 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5089 | &key ) ); |
| 5090 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5091 | key_bits = psa_get_key_bits( &attributes ); |
| 5092 | |
| 5093 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 5094 | TEST_ASSERT( signature_size != 0 ); |
| 5095 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
| 5096 | ASSERT_ALLOC( signature, signature_size ); |
| 5097 | |
| 5098 | PSA_ASSERT( psa_sign_message( key, alg, |
| 5099 | input_data->x, input_data->len, |
| 5100 | signature, signature_size, |
| 5101 | &signature_length ) ); |
| 5102 | TEST_ASSERT( signature_length <= signature_size ); |
| 5103 | TEST_ASSERT( signature_length > 0 ); |
| 5104 | |
| 5105 | PSA_ASSERT( psa_verify_message( key, alg, |
| 5106 | input_data->x, input_data->len, |
| 5107 | signature, signature_length ) ); |
| 5108 | |
| 5109 | if( input_data->len != 0 ) |
| 5110 | { |
| 5111 | /* Flip a bit in the input and verify that the signature is now |
| 5112 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 5113 | * because ECDSA may ignore the last few bits of the input. */ |
| 5114 | input_data->x[0] ^= 1; |
| 5115 | TEST_EQUAL( psa_verify_message( key, alg, |
| 5116 | input_data->x, input_data->len, |
| 5117 | signature, signature_length ), |
| 5118 | PSA_ERROR_INVALID_SIGNATURE ); |
| 5119 | } |
| 5120 | |
| 5121 | exit: |
| 5122 | psa_reset_key_attributes( &attributes ); |
| 5123 | |
| 5124 | psa_destroy_key( key ); |
| 5125 | mbedtls_free( signature ); |
| 5126 | PSA_DONE( ); |
| 5127 | } |
| 5128 | /* END_CASE */ |
| 5129 | |
| 5130 | /* BEGIN_CASE */ |
| 5131 | void verify_message( int key_type_arg, |
| 5132 | data_t *key_data, |
| 5133 | int alg_arg, |
| 5134 | data_t *input_data, |
| 5135 | data_t *signature_data ) |
| 5136 | { |
| 5137 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5138 | psa_key_type_t key_type = key_type_arg; |
| 5139 | psa_algorithm_t alg = alg_arg; |
| 5140 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5141 | |
| 5142 | TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE ); |
| 5143 | |
| 5144 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5145 | |
| 5146 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 5147 | psa_set_key_algorithm( &attributes, alg ); |
| 5148 | psa_set_key_type( &attributes, key_type ); |
| 5149 | |
| 5150 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5151 | &key ) ); |
| 5152 | |
| 5153 | PSA_ASSERT( psa_verify_message( key, alg, |
| 5154 | input_data->x, input_data->len, |
| 5155 | signature_data->x, signature_data->len ) ); |
| 5156 | |
| 5157 | exit: |
| 5158 | psa_reset_key_attributes( &attributes ); |
| 5159 | psa_destroy_key( key ); |
| 5160 | PSA_DONE( ); |
| 5161 | } |
| 5162 | /* END_CASE */ |
| 5163 | |
| 5164 | /* BEGIN_CASE */ |
| 5165 | void verify_message_fail( int key_type_arg, |
| 5166 | data_t *key_data, |
| 5167 | int alg_arg, |
| 5168 | data_t *hash_data, |
| 5169 | data_t *signature_data, |
| 5170 | int expected_status_arg ) |
| 5171 | { |
| 5172 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5173 | psa_key_type_t key_type = key_type_arg; |
| 5174 | psa_algorithm_t alg = alg_arg; |
| 5175 | psa_status_t actual_status; |
| 5176 | psa_status_t expected_status = expected_status_arg; |
| 5177 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5178 | |
| 5179 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5180 | |
| 5181 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 5182 | psa_set_key_algorithm( &attributes, alg ); |
| 5183 | psa_set_key_type( &attributes, key_type ); |
| 5184 | |
| 5185 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5186 | &key ) ); |
| 5187 | |
| 5188 | actual_status = psa_verify_message( key, alg, |
| 5189 | hash_data->x, hash_data->len, |
| 5190 | signature_data->x, |
| 5191 | signature_data->len ); |
| 5192 | TEST_EQUAL( actual_status, expected_status ); |
| 5193 | |
| 5194 | exit: |
| 5195 | psa_reset_key_attributes( &attributes ); |
| 5196 | psa_destroy_key( key ); |
| 5197 | PSA_DONE( ); |
| 5198 | } |
| 5199 | /* END_CASE */ |
| 5200 | |
| 5201 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5202 | void asymmetric_encrypt( int key_type_arg, |
| 5203 | data_t *key_data, |
| 5204 | int alg_arg, |
| 5205 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5206 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5207 | int expected_output_length_arg, |
| 5208 | int expected_status_arg ) |
| 5209 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5210 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5211 | psa_key_type_t key_type = key_type_arg; |
| 5212 | psa_algorithm_t alg = alg_arg; |
| 5213 | size_t expected_output_length = expected_output_length_arg; |
| 5214 | size_t key_bits; |
| 5215 | unsigned char *output = NULL; |
| 5216 | size_t output_size; |
| 5217 | size_t output_length = ~0; |
| 5218 | psa_status_t actual_status; |
| 5219 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5220 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5221 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5222 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5223 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5224 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5225 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5226 | psa_set_key_algorithm( &attributes, alg ); |
| 5227 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5228 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5229 | &key ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5230 | |
| 5231 | /* Determine the maximum output length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5232 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5233 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5234 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5235 | 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] | 5236 | TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5237 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5238 | |
| 5239 | /* Encrypt the input */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5240 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5241 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5242 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5243 | output, output_size, |
| 5244 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5245 | TEST_EQUAL( actual_status, expected_status ); |
| 5246 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5247 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5248 | /* If the label is empty, the test framework puts a non-null pointer |
| 5249 | * in label->x. Test that a null pointer works as well. */ |
| 5250 | if( label->len == 0 ) |
| 5251 | { |
| 5252 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 5253 | if( output_size != 0 ) |
| 5254 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5255 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5256 | input_data->x, input_data->len, |
| 5257 | NULL, label->len, |
| 5258 | output, output_size, |
| 5259 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5260 | TEST_EQUAL( actual_status, expected_status ); |
| 5261 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5262 | } |
| 5263 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5264 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5265 | /* |
| 5266 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5267 | * thus reset them as required. |
| 5268 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5269 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5270 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5271 | psa_destroy_key( key ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5272 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5273 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5274 | } |
| 5275 | /* END_CASE */ |
| 5276 | |
| 5277 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5278 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 5279 | data_t *key_data, |
| 5280 | int alg_arg, |
| 5281 | data_t *input_data, |
| 5282 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5283 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5284 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5285 | psa_key_type_t key_type = key_type_arg; |
| 5286 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5287 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5288 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5289 | size_t output_size; |
| 5290 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 5291 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5292 | size_t output2_size; |
| 5293 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5294 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5295 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5296 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5297 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5298 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 5299 | psa_set_key_algorithm( &attributes, alg ); |
| 5300 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 5301 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5302 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5303 | &key ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5304 | |
| 5305 | /* Determine the maximum ciphertext length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5306 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5307 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5308 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5309 | 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] | 5310 | TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5311 | ASSERT_ALLOC( output, output_size ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5312 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5313 | output2_size = input_data->len; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5314 | TEST_ASSERT( output2_size <= |
| 5315 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) ); |
| 5316 | TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5317 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5318 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 5319 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 5320 | * the original plaintext because of the non-optional random |
| 5321 | * part of encryption process which prevents using fixed vectors. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5322 | PSA_ASSERT( psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5323 | input_data->x, input_data->len, |
| 5324 | label->x, label->len, |
| 5325 | output, output_size, |
| 5326 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5327 | /* We don't know what ciphertext length to expect, but check that |
| 5328 | * it looks sensible. */ |
| 5329 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 5330 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5331 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5332 | output, output_length, |
| 5333 | label->x, label->len, |
| 5334 | output2, output2_size, |
| 5335 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 5336 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 5337 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5338 | |
| 5339 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5340 | /* |
| 5341 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5342 | * thus reset them as required. |
| 5343 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5344 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5345 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5346 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 5347 | mbedtls_free( output ); |
| 5348 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5349 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5350 | } |
| 5351 | /* END_CASE */ |
| 5352 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5353 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5354 | void asymmetric_decrypt( int key_type_arg, |
| 5355 | data_t *key_data, |
| 5356 | int alg_arg, |
| 5357 | data_t *input_data, |
| 5358 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 5359 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5360 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5361 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5362 | psa_key_type_t key_type = key_type_arg; |
| 5363 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5364 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5365 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 5366 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5367 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5368 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5369 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5370 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5371 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5372 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 5373 | psa_set_key_algorithm( &attributes, alg ); |
| 5374 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 5375 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5376 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5377 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5378 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5379 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5380 | key_bits = psa_get_key_bits( &attributes ); |
| 5381 | |
| 5382 | /* Determine the maximum ciphertext length */ |
| 5383 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 5384 | TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
| 5385 | ASSERT_ALLOC( output, output_size ); |
| 5386 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5387 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5388 | input_data->x, input_data->len, |
| 5389 | label->x, label->len, |
| 5390 | output, |
| 5391 | output_size, |
| 5392 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 5393 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 5394 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5395 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5396 | /* If the label is empty, the test framework puts a non-null pointer |
| 5397 | * in label->x. Test that a null pointer works as well. */ |
| 5398 | if( label->len == 0 ) |
| 5399 | { |
| 5400 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 5401 | if( output_size != 0 ) |
| 5402 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5403 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5404 | input_data->x, input_data->len, |
| 5405 | NULL, label->len, |
| 5406 | output, |
| 5407 | output_size, |
| 5408 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 5409 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 5410 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5411 | } |
| 5412 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5413 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5414 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5415 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 5416 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5417 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5418 | } |
| 5419 | /* END_CASE */ |
| 5420 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5421 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5422 | void asymmetric_decrypt_fail( int key_type_arg, |
| 5423 | data_t *key_data, |
| 5424 | int alg_arg, |
| 5425 | data_t *input_data, |
| 5426 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 5427 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5428 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5429 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5430 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5431 | psa_key_type_t key_type = key_type_arg; |
| 5432 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5433 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 5434 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5435 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5436 | psa_status_t actual_status; |
| 5437 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5438 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5439 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5440 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 5441 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5442 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5443 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5444 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 5445 | psa_set_key_algorithm( &attributes, alg ); |
| 5446 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 5447 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5448 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5449 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5450 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5451 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 5452 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5453 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 5454 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5455 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5456 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5457 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5458 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5459 | /* If the label is empty, the test framework puts a non-null pointer |
| 5460 | * in label->x. Test that a null pointer works as well. */ |
| 5461 | if( label->len == 0 ) |
| 5462 | { |
| 5463 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 5464 | if( output_size != 0 ) |
| 5465 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5466 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5467 | input_data->x, input_data->len, |
| 5468 | NULL, label->len, |
| 5469 | output, output_size, |
| 5470 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5471 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5472 | TEST_ASSERT( output_length <= output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5473 | } |
| 5474 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5475 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5476 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5477 | psa_destroy_key( key ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5478 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5479 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5480 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 5481 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5482 | |
| 5483 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5484 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 5485 | { |
| 5486 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 5487 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 5488 | * though it's OK by the C standard. We could test for this, but we'd need |
| 5489 | * to supress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 5490 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5491 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 5492 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5493 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 5494 | |
| 5495 | memset( &zero, 0, sizeof( zero ) ); |
| 5496 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5497 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5498 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5499 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5500 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5501 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5502 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5503 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 5504 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5505 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5506 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 5507 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 5508 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 5509 | } |
| 5510 | /* END_CASE */ |
| 5511 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 5512 | /* BEGIN_CASE */ |
| 5513 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5514 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5515 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5516 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5517 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5518 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5519 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5520 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 5521 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 5522 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5523 | |
| 5524 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5525 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5526 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5527 | } |
| 5528 | /* END_CASE */ |
| 5529 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5530 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 5531 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 5532 | int expected_status_arg ) |
| 5533 | { |
| 5534 | psa_algorithm_t alg = alg_arg; |
| 5535 | size_t capacity = capacity_arg; |
| 5536 | psa_status_t expected_status = expected_status_arg; |
| 5537 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5538 | |
| 5539 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5540 | |
| 5541 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 5542 | |
| 5543 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 5544 | expected_status ); |
| 5545 | |
| 5546 | exit: |
| 5547 | psa_key_derivation_abort( &operation ); |
| 5548 | PSA_DONE( ); |
| 5549 | } |
| 5550 | /* END_CASE */ |
| 5551 | |
| 5552 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5553 | void derive_input( int alg_arg, |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 5554 | int step_arg1, int key_type_arg1, data_t *input1, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5555 | int expected_status_arg1, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 5556 | int step_arg2, int key_type_arg2, data_t *input2, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5557 | int expected_status_arg2, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 5558 | int step_arg3, int key_type_arg3, data_t *input3, |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 5559 | int expected_status_arg3, |
| 5560 | int output_key_type_arg, int expected_output_status_arg ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5561 | { |
| 5562 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 5563 | psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3}; |
| 5564 | 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] | 5565 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 5566 | expected_status_arg2, |
| 5567 | expected_status_arg3}; |
| 5568 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5569 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 5570 | MBEDTLS_SVC_KEY_ID_INIT, |
| 5571 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5572 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5573 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5574 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 5575 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5576 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 5577 | psa_status_t expected_output_status = expected_output_status_arg; |
| 5578 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5579 | |
| 5580 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5581 | |
| 5582 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5583 | psa_set_key_algorithm( &attributes, alg ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5584 | |
| 5585 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 5586 | |
| 5587 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 5588 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5589 | if( key_types[i] != PSA_KEY_TYPE_NONE ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5590 | { |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 5591 | psa_set_key_type( &attributes, key_types[i] ); |
| 5592 | PSA_ASSERT( psa_import_key( &attributes, |
| 5593 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5594 | &keys[i] ) ); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 5595 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) && |
| 5596 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5597 | { |
| 5598 | // When taking a private key as secret input, use key agreement |
| 5599 | // to add the shared secret to the derivation |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5600 | TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self( |
| 5601 | &operation, keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 5602 | expected_statuses[i] ); |
| 5603 | } |
| 5604 | else |
| 5605 | { |
| 5606 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5607 | keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 5608 | expected_statuses[i] ); |
| 5609 | } |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 5610 | } |
| 5611 | else |
| 5612 | { |
| 5613 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 5614 | &operation, steps[i], |
| 5615 | inputs[i]->x, inputs[i]->len ), |
| 5616 | expected_statuses[i] ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5617 | } |
| 5618 | } |
| 5619 | |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 5620 | if( output_key_type != PSA_KEY_TYPE_NONE ) |
| 5621 | { |
| 5622 | psa_reset_key_attributes( &attributes ); |
| 5623 | psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA ); |
| 5624 | psa_set_key_bits( &attributes, 8 ); |
| 5625 | actual_output_status = |
| 5626 | psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5627 | &output_key ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 5628 | } |
| 5629 | else |
| 5630 | { |
| 5631 | uint8_t buffer[1]; |
| 5632 | actual_output_status = |
| 5633 | psa_key_derivation_output_bytes( &operation, |
| 5634 | buffer, sizeof( buffer ) ); |
| 5635 | } |
| 5636 | TEST_EQUAL( actual_output_status, expected_output_status ); |
| 5637 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5638 | exit: |
| 5639 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5640 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 5641 | psa_destroy_key( keys[i] ); |
| 5642 | psa_destroy_key( output_key ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5643 | PSA_DONE( ); |
| 5644 | } |
| 5645 | /* END_CASE */ |
| 5646 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 5647 | /* BEGIN_CASE */ |
| 5648 | void test_derive_invalid_key_derivation_state( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 5649 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 5650 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5651 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 5652 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5653 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 5654 | unsigned char input1[] = "Input 1"; |
| 5655 | size_t input1_length = sizeof( input1 ); |
| 5656 | unsigned char input2[] = "Input 2"; |
| 5657 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5658 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 5659 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 5660 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 5661 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 5662 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5663 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 5664 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5665 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5666 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5667 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5668 | psa_set_key_algorithm( &attributes, alg ); |
| 5669 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5670 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 5671 | PSA_ASSERT( psa_import_key( &attributes, |
| 5672 | key_data, sizeof( key_data ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5673 | &key ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5674 | |
| 5675 | /* valid key derivation */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5676 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 5677 | input1, input1_length, |
| 5678 | input2, input2_length, |
| 5679 | capacity ) ) |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 5680 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5681 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5682 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 5683 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 5684 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5685 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5686 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5687 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5688 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5689 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5690 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5691 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5692 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5693 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5694 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5695 | } |
| 5696 | /* END_CASE */ |
| 5697 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5698 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5699 | void test_derive_invalid_key_derivation_tests( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5700 | { |
| 5701 | uint8_t output_buffer[16]; |
| 5702 | size_t buffer_size = 16; |
| 5703 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5704 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5705 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5706 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 5707 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5708 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5709 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5710 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5711 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5712 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5713 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5714 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5715 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 5716 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5717 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5718 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5719 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5720 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5721 | |
| 5722 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5723 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 5724 | } |
| 5725 | /* END_CASE */ |
| 5726 | |
| 5727 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5728 | void derive_output( int alg_arg, |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5729 | int step1_arg, data_t *input1, |
| 5730 | int step2_arg, data_t *input2, |
| 5731 | int step3_arg, data_t *input3, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5732 | int requested_capacity_arg, |
| 5733 | data_t *expected_output1, |
| 5734 | data_t *expected_output2 ) |
| 5735 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5736 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5737 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg}; |
| 5738 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5739 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 5740 | MBEDTLS_SVC_KEY_ID_INIT, |
| 5741 | MBEDTLS_SVC_KEY_ID_INIT }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5742 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5743 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5744 | uint8_t *expected_outputs[2] = |
| 5745 | {expected_output1->x, expected_output2->x}; |
| 5746 | size_t output_sizes[2] = |
| 5747 | {expected_output1->len, expected_output2->len}; |
| 5748 | size_t output_buffer_size = 0; |
| 5749 | uint8_t *output_buffer = NULL; |
| 5750 | size_t expected_capacity; |
| 5751 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5752 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5753 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5754 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5755 | |
| 5756 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 5757 | { |
| 5758 | if( output_sizes[i] > output_buffer_size ) |
| 5759 | output_buffer_size = output_sizes[i]; |
| 5760 | if( output_sizes[i] == 0 ) |
| 5761 | expected_outputs[i] = NULL; |
| 5762 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5763 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5764 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5765 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5766 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5767 | psa_set_key_algorithm( &attributes, alg ); |
| 5768 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5769 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5770 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5771 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 5772 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 5773 | requested_capacity ) ); |
| 5774 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5775 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5776 | switch( steps[i] ) |
| 5777 | { |
| 5778 | case 0: |
| 5779 | break; |
| 5780 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5781 | PSA_ASSERT( psa_import_key( &attributes, |
| 5782 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5783 | &keys[i] ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5784 | |
| 5785 | if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 5786 | { |
| 5787 | PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) ); |
| 5788 | TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <= |
| 5789 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ); |
| 5790 | } |
| 5791 | |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5792 | PSA_ASSERT( psa_key_derivation_input_key( |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5793 | &operation, steps[i], keys[i] ) ); |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5794 | break; |
| 5795 | default: |
| 5796 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 5797 | &operation, steps[i], |
| 5798 | inputs[i]->x, inputs[i]->len ) ); |
| 5799 | break; |
| 5800 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5801 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5802 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5803 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5804 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5805 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5806 | expected_capacity = requested_capacity; |
| 5807 | |
| 5808 | /* Expansion phase. */ |
| 5809 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 5810 | { |
| 5811 | /* Read some bytes. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5812 | status = psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5813 | output_buffer, output_sizes[i] ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5814 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 5815 | { |
| 5816 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 5817 | TEST_ASSERT( status == PSA_SUCCESS || |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5818 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5819 | continue; |
| 5820 | } |
| 5821 | else if( expected_capacity == 0 || |
| 5822 | output_sizes[i] > expected_capacity ) |
| 5823 | { |
| 5824 | /* Capacity exceeded. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5825 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5826 | expected_capacity = 0; |
| 5827 | continue; |
| 5828 | } |
| 5829 | /* Success. Check the read data. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5830 | PSA_ASSERT( status ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5831 | if( output_sizes[i] != 0 ) |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5832 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 5833 | expected_outputs[i], output_sizes[i] ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5834 | /* Check the operation status. */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5835 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5836 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5837 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5838 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5839 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5840 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5841 | |
| 5842 | exit: |
| 5843 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5844 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5845 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 5846 | psa_destroy_key( keys[i] ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5847 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5848 | } |
| 5849 | /* END_CASE */ |
| 5850 | |
| 5851 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5852 | void derive_full( int alg_arg, |
| 5853 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 5854 | data_t *input1, |
| 5855 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5856 | int requested_capacity_arg ) |
| 5857 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5858 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5859 | psa_algorithm_t alg = alg_arg; |
| 5860 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5861 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5862 | unsigned char output_buffer[16]; |
| 5863 | size_t expected_capacity = requested_capacity; |
| 5864 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5865 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5866 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5867 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5868 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5869 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5870 | psa_set_key_algorithm( &attributes, alg ); |
| 5871 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5872 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5873 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5874 | &key ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5875 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5876 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 5877 | input1->x, input1->len, |
| 5878 | input2->x, input2->len, |
| 5879 | requested_capacity ) ) |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 5880 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 5881 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5882 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5883 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5884 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5885 | |
| 5886 | /* Expansion phase. */ |
| 5887 | while( current_capacity > 0 ) |
| 5888 | { |
| 5889 | size_t read_size = sizeof( output_buffer ); |
| 5890 | if( read_size > current_capacity ) |
| 5891 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5892 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5893 | output_buffer, |
| 5894 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5895 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5896 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5897 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5898 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5899 | } |
| 5900 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5901 | /* Check that the operation refuses to go over capacity. */ |
| 5902 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5903 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5904 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5905 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5906 | |
| 5907 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5908 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5909 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5910 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5911 | } |
| 5912 | /* END_CASE */ |
| 5913 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 5914 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5915 | void derive_key_exercise( int alg_arg, |
| 5916 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 5917 | data_t *input1, |
| 5918 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5919 | int derived_type_arg, |
| 5920 | int derived_bits_arg, |
| 5921 | int derived_usage_arg, |
| 5922 | int derived_alg_arg ) |
| 5923 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5924 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5925 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5926 | psa_algorithm_t alg = alg_arg; |
| 5927 | psa_key_type_t derived_type = derived_type_arg; |
| 5928 | size_t derived_bits = derived_bits_arg; |
| 5929 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 5930 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 5931 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5932 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5933 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5934 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5935 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5936 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5937 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5938 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5939 | psa_set_key_algorithm( &attributes, alg ); |
| 5940 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5941 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5942 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5943 | |
| 5944 | /* Derive a key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5945 | if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 5946 | input1->x, input1->len, |
| 5947 | input2->x, input2->len, |
| 5948 | capacity ) ) |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 5949 | goto exit; |
| 5950 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5951 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 5952 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 5953 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5954 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5955 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5956 | &derived_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5957 | |
| 5958 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5959 | PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5960 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 5961 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5962 | |
| 5963 | /* Exercise the derived key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5964 | if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5965 | goto exit; |
| 5966 | |
| 5967 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5968 | /* |
| 5969 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5970 | * thus reset them as required. |
| 5971 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5972 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5973 | |
| 5974 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5975 | psa_destroy_key( base_key ); |
| 5976 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5977 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5978 | } |
| 5979 | /* END_CASE */ |
| 5980 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 5981 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5982 | void derive_key_export( int alg_arg, |
| 5983 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 5984 | data_t *input1, |
| 5985 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5986 | int bytes1_arg, |
| 5987 | int bytes2_arg ) |
| 5988 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5989 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5990 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5991 | psa_algorithm_t alg = alg_arg; |
| 5992 | size_t bytes1 = bytes1_arg; |
| 5993 | size_t bytes2 = bytes2_arg; |
| 5994 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5995 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5996 | uint8_t *output_buffer = NULL; |
| 5997 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5998 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5999 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6000 | size_t length; |
| 6001 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6002 | ASSERT_ALLOC( output_buffer, capacity ); |
| 6003 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6004 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6005 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6006 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 6007 | psa_set_key_algorithm( &base_attributes, alg ); |
| 6008 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6009 | 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] | 6010 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6011 | |
| 6012 | /* Derive some material and output it. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 6013 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 6014 | input1->x, input1->len, |
| 6015 | input2->x, input2->len, |
| 6016 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 6017 | goto exit; |
| 6018 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6019 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6020 | output_buffer, |
| 6021 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6022 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6023 | |
| 6024 | /* 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] | 6025 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 6026 | input1->x, input1->len, |
| 6027 | input2->x, input2->len, |
| 6028 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 6029 | goto exit; |
| 6030 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6031 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 6032 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 6033 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 6034 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6035 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6036 | &derived_key ) ); |
| 6037 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6038 | export_buffer, bytes1, |
| 6039 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6040 | TEST_EQUAL( length, bytes1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6041 | PSA_ASSERT( psa_destroy_key( derived_key ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 6042 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6043 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6044 | &derived_key ) ); |
| 6045 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6046 | export_buffer + bytes1, bytes2, |
| 6047 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6048 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6049 | |
| 6050 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 6051 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 6052 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6053 | |
| 6054 | exit: |
| 6055 | mbedtls_free( output_buffer ); |
| 6056 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6057 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6058 | psa_destroy_key( base_key ); |
| 6059 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6060 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6061 | } |
| 6062 | /* END_CASE */ |
| 6063 | |
| 6064 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 6065 | void derive_key( int alg_arg, |
| 6066 | data_t *key_data, data_t *input1, data_t *input2, |
| 6067 | int type_arg, int bits_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 6068 | int expected_status_arg, |
| 6069 | int is_large_output ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 6070 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6071 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6072 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 6073 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 6074 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 6075 | size_t bits = bits_arg; |
| 6076 | psa_status_t expected_status = expected_status_arg; |
| 6077 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 6078 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6079 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6080 | |
| 6081 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6082 | |
| 6083 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 6084 | psa_set_key_algorithm( &base_attributes, alg ); |
| 6085 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 6086 | 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] | 6087 | &base_key ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 6088 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 6089 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 6090 | input1->x, input1->len, |
| 6091 | input2->x, input2->len, |
| 6092 | SIZE_MAX ) ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 6093 | goto exit; |
| 6094 | |
| 6095 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 6096 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 6097 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 6098 | psa_set_key_bits( &derived_attributes, bits ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 6099 | |
| 6100 | psa_status_t status = |
| 6101 | psa_key_derivation_output_key( &derived_attributes, |
| 6102 | &operation, |
| 6103 | &derived_key ); |
| 6104 | if( is_large_output > 0 ) |
| 6105 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 6106 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 6107 | |
| 6108 | exit: |
| 6109 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6110 | psa_destroy_key( base_key ); |
| 6111 | psa_destroy_key( derived_key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 6112 | PSA_DONE( ); |
| 6113 | } |
| 6114 | /* END_CASE */ |
| 6115 | |
| 6116 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6117 | void key_agreement_setup( int alg_arg, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 6118 | int our_key_type_arg, int our_key_alg_arg, |
| 6119 | data_t *our_key_data, data_t *peer_key_data, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6120 | int expected_status_arg ) |
| 6121 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6122 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6123 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 6124 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6125 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6126 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6127 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 6128 | psa_status_t expected_status = expected_status_arg; |
| 6129 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6130 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6131 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6132 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6133 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 6134 | psa_set_key_algorithm( &attributes, our_key_alg ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6135 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6136 | PSA_ASSERT( psa_import_key( &attributes, |
| 6137 | our_key_data->x, our_key_data->len, |
| 6138 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6139 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 6140 | /* The tests currently include inputs that should fail at either step. |
| 6141 | * Test cases that fail at the setup step should be changed to call |
| 6142 | * key_derivation_setup instead, and this function should be renamed |
| 6143 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6144 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 6145 | if( status == PSA_SUCCESS ) |
| 6146 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6147 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 6148 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 6149 | our_key, |
| 6150 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 6151 | expected_status ); |
| 6152 | } |
| 6153 | else |
| 6154 | { |
| 6155 | TEST_ASSERT( status == expected_status ); |
| 6156 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6157 | |
| 6158 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6159 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6160 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6161 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6162 | } |
| 6163 | /* END_CASE */ |
| 6164 | |
| 6165 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 6166 | void raw_key_agreement( int alg_arg, |
| 6167 | int our_key_type_arg, data_t *our_key_data, |
| 6168 | data_t *peer_key_data, |
| 6169 | data_t *expected_output ) |
| 6170 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6171 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 6172 | psa_algorithm_t alg = alg_arg; |
| 6173 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6174 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 6175 | unsigned char *output = NULL; |
| 6176 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 6177 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 6178 | |
| 6179 | ASSERT_ALLOC( output, expected_output->len ); |
| 6180 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6181 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6182 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 6183 | psa_set_key_algorithm( &attributes, alg ); |
| 6184 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6185 | PSA_ASSERT( psa_import_key( &attributes, |
| 6186 | our_key_data->x, our_key_data->len, |
| 6187 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 6188 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 6189 | PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) ); |
| 6190 | key_bits = psa_get_key_bits( &attributes ); |
| 6191 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 6192 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 6193 | peer_key_data->x, peer_key_data->len, |
| 6194 | output, expected_output->len, |
| 6195 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 6196 | ASSERT_COMPARE( output, output_length, |
| 6197 | expected_output->x, expected_output->len ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 6198 | TEST_ASSERT( output_length <= |
| 6199 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) ); |
| 6200 | TEST_ASSERT( output_length <= |
| 6201 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 6202 | |
| 6203 | exit: |
| 6204 | mbedtls_free( output ); |
| 6205 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6206 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 6207 | } |
| 6208 | /* END_CASE */ |
| 6209 | |
| 6210 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6211 | void key_agreement_capacity( int alg_arg, |
| 6212 | int our_key_type_arg, data_t *our_key_data, |
| 6213 | data_t *peer_key_data, |
| 6214 | int expected_capacity_arg ) |
| 6215 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6216 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6217 | psa_algorithm_t alg = alg_arg; |
| 6218 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6219 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6220 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6221 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 6222 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6223 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6224 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6225 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6226 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 6227 | psa_set_key_algorithm( &attributes, alg ); |
| 6228 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6229 | PSA_ASSERT( psa_import_key( &attributes, |
| 6230 | our_key_data->x, our_key_data->len, |
| 6231 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6232 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6233 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6234 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 6235 | &operation, |
| 6236 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 6237 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 6238 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 6239 | { |
| 6240 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6241 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 6242 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 6243 | NULL, 0 ) ); |
| 6244 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6245 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 6246 | /* Test the advertized capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 6247 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6248 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6249 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6250 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 6251 | /* Test the actual capacity by reading the output. */ |
| 6252 | while( actual_capacity > sizeof( output ) ) |
| 6253 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6254 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6255 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 6256 | actual_capacity -= sizeof( output ); |
| 6257 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6258 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6259 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6260 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 6261 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 6262 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6263 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6264 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6265 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6266 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6267 | } |
| 6268 | /* END_CASE */ |
| 6269 | |
| 6270 | /* BEGIN_CASE */ |
| 6271 | void key_agreement_output( int alg_arg, |
| 6272 | int our_key_type_arg, data_t *our_key_data, |
| 6273 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 6274 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6275 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6276 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6277 | psa_algorithm_t alg = alg_arg; |
| 6278 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6279 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6280 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 6281 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6282 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 6283 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 6284 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6285 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6286 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6287 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6288 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 6289 | psa_set_key_algorithm( &attributes, alg ); |
| 6290 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6291 | PSA_ASSERT( psa_import_key( &attributes, |
| 6292 | our_key_data->x, our_key_data->len, |
| 6293 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6294 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6295 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6296 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 6297 | &operation, |
| 6298 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 6299 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 6300 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 6301 | { |
| 6302 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6303 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 6304 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 6305 | NULL, 0 ) ); |
| 6306 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6307 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6308 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6309 | actual_output, |
| 6310 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 6311 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 6312 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 6313 | if( expected_output2->len != 0 ) |
| 6314 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6315 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6316 | actual_output, |
| 6317 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 6318 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 6319 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 6320 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6321 | |
| 6322 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6323 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6324 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6325 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6326 | mbedtls_free( actual_output ); |
| 6327 | } |
| 6328 | /* END_CASE */ |
| 6329 | |
| 6330 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6331 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6332 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6333 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6334 | unsigned char *output = NULL; |
| 6335 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6336 | size_t i; |
| 6337 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6338 | |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 6339 | TEST_ASSERT( bytes_arg >= 0 ); |
| 6340 | |
Gilles Peskine | 9189202 | 2021-02-08 19:50:26 +0100 | [diff] [blame] | 6341 | ASSERT_ALLOC( output, bytes ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6342 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6343 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6344 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6345 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6346 | /* Run several times, to ensure that every output byte will be |
| 6347 | * nonzero at least once with overwhelming probability |
| 6348 | * (2^(-8*number_of_runs)). */ |
| 6349 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6350 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 6351 | if( bytes != 0 ) |
| 6352 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6353 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6354 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6355 | for( i = 0; i < bytes; i++ ) |
| 6356 | { |
| 6357 | if( output[i] != 0 ) |
| 6358 | ++changed[i]; |
| 6359 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6360 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6361 | |
| 6362 | /* Check that every byte was changed to nonzero at least once. This |
| 6363 | * validates that psa_generate_random is overwriting every byte of |
| 6364 | * the output buffer. */ |
| 6365 | for( i = 0; i < bytes; i++ ) |
| 6366 | { |
| 6367 | TEST_ASSERT( changed[i] != 0 ); |
| 6368 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6369 | |
| 6370 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6371 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6372 | mbedtls_free( output ); |
| 6373 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6374 | } |
| 6375 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6376 | |
| 6377 | /* BEGIN_CASE */ |
| 6378 | void generate_key( int type_arg, |
| 6379 | int bits_arg, |
| 6380 | int usage_arg, |
| 6381 | int alg_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 6382 | int expected_status_arg, |
| 6383 | int is_large_key ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6384 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6385 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6386 | psa_key_type_t type = type_arg; |
| 6387 | psa_key_usage_t usage = usage_arg; |
| 6388 | size_t bits = bits_arg; |
| 6389 | psa_algorithm_t alg = alg_arg; |
| 6390 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6391 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6392 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6393 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6394 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6395 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6396 | psa_set_key_usage_flags( &attributes, usage ); |
| 6397 | psa_set_key_algorithm( &attributes, alg ); |
| 6398 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 6399 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6400 | |
| 6401 | /* Generate a key */ |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 6402 | psa_status_t status = psa_generate_key( &attributes, &key ); |
| 6403 | |
| 6404 | if( is_large_key > 0 ) |
| 6405 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 6406 | TEST_EQUAL( status , expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6407 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6408 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6409 | |
| 6410 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6411 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6412 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 6413 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6414 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 6415 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 6416 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 6417 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6418 | |
| 6419 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6420 | /* |
| 6421 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6422 | * thus reset them as required. |
| 6423 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6424 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6425 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6426 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6427 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6428 | } |
| 6429 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 6430 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 6431 | /* 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] | 6432 | void generate_key_rsa( int bits_arg, |
| 6433 | data_t *e_arg, |
| 6434 | int expected_status_arg ) |
| 6435 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6436 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6437 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6438 | size_t bits = bits_arg; |
| 6439 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 6440 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 6441 | psa_status_t expected_status = expected_status_arg; |
| 6442 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6443 | uint8_t *exported = NULL; |
| 6444 | size_t exported_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 6445 | PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6446 | size_t exported_length = SIZE_MAX; |
| 6447 | uint8_t *e_read_buffer = NULL; |
| 6448 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 6449 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6450 | size_t e_read_length = SIZE_MAX; |
| 6451 | |
| 6452 | if( e_arg->len == 0 || |
| 6453 | ( e_arg->len == 3 && |
| 6454 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 6455 | { |
| 6456 | is_default_public_exponent = 1; |
| 6457 | e_read_size = 0; |
| 6458 | } |
| 6459 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 6460 | ASSERT_ALLOC( exported, exported_size ); |
| 6461 | |
| 6462 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6463 | |
| 6464 | psa_set_key_usage_flags( &attributes, usage ); |
| 6465 | psa_set_key_algorithm( &attributes, alg ); |
| 6466 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 6467 | e_arg->x, e_arg->len ) ); |
| 6468 | psa_set_key_bits( &attributes, bits ); |
| 6469 | |
| 6470 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6471 | TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6472 | if( expected_status != PSA_SUCCESS ) |
| 6473 | goto exit; |
| 6474 | |
| 6475 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6476 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6477 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 6478 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 6479 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 6480 | e_read_buffer, e_read_size, |
| 6481 | &e_read_length ) ); |
| 6482 | if( is_default_public_exponent ) |
| 6483 | TEST_EQUAL( e_read_length, 0 ); |
| 6484 | else |
| 6485 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 6486 | |
| 6487 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 6488 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6489 | goto exit; |
| 6490 | |
| 6491 | /* Export the key and check the public exponent. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6492 | PSA_ASSERT( psa_export_public_key( key, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6493 | exported, exported_size, |
| 6494 | &exported_length ) ); |
| 6495 | { |
| 6496 | uint8_t *p = exported; |
| 6497 | uint8_t *end = exported + exported_length; |
| 6498 | size_t len; |
| 6499 | /* RSAPublicKey ::= SEQUENCE { |
| 6500 | * modulus INTEGER, -- n |
| 6501 | * publicExponent INTEGER } -- e |
| 6502 | */ |
| 6503 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6504 | MBEDTLS_ASN1_SEQUENCE | |
| 6505 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 6506 | TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6507 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 6508 | MBEDTLS_ASN1_INTEGER ) ); |
| 6509 | if( len >= 1 && p[0] == 0 ) |
| 6510 | { |
| 6511 | ++p; |
| 6512 | --len; |
| 6513 | } |
| 6514 | if( e_arg->len == 0 ) |
| 6515 | { |
| 6516 | TEST_EQUAL( len, 3 ); |
| 6517 | TEST_EQUAL( p[0], 1 ); |
| 6518 | TEST_EQUAL( p[1], 0 ); |
| 6519 | TEST_EQUAL( p[2], 1 ); |
| 6520 | } |
| 6521 | else |
| 6522 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 6523 | } |
| 6524 | |
| 6525 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6526 | /* |
| 6527 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 6528 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 6529 | */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6530 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6531 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6532 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6533 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6534 | mbedtls_free( e_read_buffer ); |
| 6535 | mbedtls_free( exported ); |
| 6536 | } |
| 6537 | /* END_CASE */ |
| 6538 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6539 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6540 | void persistent_key_load_key_from_storage( data_t *data, |
| 6541 | int type_arg, int bits_arg, |
| 6542 | int usage_flags_arg, int alg_arg, |
| 6543 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6544 | { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 6545 | 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] | 6546 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6547 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6548 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6549 | psa_key_type_t type = type_arg; |
| 6550 | size_t bits = bits_arg; |
| 6551 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 6552 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6553 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6554 | unsigned char *first_export = NULL; |
| 6555 | unsigned char *second_export = NULL; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 6556 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6557 | size_t first_exported_length; |
| 6558 | size_t second_exported_length; |
| 6559 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6560 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 6561 | { |
| 6562 | ASSERT_ALLOC( first_export, export_size ); |
| 6563 | ASSERT_ALLOC( second_export, export_size ); |
| 6564 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6565 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6566 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6567 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 6568 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6569 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 6570 | psa_set_key_algorithm( &attributes, alg ); |
| 6571 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 6572 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6573 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6574 | switch( generation_method ) |
| 6575 | { |
| 6576 | case IMPORT_KEY: |
| 6577 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6578 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6579 | &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6580 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6581 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6582 | case GENERATE_KEY: |
| 6583 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6584 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6585 | break; |
| 6586 | |
| 6587 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 6588 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6589 | { |
| 6590 | /* Create base key */ |
| 6591 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 6592 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6593 | psa_set_key_usage_flags( &base_attributes, |
| 6594 | PSA_KEY_USAGE_DERIVE ); |
| 6595 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 6596 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6597 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 6598 | data->x, data->len, |
| 6599 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6600 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6601 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6602 | PSA_ASSERT( psa_key_derivation_input_key( |
| 6603 | &operation, |
| 6604 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6605 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6606 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6607 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6608 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 6609 | &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6610 | &key ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6611 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6612 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6613 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6614 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 6615 | #else |
| 6616 | TEST_ASSUME( ! "KDF not supported in this configuration" ); |
| 6617 | #endif |
| 6618 | break; |
| 6619 | |
| 6620 | default: |
| 6621 | TEST_ASSERT( ! "generation_method not implemented in test" ); |
| 6622 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6623 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6624 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6625 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6626 | /* Export the key if permitted by the key policy. */ |
| 6627 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 6628 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6629 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6630 | first_export, export_size, |
| 6631 | &first_exported_length ) ); |
| 6632 | if( generation_method == IMPORT_KEY ) |
| 6633 | ASSERT_COMPARE( data->x, data->len, |
| 6634 | first_export, first_exported_length ); |
| 6635 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6636 | |
| 6637 | /* Shutdown and restart */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6638 | PSA_ASSERT( psa_purge_key( key ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6639 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6640 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6641 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6642 | /* Check key slot still contains key data */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6643 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 6644 | TEST_ASSERT( mbedtls_svc_key_id_equal( |
| 6645 | psa_get_key_id( &attributes ), key_id ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6646 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 6647 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 6648 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 6649 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 6650 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags ); |
| 6651 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6652 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6653 | /* Export the key again if permitted by the key policy. */ |
| 6654 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6655 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6656 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6657 | second_export, export_size, |
| 6658 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6659 | ASSERT_COMPARE( first_export, first_exported_length, |
| 6660 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6661 | } |
| 6662 | |
| 6663 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 6664 | if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6665 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6666 | |
| 6667 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6668 | /* |
| 6669 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6670 | * thus reset them as required. |
| 6671 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6672 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6673 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6674 | mbedtls_free( first_export ); |
| 6675 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6676 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6677 | psa_destroy_key( base_key ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6678 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6679 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6680 | } |
| 6681 | /* END_CASE */ |