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 | |
Gilles Peskine | f627931 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 19 | /* If this comes up, it's a bug in the test code or in the test data. */ |
| 20 | #define UNUSED 0xdeadbeef |
| 21 | |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 22 | /* Assert that an operation is (not) active. |
| 23 | * This serves as a proxy for checking if the operation is aborted. */ |
| 24 | #define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 ) |
| 25 | #define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 ) |
| 26 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 27 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 28 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 29 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 30 | /** Test if a buffer contains a constant byte value. |
| 31 | * |
| 32 | * `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] | 33 | * |
| 34 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 35 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 36 | * \param size Size of the buffer in bytes. |
| 37 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 38 | * \return 1 if the buffer is all-bits-zero. |
| 39 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 40 | */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 41 | 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] | 42 | { |
| 43 | size_t i; |
| 44 | for( i = 0; i < size; i++ ) |
| 45 | { |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 46 | if( ( (unsigned char *) buffer )[i] != c ) |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 47 | return( 0 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 48 | } |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 49 | return( 1 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 50 | } |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 51 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 52 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
| 53 | static int asn1_write_10x( unsigned char **p, |
| 54 | unsigned char *start, |
| 55 | size_t bits, |
| 56 | unsigned char x ) |
| 57 | { |
| 58 | int ret; |
| 59 | int len = bits / 8 + 1; |
Gilles Peskine | 480416a | 2018-06-28 19:04:07 +0200 | [diff] [blame] | 60 | if( bits == 0 ) |
| 61 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
| 62 | if( bits <= 8 && x >= 1 << ( bits - 1 ) ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 63 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 64 | if( *p < start || *p - start < (ptrdiff_t) len ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 65 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 66 | *p -= len; |
| 67 | ( *p )[len-1] = x; |
| 68 | if( bits % 8 == 0 ) |
| 69 | ( *p )[1] |= 1; |
| 70 | else |
| 71 | ( *p )[0] |= 1 << ( bits % 8 ); |
| 72 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 73 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
| 74 | MBEDTLS_ASN1_INTEGER ) ); |
| 75 | return( len ); |
| 76 | } |
| 77 | |
| 78 | static int construct_fake_rsa_key( unsigned char *buffer, |
| 79 | size_t buffer_size, |
| 80 | unsigned char **p, |
| 81 | size_t bits, |
| 82 | int keypair ) |
| 83 | { |
| 84 | size_t half_bits = ( bits + 1 ) / 2; |
| 85 | int ret; |
| 86 | int len = 0; |
| 87 | /* Construct something that looks like a DER encoding of |
| 88 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 89 | * RSAPrivateKey ::= SEQUENCE { |
| 90 | * version Version, |
| 91 | * modulus INTEGER, -- n |
| 92 | * publicExponent INTEGER, -- e |
| 93 | * privateExponent INTEGER, -- d |
| 94 | * prime1 INTEGER, -- p |
| 95 | * prime2 INTEGER, -- q |
| 96 | * exponent1 INTEGER, -- d mod (p-1) |
| 97 | * exponent2 INTEGER, -- d mod (q-1) |
| 98 | * coefficient INTEGER, -- (inverse of q) mod p |
| 99 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 100 | * } |
| 101 | * Or, for a public key, the same structure with only |
| 102 | * version, modulus and publicExponent. |
| 103 | */ |
| 104 | *p = buffer + buffer_size; |
| 105 | if( keypair ) |
| 106 | { |
| 107 | MBEDTLS_ASN1_CHK_ADD( len, /* pq */ |
| 108 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 109 | MBEDTLS_ASN1_CHK_ADD( len, /* dq */ |
| 110 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 111 | MBEDTLS_ASN1_CHK_ADD( len, /* dp */ |
| 112 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 113 | MBEDTLS_ASN1_CHK_ADD( len, /* q */ |
| 114 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 115 | MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */ |
| 116 | asn1_write_10x( p, buffer, half_bits, 3 ) ); |
| 117 | MBEDTLS_ASN1_CHK_ADD( len, /* d */ |
| 118 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 119 | } |
| 120 | MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */ |
| 121 | asn1_write_10x( p, buffer, 17, 1 ) ); |
| 122 | MBEDTLS_ASN1_CHK_ADD( len, /* n */ |
| 123 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 124 | if( keypair ) |
| 125 | MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */ |
| 126 | mbedtls_asn1_write_int( p, buffer, 0 ) ); |
| 127 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) ); |
| 128 | { |
| 129 | const unsigned char tag = |
| 130 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
| 131 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) ); |
| 132 | } |
| 133 | return( len ); |
| 134 | } |
| 135 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 136 | int exercise_mac_setup( psa_key_type_t key_type, |
| 137 | const unsigned char *key_bytes, |
| 138 | size_t key_length, |
| 139 | psa_algorithm_t alg, |
| 140 | psa_mac_operation_t *operation, |
| 141 | psa_status_t *status ) |
| 142 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 143 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 144 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 145 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 146 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 147 | psa_set_key_algorithm( &attributes, alg ); |
| 148 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 149 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 150 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 151 | *status = psa_mac_sign_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 152 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 153 | PSA_ASSERT( psa_mac_abort( operation ) ); |
| 154 | /* If setup failed, reproduce the failure, so that the caller can |
| 155 | * test the resulting state of the operation object. */ |
| 156 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 157 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 158 | TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 159 | } |
| 160 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 161 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 162 | return( 1 ); |
| 163 | |
| 164 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 165 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 166 | return( 0 ); |
| 167 | } |
| 168 | |
| 169 | int exercise_cipher_setup( psa_key_type_t key_type, |
| 170 | const unsigned char *key_bytes, |
| 171 | size_t key_length, |
| 172 | psa_algorithm_t alg, |
| 173 | psa_cipher_operation_t *operation, |
| 174 | psa_status_t *status ) |
| 175 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 176 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 177 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 178 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 179 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 180 | psa_set_key_algorithm( &attributes, alg ); |
| 181 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 182 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 183 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 184 | *status = psa_cipher_encrypt_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 185 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 186 | PSA_ASSERT( psa_cipher_abort( operation ) ); |
| 187 | /* If setup failed, reproduce the failure, so that the caller can |
| 188 | * test the resulting state of the operation object. */ |
| 189 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 190 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 191 | TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ), |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 192 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 195 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 196 | return( 1 ); |
| 197 | |
| 198 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 199 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 200 | return( 0 ); |
| 201 | } |
| 202 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 203 | 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] | 204 | { |
| 205 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 206 | 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] | 207 | uint8_t buffer[1]; |
| 208 | size_t length; |
| 209 | int ok = 0; |
| 210 | |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 211 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 212 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 213 | psa_set_key_algorithm( &attributes, PSA_ALG_CTR ); |
| 214 | psa_set_key_type( &attributes, PSA_KEY_TYPE_AES ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 215 | TEST_EQUAL( psa_get_key_attributes( key, &attributes ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 216 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 217 | TEST_EQUAL( |
| 218 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 ); |
| 219 | TEST_EQUAL( |
| 220 | 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] | 221 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 222 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 223 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 224 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
| 225 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
| 226 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 227 | TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 228 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 229 | TEST_EQUAL( psa_export_public_key( key, |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 230 | buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 231 | PSA_ERROR_INVALID_HANDLE ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 232 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 233 | ok = 1; |
| 234 | |
| 235 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 236 | /* |
| 237 | * Key attributes may have been returned by psa_get_key_attributes() |
| 238 | * thus reset them as required. |
| 239 | */ |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 240 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 241 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 242 | return( ok ); |
| 243 | } |
| 244 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 245 | /* Assert that a key isn't reported as having a slot number. */ |
| 246 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 247 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 248 | do \ |
| 249 | { \ |
| 250 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
| 251 | TEST_EQUAL( psa_get_key_slot_number( \ |
| 252 | attributes, \ |
| 253 | &ASSERT_NO_SLOT_NUMBER_slot_number ), \ |
| 254 | PSA_ERROR_INVALID_ARGUMENT ); \ |
| 255 | } \ |
| 256 | while( 0 ) |
| 257 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 258 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 259 | ( (void) 0 ) |
| 260 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 261 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 262 | /* An overapproximation of the amount of storage needed for a key of the |
| 263 | * given type and with the given content. The API doesn't make it easy |
| 264 | * to find a good value for the size. The current implementation doesn't |
| 265 | * care about the value anyway. */ |
| 266 | #define KEY_BITS_FROM_DATA( type, data ) \ |
| 267 | ( data )->len |
| 268 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 269 | typedef enum { |
| 270 | IMPORT_KEY = 0, |
| 271 | GENERATE_KEY = 1, |
| 272 | DERIVE_KEY = 2 |
| 273 | } generate_method; |
| 274 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 275 | /* END_HEADER */ |
| 276 | |
| 277 | /* BEGIN_DEPENDENCIES |
| 278 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 279 | * END_DEPENDENCIES |
| 280 | */ |
| 281 | |
| 282 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 283 | void static_checks( ) |
| 284 | { |
| 285 | size_t max_truncated_mac_size = |
| 286 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 287 | |
| 288 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 289 | * encoding. The shifted mask is the maximum truncated value. The |
| 290 | * untruncated algorithm may be one byte larger. */ |
| 291 | TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 292 | |
| 293 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 294 | /* Check deprecated constants. */ |
| 295 | TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR ); |
| 296 | TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS ); |
| 297 | TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST ); |
| 298 | TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA ); |
| 299 | TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED ); |
| 300 | TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH ); |
| 301 | TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH ); |
| 302 | TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 303 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 304 | TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 305 | TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 306 | TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 307 | TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 308 | TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 309 | TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 310 | TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 311 | TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 312 | TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 313 | TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 314 | TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 ); |
| 315 | TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 316 | TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 317 | TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 318 | TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 319 | TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 320 | TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 321 | TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 322 | TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 323 | TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 324 | TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 325 | TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 326 | TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 327 | TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 ); |
| 328 | TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 ); |
| 329 | TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 ); |
| 330 | TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 ); |
| 331 | TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 ); |
| 332 | TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY ); |
| 333 | TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY ); |
| 334 | |
| 335 | TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 336 | TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 337 | TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 ); |
| 338 | TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 339 | TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 340 | TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 ); |
| 341 | TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 ); |
| 342 | TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY ); |
Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 343 | |
Paul Elliott | 75e2703 | 2020-06-03 15:17:39 +0100 | [diff] [blame] | 344 | TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 ); |
| 345 | TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 ); |
| 346 | TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 ); |
| 347 | TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 ); |
| 348 | TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 ); |
| 349 | |
| 350 | TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 ); |
| 351 | TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM ); |
Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 352 | #endif |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 353 | } |
| 354 | /* END_CASE */ |
| 355 | |
| 356 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 357 | void import_with_policy( int type_arg, |
| 358 | int usage_arg, int alg_arg, |
| 359 | int expected_status_arg ) |
| 360 | { |
| 361 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 362 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 363 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 364 | psa_key_type_t type = type_arg; |
| 365 | psa_key_usage_t usage = usage_arg; |
| 366 | psa_algorithm_t alg = alg_arg; |
| 367 | psa_status_t expected_status = expected_status_arg; |
| 368 | const uint8_t key_material[16] = {0}; |
| 369 | psa_status_t status; |
| 370 | |
| 371 | PSA_ASSERT( psa_crypto_init( ) ); |
| 372 | |
| 373 | psa_set_key_type( &attributes, type ); |
| 374 | psa_set_key_usage_flags( &attributes, usage ); |
| 375 | psa_set_key_algorithm( &attributes, alg ); |
| 376 | |
| 377 | status = psa_import_key( &attributes, |
| 378 | key_material, sizeof( key_material ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 379 | &key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 380 | TEST_EQUAL( status, expected_status ); |
| 381 | if( status != PSA_SUCCESS ) |
| 382 | goto exit; |
| 383 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 384 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 385 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
gabor-mezei-arm | 4d9009e | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 386 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 387 | mbedtls_test_update_key_usage_flags( usage ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 388 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 389 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 390 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 391 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 392 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 393 | |
| 394 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 395 | /* |
| 396 | * Key attributes may have been returned by psa_get_key_attributes() |
| 397 | * thus reset them as required. |
| 398 | */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 399 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 400 | |
| 401 | psa_destroy_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 402 | PSA_DONE( ); |
| 403 | } |
| 404 | /* END_CASE */ |
| 405 | |
| 406 | /* BEGIN_CASE */ |
| 407 | void import_with_data( data_t *data, int type_arg, |
| 408 | int attr_bits_arg, |
| 409 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 410 | { |
| 411 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 412 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 413 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 414 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 415 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 416 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 417 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 418 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 419 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 420 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 421 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 422 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 423 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 424 | status = psa_import_key( &attributes, data->x, data->len, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 425 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 426 | if( status != PSA_SUCCESS ) |
| 427 | goto exit; |
| 428 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 429 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 430 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 431 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 432 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 433 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 434 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 435 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 436 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 437 | |
| 438 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 439 | /* |
| 440 | * Key attributes may have been returned by psa_get_key_attributes() |
| 441 | * thus reset them as required. |
| 442 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 443 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 444 | |
| 445 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 446 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 447 | } |
| 448 | /* END_CASE */ |
| 449 | |
| 450 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 451 | void import_large_key( int type_arg, int byte_size_arg, |
| 452 | int expected_status_arg ) |
| 453 | { |
| 454 | psa_key_type_t type = type_arg; |
| 455 | size_t byte_size = byte_size_arg; |
| 456 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 457 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 458 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 459 | psa_status_t status; |
| 460 | uint8_t *buffer = NULL; |
| 461 | size_t buffer_size = byte_size + 1; |
| 462 | size_t n; |
| 463 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 464 | /* Skip the test case if the target running the test cannot |
| 465 | * accomodate large keys due to heap size constraints */ |
| 466 | ASSERT_ALLOC_WEAK( buffer, buffer_size ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 467 | memset( buffer, 'K', byte_size ); |
| 468 | |
| 469 | PSA_ASSERT( psa_crypto_init( ) ); |
| 470 | |
| 471 | /* Try importing the key */ |
| 472 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 473 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 474 | status = psa_import_key( &attributes, buffer, byte_size, &key ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 475 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 476 | TEST_EQUAL( status, expected_status ); |
| 477 | |
| 478 | if( status == PSA_SUCCESS ) |
| 479 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 480 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 481 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 482 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 483 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 484 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 485 | memset( buffer, 0, byte_size + 1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 486 | PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 487 | for( n = 0; n < byte_size; n++ ) |
| 488 | TEST_EQUAL( buffer[n], 'K' ); |
| 489 | for( n = byte_size; n < buffer_size; n++ ) |
| 490 | TEST_EQUAL( buffer[n], 0 ); |
| 491 | } |
| 492 | |
| 493 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 494 | /* |
| 495 | * Key attributes may have been returned by psa_get_key_attributes() |
| 496 | * thus reset them as required. |
| 497 | */ |
| 498 | psa_reset_key_attributes( &attributes ); |
| 499 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 500 | psa_destroy_key( key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 501 | PSA_DONE( ); |
| 502 | mbedtls_free( buffer ); |
| 503 | } |
| 504 | /* END_CASE */ |
| 505 | |
| 506 | /* BEGIN_CASE */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 507 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 508 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 509 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 510 | size_t bits = bits_arg; |
| 511 | psa_status_t expected_status = expected_status_arg; |
| 512 | psa_status_t status; |
| 513 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 514 | 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] | 515 | size_t buffer_size = /* Slight overapproximations */ |
| 516 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 517 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 518 | unsigned char *p; |
| 519 | int ret; |
| 520 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 521 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 522 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 523 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 524 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 525 | |
| 526 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 527 | bits, keypair ) ) >= 0 ); |
| 528 | length = ret; |
| 529 | |
| 530 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 531 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 532 | status = psa_import_key( &attributes, p, length, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 533 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 534 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 535 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 536 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 537 | |
| 538 | exit: |
| 539 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 540 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 541 | } |
| 542 | /* END_CASE */ |
| 543 | |
| 544 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 545 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 546 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 547 | int usage_arg, int alg_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 548 | int expected_bits, |
| 549 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 550 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 551 | int canonical_input ) |
| 552 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 553 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 554 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 555 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 556 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 557 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 558 | unsigned char *exported = NULL; |
| 559 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 560 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 561 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 562 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 563 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 564 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 565 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 566 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 567 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 568 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 569 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 570 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 571 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 572 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 573 | psa_set_key_algorithm( &attributes, alg ); |
| 574 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 575 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 576 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 577 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 578 | |
| 579 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 580 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 581 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 582 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 583 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 584 | |
| 585 | /* Export the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 586 | status = psa_export_key( key, exported, export_size, &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 587 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 588 | |
| 589 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 590 | * and export_size. On errors, the exported length must be 0. */ |
| 591 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 592 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
| 593 | TEST_ASSERT( exported_length <= export_size ); |
| 594 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 595 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 596 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 597 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 598 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 599 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 600 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 601 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 602 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 603 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 604 | * this validates the canonical representations. For canonical inputs, |
| 605 | * this doesn't directly validate the implementation, but it still helps |
| 606 | * by cross-validating the test data with the sanity check code. */ |
| 607 | if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) ) |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 608 | goto exit; |
| 609 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 610 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 611 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 612 | else |
| 613 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 614 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 615 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 616 | &key2 ) ); |
| 617 | PSA_ASSERT( psa_export_key( key2, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 618 | reexported, |
| 619 | export_size, |
| 620 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 621 | ASSERT_COMPARE( exported, exported_length, |
| 622 | reexported, reexported_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 623 | PSA_ASSERT( psa_destroy_key( key2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 624 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 625 | TEST_ASSERT( exported_length <= |
| 626 | PSA_EXPORT_KEY_OUTPUT_SIZE( type, |
| 627 | psa_get_key_bits( &got_attributes ) ) ); |
| 628 | TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 629 | |
| 630 | destroy: |
| 631 | /* Destroy the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 632 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 633 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 634 | |
| 635 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 636 | /* |
| 637 | * Key attributes may have been returned by psa_get_key_attributes() |
| 638 | * thus reset them as required. |
| 639 | */ |
| 640 | psa_reset_key_attributes( &got_attributes ); |
| 641 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 642 | mbedtls_free( exported ); |
| 643 | mbedtls_free( reexported ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 644 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 645 | } |
| 646 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 647 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 648 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 649 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 650 | int type_arg, |
| 651 | int alg_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 652 | int export_size_delta, |
| 653 | int expected_export_status_arg, |
| 654 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 655 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 656 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 657 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 658 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 659 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 660 | psa_status_t status; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 661 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 662 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 663 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 664 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 665 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 666 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 667 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 668 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 669 | psa_set_key_algorithm( &attributes, alg ); |
| 670 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 671 | |
| 672 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 673 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 674 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 675 | /* Export the public key */ |
| 676 | ASSERT_ALLOC( exported, export_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 677 | status = psa_export_public_key( key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 678 | exported, export_size, |
| 679 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 680 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 681 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 682 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 683 | 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] | 684 | size_t bits; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 685 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 686 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 687 | TEST_ASSERT( expected_public_key->len <= |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 688 | PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 689 | TEST_ASSERT( expected_public_key->len <= |
| 690 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 691 | TEST_ASSERT( expected_public_key->len <= |
| 692 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 693 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 694 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 695 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 696 | |
| 697 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 698 | /* |
| 699 | * Key attributes may have been returned by psa_get_key_attributes() |
| 700 | * thus reset them as required. |
| 701 | */ |
| 702 | psa_reset_key_attributes( &attributes ); |
| 703 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 704 | mbedtls_free( exported ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 705 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 706 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 707 | } |
| 708 | /* END_CASE */ |
| 709 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 710 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 711 | void import_and_exercise_key( data_t *data, |
| 712 | int type_arg, |
| 713 | int bits_arg, |
| 714 | int alg_arg ) |
| 715 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 716 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 717 | psa_key_type_t type = type_arg; |
| 718 | size_t bits = bits_arg; |
| 719 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 720 | 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] | 721 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 722 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 723 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 724 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 725 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 726 | psa_set_key_usage_flags( &attributes, usage ); |
| 727 | psa_set_key_algorithm( &attributes, alg ); |
| 728 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 729 | |
| 730 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 731 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 732 | |
| 733 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 734 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 735 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 736 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 737 | |
| 738 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 739 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 740 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 741 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 742 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 743 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 744 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 745 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 746 | /* |
| 747 | * Key attributes may have been returned by psa_get_key_attributes() |
| 748 | * thus reset them as required. |
| 749 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 750 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 751 | |
| 752 | psa_reset_key_attributes( &attributes ); |
| 753 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 754 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 755 | } |
| 756 | /* END_CASE */ |
| 757 | |
| 758 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 759 | void effective_key_attributes( int type_arg, int expected_type_arg, |
| 760 | int bits_arg, int expected_bits_arg, |
| 761 | int usage_arg, int expected_usage_arg, |
| 762 | int alg_arg, int expected_alg_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 763 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 764 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 765 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 766 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 767 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 768 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 769 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 770 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 771 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 772 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 773 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 774 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 775 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 776 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 777 | psa_set_key_usage_flags( &attributes, usage ); |
| 778 | psa_set_key_algorithm( &attributes, alg ); |
| 779 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 780 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 781 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 782 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 783 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 784 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 785 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 786 | TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type ); |
| 787 | TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits ); |
| 788 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 789 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 790 | |
| 791 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 792 | /* |
| 793 | * Key attributes may have been returned by psa_get_key_attributes() |
| 794 | * thus reset them as required. |
| 795 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 796 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 797 | |
| 798 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 799 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 800 | } |
| 801 | /* END_CASE */ |
| 802 | |
| 803 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 804 | void check_key_policy( int type_arg, int bits_arg, |
| 805 | int usage_arg, int alg_arg ) |
| 806 | { |
| 807 | test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg, |
gabor-mezei-arm | 58e510f | 2021-06-28 14:36:03 +0200 | [diff] [blame] | 808 | usage_arg, |
| 809 | mbedtls_test_update_key_usage_flags( usage_arg ), |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 810 | alg_arg, alg_arg ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 811 | goto exit; |
| 812 | } |
| 813 | /* END_CASE */ |
| 814 | |
| 815 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 816 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 817 | { |
| 818 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 819 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 820 | * though it's OK by the C standard. We could test for this, but we'd need |
| 821 | * to supress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 822 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 823 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 824 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 825 | |
| 826 | memset( &zero, 0, sizeof( zero ) ); |
| 827 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 828 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 829 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 830 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 831 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 832 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 833 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 834 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 835 | |
| 836 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 837 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 838 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 839 | |
| 840 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 841 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 842 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 843 | |
| 844 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 845 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 846 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 847 | } |
| 848 | /* END_CASE */ |
| 849 | |
| 850 | /* BEGIN_CASE */ |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 851 | void mac_key_policy( int policy_usage_arg, |
| 852 | int policy_alg_arg, |
| 853 | int key_type_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 854 | data_t *key_data, |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 855 | int exercise_alg_arg, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 856 | int expected_status_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 857 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 858 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 859 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 860 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 861 | psa_key_type_t key_type = key_type_arg; |
| 862 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 863 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 864 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 865 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 866 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 867 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 868 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 869 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 870 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 871 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 872 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 873 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 874 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 875 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 876 | &key ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 877 | |
gabor-mezei-arm | 659af9e | 2021-06-29 11:06:16 +0200 | [diff] [blame] | 878 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
| 879 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 880 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 881 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 882 | if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 883 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 884 | else |
| 885 | TEST_EQUAL( status, expected_status ); |
| 886 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 887 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 888 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 889 | memset( mac, 0, sizeof( mac ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 890 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 891 | if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 892 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 893 | else |
| 894 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 895 | |
| 896 | exit: |
| 897 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 898 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 899 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 900 | } |
| 901 | /* END_CASE */ |
| 902 | |
| 903 | /* BEGIN_CASE */ |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 904 | void cipher_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 905 | int policy_alg, |
| 906 | int key_type, |
| 907 | data_t *key_data, |
| 908 | int exercise_alg ) |
| 909 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 910 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 911 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 912 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 913 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 914 | psa_status_t status; |
| 915 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 916 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 917 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 918 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 919 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 920 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 921 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 922 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 923 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 924 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 925 | /* Check if no key usage flag implication is done */ |
| 926 | TEST_EQUAL( policy_usage, |
| 927 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 928 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 929 | status = psa_cipher_encrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 930 | if( policy_alg == exercise_alg && |
| 931 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 932 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 933 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 934 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 935 | psa_cipher_abort( &operation ); |
| 936 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 937 | status = psa_cipher_decrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 938 | if( policy_alg == exercise_alg && |
| 939 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 940 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 941 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 942 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 943 | |
| 944 | exit: |
| 945 | psa_cipher_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 946 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 947 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 948 | } |
| 949 | /* END_CASE */ |
| 950 | |
| 951 | /* BEGIN_CASE */ |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 952 | void aead_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 953 | int policy_alg, |
| 954 | int key_type, |
| 955 | data_t *key_data, |
| 956 | int nonce_length_arg, |
| 957 | int tag_length_arg, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 958 | int exercise_alg, |
| 959 | int expected_status_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 960 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 961 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 962 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 963 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 964 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 965 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 966 | unsigned char nonce[16] = {0}; |
| 967 | size_t nonce_length = nonce_length_arg; |
| 968 | unsigned char tag[16]; |
| 969 | size_t tag_length = tag_length_arg; |
| 970 | size_t output_length; |
| 971 | |
| 972 | TEST_ASSERT( nonce_length <= sizeof( nonce ) ); |
| 973 | TEST_ASSERT( tag_length <= sizeof( tag ) ); |
| 974 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 975 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 976 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 977 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 978 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 979 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 980 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 981 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 982 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 983 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 984 | /* Check if no key usage implication is done */ |
| 985 | TEST_EQUAL( policy_usage, |
| 986 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 987 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 988 | status = psa_aead_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 989 | nonce, nonce_length, |
| 990 | NULL, 0, |
| 991 | NULL, 0, |
| 992 | tag, tag_length, |
| 993 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 994 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 995 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 996 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 997 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 998 | |
| 999 | memset( tag, 0, sizeof( tag ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1000 | status = psa_aead_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1001 | nonce, nonce_length, |
| 1002 | NULL, 0, |
| 1003 | tag, tag_length, |
| 1004 | NULL, 0, |
| 1005 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1006 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 1007 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1008 | else if( expected_status == PSA_SUCCESS ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1009 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1010 | else |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1011 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1012 | |
| 1013 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1014 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1015 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1016 | } |
| 1017 | /* END_CASE */ |
| 1018 | |
| 1019 | /* BEGIN_CASE */ |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1020 | void asymmetric_encryption_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1021 | int policy_alg, |
| 1022 | int key_type, |
| 1023 | data_t *key_data, |
| 1024 | int exercise_alg ) |
| 1025 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1026 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1027 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1028 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1029 | psa_status_t status; |
| 1030 | size_t key_bits; |
| 1031 | size_t buffer_length; |
| 1032 | unsigned char *buffer = NULL; |
| 1033 | size_t output_length; |
| 1034 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1035 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1036 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1037 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1038 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1039 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1040 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1041 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1042 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1043 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1044 | /* Check if no key usage implication is done */ |
| 1045 | TEST_EQUAL( policy_usage, |
| 1046 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1047 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1048 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1049 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1050 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 1051 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1052 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1053 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1054 | status = psa_asymmetric_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1055 | NULL, 0, |
| 1056 | NULL, 0, |
| 1057 | buffer, buffer_length, |
| 1058 | &output_length ); |
| 1059 | if( policy_alg == exercise_alg && |
| 1060 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1061 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1062 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1063 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1064 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 1065 | if( buffer_length != 0 ) |
| 1066 | memset( buffer, 0, buffer_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1067 | status = psa_asymmetric_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1068 | buffer, buffer_length, |
| 1069 | NULL, 0, |
| 1070 | buffer, buffer_length, |
| 1071 | &output_length ); |
| 1072 | if( policy_alg == exercise_alg && |
| 1073 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1074 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1075 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1076 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1077 | |
| 1078 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1079 | /* |
| 1080 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1081 | * thus reset them as required. |
| 1082 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1083 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1084 | |
| 1085 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1086 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1087 | mbedtls_free( buffer ); |
| 1088 | } |
| 1089 | /* END_CASE */ |
| 1090 | |
| 1091 | /* BEGIN_CASE */ |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1092 | void asymmetric_signature_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1093 | int policy_alg, |
| 1094 | int key_type, |
| 1095 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1096 | int exercise_alg, |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1097 | int payload_length_arg, |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1098 | int expected_usage_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1099 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1100 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1101 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1102 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 1103 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1104 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1105 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 1106 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 1107 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 1108 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 1109 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 1110 | int compatible_alg = payload_length_arg > 0; |
| 1111 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1112 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1113 | size_t signature_length; |
| 1114 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1115 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1116 | in the expected usage flags. */ |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1117 | TEST_EQUAL( expected_usage, |
| 1118 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1119 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1120 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1121 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1122 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1123 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1124 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1125 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1126 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1127 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1128 | |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1129 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1130 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1131 | status = psa_sign_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1132 | payload, payload_length, |
| 1133 | signature, sizeof( signature ), |
| 1134 | &signature_length ); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1135 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1136 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1137 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1138 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1139 | |
| 1140 | memset( signature, 0, sizeof( signature ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1141 | status = psa_verify_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1142 | payload, payload_length, |
| 1143 | signature, sizeof( signature ) ); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1144 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1145 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1146 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1147 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1148 | |
gabor-mezei-arm | 79df41d | 2021-06-28 14:53:49 +0200 | [diff] [blame] | 1149 | if( PSA_ALG_IS_HASH_AND_SIGN( exercise_alg ) && |
| 1150 | PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) ) |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1151 | { |
| 1152 | status = psa_sign_message( key, exercise_alg, |
| 1153 | payload, payload_length, |
| 1154 | signature, sizeof( signature ), |
| 1155 | &signature_length ); |
| 1156 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 ) |
| 1157 | PSA_ASSERT( status ); |
| 1158 | else |
| 1159 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1160 | |
| 1161 | memset( signature, 0, sizeof( signature ) ); |
| 1162 | status = psa_verify_message( key, exercise_alg, |
| 1163 | payload, payload_length, |
| 1164 | signature, sizeof( signature ) ); |
| 1165 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 ) |
| 1166 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 1167 | else |
| 1168 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1169 | } |
| 1170 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1171 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1172 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1173 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1174 | } |
| 1175 | /* END_CASE */ |
| 1176 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1177 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1178 | void derive_key_policy( int policy_usage, |
| 1179 | int policy_alg, |
| 1180 | int key_type, |
| 1181 | data_t *key_data, |
| 1182 | int exercise_alg ) |
| 1183 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1184 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1185 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1186 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1187 | psa_status_t status; |
| 1188 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1189 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1190 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1191 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1192 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1193 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1194 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1195 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1196 | &key ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1197 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1198 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 1199 | |
| 1200 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 1201 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1202 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1203 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 1204 | &operation, |
| 1205 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 1206 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1207 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1208 | |
| 1209 | status = psa_key_derivation_input_key( &operation, |
| 1210 | PSA_KEY_DERIVATION_INPUT_SECRET, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1211 | key ); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1212 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1213 | if( policy_alg == exercise_alg && |
| 1214 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1215 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1216 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1217 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1218 | |
| 1219 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1220 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1221 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1222 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1223 | } |
| 1224 | /* END_CASE */ |
| 1225 | |
| 1226 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1227 | void agreement_key_policy( int policy_usage, |
| 1228 | int policy_alg, |
| 1229 | int key_type_arg, |
| 1230 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1231 | int exercise_alg, |
| 1232 | int expected_status_arg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1233 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1234 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1235 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1236 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1237 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1238 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1239 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1240 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1241 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1242 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1243 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1244 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1245 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1246 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1247 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1248 | &key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1249 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1250 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1251 | status = mbedtls_test_psa_key_agreement_with_self( &operation, key ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1252 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1253 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1254 | |
| 1255 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1256 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1257 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1258 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1259 | } |
| 1260 | /* END_CASE */ |
| 1261 | |
| 1262 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1263 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 1264 | int usage_arg, int alg_arg, int alg2_arg ) |
| 1265 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1266 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1267 | psa_key_type_t key_type = key_type_arg; |
| 1268 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1269 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1270 | psa_key_usage_t usage = usage_arg; |
| 1271 | psa_algorithm_t alg = alg_arg; |
| 1272 | psa_algorithm_t alg2 = alg2_arg; |
| 1273 | |
| 1274 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1275 | |
| 1276 | psa_set_key_usage_flags( &attributes, usage ); |
| 1277 | psa_set_key_algorithm( &attributes, alg ); |
| 1278 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 1279 | psa_set_key_type( &attributes, key_type ); |
| 1280 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1281 | &key ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1282 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1283 | /* Update the usage flags to obtain implicit usage flags */ |
| 1284 | usage = mbedtls_test_update_key_usage_flags( usage ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1285 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1286 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 1287 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 1288 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 1289 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1290 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1291 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1292 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1293 | goto exit; |
| 1294 | |
| 1295 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1296 | /* |
| 1297 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1298 | * thus reset them as required. |
| 1299 | */ |
| 1300 | psa_reset_key_attributes( &got_attributes ); |
| 1301 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1302 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1303 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1304 | } |
| 1305 | /* END_CASE */ |
| 1306 | |
| 1307 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1308 | void raw_agreement_key_policy( int policy_usage, |
| 1309 | int policy_alg, |
| 1310 | int key_type_arg, |
| 1311 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1312 | int exercise_alg, |
| 1313 | int expected_status_arg ) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1314 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1315 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1316 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1317 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1318 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1319 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1320 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1321 | |
| 1322 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1323 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1324 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1325 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1326 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1327 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1328 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1329 | &key ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1330 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1331 | status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1332 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1333 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1334 | |
| 1335 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1336 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1337 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1338 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1339 | } |
| 1340 | /* END_CASE */ |
| 1341 | |
| 1342 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1343 | void copy_success( int source_usage_arg, |
| 1344 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1345 | int type_arg, data_t *material, |
| 1346 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1347 | int target_usage_arg, |
| 1348 | int target_alg_arg, int target_alg2_arg, |
| 1349 | int expected_usage_arg, |
| 1350 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1351 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1352 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1353 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1354 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 1355 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1356 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1357 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1358 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1359 | uint8_t *export_buffer = NULL; |
| 1360 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1361 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1362 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1363 | /* Prepare the source key. */ |
| 1364 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 1365 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1366 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1367 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1368 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 1369 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1370 | &source_key ) ); |
| 1371 | PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1372 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1373 | /* Prepare the target attributes. */ |
| 1374 | if( copy_attributes ) |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1375 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1376 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1377 | /* Set volatile lifetime to reset the key identifier to 0. */ |
| 1378 | psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE ); |
| 1379 | } |
| 1380 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1381 | if( target_usage_arg != -1 ) |
| 1382 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 1383 | if( target_alg_arg != -1 ) |
| 1384 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1385 | if( target_alg2_arg != -1 ) |
| 1386 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1387 | |
| 1388 | /* Copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1389 | PSA_ASSERT( psa_copy_key( source_key, |
| 1390 | &target_attributes, &target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1391 | |
| 1392 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1393 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1394 | |
| 1395 | /* Test that the target slot has the expected content and policy. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1396 | PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1397 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 1398 | psa_get_key_type( &target_attributes ) ); |
| 1399 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 1400 | psa_get_key_bits( &target_attributes ) ); |
| 1401 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 1402 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1403 | TEST_EQUAL( expected_alg2, |
| 1404 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1405 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 1406 | { |
| 1407 | size_t length; |
| 1408 | ASSERT_ALLOC( export_buffer, material->len ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1409 | PSA_ASSERT( psa_export_key( target_key, export_buffer, |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1410 | material->len, &length ) ); |
| 1411 | ASSERT_COMPARE( material->x, material->len, |
| 1412 | export_buffer, length ); |
| 1413 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1414 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1415 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1416 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1417 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) ) |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1418 | goto exit; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1419 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1420 | PSA_ASSERT( psa_destroy_key( target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1421 | |
| 1422 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1423 | /* |
| 1424 | * Source and target key attributes may have been returned by |
| 1425 | * psa_get_key_attributes() thus reset them as required. |
| 1426 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1427 | psa_reset_key_attributes( &source_attributes ); |
| 1428 | psa_reset_key_attributes( &target_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1429 | |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1430 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1431 | mbedtls_free( export_buffer ); |
| 1432 | } |
| 1433 | /* END_CASE */ |
| 1434 | |
| 1435 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1436 | void copy_fail( int source_usage_arg, |
| 1437 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1438 | int type_arg, data_t *material, |
| 1439 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1440 | int target_usage_arg, |
| 1441 | int target_alg_arg, int target_alg2_arg, |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 1442 | int target_id_arg, int target_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1443 | int expected_status_arg ) |
| 1444 | { |
| 1445 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1446 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1447 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1448 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 1449 | 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] | 1450 | |
| 1451 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1452 | |
| 1453 | /* Prepare the source key. */ |
| 1454 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 1455 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1456 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1457 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1458 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 1459 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1460 | &source_key ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1461 | |
| 1462 | /* Prepare the target attributes. */ |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 1463 | psa_set_key_id( &target_attributes, key_id ); |
| 1464 | psa_set_key_lifetime( &target_attributes, target_lifetime_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1465 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 1466 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 1467 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 1468 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1469 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1470 | |
| 1471 | /* Try to copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1472 | TEST_EQUAL( psa_copy_key( source_key, |
| 1473 | &target_attributes, &target_key ), |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1474 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1475 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1476 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1477 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1478 | exit: |
| 1479 | psa_reset_key_attributes( &source_attributes ); |
| 1480 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1481 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1482 | } |
| 1483 | /* END_CASE */ |
| 1484 | |
| 1485 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1486 | void hash_operation_init( ) |
| 1487 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1488 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1489 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1490 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1491 | * though it's OK by the C standard. We could test for this, but we'd need |
| 1492 | * to supress the Clang warning for the test. */ |
| 1493 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 1494 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 1495 | psa_hash_operation_t zero; |
| 1496 | |
| 1497 | memset( &zero, 0, sizeof( zero ) ); |
| 1498 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1499 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1500 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 1501 | PSA_ERROR_BAD_STATE ); |
| 1502 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 1503 | PSA_ERROR_BAD_STATE ); |
| 1504 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 1505 | PSA_ERROR_BAD_STATE ); |
| 1506 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1507 | /* A default hash operation should be abortable without error. */ |
| 1508 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 1509 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 1510 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1511 | } |
| 1512 | /* END_CASE */ |
| 1513 | |
| 1514 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1515 | void hash_setup( int alg_arg, |
| 1516 | int expected_status_arg ) |
| 1517 | { |
| 1518 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1519 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1520 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1521 | psa_status_t status; |
| 1522 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1523 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1524 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1525 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1526 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1527 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 1528 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 1529 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1530 | |
| 1531 | /* If setup failed, reproduce the failure, so as to |
| 1532 | * test the resulting state of the operation object. */ |
| 1533 | if( status != PSA_SUCCESS ) |
| 1534 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 1535 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1536 | /* Now the operation object should be reusable. */ |
| 1537 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 1538 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 1539 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1540 | #endif |
| 1541 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1542 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1543 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1544 | } |
| 1545 | /* END_CASE */ |
| 1546 | |
| 1547 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1548 | void hash_compute_fail( int alg_arg, data_t *input, |
| 1549 | int output_size_arg, int expected_status_arg ) |
| 1550 | { |
| 1551 | psa_algorithm_t alg = alg_arg; |
| 1552 | uint8_t *output = NULL; |
| 1553 | size_t output_size = output_size_arg; |
| 1554 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 1555 | psa_status_t expected_status = expected_status_arg; |
| 1556 | psa_status_t status; |
| 1557 | |
| 1558 | ASSERT_ALLOC( output, output_size ); |
| 1559 | |
| 1560 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1561 | |
| 1562 | status = psa_hash_compute( alg, input->x, input->len, |
| 1563 | output, output_size, &output_length ); |
| 1564 | TEST_EQUAL( status, expected_status ); |
| 1565 | TEST_ASSERT( output_length <= output_size ); |
| 1566 | |
| 1567 | exit: |
| 1568 | mbedtls_free( output ); |
| 1569 | PSA_DONE( ); |
| 1570 | } |
| 1571 | /* END_CASE */ |
| 1572 | |
| 1573 | /* BEGIN_CASE */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 1574 | void hash_compare_fail( int alg_arg, data_t *input, |
| 1575 | data_t *reference_hash, |
| 1576 | int expected_status_arg ) |
| 1577 | { |
| 1578 | psa_algorithm_t alg = alg_arg; |
| 1579 | psa_status_t expected_status = expected_status_arg; |
| 1580 | psa_status_t status; |
| 1581 | |
| 1582 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1583 | |
| 1584 | status = psa_hash_compare( alg, input->x, input->len, |
| 1585 | reference_hash->x, reference_hash->len ); |
| 1586 | TEST_EQUAL( status, expected_status ); |
| 1587 | |
| 1588 | exit: |
| 1589 | PSA_DONE( ); |
| 1590 | } |
| 1591 | /* END_CASE */ |
| 1592 | |
| 1593 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1594 | void hash_compute_compare( int alg_arg, data_t *input, |
| 1595 | data_t *expected_output ) |
| 1596 | { |
| 1597 | psa_algorithm_t alg = alg_arg; |
| 1598 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 1599 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 1600 | size_t i; |
| 1601 | |
| 1602 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1603 | |
| 1604 | /* Compute with tight buffer */ |
| 1605 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1606 | output, PSA_HASH_LENGTH( alg ), |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1607 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1608 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1609 | ASSERT_COMPARE( output, output_length, |
| 1610 | expected_output->x, expected_output->len ); |
| 1611 | |
| 1612 | /* Compute with larger buffer */ |
| 1613 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
| 1614 | output, sizeof( output ), |
| 1615 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1616 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1617 | ASSERT_COMPARE( output, output_length, |
| 1618 | expected_output->x, expected_output->len ); |
| 1619 | |
| 1620 | /* Compare with correct hash */ |
| 1621 | PSA_ASSERT( psa_hash_compare( alg, input->x, input->len, |
| 1622 | output, output_length ) ); |
| 1623 | |
| 1624 | /* Compare with trailing garbage */ |
| 1625 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 1626 | output, output_length + 1 ), |
| 1627 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1628 | |
| 1629 | /* Compare with truncated hash */ |
| 1630 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 1631 | output, output_length - 1 ), |
| 1632 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1633 | |
| 1634 | /* Compare with corrupted value */ |
| 1635 | for( i = 0; i < output_length; i++ ) |
| 1636 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 1637 | mbedtls_test_set_step( i ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1638 | output[i] ^= 1; |
| 1639 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 1640 | output, output_length ), |
| 1641 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1642 | output[i] ^= 1; |
| 1643 | } |
| 1644 | |
| 1645 | exit: |
| 1646 | PSA_DONE( ); |
| 1647 | } |
| 1648 | /* END_CASE */ |
| 1649 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 1650 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1651 | void hash_bad_order( ) |
| 1652 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1653 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1654 | unsigned char input[] = ""; |
| 1655 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1656 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1657 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 1658 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 1659 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1660 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1661 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1662 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1663 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1664 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1665 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1666 | /* Call setup twice in a row. */ |
| 1667 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1668 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1669 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 1670 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1671 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1672 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1673 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1674 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1675 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1676 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1677 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1678 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1679 | |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1680 | /* Check that update calls abort on error. */ |
| 1681 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 54f7351 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 1682 | operation.id = UINT_MAX; |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1683 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
| 1684 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
| 1685 | PSA_ERROR_BAD_STATE ); |
| 1686 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 1687 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1688 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 1689 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1690 | /* Call update after finish. */ |
| 1691 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1692 | PSA_ASSERT( psa_hash_finish( &operation, |
| 1693 | hash, sizeof( hash ), &hash_len ) ); |
| 1694 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1695 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1696 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1697 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1698 | /* Call verify without calling setup beforehand. */ |
| 1699 | TEST_EQUAL( psa_hash_verify( &operation, |
| 1700 | valid_hash, sizeof( valid_hash ) ), |
| 1701 | PSA_ERROR_BAD_STATE ); |
| 1702 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1703 | |
| 1704 | /* Call verify after finish. */ |
| 1705 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1706 | PSA_ASSERT( psa_hash_finish( &operation, |
| 1707 | hash, sizeof( hash ), &hash_len ) ); |
| 1708 | TEST_EQUAL( psa_hash_verify( &operation, |
| 1709 | valid_hash, sizeof( valid_hash ) ), |
| 1710 | PSA_ERROR_BAD_STATE ); |
| 1711 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1712 | |
| 1713 | /* Call verify twice in a row. */ |
| 1714 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1715 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1716 | PSA_ASSERT( psa_hash_verify( &operation, |
| 1717 | valid_hash, sizeof( valid_hash ) ) ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1718 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1719 | TEST_EQUAL( psa_hash_verify( &operation, |
| 1720 | valid_hash, sizeof( valid_hash ) ), |
| 1721 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1722 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1723 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1724 | |
| 1725 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1726 | TEST_EQUAL( psa_hash_finish( &operation, |
| 1727 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1728 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1729 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1730 | |
| 1731 | /* Call finish twice in a row. */ |
| 1732 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1733 | PSA_ASSERT( psa_hash_finish( &operation, |
| 1734 | hash, sizeof( hash ), &hash_len ) ); |
| 1735 | TEST_EQUAL( psa_hash_finish( &operation, |
| 1736 | hash, sizeof( hash ), &hash_len ), |
| 1737 | PSA_ERROR_BAD_STATE ); |
| 1738 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1739 | |
| 1740 | /* Call finish after calling verify. */ |
| 1741 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1742 | PSA_ASSERT( psa_hash_verify( &operation, |
| 1743 | valid_hash, sizeof( valid_hash ) ) ); |
| 1744 | TEST_EQUAL( psa_hash_finish( &operation, |
| 1745 | hash, sizeof( hash ), &hash_len ), |
| 1746 | PSA_ERROR_BAD_STATE ); |
| 1747 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1748 | |
| 1749 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1750 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1751 | } |
| 1752 | /* END_CASE */ |
| 1753 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 1754 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1755 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1756 | { |
| 1757 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1758 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 1759 | * appended to it */ |
| 1760 | unsigned char hash[] = { |
| 1761 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 1762 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 1763 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1764 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1765 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1766 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1767 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1768 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1769 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1770 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1771 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1772 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1773 | PSA_ERROR_INVALID_SIGNATURE ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1774 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 1775 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1776 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1777 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1778 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1779 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1780 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1781 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1782 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1783 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1784 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1785 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1786 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 1787 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1788 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1789 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1790 | } |
| 1791 | /* END_CASE */ |
| 1792 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 1793 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 1794 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1795 | { |
| 1796 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 1797 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1798 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1799 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1800 | size_t hash_len; |
| 1801 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1802 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1803 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1804 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1805 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1806 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1807 | hash, expected_size - 1, &hash_len ), |
| 1808 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1809 | |
| 1810 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1811 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1812 | } |
| 1813 | /* END_CASE */ |
| 1814 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 1815 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1816 | void hash_clone_source_state( ) |
| 1817 | { |
| 1818 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 1819 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 1820 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 1821 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 1822 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 1823 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 1824 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 1825 | size_t hash_len; |
| 1826 | |
| 1827 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1828 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 1829 | |
| 1830 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 1831 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 1832 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 1833 | hash, sizeof( hash ), &hash_len ) ); |
| 1834 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 1835 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 1836 | |
| 1837 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 1838 | PSA_ERROR_BAD_STATE ); |
| 1839 | |
| 1840 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 1841 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 1842 | hash, sizeof( hash ), &hash_len ) ); |
| 1843 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 1844 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 1845 | hash, sizeof( hash ), &hash_len ) ); |
| 1846 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 1847 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 1848 | hash, sizeof( hash ), &hash_len ) ); |
| 1849 | |
| 1850 | exit: |
| 1851 | psa_hash_abort( &op_source ); |
| 1852 | psa_hash_abort( &op_init ); |
| 1853 | psa_hash_abort( &op_setup ); |
| 1854 | psa_hash_abort( &op_finished ); |
| 1855 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1856 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1857 | } |
| 1858 | /* END_CASE */ |
| 1859 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 1860 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1861 | void hash_clone_target_state( ) |
| 1862 | { |
| 1863 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 1864 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 1865 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 1866 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 1867 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 1868 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 1869 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 1870 | size_t hash_len; |
| 1871 | |
| 1872 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1873 | |
| 1874 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 1875 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 1876 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 1877 | hash, sizeof( hash ), &hash_len ) ); |
| 1878 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 1879 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 1880 | |
| 1881 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 1882 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 1883 | hash, sizeof( hash ), &hash_len ) ); |
| 1884 | |
| 1885 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 1886 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 1887 | PSA_ERROR_BAD_STATE ); |
| 1888 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 1889 | PSA_ERROR_BAD_STATE ); |
| 1890 | |
| 1891 | exit: |
| 1892 | psa_hash_abort( &op_target ); |
| 1893 | psa_hash_abort( &op_init ); |
| 1894 | psa_hash_abort( &op_setup ); |
| 1895 | psa_hash_abort( &op_finished ); |
| 1896 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1897 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1898 | } |
| 1899 | /* END_CASE */ |
| 1900 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1901 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1902 | void mac_operation_init( ) |
| 1903 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1904 | const uint8_t input[1] = { 0 }; |
| 1905 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1906 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1907 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1908 | * though it's OK by the C standard. We could test for this, but we'd need |
| 1909 | * to supress the Clang warning for the test. */ |
| 1910 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 1911 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 1912 | psa_mac_operation_t zero; |
| 1913 | |
| 1914 | memset( &zero, 0, sizeof( zero ) ); |
| 1915 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1916 | /* A freshly-initialized MAC operation should not be usable. */ |
| 1917 | TEST_EQUAL( psa_mac_update( &func, |
| 1918 | input, sizeof( input ) ), |
| 1919 | PSA_ERROR_BAD_STATE ); |
| 1920 | TEST_EQUAL( psa_mac_update( &init, |
| 1921 | input, sizeof( input ) ), |
| 1922 | PSA_ERROR_BAD_STATE ); |
| 1923 | TEST_EQUAL( psa_mac_update( &zero, |
| 1924 | input, sizeof( input ) ), |
| 1925 | PSA_ERROR_BAD_STATE ); |
| 1926 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1927 | /* A default MAC operation should be abortable without error. */ |
| 1928 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 1929 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 1930 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1931 | } |
| 1932 | /* END_CASE */ |
| 1933 | |
| 1934 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1935 | void mac_setup( int key_type_arg, |
| 1936 | data_t *key, |
| 1937 | int alg_arg, |
| 1938 | int expected_status_arg ) |
| 1939 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1940 | psa_key_type_t key_type = key_type_arg; |
| 1941 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1942 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1943 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1944 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 1945 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 1946 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 1947 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1948 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1949 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1950 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1951 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 1952 | &operation, &status ) ) |
| 1953 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1954 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1955 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1956 | /* The operation object should be reusable. */ |
| 1957 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 1958 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 1959 | smoke_test_key_data, |
| 1960 | sizeof( smoke_test_key_data ), |
| 1961 | KNOWN_SUPPORTED_MAC_ALG, |
| 1962 | &operation, &status ) ) |
| 1963 | goto exit; |
| 1964 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1965 | #endif |
| 1966 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1967 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1968 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1969 | } |
| 1970 | /* END_CASE */ |
| 1971 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 1972 | /* 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] | 1973 | void mac_bad_order( ) |
| 1974 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1975 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1976 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 1977 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1978 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1979 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 1980 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 1981 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1982 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1983 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 1984 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 1985 | size_t sign_mac_length = 0; |
| 1986 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 1987 | const uint8_t verify_mac[] = { |
| 1988 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 1989 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 1990 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 1991 | |
| 1992 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1993 | 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] | 1994 | psa_set_key_algorithm( &attributes, alg ); |
| 1995 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1996 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1997 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 1998 | &key ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1999 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2000 | /* Call update without calling setup beforehand. */ |
| 2001 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2002 | PSA_ERROR_BAD_STATE ); |
| 2003 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2004 | |
| 2005 | /* Call sign finish without calling setup beforehand. */ |
| 2006 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 2007 | &sign_mac_length), |
| 2008 | PSA_ERROR_BAD_STATE ); |
| 2009 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2010 | |
| 2011 | /* Call verify finish without calling setup beforehand. */ |
| 2012 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2013 | verify_mac, sizeof( verify_mac ) ), |
| 2014 | PSA_ERROR_BAD_STATE ); |
| 2015 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2016 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2017 | /* Call setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2018 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2019 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2020 | TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2021 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2022 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2023 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2024 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2025 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2026 | /* Call update after sign finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2027 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2028 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2029 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2030 | sign_mac, sizeof( sign_mac ), |
| 2031 | &sign_mac_length ) ); |
| 2032 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2033 | PSA_ERROR_BAD_STATE ); |
| 2034 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2035 | |
| 2036 | /* Call update after verify finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2037 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2038 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2039 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2040 | verify_mac, sizeof( verify_mac ) ) ); |
| 2041 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2042 | PSA_ERROR_BAD_STATE ); |
| 2043 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2044 | |
| 2045 | /* Call sign finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2046 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2047 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2048 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2049 | sign_mac, sizeof( sign_mac ), |
| 2050 | &sign_mac_length ) ); |
| 2051 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2052 | sign_mac, sizeof( sign_mac ), |
| 2053 | &sign_mac_length ), |
| 2054 | PSA_ERROR_BAD_STATE ); |
| 2055 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2056 | |
| 2057 | /* Call verify finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2058 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2059 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2060 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2061 | verify_mac, sizeof( verify_mac ) ) ); |
| 2062 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2063 | verify_mac, sizeof( verify_mac ) ), |
| 2064 | PSA_ERROR_BAD_STATE ); |
| 2065 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2066 | |
| 2067 | /* Setup sign but try verify. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2068 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2069 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2070 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2071 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2072 | verify_mac, sizeof( verify_mac ) ), |
| 2073 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2074 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2075 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2076 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2077 | |
| 2078 | /* Setup verify but try sign. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2079 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2080 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2081 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2082 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2083 | sign_mac, sizeof( sign_mac ), |
| 2084 | &sign_mac_length ), |
| 2085 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2086 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2087 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2088 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2089 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2090 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2091 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2092 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2093 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2094 | } |
| 2095 | /* END_CASE */ |
| 2096 | |
| 2097 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2098 | void mac_sign( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2099 | data_t *key_data, |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2100 | int alg_arg, |
| 2101 | data_t *input, |
| 2102 | data_t *expected_mac ) |
| 2103 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2104 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2105 | psa_key_type_t key_type = key_type_arg; |
| 2106 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2107 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2108 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2109 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2110 | size_t mac_buffer_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2111 | 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] | 2112 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2113 | const size_t output_sizes_to_test[] = { |
| 2114 | 0, |
| 2115 | 1, |
| 2116 | expected_mac->len - 1, |
| 2117 | expected_mac->len, |
| 2118 | expected_mac->len + 1, |
| 2119 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2120 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2121 | TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2122 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 3d404d6 | 2020-08-25 23:47:36 +0200 | [diff] [blame] | 2123 | TEST_ASSERT( expected_mac->len == mac_buffer_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2124 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2125 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2126 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2127 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2128 | psa_set_key_algorithm( &attributes, alg ); |
| 2129 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2130 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2131 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2132 | &key ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2133 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2134 | for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ ) |
| 2135 | { |
| 2136 | const size_t output_size = output_sizes_to_test[i]; |
| 2137 | psa_status_t expected_status = |
| 2138 | ( output_size >= expected_mac->len ? PSA_SUCCESS : |
| 2139 | PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2140 | |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2141 | mbedtls_test_set_step( output_size ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2142 | ASSERT_ALLOC( actual_mac, output_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2143 | |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2144 | /* Calculate the MAC, one-shot case. */ |
| 2145 | TEST_EQUAL( psa_mac_compute( key, alg, |
| 2146 | input->x, input->len, |
| 2147 | actual_mac, output_size, &mac_length ), |
| 2148 | expected_status ); |
| 2149 | if( expected_status == PSA_SUCCESS ) |
| 2150 | { |
| 2151 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 2152 | actual_mac, mac_length ); |
| 2153 | } |
| 2154 | |
| 2155 | if( output_size > 0 ) |
| 2156 | memset( actual_mac, 0, output_size ); |
| 2157 | |
| 2158 | /* Calculate the MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2159 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2160 | PSA_ASSERT( psa_mac_update( &operation, |
| 2161 | input->x, input->len ) ); |
| 2162 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2163 | actual_mac, output_size, |
| 2164 | &mac_length ), |
| 2165 | expected_status ); |
| 2166 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2167 | |
| 2168 | if( expected_status == PSA_SUCCESS ) |
| 2169 | { |
| 2170 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 2171 | actual_mac, mac_length ); |
| 2172 | } |
| 2173 | mbedtls_free( actual_mac ); |
| 2174 | actual_mac = NULL; |
| 2175 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2176 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2177 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2178 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2179 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2180 | PSA_DONE( ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2181 | mbedtls_free( actual_mac ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2182 | } |
| 2183 | /* END_CASE */ |
| 2184 | |
| 2185 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2186 | void mac_verify( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2187 | data_t *key_data, |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2188 | int alg_arg, |
| 2189 | data_t *input, |
| 2190 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2191 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2192 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2193 | psa_key_type_t key_type = key_type_arg; |
| 2194 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2195 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2196 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2197 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2198 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 2199 | TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE ); |
| 2200 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2201 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2202 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2203 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2204 | psa_set_key_algorithm( &attributes, alg ); |
| 2205 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2206 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2207 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2208 | &key ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2209 | |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2210 | /* Verify correct MAC, one-shot case. */ |
| 2211 | PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len, |
| 2212 | expected_mac->x, expected_mac->len ) ); |
| 2213 | |
| 2214 | /* Verify correct MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2215 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2216 | PSA_ASSERT( psa_mac_update( &operation, |
| 2217 | input->x, input->len ) ); |
| 2218 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2219 | expected_mac->x, |
| 2220 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2221 | |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2222 | /* Test a MAC that's too short, one-shot case. */ |
| 2223 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 2224 | input->x, input->len, |
| 2225 | expected_mac->x, |
| 2226 | expected_mac->len - 1 ), |
| 2227 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2228 | |
| 2229 | /* Test a MAC that's too short, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2230 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2231 | PSA_ASSERT( psa_mac_update( &operation, |
| 2232 | input->x, input->len ) ); |
| 2233 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2234 | expected_mac->x, |
| 2235 | expected_mac->len - 1 ), |
| 2236 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2237 | |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2238 | /* Test a MAC that's too long, one-shot case. */ |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2239 | ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 ); |
| 2240 | memcpy( perturbed_mac, expected_mac->x, expected_mac->len ); |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2241 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 2242 | input->x, input->len, |
| 2243 | perturbed_mac, expected_mac->len + 1 ), |
| 2244 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2245 | |
| 2246 | /* Test a MAC that's too long, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2247 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2248 | PSA_ASSERT( psa_mac_update( &operation, |
| 2249 | input->x, input->len ) ); |
| 2250 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2251 | perturbed_mac, |
| 2252 | expected_mac->len + 1 ), |
| 2253 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2254 | |
| 2255 | /* Test changing one byte. */ |
| 2256 | for( size_t i = 0; i < expected_mac->len; i++ ) |
| 2257 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2258 | mbedtls_test_set_step( i ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2259 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2260 | |
| 2261 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 2262 | input->x, input->len, |
| 2263 | perturbed_mac, expected_mac->len ), |
| 2264 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2265 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2266 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2267 | PSA_ASSERT( psa_mac_update( &operation, |
| 2268 | input->x, input->len ) ); |
| 2269 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2270 | perturbed_mac, |
| 2271 | expected_mac->len ), |
| 2272 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2273 | perturbed_mac[i] ^= 1; |
| 2274 | } |
| 2275 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2276 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2277 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2278 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2279 | PSA_DONE( ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2280 | mbedtls_free( perturbed_mac ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2281 | } |
| 2282 | /* END_CASE */ |
| 2283 | |
| 2284 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2285 | void cipher_operation_init( ) |
| 2286 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2287 | const uint8_t input[1] = { 0 }; |
| 2288 | unsigned char output[1] = { 0 }; |
| 2289 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2290 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2291 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2292 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2293 | * to supress the Clang warning for the test. */ |
| 2294 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 2295 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 2296 | psa_cipher_operation_t zero; |
| 2297 | |
| 2298 | memset( &zero, 0, sizeof( zero ) ); |
| 2299 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2300 | /* A freshly-initialized cipher operation should not be usable. */ |
| 2301 | TEST_EQUAL( psa_cipher_update( &func, |
| 2302 | input, sizeof( input ), |
| 2303 | output, sizeof( output ), |
| 2304 | &output_length ), |
| 2305 | PSA_ERROR_BAD_STATE ); |
| 2306 | TEST_EQUAL( psa_cipher_update( &init, |
| 2307 | input, sizeof( input ), |
| 2308 | output, sizeof( output ), |
| 2309 | &output_length ), |
| 2310 | PSA_ERROR_BAD_STATE ); |
| 2311 | TEST_EQUAL( psa_cipher_update( &zero, |
| 2312 | input, sizeof( input ), |
| 2313 | output, sizeof( output ), |
| 2314 | &output_length ), |
| 2315 | PSA_ERROR_BAD_STATE ); |
| 2316 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2317 | /* A default cipher operation should be abortable without error. */ |
| 2318 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 2319 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 2320 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2321 | } |
| 2322 | /* END_CASE */ |
| 2323 | |
| 2324 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2325 | void cipher_setup( int key_type_arg, |
| 2326 | data_t *key, |
| 2327 | int alg_arg, |
| 2328 | int expected_status_arg ) |
| 2329 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2330 | psa_key_type_t key_type = key_type_arg; |
| 2331 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2332 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2333 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2334 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 2335 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2336 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2337 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2338 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2339 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2340 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2341 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 2342 | &operation, &status ) ) |
| 2343 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2344 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2345 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2346 | /* The operation object should be reusable. */ |
| 2347 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 2348 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 2349 | smoke_test_key_data, |
| 2350 | sizeof( smoke_test_key_data ), |
| 2351 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 2352 | &operation, &status ) ) |
| 2353 | goto exit; |
| 2354 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2355 | #endif |
| 2356 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2357 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2358 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2359 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2360 | } |
| 2361 | /* END_CASE */ |
| 2362 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2363 | /* 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] | 2364 | void cipher_bad_order( ) |
| 2365 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2366 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2367 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 2368 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2369 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2370 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2371 | 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] | 2372 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2373 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2374 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 2375 | const uint8_t text[] = { |
| 2376 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 2377 | 0xbb, 0xbb, 0xbb, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2378 | 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] | 2379 | size_t length = 0; |
| 2380 | |
| 2381 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2382 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 2383 | psa_set_key_algorithm( &attributes, alg ); |
| 2384 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2385 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 2386 | &key ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2387 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2388 | /* Call encrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2389 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2390 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2391 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2392 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2393 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2394 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2395 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2396 | |
| 2397 | /* Call decrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2398 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2399 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2400 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2401 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2402 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2403 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2404 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2405 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2406 | /* Generate an IV without calling setup beforehand. */ |
| 2407 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2408 | buffer, sizeof( buffer ), |
| 2409 | &length ), |
| 2410 | PSA_ERROR_BAD_STATE ); |
| 2411 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2412 | |
| 2413 | /* Generate an IV twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2414 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2415 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 2416 | buffer, sizeof( buffer ), |
| 2417 | &length ) ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2418 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2419 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2420 | buffer, sizeof( buffer ), |
| 2421 | &length ), |
| 2422 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2423 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2424 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2425 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2426 | |
| 2427 | /* Generate an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2428 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2429 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2430 | iv, sizeof( iv ) ) ); |
| 2431 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2432 | buffer, sizeof( buffer ), |
| 2433 | &length ), |
| 2434 | PSA_ERROR_BAD_STATE ); |
| 2435 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2436 | |
| 2437 | /* Set an IV without calling setup beforehand. */ |
| 2438 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2439 | iv, sizeof( iv ) ), |
| 2440 | PSA_ERROR_BAD_STATE ); |
| 2441 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2442 | |
| 2443 | /* Set an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2444 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2445 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2446 | iv, sizeof( iv ) ) ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2447 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2448 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2449 | iv, sizeof( iv ) ), |
| 2450 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2451 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2452 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2453 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2454 | |
| 2455 | /* Set an IV after it's already generated. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2456 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2457 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 2458 | buffer, sizeof( buffer ), |
| 2459 | &length ) ); |
| 2460 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2461 | iv, sizeof( iv ) ), |
| 2462 | PSA_ERROR_BAD_STATE ); |
| 2463 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2464 | |
| 2465 | /* Call update without calling setup beforehand. */ |
| 2466 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2467 | text, sizeof( text ), |
| 2468 | buffer, sizeof( buffer ), |
| 2469 | &length ), |
| 2470 | PSA_ERROR_BAD_STATE ); |
| 2471 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2472 | |
| 2473 | /* Call update without an IV where an IV is required. */ |
Dave Rodgman | 33b58ee | 2021-06-23 12:48:52 +0100 | [diff] [blame] | 2474 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2475 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2476 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2477 | text, sizeof( text ), |
| 2478 | buffer, sizeof( buffer ), |
| 2479 | &length ), |
| 2480 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2481 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2482 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2483 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2484 | |
| 2485 | /* Call update after finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2486 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2487 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2488 | iv, sizeof( iv ) ) ); |
| 2489 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2490 | buffer, sizeof( buffer ), &length ) ); |
| 2491 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2492 | text, sizeof( text ), |
| 2493 | buffer, sizeof( buffer ), |
| 2494 | &length ), |
| 2495 | PSA_ERROR_BAD_STATE ); |
| 2496 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2497 | |
| 2498 | /* Call finish without calling setup beforehand. */ |
| 2499 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2500 | buffer, sizeof( buffer ), &length ), |
| 2501 | PSA_ERROR_BAD_STATE ); |
| 2502 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2503 | |
| 2504 | /* Call finish without an IV where an IV is required. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2505 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2506 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 2507 | * for cipher modes with padding. */ |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2508 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2509 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2510 | buffer, sizeof( buffer ), &length ), |
| 2511 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2512 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2513 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2514 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2515 | |
| 2516 | /* Call finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2517 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2518 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2519 | iv, sizeof( iv ) ) ); |
| 2520 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2521 | buffer, sizeof( buffer ), &length ) ); |
| 2522 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2523 | buffer, sizeof( buffer ), &length ), |
| 2524 | PSA_ERROR_BAD_STATE ); |
| 2525 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2526 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2527 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2528 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2529 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2530 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2531 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2532 | } |
| 2533 | /* END_CASE */ |
| 2534 | |
| 2535 | /* BEGIN_CASE */ |
gabor-mezei-arm | 43611b0 | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 2536 | void cipher_encrypt_fail( int alg_arg, |
| 2537 | int key_type_arg, |
| 2538 | data_t *key_data, |
| 2539 | data_t *input, |
| 2540 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2541 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2542 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2543 | psa_status_t status; |
| 2544 | psa_key_type_t key_type = key_type_arg; |
| 2545 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2546 | psa_status_t expected_status = expected_status_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2547 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2548 | size_t output_buffer_size = 0; |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2549 | size_t output_length = 0; |
| 2550 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2551 | |
| 2552 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 2553 | { |
| 2554 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2555 | |
| 2556 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2557 | psa_set_key_algorithm( &attributes, alg ); |
| 2558 | psa_set_key_type( &attributes, key_type ); |
| 2559 | |
| 2560 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 2561 | input->len ); |
| 2562 | ASSERT_ALLOC( output, output_buffer_size ); |
| 2563 | |
| 2564 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2565 | &key ) ); |
| 2566 | } |
| 2567 | |
| 2568 | status = psa_cipher_encrypt( key, alg, input->x, input->len, output, |
| 2569 | output_buffer_size, &output_length ); |
| 2570 | |
| 2571 | TEST_EQUAL( status, expected_status ); |
| 2572 | |
| 2573 | exit: |
| 2574 | mbedtls_free( output ); |
| 2575 | psa_destroy_key( key ); |
| 2576 | PSA_DONE( ); |
| 2577 | } |
| 2578 | /* END_CASE */ |
| 2579 | |
| 2580 | /* BEGIN_CASE */ |
gabor-mezei-arm | 43611b0 | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 2581 | void cipher_encrypt_alg_without_iv( int alg_arg, |
| 2582 | int key_type_arg, |
| 2583 | data_t *key_data, |
| 2584 | data_t *input, |
| 2585 | data_t *expected_output ) |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2586 | { |
| 2587 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2588 | psa_key_type_t key_type = key_type_arg; |
| 2589 | psa_algorithm_t alg = alg_arg; |
| 2590 | unsigned char *output = NULL; |
| 2591 | size_t output_buffer_size = 0; |
| 2592 | size_t output_length = 0; |
| 2593 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2594 | |
| 2595 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2596 | |
| 2597 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2598 | psa_set_key_algorithm( &attributes, alg ); |
| 2599 | psa_set_key_type( &attributes, key_type ); |
| 2600 | |
| 2601 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 2602 | ASSERT_ALLOC( output, output_buffer_size ); |
| 2603 | |
| 2604 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2605 | &key ) ); |
| 2606 | |
| 2607 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output, |
| 2608 | output_buffer_size, &output_length ) ); |
| 2609 | TEST_ASSERT( output_length <= |
| 2610 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 2611 | TEST_ASSERT( output_length <= |
| 2612 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
| 2613 | |
| 2614 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2615 | output, output_length ); |
| 2616 | exit: |
| 2617 | mbedtls_free( output ); |
| 2618 | psa_destroy_key( key ); |
| 2619 | PSA_DONE( ); |
| 2620 | } |
| 2621 | /* END_CASE */ |
| 2622 | |
| 2623 | /* BEGIN_CASE */ |
Paul Elliott | ed33ef1 | 2021-07-14 12:31:21 +0100 | [diff] [blame^] | 2624 | void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data ) |
| 2625 | { |
| 2626 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2627 | psa_algorithm_t alg = alg_arg; |
| 2628 | psa_key_type_t key_type = key_type_arg; |
| 2629 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2630 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 2631 | psa_status_t status; |
| 2632 | |
| 2633 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2634 | |
| 2635 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2636 | psa_set_key_algorithm( &attributes, alg ); |
| 2637 | psa_set_key_type( &attributes, key_type ); |
| 2638 | |
| 2639 | /* Usage of either of these two size macros would cause divide by zero |
| 2640 | * with incorrect key types previously. Input length should be irrelevant |
| 2641 | * here. */ |
| 2642 | TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ), |
| 2643 | 0 ); |
| 2644 | TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 ); |
| 2645 | |
| 2646 | |
| 2647 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2648 | &key ) ); |
| 2649 | |
| 2650 | /* Should fail due to invalid alg type (to support invalid key type). |
| 2651 | * Encrypt or decrypt will end up in the same place. */ |
| 2652 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 2653 | |
| 2654 | TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT ); |
| 2655 | |
| 2656 | exit: |
| 2657 | psa_cipher_abort( &operation ); |
| 2658 | psa_destroy_key( key ); |
| 2659 | PSA_DONE( ); |
| 2660 | } |
| 2661 | /* END_CASE */ |
| 2662 | |
| 2663 | /* BEGIN_CASE */ |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2664 | void cipher_encrypt_validation( int alg_arg, |
| 2665 | int key_type_arg, |
| 2666 | data_t *key_data, |
| 2667 | data_t *input ) |
| 2668 | { |
| 2669 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2670 | psa_key_type_t key_type = key_type_arg; |
| 2671 | psa_algorithm_t alg = alg_arg; |
| 2672 | size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg ); |
| 2673 | unsigned char *output1 = NULL; |
| 2674 | size_t output1_buffer_size = 0; |
| 2675 | size_t output1_length = 0; |
| 2676 | unsigned char *output2 = NULL; |
| 2677 | size_t output2_buffer_size = 0; |
| 2678 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2679 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2680 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2681 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2682 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2683 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2684 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2685 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2686 | psa_set_key_algorithm( &attributes, alg ); |
| 2687 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2688 | |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2689 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 2690 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 2691 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 2692 | ASSERT_ALLOC( output1, output1_buffer_size ); |
| 2693 | ASSERT_ALLOC( output2, output2_buffer_size ); |
| 2694 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2695 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2696 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2697 | |
gabor-mezei-arm | 7aa1efd | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 2698 | /* The one-shot cipher encryption uses generated iv so validating |
| 2699 | the output is not possible. Validating with multipart encryption. */ |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2700 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1, |
| 2701 | output1_buffer_size, &output1_length ) ); |
| 2702 | TEST_ASSERT( output1_length <= |
| 2703 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 2704 | TEST_ASSERT( output1_length <= |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2705 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2706 | |
| 2707 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 2708 | PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2709 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2710 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2711 | input->x, input->len, |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2712 | output2, output2_buffer_size, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2713 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2714 | TEST_ASSERT( function_output_length <= |
| 2715 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 2716 | TEST_ASSERT( function_output_length <= |
| 2717 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2718 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2719 | |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2720 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2721 | output2 + output2_length, |
| 2722 | output2_buffer_size - output2_length, |
| 2723 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2724 | TEST_ASSERT( function_output_length <= |
| 2725 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 2726 | TEST_ASSERT( function_output_length <= |
| 2727 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2728 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2729 | |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2730 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2731 | ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size, |
| 2732 | output2, output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2733 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2734 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2735 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2736 | mbedtls_free( output1 ); |
| 2737 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2738 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2739 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2740 | } |
| 2741 | /* END_CASE */ |
| 2742 | |
| 2743 | /* BEGIN_CASE */ |
| 2744 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2745 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2746 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2747 | int first_part_size_arg, |
| 2748 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2749 | data_t *expected_output, |
| 2750 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2751 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2752 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2753 | psa_key_type_t key_type = key_type_arg; |
| 2754 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2755 | psa_status_t status; |
| 2756 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2757 | size_t first_part_size = first_part_size_arg; |
| 2758 | size_t output1_length = output1_length_arg; |
| 2759 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2760 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2761 | size_t output_buffer_size = 0; |
| 2762 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2763 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2764 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2765 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2766 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2767 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2768 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2769 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2770 | psa_set_key_algorithm( &attributes, alg ); |
| 2771 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2772 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2773 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2774 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2775 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2776 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2777 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2778 | if( iv->len > 0 ) |
| 2779 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 2780 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2781 | } |
| 2782 | |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2783 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 2784 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2785 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2786 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2787 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2788 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 2789 | output, output_buffer_size, |
| 2790 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2791 | TEST_ASSERT( function_output_length == output1_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2792 | TEST_ASSERT( function_output_length <= |
| 2793 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 2794 | TEST_ASSERT( function_output_length <= |
| 2795 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2796 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2797 | |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2798 | if( first_part_size < input->len ) |
| 2799 | { |
| 2800 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2801 | input->x + first_part_size, |
| 2802 | input->len - first_part_size, |
| 2803 | ( output_buffer_size == 0 ? NULL : |
| 2804 | output + total_output_length ), |
| 2805 | output_buffer_size - total_output_length, |
| 2806 | &function_output_length ) ); |
| 2807 | TEST_ASSERT( function_output_length == output2_length ); |
| 2808 | TEST_ASSERT( function_output_length <= |
| 2809 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 2810 | alg, |
| 2811 | input->len - first_part_size ) ); |
| 2812 | TEST_ASSERT( function_output_length <= |
| 2813 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
| 2814 | total_output_length += function_output_length; |
| 2815 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2816 | |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2817 | status = psa_cipher_finish( &operation, |
| 2818 | ( output_buffer_size == 0 ? NULL : |
| 2819 | output + total_output_length ), |
| 2820 | output_buffer_size - total_output_length, |
| 2821 | &function_output_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2822 | TEST_ASSERT( function_output_length <= |
| 2823 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 2824 | TEST_ASSERT( function_output_length <= |
| 2825 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2826 | total_output_length += function_output_length; |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2827 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2828 | |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2829 | if( expected_status == PSA_SUCCESS ) |
| 2830 | { |
| 2831 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2832 | |
| 2833 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2834 | output, total_output_length ); |
| 2835 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2836 | |
| 2837 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2838 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2839 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2840 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2841 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2842 | } |
| 2843 | /* END_CASE */ |
| 2844 | |
| 2845 | /* BEGIN_CASE */ |
| 2846 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2847 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2848 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2849 | int first_part_size_arg, |
| 2850 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2851 | data_t *expected_output, |
| 2852 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2853 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2854 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2855 | psa_key_type_t key_type = key_type_arg; |
| 2856 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2857 | psa_status_t status; |
| 2858 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2859 | size_t first_part_size = first_part_size_arg; |
| 2860 | size_t output1_length = output1_length_arg; |
| 2861 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2862 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2863 | size_t output_buffer_size = 0; |
| 2864 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2865 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2866 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2867 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2868 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2869 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2870 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2871 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 2872 | psa_set_key_algorithm( &attributes, alg ); |
| 2873 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2874 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2875 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2876 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2877 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2878 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2879 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 2880 | if( iv->len > 0 ) |
| 2881 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 2882 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2883 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2884 | |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2885 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 2886 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2887 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2888 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2889 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2890 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2891 | input->x, first_part_size, |
| 2892 | output, output_buffer_size, |
| 2893 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2894 | TEST_ASSERT( function_output_length == output1_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2895 | TEST_ASSERT( function_output_length <= |
| 2896 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 2897 | TEST_ASSERT( function_output_length <= |
| 2898 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2899 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2900 | |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2901 | if( first_part_size < input->len ) |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 2902 | { |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2903 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2904 | input->x + first_part_size, |
| 2905 | input->len - first_part_size, |
| 2906 | ( output_buffer_size == 0 ? NULL : |
| 2907 | output + total_output_length ), |
| 2908 | output_buffer_size - total_output_length, |
| 2909 | &function_output_length ) ); |
| 2910 | TEST_ASSERT( function_output_length == output2_length ); |
| 2911 | TEST_ASSERT( function_output_length <= |
| 2912 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 2913 | alg, |
| 2914 | input->len - first_part_size ) ); |
| 2915 | TEST_ASSERT( function_output_length <= |
| 2916 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
| 2917 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2918 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2919 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2920 | status = psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 2921 | ( output_buffer_size == 0 ? NULL : |
| 2922 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2923 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2924 | &function_output_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2925 | TEST_ASSERT( function_output_length <= |
| 2926 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 2927 | TEST_ASSERT( function_output_length <= |
| 2928 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2929 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2930 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2931 | |
| 2932 | if( expected_status == PSA_SUCCESS ) |
| 2933 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2934 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2935 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2936 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2937 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2938 | } |
| 2939 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2940 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2941 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2942 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2943 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2944 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2945 | } |
| 2946 | /* END_CASE */ |
| 2947 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2948 | /* BEGIN_CASE */ |
gabor-mezei-arm | 43611b0 | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 2949 | void cipher_decrypt_fail( int alg_arg, |
| 2950 | int key_type_arg, |
| 2951 | data_t *key_data, |
| 2952 | data_t *iv, |
| 2953 | data_t *input_arg, |
| 2954 | int expected_status_arg ) |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2955 | { |
| 2956 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2957 | psa_status_t status; |
| 2958 | psa_key_type_t key_type = key_type_arg; |
| 2959 | psa_algorithm_t alg = alg_arg; |
| 2960 | psa_status_t expected_status = expected_status_arg; |
| 2961 | unsigned char *input = NULL; |
| 2962 | size_t input_buffer_size = 0; |
| 2963 | unsigned char *output = NULL; |
| 2964 | size_t output_buffer_size = 0; |
| 2965 | size_t output_length = 0; |
| 2966 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2967 | |
| 2968 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 2969 | { |
| 2970 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2971 | |
| 2972 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 2973 | psa_set_key_algorithm( &attributes, alg ); |
| 2974 | psa_set_key_type( &attributes, key_type ); |
| 2975 | |
| 2976 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2977 | &key ) ); |
| 2978 | } |
| 2979 | |
| 2980 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 2981 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 2982 | if ( input_buffer_size > 0 ) |
| 2983 | { |
| 2984 | ASSERT_ALLOC( input, input_buffer_size ); |
| 2985 | memcpy( input, iv->x, iv->len ); |
| 2986 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 2987 | } |
| 2988 | |
| 2989 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 2990 | ASSERT_ALLOC( output, output_buffer_size ); |
| 2991 | |
| 2992 | status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 2993 | output_buffer_size, &output_length ); |
| 2994 | TEST_EQUAL( status, expected_status ); |
| 2995 | |
| 2996 | exit: |
| 2997 | mbedtls_free( input ); |
| 2998 | mbedtls_free( output ); |
| 2999 | psa_destroy_key( key ); |
| 3000 | PSA_DONE( ); |
| 3001 | } |
| 3002 | /* END_CASE */ |
| 3003 | |
| 3004 | /* BEGIN_CASE */ |
| 3005 | void cipher_decrypt( int alg_arg, |
| 3006 | int key_type_arg, |
| 3007 | data_t *key_data, |
| 3008 | data_t *iv, |
| 3009 | data_t *input_arg, |
| 3010 | data_t *expected_output ) |
| 3011 | { |
| 3012 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3013 | psa_key_type_t key_type = key_type_arg; |
| 3014 | psa_algorithm_t alg = alg_arg; |
| 3015 | unsigned char *input = NULL; |
| 3016 | size_t input_buffer_size = 0; |
| 3017 | unsigned char *output = NULL; |
| 3018 | size_t output_buffer_size = 0; |
| 3019 | size_t output_length = 0; |
| 3020 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3021 | |
| 3022 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3023 | |
| 3024 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3025 | psa_set_key_algorithm( &attributes, alg ); |
| 3026 | psa_set_key_type( &attributes, key_type ); |
| 3027 | |
| 3028 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 3029 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 3030 | if ( input_buffer_size > 0 ) |
| 3031 | { |
| 3032 | ASSERT_ALLOC( input, input_buffer_size ); |
| 3033 | memcpy( input, iv->x, iv->len ); |
| 3034 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 3035 | } |
| 3036 | |
| 3037 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 3038 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3039 | |
| 3040 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3041 | &key ) ); |
| 3042 | |
| 3043 | PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 3044 | output_buffer_size, &output_length ) ); |
| 3045 | TEST_ASSERT( output_length <= |
| 3046 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) ); |
| 3047 | TEST_ASSERT( output_length <= |
| 3048 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) ); |
| 3049 | |
| 3050 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3051 | output, output_length ); |
| 3052 | exit: |
| 3053 | mbedtls_free( input ); |
| 3054 | mbedtls_free( output ); |
| 3055 | psa_destroy_key( key ); |
| 3056 | PSA_DONE( ); |
| 3057 | } |
| 3058 | /* END_CASE */ |
| 3059 | |
| 3060 | /* BEGIN_CASE */ |
| 3061 | void cipher_verify_output( int alg_arg, |
| 3062 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3063 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3064 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3065 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3066 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3067 | psa_key_type_t key_type = key_type_arg; |
| 3068 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3069 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3070 | size_t output1_size = 0; |
| 3071 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3072 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3073 | size_t output2_size = 0; |
| 3074 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3075 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3076 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3077 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3078 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3079 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3080 | psa_set_key_algorithm( &attributes, alg ); |
| 3081 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3082 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3083 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3084 | &key ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3085 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3086 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3087 | |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3088 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, |
| 3089 | output1, output1_size, |
| 3090 | &output1_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3091 | TEST_ASSERT( output1_length <= |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3092 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3093 | TEST_ASSERT( output1_length <= |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3094 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3095 | |
| 3096 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3097 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3098 | |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3099 | PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length, |
| 3100 | output2, output2_size, |
| 3101 | &output2_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3102 | TEST_ASSERT( output2_length <= |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3103 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3104 | TEST_ASSERT( output2_length <= |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3105 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3106 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3107 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3108 | |
| 3109 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3110 | mbedtls_free( output1 ); |
| 3111 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3112 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3113 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3114 | } |
| 3115 | /* END_CASE */ |
| 3116 | |
| 3117 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3118 | void cipher_verify_output_multipart( int alg_arg, |
| 3119 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3120 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3121 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3122 | int first_part_size_arg ) |
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 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3125 | psa_key_type_t key_type = key_type_arg; |
| 3126 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3127 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3128 | unsigned char iv[16] = {0}; |
| 3129 | size_t iv_size = 16; |
| 3130 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3131 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3132 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3133 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3134 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3135 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3136 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3137 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3138 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3139 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3140 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3141 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3142 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3143 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3144 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3145 | psa_set_key_algorithm( &attributes, alg ); |
| 3146 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3147 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3148 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3149 | &key ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3150 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3151 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 3152 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3153 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3154 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3155 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3156 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3157 | iv, iv_size, |
| 3158 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3159 | } |
| 3160 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3161 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3162 | TEST_ASSERT( output1_buffer_size <= |
| 3163 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3164 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3165 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3166 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3167 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3168 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 3169 | output1, output1_buffer_size, |
| 3170 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3171 | TEST_ASSERT( function_output_length <= |
| 3172 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3173 | TEST_ASSERT( function_output_length <= |
| 3174 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3175 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3176 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3177 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 3178 | input->x + first_part_size, |
| 3179 | input->len - first_part_size, |
| 3180 | output1, output1_buffer_size, |
| 3181 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3182 | TEST_ASSERT( function_output_length <= |
| 3183 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3184 | alg, |
| 3185 | input->len - first_part_size ) ); |
| 3186 | TEST_ASSERT( function_output_length <= |
| 3187 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3188 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3189 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3190 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 3191 | output1 + output1_length, |
| 3192 | output1_buffer_size - output1_length, |
| 3193 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3194 | TEST_ASSERT( function_output_length <= |
| 3195 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3196 | TEST_ASSERT( function_output_length <= |
| 3197 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3198 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3199 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3200 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3201 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3202 | output2_buffer_size = output1_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3203 | TEST_ASSERT( output2_buffer_size <= |
| 3204 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 3205 | TEST_ASSERT( output2_buffer_size <= |
| 3206 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3207 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3208 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3209 | if( iv_length > 0 ) |
| 3210 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3211 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3212 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3213 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3214 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3215 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 3216 | output2, output2_buffer_size, |
| 3217 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3218 | TEST_ASSERT( function_output_length <= |
| 3219 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3220 | TEST_ASSERT( function_output_length <= |
| 3221 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3222 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3223 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3224 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 3225 | output1 + first_part_size, |
| 3226 | output1_length - first_part_size, |
| 3227 | output2, output2_buffer_size, |
| 3228 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3229 | TEST_ASSERT( function_output_length <= |
| 3230 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3231 | alg, |
| 3232 | output1_length - first_part_size ) ); |
| 3233 | TEST_ASSERT( function_output_length <= |
| 3234 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3235 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3236 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3237 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3238 | output2 + output2_length, |
| 3239 | output2_buffer_size - output2_length, |
| 3240 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3241 | TEST_ASSERT( function_output_length <= |
| 3242 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3243 | TEST_ASSERT( function_output_length <= |
| 3244 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3245 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3246 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3247 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3248 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3249 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3250 | |
| 3251 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3252 | psa_cipher_abort( &operation1 ); |
| 3253 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3254 | mbedtls_free( output1 ); |
| 3255 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3256 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3257 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3258 | } |
| 3259 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 3260 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3261 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3262 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3263 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3264 | data_t *nonce, |
| 3265 | data_t *additional_data, |
| 3266 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3267 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3268 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3269 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3270 | psa_key_type_t key_type = key_type_arg; |
| 3271 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3272 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3273 | unsigned char *output_data = NULL; |
| 3274 | size_t output_size = 0; |
| 3275 | size_t output_length = 0; |
| 3276 | unsigned char *output_data2 = NULL; |
| 3277 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 3278 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3279 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3280 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3281 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3282 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3283 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3284 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3285 | psa_set_key_algorithm( &attributes, alg ); |
| 3286 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3287 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3288 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3289 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3290 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3291 | key_bits = psa_get_key_bits( &attributes ); |
| 3292 | |
| 3293 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3294 | alg ); |
| 3295 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3296 | * should be exact. */ |
| 3297 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 3298 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 3299 | { |
| 3300 | TEST_EQUAL( output_size, |
| 3301 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3302 | TEST_ASSERT( output_size <= |
| 3303 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3304 | } |
| 3305 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3306 | |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 3307 | status = psa_aead_encrypt( key, alg, |
| 3308 | nonce->x, nonce->len, |
| 3309 | additional_data->x, |
| 3310 | additional_data->len, |
| 3311 | input_data->x, input_data->len, |
| 3312 | output_data, output_size, |
| 3313 | &output_length ); |
| 3314 | |
| 3315 | /* If the operation is not supported, just skip and not fail in case the |
| 3316 | * encryption involves a common limitation of cryptography hardwares and |
| 3317 | * an alternative implementation. */ |
| 3318 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 3319 | { |
| 3320 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3321 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 3322 | } |
| 3323 | |
| 3324 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3325 | |
| 3326 | if( PSA_SUCCESS == expected_result ) |
| 3327 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3328 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3329 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3330 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3331 | * should be exact. */ |
| 3332 | TEST_EQUAL( input_data->len, |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3333 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) ); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3334 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3335 | TEST_ASSERT( input_data->len <= |
| 3336 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) ); |
| 3337 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3338 | TEST_EQUAL( psa_aead_decrypt( key, alg, |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3339 | nonce->x, nonce->len, |
| 3340 | additional_data->x, |
| 3341 | additional_data->len, |
| 3342 | output_data, output_length, |
| 3343 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3344 | &output_length2 ), |
| 3345 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3346 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3347 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 3348 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3349 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3350 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3351 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3352 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3353 | mbedtls_free( output_data ); |
| 3354 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3355 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3356 | } |
| 3357 | /* END_CASE */ |
| 3358 | |
| 3359 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3360 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 3361 | int alg_arg, |
| 3362 | data_t *nonce, |
| 3363 | data_t *additional_data, |
| 3364 | data_t *input_data, |
| 3365 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3366 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3367 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3368 | psa_key_type_t key_type = key_type_arg; |
| 3369 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3370 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3371 | unsigned char *output_data = NULL; |
| 3372 | size_t output_size = 0; |
| 3373 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3374 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3375 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3376 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3377 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3378 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3379 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3380 | psa_set_key_algorithm( &attributes, alg ); |
| 3381 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3382 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3383 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3384 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3385 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3386 | key_bits = psa_get_key_bits( &attributes ); |
| 3387 | |
| 3388 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3389 | alg ); |
| 3390 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3391 | * should be exact. */ |
| 3392 | TEST_EQUAL( output_size, |
| 3393 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3394 | TEST_ASSERT( output_size <= |
| 3395 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3396 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3397 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3398 | status = psa_aead_encrypt( key, alg, |
| 3399 | nonce->x, nonce->len, |
| 3400 | additional_data->x, additional_data->len, |
| 3401 | input_data->x, input_data->len, |
| 3402 | output_data, output_size, |
| 3403 | &output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3404 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3405 | /* If the operation is not supported, just skip and not fail in case the |
| 3406 | * encryption involves a common limitation of cryptography hardwares and |
| 3407 | * an alternative implementation. */ |
| 3408 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3409 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3410 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3411 | 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] | 3412 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3413 | |
| 3414 | PSA_ASSERT( status ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3415 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 3416 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3417 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3418 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3419 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3420 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3421 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3422 | } |
| 3423 | /* END_CASE */ |
| 3424 | |
| 3425 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3426 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 3427 | int alg_arg, |
| 3428 | data_t *nonce, |
| 3429 | data_t *additional_data, |
| 3430 | data_t *input_data, |
| 3431 | data_t *expected_data, |
| 3432 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3433 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3434 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3435 | psa_key_type_t key_type = key_type_arg; |
| 3436 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3437 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3438 | unsigned char *output_data = NULL; |
| 3439 | size_t output_size = 0; |
| 3440 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3441 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3442 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3443 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3444 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3445 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3446 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3447 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3448 | psa_set_key_algorithm( &attributes, alg ); |
| 3449 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3450 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3451 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3452 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3453 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3454 | key_bits = psa_get_key_bits( &attributes ); |
| 3455 | |
| 3456 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3457 | alg ); |
| 3458 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 3459 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 3460 | { |
| 3461 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3462 | * should be exact. */ |
| 3463 | TEST_EQUAL( output_size, |
| 3464 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3465 | TEST_ASSERT( output_size <= |
| 3466 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3467 | } |
| 3468 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3469 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3470 | status = psa_aead_decrypt( key, alg, |
| 3471 | nonce->x, nonce->len, |
| 3472 | additional_data->x, |
| 3473 | additional_data->len, |
| 3474 | input_data->x, input_data->len, |
| 3475 | output_data, output_size, |
| 3476 | &output_length ); |
| 3477 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3478 | /* If the operation is not supported, just skip and not fail in case the |
| 3479 | * decryption involves a common limitation of cryptography hardwares and |
| 3480 | * an alternative implementation. */ |
| 3481 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3482 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3483 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3484 | 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] | 3485 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3486 | |
| 3487 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3488 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3489 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3490 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 3491 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3492 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3493 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3494 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3495 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3496 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3497 | } |
| 3498 | /* END_CASE */ |
| 3499 | |
| 3500 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3501 | void signature_size( int type_arg, |
| 3502 | int bits, |
| 3503 | int alg_arg, |
| 3504 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3505 | { |
| 3506 | psa_key_type_t type = type_arg; |
| 3507 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3508 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 3509 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3510 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 3511 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 3512 | TEST_EQUAL( actual_size, |
| 3513 | PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) ); |
| 3514 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3515 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3516 | exit: |
| 3517 | ; |
| 3518 | } |
| 3519 | /* END_CASE */ |
| 3520 | |
| 3521 | /* BEGIN_CASE */ |
gabor-mezei-arm | dc76df4 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 3522 | void sign_hash_deterministic( int key_type_arg, data_t *key_data, |
| 3523 | int alg_arg, data_t *input_data, |
| 3524 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3525 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3526 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3527 | psa_key_type_t key_type = key_type_arg; |
| 3528 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3529 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3530 | unsigned char *signature = NULL; |
| 3531 | size_t signature_size; |
| 3532 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3533 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3534 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3535 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3536 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3537 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3538 | psa_set_key_algorithm( &attributes, alg ); |
| 3539 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 3540 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3541 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3542 | &key ) ); |
| 3543 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3544 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3545 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3546 | /* Allocate a buffer which has the size advertized by the |
| 3547 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3548 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3549 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3550 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3551 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3552 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3553 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3554 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3555 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3556 | input_data->x, input_data->len, |
| 3557 | signature, signature_size, |
| 3558 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3559 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3560 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 3561 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3562 | |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 3563 | #if defined(MBEDTLS_TEST_DEPRECATED) |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3564 | memset( signature, 0, signature_size ); |
| 3565 | signature_length = INVALID_EXPORT_LENGTH; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3566 | PSA_ASSERT( psa_asymmetric_sign( key, alg, |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 3567 | input_data->x, input_data->len, |
| 3568 | signature, signature_size, |
| 3569 | &signature_length ) ); |
| 3570 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 3571 | signature, signature_length ); |
| 3572 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3573 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3574 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3575 | /* |
| 3576 | * Key attributes may have been returned by psa_get_key_attributes() |
| 3577 | * thus reset them as required. |
| 3578 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3579 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3580 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3581 | psa_destroy_key( key ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 3582 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3583 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3584 | } |
| 3585 | /* END_CASE */ |
| 3586 | |
| 3587 | /* BEGIN_CASE */ |
gabor-mezei-arm | dc76df4 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 3588 | void sign_hash_fail( int key_type_arg, data_t *key_data, |
| 3589 | int alg_arg, data_t *input_data, |
| 3590 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3591 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3592 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3593 | psa_key_type_t key_type = key_type_arg; |
| 3594 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3595 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3596 | psa_status_t actual_status; |
| 3597 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 3598 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 3599 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3600 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3601 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3602 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3603 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3604 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3605 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3606 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3607 | psa_set_key_algorithm( &attributes, alg ); |
| 3608 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 3609 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3610 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3611 | &key ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3612 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3613 | actual_status = psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3614 | input_data->x, input_data->len, |
| 3615 | signature, signature_size, |
| 3616 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3617 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3618 | /* The value of *signature_length is unspecified on error, but |
| 3619 | * whatever it is, it should be less than signature_size, so that |
| 3620 | * if the caller tries to read *signature_length bytes without |
| 3621 | * checking the error code then they don't overflow a buffer. */ |
| 3622 | TEST_ASSERT( signature_length <= signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3623 | |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3624 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 3625 | signature_length = INVALID_EXPORT_LENGTH; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3626 | TEST_EQUAL( psa_asymmetric_sign( key, alg, |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3627 | input_data->x, input_data->len, |
| 3628 | signature, signature_size, |
| 3629 | &signature_length ), |
| 3630 | expected_status ); |
| 3631 | TEST_ASSERT( signature_length <= signature_size ); |
| 3632 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3633 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3634 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3635 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3636 | psa_destroy_key( key ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3637 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3638 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3639 | } |
| 3640 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3641 | |
| 3642 | /* BEGIN_CASE */ |
gabor-mezei-arm | dc76df4 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 3643 | void sign_verify_hash( int key_type_arg, data_t *key_data, |
| 3644 | int alg_arg, data_t *input_data ) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3645 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3646 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3647 | psa_key_type_t key_type = key_type_arg; |
| 3648 | psa_algorithm_t alg = alg_arg; |
| 3649 | size_t key_bits; |
| 3650 | unsigned char *signature = NULL; |
| 3651 | size_t signature_size; |
| 3652 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3653 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3654 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3655 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3656 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3657 | 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] | 3658 | psa_set_key_algorithm( &attributes, alg ); |
| 3659 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3660 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3661 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3662 | &key ) ); |
| 3663 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3664 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3665 | |
| 3666 | /* Allocate a buffer which has the size advertized by the |
| 3667 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3668 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3669 | key_bits, alg ); |
| 3670 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3671 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3672 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3673 | |
| 3674 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3675 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3676 | input_data->x, input_data->len, |
| 3677 | signature, signature_size, |
| 3678 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3679 | /* Check that the signature length looks sensible. */ |
| 3680 | TEST_ASSERT( signature_length <= signature_size ); |
| 3681 | TEST_ASSERT( signature_length > 0 ); |
| 3682 | |
| 3683 | /* Use the library to verify that the signature is correct. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3684 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3685 | input_data->x, input_data->len, |
| 3686 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3687 | |
| 3688 | if( input_data->len != 0 ) |
| 3689 | { |
| 3690 | /* Flip a bit in the input and verify that the signature is now |
| 3691 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 3692 | * because ECDSA may ignore the last few bits of the input. */ |
| 3693 | input_data->x[0] ^= 1; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3694 | TEST_EQUAL( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3695 | input_data->x, input_data->len, |
| 3696 | signature, signature_length ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3697 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3698 | } |
| 3699 | |
| 3700 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3701 | /* |
| 3702 | * Key attributes may have been returned by psa_get_key_attributes() |
| 3703 | * thus reset them as required. |
| 3704 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3705 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3706 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3707 | psa_destroy_key( key ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3708 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3709 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3710 | } |
| 3711 | /* END_CASE */ |
| 3712 | |
| 3713 | /* BEGIN_CASE */ |
gabor-mezei-arm | dc76df4 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 3714 | void verify_hash( int key_type_arg, data_t *key_data, |
| 3715 | int alg_arg, data_t *hash_data, |
| 3716 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3717 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3718 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3719 | psa_key_type_t key_type = key_type_arg; |
| 3720 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3721 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3722 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3723 | TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3724 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3725 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3726 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3727 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3728 | psa_set_key_algorithm( &attributes, alg ); |
| 3729 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3730 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3731 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3732 | &key ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3733 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3734 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3735 | hash_data->x, hash_data->len, |
| 3736 | signature_data->x, signature_data->len ) ); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 3737 | |
| 3738 | #if defined(MBEDTLS_TEST_DEPRECATED) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3739 | PSA_ASSERT( psa_asymmetric_verify( key, alg, |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 3740 | hash_data->x, hash_data->len, |
| 3741 | signature_data->x, |
| 3742 | signature_data->len ) ); |
| 3743 | |
| 3744 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3745 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3746 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3747 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3748 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3749 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3750 | } |
| 3751 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3752 | |
| 3753 | /* BEGIN_CASE */ |
gabor-mezei-arm | dc76df4 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 3754 | void verify_hash_fail( int key_type_arg, data_t *key_data, |
| 3755 | int alg_arg, data_t *hash_data, |
| 3756 | data_t *signature_data, |
| 3757 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3758 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3759 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3760 | psa_key_type_t key_type = key_type_arg; |
| 3761 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3762 | psa_status_t actual_status; |
| 3763 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3764 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3765 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3766 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3767 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3768 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3769 | psa_set_key_algorithm( &attributes, alg ); |
| 3770 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3771 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3772 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3773 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3774 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3775 | actual_status = psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3776 | hash_data->x, hash_data->len, |
| 3777 | signature_data->x, signature_data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3778 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3779 | |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3780 | #if defined(MBEDTLS_TEST_DEPRECATED) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3781 | TEST_EQUAL( psa_asymmetric_verify( key, alg, |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3782 | hash_data->x, hash_data->len, |
| 3783 | signature_data->x, signature_data->len ), |
| 3784 | expected_status ); |
| 3785 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3786 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3787 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3788 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3789 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3790 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3791 | } |
| 3792 | /* END_CASE */ |
| 3793 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3794 | /* BEGIN_CASE */ |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3795 | void sign_message_deterministic( int key_type_arg, |
| 3796 | data_t *key_data, |
| 3797 | int alg_arg, |
| 3798 | data_t *input_data, |
| 3799 | data_t *output_data ) |
| 3800 | { |
| 3801 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3802 | psa_key_type_t key_type = key_type_arg; |
| 3803 | psa_algorithm_t alg = alg_arg; |
| 3804 | size_t key_bits; |
| 3805 | unsigned char *signature = NULL; |
| 3806 | size_t signature_size; |
| 3807 | size_t signature_length = 0xdeadbeef; |
| 3808 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3809 | |
| 3810 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3811 | |
| 3812 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 3813 | psa_set_key_algorithm( &attributes, alg ); |
| 3814 | psa_set_key_type( &attributes, key_type ); |
| 3815 | |
| 3816 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3817 | &key ) ); |
| 3818 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3819 | key_bits = psa_get_key_bits( &attributes ); |
| 3820 | |
| 3821 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 3822 | TEST_ASSERT( signature_size != 0 ); |
| 3823 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
| 3824 | ASSERT_ALLOC( signature, signature_size ); |
| 3825 | |
| 3826 | PSA_ASSERT( psa_sign_message( key, alg, |
| 3827 | input_data->x, input_data->len, |
| 3828 | signature, signature_size, |
| 3829 | &signature_length ) ); |
| 3830 | |
| 3831 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 3832 | signature, signature_length ); |
| 3833 | |
| 3834 | exit: |
| 3835 | psa_reset_key_attributes( &attributes ); |
| 3836 | |
| 3837 | psa_destroy_key( key ); |
| 3838 | mbedtls_free( signature ); |
| 3839 | PSA_DONE( ); |
| 3840 | |
| 3841 | } |
| 3842 | /* END_CASE */ |
| 3843 | |
| 3844 | /* BEGIN_CASE */ |
| 3845 | void sign_message_fail( int key_type_arg, |
| 3846 | data_t *key_data, |
| 3847 | int alg_arg, |
| 3848 | data_t *input_data, |
| 3849 | int signature_size_arg, |
| 3850 | int expected_status_arg ) |
| 3851 | { |
| 3852 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3853 | psa_key_type_t key_type = key_type_arg; |
| 3854 | psa_algorithm_t alg = alg_arg; |
| 3855 | size_t signature_size = signature_size_arg; |
| 3856 | psa_status_t actual_status; |
| 3857 | psa_status_t expected_status = expected_status_arg; |
| 3858 | unsigned char *signature = NULL; |
| 3859 | size_t signature_length = 0xdeadbeef; |
| 3860 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3861 | |
| 3862 | ASSERT_ALLOC( signature, signature_size ); |
| 3863 | |
| 3864 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3865 | |
| 3866 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 3867 | psa_set_key_algorithm( &attributes, alg ); |
| 3868 | psa_set_key_type( &attributes, key_type ); |
| 3869 | |
| 3870 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3871 | &key ) ); |
| 3872 | |
| 3873 | actual_status = psa_sign_message( key, alg, |
| 3874 | input_data->x, input_data->len, |
| 3875 | signature, signature_size, |
| 3876 | &signature_length ); |
| 3877 | TEST_EQUAL( actual_status, expected_status ); |
| 3878 | /* The value of *signature_length is unspecified on error, but |
| 3879 | * whatever it is, it should be less than signature_size, so that |
| 3880 | * if the caller tries to read *signature_length bytes without |
| 3881 | * checking the error code then they don't overflow a buffer. */ |
| 3882 | TEST_ASSERT( signature_length <= signature_size ); |
| 3883 | |
| 3884 | exit: |
| 3885 | psa_reset_key_attributes( &attributes ); |
| 3886 | psa_destroy_key( key ); |
| 3887 | mbedtls_free( signature ); |
| 3888 | PSA_DONE( ); |
| 3889 | } |
| 3890 | /* END_CASE */ |
| 3891 | |
| 3892 | /* BEGIN_CASE */ |
| 3893 | void sign_verify_message( int key_type_arg, |
| 3894 | data_t *key_data, |
| 3895 | int alg_arg, |
| 3896 | data_t *input_data ) |
| 3897 | { |
| 3898 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3899 | psa_key_type_t key_type = key_type_arg; |
| 3900 | psa_algorithm_t alg = alg_arg; |
| 3901 | size_t key_bits; |
| 3902 | unsigned char *signature = NULL; |
| 3903 | size_t signature_size; |
| 3904 | size_t signature_length = 0xdeadbeef; |
| 3905 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3906 | |
| 3907 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3908 | |
| 3909 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 3910 | PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 3911 | psa_set_key_algorithm( &attributes, alg ); |
| 3912 | psa_set_key_type( &attributes, key_type ); |
| 3913 | |
| 3914 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3915 | &key ) ); |
| 3916 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3917 | key_bits = psa_get_key_bits( &attributes ); |
| 3918 | |
| 3919 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 3920 | TEST_ASSERT( signature_size != 0 ); |
| 3921 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
| 3922 | ASSERT_ALLOC( signature, signature_size ); |
| 3923 | |
| 3924 | PSA_ASSERT( psa_sign_message( key, alg, |
| 3925 | input_data->x, input_data->len, |
| 3926 | signature, signature_size, |
| 3927 | &signature_length ) ); |
| 3928 | TEST_ASSERT( signature_length <= signature_size ); |
| 3929 | TEST_ASSERT( signature_length > 0 ); |
| 3930 | |
| 3931 | PSA_ASSERT( psa_verify_message( key, alg, |
| 3932 | input_data->x, input_data->len, |
| 3933 | signature, signature_length ) ); |
| 3934 | |
| 3935 | if( input_data->len != 0 ) |
| 3936 | { |
| 3937 | /* Flip a bit in the input and verify that the signature is now |
| 3938 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 3939 | * because ECDSA may ignore the last few bits of the input. */ |
| 3940 | input_data->x[0] ^= 1; |
| 3941 | TEST_EQUAL( psa_verify_message( key, alg, |
| 3942 | input_data->x, input_data->len, |
| 3943 | signature, signature_length ), |
| 3944 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3945 | } |
| 3946 | |
| 3947 | exit: |
| 3948 | psa_reset_key_attributes( &attributes ); |
| 3949 | |
| 3950 | psa_destroy_key( key ); |
| 3951 | mbedtls_free( signature ); |
| 3952 | PSA_DONE( ); |
| 3953 | } |
| 3954 | /* END_CASE */ |
| 3955 | |
| 3956 | /* BEGIN_CASE */ |
| 3957 | void verify_message( int key_type_arg, |
| 3958 | data_t *key_data, |
| 3959 | int alg_arg, |
| 3960 | data_t *input_data, |
| 3961 | data_t *signature_data ) |
| 3962 | { |
| 3963 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3964 | psa_key_type_t key_type = key_type_arg; |
| 3965 | psa_algorithm_t alg = alg_arg; |
| 3966 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3967 | |
| 3968 | TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE ); |
| 3969 | |
| 3970 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3971 | |
| 3972 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 3973 | psa_set_key_algorithm( &attributes, alg ); |
| 3974 | psa_set_key_type( &attributes, key_type ); |
| 3975 | |
| 3976 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3977 | &key ) ); |
| 3978 | |
| 3979 | PSA_ASSERT( psa_verify_message( key, alg, |
| 3980 | input_data->x, input_data->len, |
| 3981 | signature_data->x, signature_data->len ) ); |
| 3982 | |
| 3983 | exit: |
| 3984 | psa_reset_key_attributes( &attributes ); |
| 3985 | psa_destroy_key( key ); |
| 3986 | PSA_DONE( ); |
| 3987 | } |
| 3988 | /* END_CASE */ |
| 3989 | |
| 3990 | /* BEGIN_CASE */ |
| 3991 | void verify_message_fail( int key_type_arg, |
| 3992 | data_t *key_data, |
| 3993 | int alg_arg, |
| 3994 | data_t *hash_data, |
| 3995 | data_t *signature_data, |
| 3996 | int expected_status_arg ) |
| 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_status_t actual_status; |
| 4002 | psa_status_t expected_status = expected_status_arg; |
| 4003 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4004 | |
| 4005 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4006 | |
| 4007 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 4008 | psa_set_key_algorithm( &attributes, alg ); |
| 4009 | psa_set_key_type( &attributes, key_type ); |
| 4010 | |
| 4011 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4012 | &key ) ); |
| 4013 | |
| 4014 | actual_status = psa_verify_message( key, alg, |
| 4015 | hash_data->x, hash_data->len, |
| 4016 | signature_data->x, |
| 4017 | signature_data->len ); |
| 4018 | TEST_EQUAL( actual_status, expected_status ); |
| 4019 | |
| 4020 | exit: |
| 4021 | psa_reset_key_attributes( &attributes ); |
| 4022 | psa_destroy_key( key ); |
| 4023 | PSA_DONE( ); |
| 4024 | } |
| 4025 | /* END_CASE */ |
| 4026 | |
| 4027 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4028 | void asymmetric_encrypt( int key_type_arg, |
| 4029 | data_t *key_data, |
| 4030 | int alg_arg, |
| 4031 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4032 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4033 | int expected_output_length_arg, |
| 4034 | int expected_status_arg ) |
| 4035 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4036 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4037 | psa_key_type_t key_type = key_type_arg; |
| 4038 | psa_algorithm_t alg = alg_arg; |
| 4039 | size_t expected_output_length = expected_output_length_arg; |
| 4040 | size_t key_bits; |
| 4041 | unsigned char *output = NULL; |
| 4042 | size_t output_size; |
| 4043 | size_t output_length = ~0; |
| 4044 | psa_status_t actual_status; |
| 4045 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4046 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4047 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4048 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4049 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4050 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4051 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4052 | psa_set_key_algorithm( &attributes, alg ); |
| 4053 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4054 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4055 | &key ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4056 | |
| 4057 | /* Determine the maximum output length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4058 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4059 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4060 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4061 | 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] | 4062 | TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4063 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4064 | |
| 4065 | /* Encrypt the input */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4066 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4067 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4068 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4069 | output, output_size, |
| 4070 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4071 | TEST_EQUAL( actual_status, expected_status ); |
| 4072 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4073 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4074 | /* If the label is empty, the test framework puts a non-null pointer |
| 4075 | * in label->x. Test that a null pointer works as well. */ |
| 4076 | if( label->len == 0 ) |
| 4077 | { |
| 4078 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4079 | if( output_size != 0 ) |
| 4080 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4081 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4082 | input_data->x, input_data->len, |
| 4083 | NULL, label->len, |
| 4084 | output, output_size, |
| 4085 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4086 | TEST_EQUAL( actual_status, expected_status ); |
| 4087 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4088 | } |
| 4089 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4090 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4091 | /* |
| 4092 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4093 | * thus reset them as required. |
| 4094 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4095 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4096 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4097 | psa_destroy_key( key ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4098 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4099 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4100 | } |
| 4101 | /* END_CASE */ |
| 4102 | |
| 4103 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4104 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 4105 | data_t *key_data, |
| 4106 | int alg_arg, |
| 4107 | data_t *input_data, |
| 4108 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4109 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4110 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4111 | psa_key_type_t key_type = key_type_arg; |
| 4112 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4113 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4114 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4115 | size_t output_size; |
| 4116 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4117 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4118 | size_t output2_size; |
| 4119 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4120 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4121 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4122 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4123 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4124 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4125 | psa_set_key_algorithm( &attributes, alg ); |
| 4126 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4127 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4128 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4129 | &key ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4130 | |
| 4131 | /* Determine the maximum ciphertext length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4132 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4133 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4134 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4135 | 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] | 4136 | TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4137 | ASSERT_ALLOC( output, output_size ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4138 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4139 | output2_size = input_data->len; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4140 | TEST_ASSERT( output2_size <= |
| 4141 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) ); |
| 4142 | TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4143 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4144 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 4145 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 4146 | * the original plaintext because of the non-optional random |
| 4147 | * part of encryption process which prevents using fixed vectors. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4148 | PSA_ASSERT( psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4149 | input_data->x, input_data->len, |
| 4150 | label->x, label->len, |
| 4151 | output, output_size, |
| 4152 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4153 | /* We don't know what ciphertext length to expect, but check that |
| 4154 | * it looks sensible. */ |
| 4155 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4156 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4157 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4158 | output, output_length, |
| 4159 | label->x, label->len, |
| 4160 | output2, output2_size, |
| 4161 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4162 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 4163 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4164 | |
| 4165 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4166 | /* |
| 4167 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4168 | * thus reset them as required. |
| 4169 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4170 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4171 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4172 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4173 | mbedtls_free( output ); |
| 4174 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4175 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4176 | } |
| 4177 | /* END_CASE */ |
| 4178 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4179 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4180 | void asymmetric_decrypt( int key_type_arg, |
| 4181 | data_t *key_data, |
| 4182 | int alg_arg, |
| 4183 | data_t *input_data, |
| 4184 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 4185 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4186 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4187 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4188 | psa_key_type_t key_type = key_type_arg; |
| 4189 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4190 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4191 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 4192 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4193 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4194 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4195 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4196 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4197 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4198 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4199 | psa_set_key_algorithm( &attributes, alg ); |
| 4200 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4201 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4202 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4203 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4204 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4205 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4206 | key_bits = psa_get_key_bits( &attributes ); |
| 4207 | |
| 4208 | /* Determine the maximum ciphertext length */ |
| 4209 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 4210 | TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
| 4211 | ASSERT_ALLOC( output, output_size ); |
| 4212 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4213 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4214 | input_data->x, input_data->len, |
| 4215 | label->x, label->len, |
| 4216 | output, |
| 4217 | output_size, |
| 4218 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4219 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4220 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4221 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4222 | /* If the label is empty, the test framework puts a non-null pointer |
| 4223 | * in label->x. Test that a null pointer works as well. */ |
| 4224 | if( label->len == 0 ) |
| 4225 | { |
| 4226 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4227 | if( output_size != 0 ) |
| 4228 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4229 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4230 | input_data->x, input_data->len, |
| 4231 | NULL, label->len, |
| 4232 | output, |
| 4233 | output_size, |
| 4234 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4235 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4236 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4237 | } |
| 4238 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4239 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4240 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4241 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4242 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4243 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4244 | } |
| 4245 | /* END_CASE */ |
| 4246 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4247 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4248 | void asymmetric_decrypt_fail( int key_type_arg, |
| 4249 | data_t *key_data, |
| 4250 | int alg_arg, |
| 4251 | data_t *input_data, |
| 4252 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4253 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4254 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4255 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4256 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4257 | psa_key_type_t key_type = key_type_arg; |
| 4258 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4259 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4260 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4261 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4262 | psa_status_t actual_status; |
| 4263 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4264 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4265 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4266 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4267 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4268 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4269 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4270 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4271 | psa_set_key_algorithm( &attributes, alg ); |
| 4272 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4273 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4274 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4275 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4276 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4277 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4278 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4279 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4280 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4281 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4282 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4283 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4284 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4285 | /* If the label is empty, the test framework puts a non-null pointer |
| 4286 | * in label->x. Test that a null pointer works as well. */ |
| 4287 | if( label->len == 0 ) |
| 4288 | { |
| 4289 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4290 | if( output_size != 0 ) |
| 4291 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4292 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4293 | input_data->x, input_data->len, |
| 4294 | NULL, label->len, |
| 4295 | output, output_size, |
| 4296 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4297 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4298 | TEST_ASSERT( output_length <= output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4299 | } |
| 4300 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4301 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4302 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4303 | psa_destroy_key( key ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4304 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4305 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4306 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4307 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4308 | |
| 4309 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4310 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4311 | { |
| 4312 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 4313 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 4314 | * though it's OK by the C standard. We could test for this, but we'd need |
| 4315 | * to supress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4316 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4317 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 4318 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4319 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4320 | |
| 4321 | memset( &zero, 0, sizeof( zero ) ); |
| 4322 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4323 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4324 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4325 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4326 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4327 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4328 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4329 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4330 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4331 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4332 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 4333 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 4334 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4335 | } |
| 4336 | /* END_CASE */ |
| 4337 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4338 | /* BEGIN_CASE */ |
| 4339 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4340 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4341 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4342 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4343 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4344 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4345 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4346 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4347 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4348 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4349 | |
| 4350 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4351 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4352 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4353 | } |
| 4354 | /* END_CASE */ |
| 4355 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4356 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4357 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 4358 | int expected_status_arg ) |
| 4359 | { |
| 4360 | psa_algorithm_t alg = alg_arg; |
| 4361 | size_t capacity = capacity_arg; |
| 4362 | psa_status_t expected_status = expected_status_arg; |
| 4363 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4364 | |
| 4365 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4366 | |
| 4367 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4368 | |
| 4369 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 4370 | expected_status ); |
| 4371 | |
| 4372 | exit: |
| 4373 | psa_key_derivation_abort( &operation ); |
| 4374 | PSA_DONE( ); |
| 4375 | } |
| 4376 | /* END_CASE */ |
| 4377 | |
| 4378 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4379 | void derive_input( int alg_arg, |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4380 | int step_arg1, int key_type_arg1, data_t *input1, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4381 | int expected_status_arg1, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 4382 | int step_arg2, int key_type_arg2, data_t *input2, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4383 | int expected_status_arg2, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 4384 | int step_arg3, int key_type_arg3, data_t *input3, |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4385 | int expected_status_arg3, |
| 4386 | int output_key_type_arg, int expected_output_status_arg ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4387 | { |
| 4388 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4389 | psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3}; |
| 4390 | 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] | 4391 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 4392 | expected_status_arg2, |
| 4393 | expected_status_arg3}; |
| 4394 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4395 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 4396 | MBEDTLS_SVC_KEY_ID_INIT, |
| 4397 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4398 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4399 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4400 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4401 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4402 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4403 | psa_status_t expected_output_status = expected_output_status_arg; |
| 4404 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4405 | |
| 4406 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4407 | |
| 4408 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4409 | psa_set_key_algorithm( &attributes, alg ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4410 | |
| 4411 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4412 | |
| 4413 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 4414 | { |
Gilles Peskine | f627931 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 4415 | mbedtls_test_set_step( i ); |
| 4416 | if( steps[i] == 0 ) |
| 4417 | { |
| 4418 | /* Skip this step */ |
| 4419 | } |
| 4420 | else if( key_types[i] != PSA_KEY_TYPE_NONE ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4421 | { |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4422 | psa_set_key_type( &attributes, key_types[i] ); |
| 4423 | PSA_ASSERT( psa_import_key( &attributes, |
| 4424 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4425 | &keys[i] ) ); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 4426 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) && |
| 4427 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 4428 | { |
| 4429 | // When taking a private key as secret input, use key agreement |
| 4430 | // to add the shared secret to the derivation |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 4431 | TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self( |
| 4432 | &operation, keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 4433 | expected_statuses[i] ); |
| 4434 | } |
| 4435 | else |
| 4436 | { |
| 4437 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4438 | keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 4439 | expected_statuses[i] ); |
| 4440 | } |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4441 | } |
| 4442 | else |
| 4443 | { |
| 4444 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 4445 | &operation, steps[i], |
| 4446 | inputs[i]->x, inputs[i]->len ), |
| 4447 | expected_statuses[i] ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4448 | } |
| 4449 | } |
| 4450 | |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4451 | if( output_key_type != PSA_KEY_TYPE_NONE ) |
| 4452 | { |
| 4453 | psa_reset_key_attributes( &attributes ); |
| 4454 | psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA ); |
| 4455 | psa_set_key_bits( &attributes, 8 ); |
| 4456 | actual_output_status = |
| 4457 | psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4458 | &output_key ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4459 | } |
| 4460 | else |
| 4461 | { |
| 4462 | uint8_t buffer[1]; |
| 4463 | actual_output_status = |
| 4464 | psa_key_derivation_output_bytes( &operation, |
| 4465 | buffer, sizeof( buffer ) ); |
| 4466 | } |
| 4467 | TEST_EQUAL( actual_output_status, expected_output_status ); |
| 4468 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4469 | exit: |
| 4470 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4471 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 4472 | psa_destroy_key( keys[i] ); |
| 4473 | psa_destroy_key( output_key ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4474 | PSA_DONE( ); |
| 4475 | } |
| 4476 | /* END_CASE */ |
| 4477 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4478 | /* BEGIN_CASE */ |
Gilles Peskine | 0faba4e | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 4479 | void derive_over_capacity( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4480 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4481 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4482 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 4483 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4484 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4485 | unsigned char input1[] = "Input 1"; |
| 4486 | size_t input1_length = sizeof( input1 ); |
| 4487 | unsigned char input2[] = "Input 2"; |
| 4488 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4489 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 4490 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 4491 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4492 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4493 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4494 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4495 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4496 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4497 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4498 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4499 | psa_set_key_algorithm( &attributes, alg ); |
| 4500 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4501 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 4502 | PSA_ASSERT( psa_import_key( &attributes, |
| 4503 | key_data, sizeof( key_data ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4504 | &key ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4505 | |
| 4506 | /* valid key derivation */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 4507 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 4508 | input1, input1_length, |
| 4509 | input2, input2_length, |
| 4510 | capacity ) ) |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4511 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4512 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4513 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4514 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4515 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4516 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4517 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4518 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4519 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4520 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4521 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4522 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4523 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4524 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4525 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4526 | } |
| 4527 | /* END_CASE */ |
| 4528 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4529 | /* BEGIN_CASE */ |
Gilles Peskine | 0faba4e | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 4530 | void derive_actions_without_setup( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4531 | { |
| 4532 | uint8_t output_buffer[16]; |
| 4533 | size_t buffer_size = 16; |
| 4534 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4535 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4536 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4537 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 4538 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4539 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4540 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4541 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4542 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4543 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4544 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4545 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4546 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 4547 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4548 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4549 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4550 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4551 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4552 | |
| 4553 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4554 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4555 | } |
| 4556 | /* END_CASE */ |
| 4557 | |
| 4558 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4559 | void derive_output( int alg_arg, |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4560 | int step1_arg, data_t *input1, |
| 4561 | int step2_arg, data_t *input2, |
| 4562 | int step3_arg, data_t *input3, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4563 | int requested_capacity_arg, |
| 4564 | data_t *expected_output1, |
| 4565 | data_t *expected_output2 ) |
| 4566 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4567 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4568 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg}; |
| 4569 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4570 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 4571 | MBEDTLS_SVC_KEY_ID_INIT, |
| 4572 | MBEDTLS_SVC_KEY_ID_INIT }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4573 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4574 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4575 | uint8_t *expected_outputs[2] = |
| 4576 | {expected_output1->x, expected_output2->x}; |
| 4577 | size_t output_sizes[2] = |
| 4578 | {expected_output1->len, expected_output2->len}; |
| 4579 | size_t output_buffer_size = 0; |
| 4580 | uint8_t *output_buffer = NULL; |
| 4581 | size_t expected_capacity; |
| 4582 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4583 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4584 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4585 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4586 | |
| 4587 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 4588 | { |
| 4589 | if( output_sizes[i] > output_buffer_size ) |
| 4590 | output_buffer_size = output_sizes[i]; |
| 4591 | if( output_sizes[i] == 0 ) |
| 4592 | expected_outputs[i] = NULL; |
| 4593 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4594 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4595 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4596 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4597 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4598 | psa_set_key_algorithm( &attributes, alg ); |
| 4599 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4600 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4601 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4602 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4603 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 4604 | requested_capacity ) ); |
| 4605 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4606 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4607 | switch( steps[i] ) |
| 4608 | { |
| 4609 | case 0: |
| 4610 | break; |
| 4611 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 4612 | PSA_ASSERT( psa_import_key( &attributes, |
| 4613 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4614 | &keys[i] ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4615 | |
| 4616 | if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 4617 | { |
| 4618 | PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) ); |
| 4619 | TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <= |
| 4620 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ); |
| 4621 | } |
| 4622 | |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4623 | PSA_ASSERT( psa_key_derivation_input_key( |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4624 | &operation, steps[i], keys[i] ) ); |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4625 | break; |
| 4626 | default: |
| 4627 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 4628 | &operation, steps[i], |
| 4629 | inputs[i]->x, inputs[i]->len ) ); |
| 4630 | break; |
| 4631 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4632 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4633 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4634 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4635 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4636 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4637 | expected_capacity = requested_capacity; |
| 4638 | |
| 4639 | /* Expansion phase. */ |
| 4640 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 4641 | { |
| 4642 | /* Read some bytes. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4643 | status = psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4644 | output_buffer, output_sizes[i] ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4645 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 4646 | { |
| 4647 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 4648 | TEST_ASSERT( status == PSA_SUCCESS || |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4649 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4650 | continue; |
| 4651 | } |
| 4652 | else if( expected_capacity == 0 || |
| 4653 | output_sizes[i] > expected_capacity ) |
| 4654 | { |
| 4655 | /* Capacity exceeded. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4656 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4657 | expected_capacity = 0; |
| 4658 | continue; |
| 4659 | } |
| 4660 | /* Success. Check the read data. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4661 | PSA_ASSERT( status ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4662 | if( output_sizes[i] != 0 ) |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4663 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 4664 | expected_outputs[i], output_sizes[i] ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4665 | /* Check the operation status. */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4666 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4667 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4668 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4669 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4670 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4671 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4672 | |
| 4673 | exit: |
| 4674 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4675 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4676 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 4677 | psa_destroy_key( keys[i] ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4678 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4679 | } |
| 4680 | /* END_CASE */ |
| 4681 | |
| 4682 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4683 | void derive_full( int alg_arg, |
| 4684 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 4685 | data_t *input1, |
| 4686 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4687 | int requested_capacity_arg ) |
| 4688 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4689 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4690 | psa_algorithm_t alg = alg_arg; |
| 4691 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4692 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4693 | unsigned char output_buffer[16]; |
| 4694 | size_t expected_capacity = requested_capacity; |
| 4695 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4696 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4697 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4698 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4699 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4700 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4701 | psa_set_key_algorithm( &attributes, alg ); |
| 4702 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4703 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4704 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4705 | &key ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4706 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 4707 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 4708 | input1->x, input1->len, |
| 4709 | input2->x, input2->len, |
| 4710 | requested_capacity ) ) |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 4711 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 4712 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4713 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4714 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4715 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4716 | |
| 4717 | /* Expansion phase. */ |
| 4718 | while( current_capacity > 0 ) |
| 4719 | { |
| 4720 | size_t read_size = sizeof( output_buffer ); |
| 4721 | if( read_size > current_capacity ) |
| 4722 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4723 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4724 | output_buffer, |
| 4725 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4726 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4727 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4728 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4729 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4730 | } |
| 4731 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4732 | /* Check that the operation refuses to go over capacity. */ |
| 4733 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4734 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4735 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4736 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4737 | |
| 4738 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4739 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4740 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4741 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4742 | } |
| 4743 | /* END_CASE */ |
| 4744 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4745 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4746 | void derive_key_exercise( int alg_arg, |
| 4747 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4748 | data_t *input1, |
| 4749 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4750 | int derived_type_arg, |
| 4751 | int derived_bits_arg, |
| 4752 | int derived_usage_arg, |
| 4753 | int derived_alg_arg ) |
| 4754 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4755 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4756 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4757 | psa_algorithm_t alg = alg_arg; |
| 4758 | psa_key_type_t derived_type = derived_type_arg; |
| 4759 | size_t derived_bits = derived_bits_arg; |
| 4760 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 4761 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 4762 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4763 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4764 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4765 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4766 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4767 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4768 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4769 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4770 | psa_set_key_algorithm( &attributes, alg ); |
| 4771 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4772 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4773 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4774 | |
| 4775 | /* Derive a key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 4776 | if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 4777 | input1->x, input1->len, |
| 4778 | input2->x, input2->len, |
| 4779 | capacity ) ) |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4780 | goto exit; |
| 4781 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4782 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 4783 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 4784 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4785 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4786 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4787 | &derived_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4788 | |
| 4789 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4790 | PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4791 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 4792 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4793 | |
| 4794 | /* Exercise the derived key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 4795 | if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4796 | goto exit; |
| 4797 | |
| 4798 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4799 | /* |
| 4800 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4801 | * thus reset them as required. |
| 4802 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4803 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4804 | |
| 4805 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4806 | psa_destroy_key( base_key ); |
| 4807 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4808 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4809 | } |
| 4810 | /* END_CASE */ |
| 4811 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4812 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4813 | void derive_key_export( int alg_arg, |
| 4814 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4815 | data_t *input1, |
| 4816 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4817 | int bytes1_arg, |
| 4818 | int bytes2_arg ) |
| 4819 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4820 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4821 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4822 | psa_algorithm_t alg = alg_arg; |
| 4823 | size_t bytes1 = bytes1_arg; |
| 4824 | size_t bytes2 = bytes2_arg; |
| 4825 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4826 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4827 | uint8_t *output_buffer = NULL; |
| 4828 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4829 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4830 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4831 | size_t length; |
| 4832 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4833 | ASSERT_ALLOC( output_buffer, capacity ); |
| 4834 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4835 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4836 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4837 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 4838 | psa_set_key_algorithm( &base_attributes, alg ); |
| 4839 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4840 | 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] | 4841 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4842 | |
| 4843 | /* Derive some material and output it. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 4844 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 4845 | input1->x, input1->len, |
| 4846 | input2->x, input2->len, |
| 4847 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4848 | goto exit; |
| 4849 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4850 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4851 | output_buffer, |
| 4852 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4853 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4854 | |
| 4855 | /* 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] | 4856 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 4857 | input1->x, input1->len, |
| 4858 | input2->x, input2->len, |
| 4859 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4860 | goto exit; |
| 4861 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4862 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 4863 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 4864 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4865 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4866 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4867 | &derived_key ) ); |
| 4868 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4869 | export_buffer, bytes1, |
| 4870 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4871 | TEST_EQUAL( length, bytes1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4872 | PSA_ASSERT( psa_destroy_key( derived_key ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4873 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4874 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4875 | &derived_key ) ); |
| 4876 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4877 | export_buffer + bytes1, bytes2, |
| 4878 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4879 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4880 | |
| 4881 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4882 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 4883 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4884 | |
| 4885 | exit: |
| 4886 | mbedtls_free( output_buffer ); |
| 4887 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4888 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4889 | psa_destroy_key( base_key ); |
| 4890 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4891 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4892 | } |
| 4893 | /* END_CASE */ |
| 4894 | |
| 4895 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 4896 | void derive_key( int alg_arg, |
| 4897 | data_t *key_data, data_t *input1, data_t *input2, |
| 4898 | int type_arg, int bits_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 4899 | int expected_status_arg, |
| 4900 | int is_large_output ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4901 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4902 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4903 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4904 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 4905 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4906 | size_t bits = bits_arg; |
| 4907 | psa_status_t expected_status = expected_status_arg; |
| 4908 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4909 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4910 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4911 | |
| 4912 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4913 | |
| 4914 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 4915 | psa_set_key_algorithm( &base_attributes, alg ); |
| 4916 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 4917 | 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] | 4918 | &base_key ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4919 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 4920 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 4921 | input1->x, input1->len, |
| 4922 | input2->x, input2->len, |
| 4923 | SIZE_MAX ) ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4924 | goto exit; |
| 4925 | |
| 4926 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 4927 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 4928 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4929 | psa_set_key_bits( &derived_attributes, bits ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 4930 | |
| 4931 | psa_status_t status = |
| 4932 | psa_key_derivation_output_key( &derived_attributes, |
| 4933 | &operation, |
| 4934 | &derived_key ); |
| 4935 | if( is_large_output > 0 ) |
| 4936 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4937 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4938 | |
| 4939 | exit: |
| 4940 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4941 | psa_destroy_key( base_key ); |
| 4942 | psa_destroy_key( derived_key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4943 | PSA_DONE( ); |
| 4944 | } |
| 4945 | /* END_CASE */ |
| 4946 | |
| 4947 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4948 | void key_agreement_setup( int alg_arg, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 4949 | int our_key_type_arg, int our_key_alg_arg, |
| 4950 | data_t *our_key_data, data_t *peer_key_data, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4951 | int expected_status_arg ) |
| 4952 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4953 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4954 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 4955 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4956 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4957 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4958 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4959 | psa_status_t expected_status = expected_status_arg; |
| 4960 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4961 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4962 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4963 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4964 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 4965 | psa_set_key_algorithm( &attributes, our_key_alg ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4966 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4967 | PSA_ASSERT( psa_import_key( &attributes, |
| 4968 | our_key_data->x, our_key_data->len, |
| 4969 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4970 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4971 | /* The tests currently include inputs that should fail at either step. |
| 4972 | * Test cases that fail at the setup step should be changed to call |
| 4973 | * key_derivation_setup instead, and this function should be renamed |
| 4974 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4975 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4976 | if( status == PSA_SUCCESS ) |
| 4977 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4978 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 4979 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 4980 | our_key, |
| 4981 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4982 | expected_status ); |
| 4983 | } |
| 4984 | else |
| 4985 | { |
| 4986 | TEST_ASSERT( status == expected_status ); |
| 4987 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4988 | |
| 4989 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4990 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4991 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4992 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4993 | } |
| 4994 | /* END_CASE */ |
| 4995 | |
| 4996 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4997 | void raw_key_agreement( int alg_arg, |
| 4998 | int our_key_type_arg, data_t *our_key_data, |
| 4999 | data_t *peer_key_data, |
| 5000 | data_t *expected_output ) |
| 5001 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5002 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5003 | psa_algorithm_t alg = alg_arg; |
| 5004 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5005 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5006 | unsigned char *output = NULL; |
| 5007 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5008 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5009 | |
| 5010 | ASSERT_ALLOC( output, expected_output->len ); |
| 5011 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5012 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5013 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5014 | psa_set_key_algorithm( &attributes, alg ); |
| 5015 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5016 | PSA_ASSERT( psa_import_key( &attributes, |
| 5017 | our_key_data->x, our_key_data->len, |
| 5018 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5019 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5020 | PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) ); |
| 5021 | key_bits = psa_get_key_bits( &attributes ); |
| 5022 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5023 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 5024 | peer_key_data->x, peer_key_data->len, |
| 5025 | output, expected_output->len, |
| 5026 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5027 | ASSERT_COMPARE( output, output_length, |
| 5028 | expected_output->x, expected_output->len ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5029 | TEST_ASSERT( output_length <= |
| 5030 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) ); |
| 5031 | TEST_ASSERT( output_length <= |
| 5032 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5033 | |
| 5034 | exit: |
| 5035 | mbedtls_free( output ); |
| 5036 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5037 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5038 | } |
| 5039 | /* END_CASE */ |
| 5040 | |
| 5041 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5042 | void key_agreement_capacity( int alg_arg, |
| 5043 | int our_key_type_arg, data_t *our_key_data, |
| 5044 | data_t *peer_key_data, |
| 5045 | int expected_capacity_arg ) |
| 5046 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5047 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5048 | psa_algorithm_t alg = alg_arg; |
| 5049 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5050 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5051 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5052 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5053 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5054 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5055 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5056 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5057 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5058 | psa_set_key_algorithm( &attributes, alg ); |
| 5059 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5060 | PSA_ASSERT( psa_import_key( &attributes, |
| 5061 | our_key_data->x, our_key_data->len, |
| 5062 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5063 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5064 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5065 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 5066 | &operation, |
| 5067 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 5068 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5069 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 5070 | { |
| 5071 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5072 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5073 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5074 | NULL, 0 ) ); |
| 5075 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5076 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5077 | /* Test the advertized capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5078 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5079 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5080 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5081 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5082 | /* Test the actual capacity by reading the output. */ |
| 5083 | while( actual_capacity > sizeof( output ) ) |
| 5084 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5085 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5086 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5087 | actual_capacity -= sizeof( output ); |
| 5088 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5089 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5090 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5091 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5092 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5093 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5094 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5095 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5096 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5097 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5098 | } |
| 5099 | /* END_CASE */ |
| 5100 | |
| 5101 | /* BEGIN_CASE */ |
| 5102 | void key_agreement_output( int alg_arg, |
| 5103 | int our_key_type_arg, data_t *our_key_data, |
| 5104 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5105 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5106 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5107 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5108 | psa_algorithm_t alg = alg_arg; |
| 5109 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5110 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5111 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5112 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5113 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5114 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 5115 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5116 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5117 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5118 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5119 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5120 | psa_set_key_algorithm( &attributes, alg ); |
| 5121 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5122 | PSA_ASSERT( psa_import_key( &attributes, |
| 5123 | our_key_data->x, our_key_data->len, |
| 5124 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5125 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5126 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5127 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 5128 | &operation, |
| 5129 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 5130 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5131 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 5132 | { |
| 5133 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5134 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5135 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5136 | NULL, 0 ) ); |
| 5137 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5138 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5139 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5140 | actual_output, |
| 5141 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5142 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 5143 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5144 | if( expected_output2->len != 0 ) |
| 5145 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5146 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5147 | actual_output, |
| 5148 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5149 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 5150 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5151 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5152 | |
| 5153 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5154 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5155 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5156 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5157 | mbedtls_free( actual_output ); |
| 5158 | } |
| 5159 | /* END_CASE */ |
| 5160 | |
| 5161 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5162 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5163 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5164 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5165 | unsigned char *output = NULL; |
| 5166 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5167 | size_t i; |
| 5168 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5169 | |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 5170 | TEST_ASSERT( bytes_arg >= 0 ); |
| 5171 | |
Gilles Peskine | 9189202 | 2021-02-08 19:50:26 +0100 | [diff] [blame] | 5172 | ASSERT_ALLOC( output, bytes ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5173 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5174 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5175 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5176 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5177 | /* Run several times, to ensure that every output byte will be |
| 5178 | * nonzero at least once with overwhelming probability |
| 5179 | * (2^(-8*number_of_runs)). */ |
| 5180 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5181 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 5182 | if( bytes != 0 ) |
| 5183 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5184 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5185 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5186 | for( i = 0; i < bytes; i++ ) |
| 5187 | { |
| 5188 | if( output[i] != 0 ) |
| 5189 | ++changed[i]; |
| 5190 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5191 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5192 | |
| 5193 | /* Check that every byte was changed to nonzero at least once. This |
| 5194 | * validates that psa_generate_random is overwriting every byte of |
| 5195 | * the output buffer. */ |
| 5196 | for( i = 0; i < bytes; i++ ) |
| 5197 | { |
| 5198 | TEST_ASSERT( changed[i] != 0 ); |
| 5199 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5200 | |
| 5201 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5202 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5203 | mbedtls_free( output ); |
| 5204 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5205 | } |
| 5206 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5207 | |
| 5208 | /* BEGIN_CASE */ |
| 5209 | void generate_key( int type_arg, |
| 5210 | int bits_arg, |
| 5211 | int usage_arg, |
| 5212 | int alg_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 5213 | int expected_status_arg, |
| 5214 | int is_large_key ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5215 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5216 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5217 | psa_key_type_t type = type_arg; |
| 5218 | psa_key_usage_t usage = usage_arg; |
| 5219 | size_t bits = bits_arg; |
| 5220 | psa_algorithm_t alg = alg_arg; |
| 5221 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5222 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5223 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5224 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5225 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5226 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5227 | psa_set_key_usage_flags( &attributes, usage ); |
| 5228 | psa_set_key_algorithm( &attributes, alg ); |
| 5229 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5230 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5231 | |
| 5232 | /* Generate a key */ |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 5233 | psa_status_t status = psa_generate_key( &attributes, &key ); |
| 5234 | |
| 5235 | if( is_large_key > 0 ) |
| 5236 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5237 | TEST_EQUAL( status , expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5238 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5239 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5240 | |
| 5241 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5242 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5243 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 5244 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5245 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 5246 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5247 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 5248 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5249 | |
| 5250 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5251 | /* |
| 5252 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5253 | * thus reset them as required. |
| 5254 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5255 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5256 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5257 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5258 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5259 | } |
| 5260 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5261 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 5262 | /* 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] | 5263 | void generate_key_rsa( int bits_arg, |
| 5264 | data_t *e_arg, |
| 5265 | int expected_status_arg ) |
| 5266 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5267 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5268 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5269 | size_t bits = bits_arg; |
| 5270 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 5271 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 5272 | psa_status_t expected_status = expected_status_arg; |
| 5273 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5274 | uint8_t *exported = NULL; |
| 5275 | size_t exported_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5276 | PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5277 | size_t exported_length = SIZE_MAX; |
| 5278 | uint8_t *e_read_buffer = NULL; |
| 5279 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 5280 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5281 | size_t e_read_length = SIZE_MAX; |
| 5282 | |
| 5283 | if( e_arg->len == 0 || |
| 5284 | ( e_arg->len == 3 && |
| 5285 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 5286 | { |
| 5287 | is_default_public_exponent = 1; |
| 5288 | e_read_size = 0; |
| 5289 | } |
| 5290 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 5291 | ASSERT_ALLOC( exported, exported_size ); |
| 5292 | |
| 5293 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5294 | |
| 5295 | psa_set_key_usage_flags( &attributes, usage ); |
| 5296 | psa_set_key_algorithm( &attributes, alg ); |
| 5297 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 5298 | e_arg->x, e_arg->len ) ); |
| 5299 | psa_set_key_bits( &attributes, bits ); |
| 5300 | |
| 5301 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5302 | TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5303 | if( expected_status != PSA_SUCCESS ) |
| 5304 | goto exit; |
| 5305 | |
| 5306 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5307 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5308 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 5309 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 5310 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 5311 | e_read_buffer, e_read_size, |
| 5312 | &e_read_length ) ); |
| 5313 | if( is_default_public_exponent ) |
| 5314 | TEST_EQUAL( e_read_length, 0 ); |
| 5315 | else |
| 5316 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 5317 | |
| 5318 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5319 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5320 | goto exit; |
| 5321 | |
| 5322 | /* Export the key and check the public exponent. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5323 | PSA_ASSERT( psa_export_public_key( key, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5324 | exported, exported_size, |
| 5325 | &exported_length ) ); |
| 5326 | { |
| 5327 | uint8_t *p = exported; |
| 5328 | uint8_t *end = exported + exported_length; |
| 5329 | size_t len; |
| 5330 | /* RSAPublicKey ::= SEQUENCE { |
| 5331 | * modulus INTEGER, -- n |
| 5332 | * publicExponent INTEGER } -- e |
| 5333 | */ |
| 5334 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5335 | MBEDTLS_ASN1_SEQUENCE | |
| 5336 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 5337 | TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5338 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 5339 | MBEDTLS_ASN1_INTEGER ) ); |
| 5340 | if( len >= 1 && p[0] == 0 ) |
| 5341 | { |
| 5342 | ++p; |
| 5343 | --len; |
| 5344 | } |
| 5345 | if( e_arg->len == 0 ) |
| 5346 | { |
| 5347 | TEST_EQUAL( len, 3 ); |
| 5348 | TEST_EQUAL( p[0], 1 ); |
| 5349 | TEST_EQUAL( p[1], 0 ); |
| 5350 | TEST_EQUAL( p[2], 1 ); |
| 5351 | } |
| 5352 | else |
| 5353 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 5354 | } |
| 5355 | |
| 5356 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5357 | /* |
| 5358 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 5359 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 5360 | */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5361 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5362 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5363 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5364 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5365 | mbedtls_free( e_read_buffer ); |
| 5366 | mbedtls_free( exported ); |
| 5367 | } |
| 5368 | /* END_CASE */ |
| 5369 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5370 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5371 | void persistent_key_load_key_from_storage( data_t *data, |
| 5372 | int type_arg, int bits_arg, |
| 5373 | int usage_flags_arg, int alg_arg, |
| 5374 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5375 | { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 5376 | 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] | 5377 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5378 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5379 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5380 | psa_key_type_t type = type_arg; |
| 5381 | size_t bits = bits_arg; |
| 5382 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 5383 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5384 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5385 | unsigned char *first_export = NULL; |
| 5386 | unsigned char *second_export = NULL; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5387 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5388 | size_t first_exported_length; |
| 5389 | size_t second_exported_length; |
| 5390 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5391 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 5392 | { |
| 5393 | ASSERT_ALLOC( first_export, export_size ); |
| 5394 | ASSERT_ALLOC( second_export, export_size ); |
| 5395 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5396 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5397 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5398 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 5399 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5400 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 5401 | psa_set_key_algorithm( &attributes, alg ); |
| 5402 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5403 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5404 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5405 | switch( generation_method ) |
| 5406 | { |
| 5407 | case IMPORT_KEY: |
| 5408 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5409 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5410 | &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5411 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5412 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5413 | case GENERATE_KEY: |
| 5414 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5415 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5416 | break; |
| 5417 | |
| 5418 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 5419 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5420 | { |
| 5421 | /* Create base key */ |
| 5422 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 5423 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5424 | psa_set_key_usage_flags( &base_attributes, |
| 5425 | PSA_KEY_USAGE_DERIVE ); |
| 5426 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 5427 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5428 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 5429 | data->x, data->len, |
| 5430 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5431 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5432 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5433 | PSA_ASSERT( psa_key_derivation_input_key( |
| 5434 | &operation, |
| 5435 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5436 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5437 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5438 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5439 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 5440 | &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5441 | &key ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5442 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5443 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5444 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5445 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 5446 | #else |
| 5447 | TEST_ASSUME( ! "KDF not supported in this configuration" ); |
| 5448 | #endif |
| 5449 | break; |
| 5450 | |
| 5451 | default: |
| 5452 | TEST_ASSERT( ! "generation_method not implemented in test" ); |
| 5453 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5454 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5455 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5456 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5457 | /* Export the key if permitted by the key policy. */ |
| 5458 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 5459 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5460 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5461 | first_export, export_size, |
| 5462 | &first_exported_length ) ); |
| 5463 | if( generation_method == IMPORT_KEY ) |
| 5464 | ASSERT_COMPARE( data->x, data->len, |
| 5465 | first_export, first_exported_length ); |
| 5466 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5467 | |
| 5468 | /* Shutdown and restart */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5469 | PSA_ASSERT( psa_purge_key( key ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5470 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5471 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5472 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5473 | /* Check key slot still contains key data */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5474 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 5475 | TEST_ASSERT( mbedtls_svc_key_id_equal( |
| 5476 | psa_get_key_id( &attributes ), key_id ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5477 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 5478 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 5479 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 5480 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
gabor-mezei-arm | 4d9009e | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 5481 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 5482 | mbedtls_test_update_key_usage_flags( usage_flags ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5483 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5484 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5485 | /* Export the key again if permitted by the key policy. */ |
| 5486 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5487 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5488 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5489 | second_export, export_size, |
| 5490 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5491 | ASSERT_COMPARE( first_export, first_exported_length, |
| 5492 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5493 | } |
| 5494 | |
| 5495 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5496 | if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5497 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5498 | |
| 5499 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5500 | /* |
| 5501 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5502 | * thus reset them as required. |
| 5503 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5504 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5505 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5506 | mbedtls_free( first_export ); |
| 5507 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5508 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5509 | psa_destroy_key( base_key ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5510 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5511 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5512 | } |
| 5513 | /* END_CASE */ |