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 | } |
Andrzej Kurek | e001596 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 51 | #if defined(MBEDTLS_ASN1_WRITE_C) |
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 | } |
Andrzej Kurek | e001596 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 135 | #endif /* MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 136 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 137 | int exercise_mac_setup( psa_key_type_t key_type, |
| 138 | const unsigned char *key_bytes, |
| 139 | size_t key_length, |
| 140 | psa_algorithm_t alg, |
| 141 | psa_mac_operation_t *operation, |
| 142 | psa_status_t *status ) |
| 143 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 144 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 145 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 146 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 147 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 148 | psa_set_key_algorithm( &attributes, alg ); |
| 149 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 150 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 151 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 152 | *status = psa_mac_sign_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 153 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 154 | PSA_ASSERT( psa_mac_abort( operation ) ); |
| 155 | /* If setup failed, reproduce the failure, so that the caller can |
| 156 | * test the resulting state of the operation object. */ |
| 157 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 158 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 159 | TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 162 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 163 | return( 1 ); |
| 164 | |
| 165 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 166 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 167 | return( 0 ); |
| 168 | } |
| 169 | |
| 170 | int exercise_cipher_setup( psa_key_type_t key_type, |
| 171 | const unsigned char *key_bytes, |
| 172 | size_t key_length, |
| 173 | psa_algorithm_t alg, |
| 174 | psa_cipher_operation_t *operation, |
| 175 | psa_status_t *status ) |
| 176 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 177 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 178 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 179 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 180 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 181 | psa_set_key_algorithm( &attributes, alg ); |
| 182 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 183 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 184 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 185 | *status = psa_cipher_encrypt_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 186 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 187 | PSA_ASSERT( psa_cipher_abort( operation ) ); |
| 188 | /* If setup failed, reproduce the failure, so that the caller can |
| 189 | * test the resulting state of the operation object. */ |
| 190 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 191 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 192 | TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ), |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 193 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 196 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 197 | return( 1 ); |
| 198 | |
| 199 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 200 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 201 | return( 0 ); |
| 202 | } |
| 203 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 204 | 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] | 205 | { |
| 206 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 207 | 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] | 208 | uint8_t buffer[1]; |
| 209 | size_t length; |
| 210 | int ok = 0; |
| 211 | |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 212 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 213 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 214 | psa_set_key_algorithm( &attributes, PSA_ALG_CTR ); |
| 215 | psa_set_key_type( &attributes, PSA_KEY_TYPE_AES ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 216 | TEST_EQUAL( psa_get_key_attributes( key, &attributes ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 217 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 218 | TEST_EQUAL( |
| 219 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 ); |
| 220 | TEST_EQUAL( |
| 221 | 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] | 222 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 223 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 224 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 225 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
| 226 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
| 227 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 228 | TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 229 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 230 | TEST_EQUAL( psa_export_public_key( key, |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 231 | buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 232 | PSA_ERROR_INVALID_HANDLE ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 233 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 234 | ok = 1; |
| 235 | |
| 236 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 237 | /* |
| 238 | * Key attributes may have been returned by psa_get_key_attributes() |
| 239 | * thus reset them as required. |
| 240 | */ |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 241 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 242 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 243 | return( ok ); |
| 244 | } |
| 245 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 246 | /* Assert that a key isn't reported as having a slot number. */ |
| 247 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 248 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 249 | do \ |
| 250 | { \ |
| 251 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
| 252 | TEST_EQUAL( psa_get_key_slot_number( \ |
| 253 | attributes, \ |
| 254 | &ASSERT_NO_SLOT_NUMBER_slot_number ), \ |
| 255 | PSA_ERROR_INVALID_ARGUMENT ); \ |
| 256 | } \ |
| 257 | while( 0 ) |
| 258 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 259 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 260 | ( (void) 0 ) |
| 261 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 262 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 263 | /* An overapproximation of the amount of storage needed for a key of the |
| 264 | * given type and with the given content. The API doesn't make it easy |
| 265 | * to find a good value for the size. The current implementation doesn't |
| 266 | * care about the value anyway. */ |
| 267 | #define KEY_BITS_FROM_DATA( type, data ) \ |
| 268 | ( data )->len |
| 269 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 270 | typedef enum { |
| 271 | IMPORT_KEY = 0, |
| 272 | GENERATE_KEY = 1, |
| 273 | DERIVE_KEY = 2 |
| 274 | } generate_method; |
| 275 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 276 | /* END_HEADER */ |
| 277 | |
| 278 | /* BEGIN_DEPENDENCIES |
| 279 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 280 | * END_DEPENDENCIES |
| 281 | */ |
| 282 | |
| 283 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 284 | void static_checks( ) |
| 285 | { |
| 286 | size_t max_truncated_mac_size = |
| 287 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 288 | |
| 289 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 290 | * encoding. The shifted mask is the maximum truncated value. The |
| 291 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 292 | TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 293 | |
| 294 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 295 | /* Check deprecated constants. */ |
| 296 | TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR ); |
| 297 | TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS ); |
| 298 | TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST ); |
| 299 | TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA ); |
| 300 | TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED ); |
| 301 | TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH ); |
| 302 | TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH ); |
| 303 | TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 304 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 305 | TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 306 | TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 307 | TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 308 | TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 309 | TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 310 | TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 311 | TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 312 | TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 313 | TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 314 | TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 315 | TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 ); |
| 316 | TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 317 | TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 318 | TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 319 | TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 320 | TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 321 | TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 322 | TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 323 | TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 324 | TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 325 | TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 326 | TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 327 | TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 328 | TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 ); |
| 329 | TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 ); |
| 330 | TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 ); |
| 331 | TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 ); |
| 332 | TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 ); |
| 333 | TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY ); |
| 334 | TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY ); |
| 335 | |
| 336 | TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 337 | TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 338 | TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 ); |
| 339 | TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 340 | TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 341 | TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 ); |
| 342 | TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 ); |
| 343 | TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY ); |
Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 344 | |
Paul Elliott | 75e2703 | 2020-06-03 15:17:39 +0100 | [diff] [blame] | 345 | TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 ); |
| 346 | TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 ); |
| 347 | TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 ); |
| 348 | TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 ); |
| 349 | TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 ); |
| 350 | |
| 351 | TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 ); |
| 352 | TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM ); |
Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 353 | #endif |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 354 | } |
| 355 | /* END_CASE */ |
| 356 | |
| 357 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 358 | void import_with_policy( int type_arg, |
| 359 | int usage_arg, int alg_arg, |
| 360 | int expected_status_arg ) |
| 361 | { |
| 362 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 363 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 364 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 365 | psa_key_type_t type = type_arg; |
| 366 | psa_key_usage_t usage = usage_arg; |
| 367 | psa_algorithm_t alg = alg_arg; |
| 368 | psa_status_t expected_status = expected_status_arg; |
| 369 | const uint8_t key_material[16] = {0}; |
| 370 | psa_status_t status; |
| 371 | |
| 372 | PSA_ASSERT( psa_crypto_init( ) ); |
| 373 | |
| 374 | psa_set_key_type( &attributes, type ); |
| 375 | psa_set_key_usage_flags( &attributes, usage ); |
| 376 | psa_set_key_algorithm( &attributes, alg ); |
| 377 | |
| 378 | status = psa_import_key( &attributes, |
| 379 | key_material, sizeof( key_material ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 380 | &key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 381 | TEST_EQUAL( status, expected_status ); |
| 382 | if( status != PSA_SUCCESS ) |
| 383 | goto exit; |
| 384 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 385 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 386 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
gabor-mezei-arm | 4d9009e | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 387 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 388 | mbedtls_test_update_key_usage_flags( usage ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 389 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 390 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 391 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 392 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 393 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 394 | |
| 395 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 396 | /* |
| 397 | * Key attributes may have been returned by psa_get_key_attributes() |
| 398 | * thus reset them as required. |
| 399 | */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 400 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 401 | |
| 402 | psa_destroy_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 403 | PSA_DONE( ); |
| 404 | } |
| 405 | /* END_CASE */ |
| 406 | |
| 407 | /* BEGIN_CASE */ |
| 408 | void import_with_data( data_t *data, int type_arg, |
| 409 | int attr_bits_arg, |
| 410 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 411 | { |
| 412 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 413 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 414 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 415 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 416 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 417 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 418 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 419 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 420 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 421 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 422 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 423 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 424 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 425 | status = psa_import_key( &attributes, data->x, data->len, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 426 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 427 | if( status != PSA_SUCCESS ) |
| 428 | goto exit; |
| 429 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 430 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 431 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 432 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 433 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 434 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 435 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 436 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 437 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 438 | |
| 439 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 440 | /* |
| 441 | * Key attributes may have been returned by psa_get_key_attributes() |
| 442 | * thus reset them as required. |
| 443 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 444 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 445 | |
| 446 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 447 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 448 | } |
| 449 | /* END_CASE */ |
| 450 | |
| 451 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 452 | void import_large_key( int type_arg, int byte_size_arg, |
| 453 | int expected_status_arg ) |
| 454 | { |
| 455 | psa_key_type_t type = type_arg; |
| 456 | size_t byte_size = byte_size_arg; |
| 457 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 458 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 459 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 460 | psa_status_t status; |
| 461 | uint8_t *buffer = NULL; |
| 462 | size_t buffer_size = byte_size + 1; |
| 463 | size_t n; |
| 464 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 465 | /* Skip the test case if the target running the test cannot |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 466 | * accommodate large keys due to heap size constraints */ |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 467 | ASSERT_ALLOC_WEAK( buffer, buffer_size ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 468 | memset( buffer, 'K', byte_size ); |
| 469 | |
| 470 | PSA_ASSERT( psa_crypto_init( ) ); |
| 471 | |
| 472 | /* Try importing the key */ |
| 473 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 474 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 475 | status = psa_import_key( &attributes, buffer, byte_size, &key ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 476 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 477 | TEST_EQUAL( status, expected_status ); |
| 478 | |
| 479 | if( status == PSA_SUCCESS ) |
| 480 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 481 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 482 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 483 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 484 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 485 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 486 | memset( buffer, 0, byte_size + 1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 487 | PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 488 | for( n = 0; n < byte_size; n++ ) |
| 489 | TEST_EQUAL( buffer[n], 'K' ); |
| 490 | for( n = byte_size; n < buffer_size; n++ ) |
| 491 | TEST_EQUAL( buffer[n], 0 ); |
| 492 | } |
| 493 | |
| 494 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 495 | /* |
| 496 | * Key attributes may have been returned by psa_get_key_attributes() |
| 497 | * thus reset them as required. |
| 498 | */ |
| 499 | psa_reset_key_attributes( &attributes ); |
| 500 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 501 | psa_destroy_key( key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 502 | PSA_DONE( ); |
| 503 | mbedtls_free( buffer ); |
| 504 | } |
| 505 | /* END_CASE */ |
| 506 | |
Andrzej Kurek | e001596 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 507 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 508 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 509 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 510 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 511 | size_t bits = bits_arg; |
| 512 | psa_status_t expected_status = expected_status_arg; |
| 513 | psa_status_t status; |
| 514 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 515 | 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] | 516 | size_t buffer_size = /* Slight overapproximations */ |
| 517 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 518 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 519 | unsigned char *p; |
| 520 | int ret; |
| 521 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 522 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 523 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 524 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 525 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 526 | |
| 527 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 528 | bits, keypair ) ) >= 0 ); |
| 529 | length = ret; |
| 530 | |
| 531 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 532 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 533 | status = psa_import_key( &attributes, p, length, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 534 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 535 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 536 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 537 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 538 | |
| 539 | exit: |
| 540 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 541 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 542 | } |
| 543 | /* END_CASE */ |
| 544 | |
| 545 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 546 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 547 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 548 | int usage_arg, int alg_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 549 | int expected_bits, |
| 550 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 551 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 552 | int canonical_input ) |
| 553 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 554 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 555 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 556 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 557 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 558 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 559 | unsigned char *exported = NULL; |
| 560 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 561 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 562 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 563 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 564 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 565 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 566 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 567 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 568 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 569 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 570 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 571 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 572 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 573 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 574 | psa_set_key_algorithm( &attributes, alg ); |
| 575 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 576 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 577 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 578 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 579 | |
| 580 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 581 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 582 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 583 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 584 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 585 | |
| 586 | /* Export the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 587 | status = psa_export_key( key, exported, export_size, &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 588 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 589 | |
| 590 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 591 | * and export_size. On errors, the exported length must be 0. */ |
| 592 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 593 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 594 | TEST_LE_U( exported_length, export_size ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 595 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 596 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 597 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 598 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 599 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 600 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 601 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 602 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 603 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 604 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 605 | * this validates the canonical representations. For canonical inputs, |
| 606 | * this doesn't directly validate the implementation, but it still helps |
| 607 | * by cross-validating the test data with the sanity check code. */ |
| 608 | if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) ) |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 609 | goto exit; |
| 610 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 611 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 612 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 613 | else |
| 614 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 615 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 616 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 617 | &key2 ) ); |
| 618 | PSA_ASSERT( psa_export_key( key2, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 619 | reexported, |
| 620 | export_size, |
| 621 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 622 | ASSERT_COMPARE( exported, exported_length, |
| 623 | reexported, reexported_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 624 | PSA_ASSERT( psa_destroy_key( key2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 625 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 626 | TEST_ASSERT( exported_length <= |
| 627 | PSA_EXPORT_KEY_OUTPUT_SIZE( type, |
| 628 | psa_get_key_bits( &got_attributes ) ) ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 629 | TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 630 | |
| 631 | destroy: |
| 632 | /* Destroy the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 633 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 634 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 635 | |
| 636 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 637 | /* |
| 638 | * Key attributes may have been returned by psa_get_key_attributes() |
| 639 | * thus reset them as required. |
| 640 | */ |
| 641 | psa_reset_key_attributes( &got_attributes ); |
| 642 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 643 | mbedtls_free( exported ); |
| 644 | mbedtls_free( reexported ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 645 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 646 | } |
| 647 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 648 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 649 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 650 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 651 | int type_arg, |
| 652 | int alg_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 653 | int export_size_delta, |
| 654 | int expected_export_status_arg, |
| 655 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 656 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 657 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 658 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 659 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 660 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 661 | psa_status_t status; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 662 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 663 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 664 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 665 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 666 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 667 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 668 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 669 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 670 | psa_set_key_algorithm( &attributes, alg ); |
| 671 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 672 | |
| 673 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 674 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 675 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 676 | /* Export the public key */ |
| 677 | ASSERT_ALLOC( exported, export_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 678 | status = psa_export_public_key( key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 679 | exported, export_size, |
| 680 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 681 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 682 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 683 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 684 | 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] | 685 | size_t bits; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 686 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 687 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 688 | TEST_LE_U( expected_public_key->len, |
| 689 | PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 690 | TEST_LE_U( expected_public_key->len, |
| 691 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 692 | TEST_LE_U( expected_public_key->len, |
| 693 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 694 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 695 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 696 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 697 | |
| 698 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 699 | /* |
| 700 | * Key attributes may have been returned by psa_get_key_attributes() |
| 701 | * thus reset them as required. |
| 702 | */ |
| 703 | psa_reset_key_attributes( &attributes ); |
| 704 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 705 | mbedtls_free( exported ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 706 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 707 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 708 | } |
| 709 | /* END_CASE */ |
| 710 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 711 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 712 | void import_and_exercise_key( data_t *data, |
| 713 | int type_arg, |
| 714 | int bits_arg, |
| 715 | int alg_arg ) |
| 716 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 717 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 718 | psa_key_type_t type = type_arg; |
| 719 | size_t bits = bits_arg; |
| 720 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 721 | 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] | 722 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 723 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 724 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 725 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 726 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 727 | psa_set_key_usage_flags( &attributes, usage ); |
| 728 | psa_set_key_algorithm( &attributes, alg ); |
| 729 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 730 | |
| 731 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 732 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 733 | |
| 734 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 735 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 736 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 737 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 738 | |
| 739 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 740 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 741 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 742 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 743 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 744 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 745 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 746 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 747 | /* |
| 748 | * Key attributes may have been returned by psa_get_key_attributes() |
| 749 | * thus reset them as required. |
| 750 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 751 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 752 | |
| 753 | psa_reset_key_attributes( &attributes ); |
| 754 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 755 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 756 | } |
| 757 | /* END_CASE */ |
| 758 | |
| 759 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 760 | void effective_key_attributes( int type_arg, int expected_type_arg, |
| 761 | int bits_arg, int expected_bits_arg, |
| 762 | int usage_arg, int expected_usage_arg, |
| 763 | int alg_arg, int expected_alg_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 764 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 765 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 766 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 767 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 768 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 769 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 770 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 771 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 772 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 773 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 774 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 775 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 776 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 777 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 778 | psa_set_key_usage_flags( &attributes, usage ); |
| 779 | psa_set_key_algorithm( &attributes, alg ); |
| 780 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 781 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 782 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 783 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 784 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 785 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 786 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 787 | TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type ); |
| 788 | TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits ); |
| 789 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 790 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 791 | |
| 792 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 793 | /* |
| 794 | * Key attributes may have been returned by psa_get_key_attributes() |
| 795 | * thus reset them as required. |
| 796 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 797 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 798 | |
| 799 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 800 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 801 | } |
| 802 | /* END_CASE */ |
| 803 | |
| 804 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 805 | void check_key_policy( int type_arg, int bits_arg, |
| 806 | int usage_arg, int alg_arg ) |
| 807 | { |
| 808 | 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] | 809 | usage_arg, |
| 810 | mbedtls_test_update_key_usage_flags( usage_arg ), |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 811 | alg_arg, alg_arg ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 812 | goto exit; |
| 813 | } |
| 814 | /* END_CASE */ |
| 815 | |
| 816 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 817 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 818 | { |
| 819 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 820 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 821 | * though it's OK by the C standard. We could test for this, but we'd need |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 822 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 823 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 824 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 825 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 826 | |
| 827 | memset( &zero, 0, sizeof( zero ) ); |
| 828 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 829 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 830 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 831 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 832 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 833 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 834 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 835 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 836 | |
| 837 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 838 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 839 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 840 | |
| 841 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 842 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 843 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 844 | |
| 845 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 846 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 847 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 848 | } |
| 849 | /* END_CASE */ |
| 850 | |
| 851 | /* BEGIN_CASE */ |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 852 | void mac_key_policy( int policy_usage_arg, |
| 853 | int policy_alg_arg, |
| 854 | int key_type_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 855 | data_t *key_data, |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 856 | int exercise_alg_arg, |
Mateusz Starzyk | 25e65db | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 857 | int expected_status_sign_arg, |
| 858 | int expected_status_verify_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 859 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 860 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 861 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 862 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 863 | psa_key_type_t key_type = key_type_arg; |
| 864 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 865 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 866 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 867 | psa_status_t status; |
Mateusz Starzyk | 25e65db | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 868 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 869 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 870 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 871 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 872 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 873 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 874 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 875 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 876 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 877 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 878 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 879 | &key ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 880 | |
gabor-mezei-arm | 659af9e | 2021-06-29 11:06:16 +0200 | [diff] [blame] | 881 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
| 882 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 883 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 884 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | 25e65db | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 885 | TEST_EQUAL( status, expected_status_sign ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 886 | |
Mateusz Starzyk | e6e02b6 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 887 | /* Calculate the MAC, one-shot case. */ |
| 888 | uint8_t input[128] = {0}; |
| 889 | size_t mac_len; |
| 890 | TEST_EQUAL( psa_mac_compute( key, exercise_alg, |
| 891 | input, 128, |
| 892 | mac, PSA_MAC_MAX_SIZE, &mac_len ), |
| 893 | expected_status_sign ); |
| 894 | |
| 895 | /* Verify correct MAC, one-shot case. */ |
| 896 | status = psa_mac_verify( key, exercise_alg, input, 128, |
| 897 | mac, mac_len ); |
| 898 | |
| 899 | if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) |
| 900 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 901 | else |
| 902 | TEST_EQUAL( status, expected_status_verify ); |
| 903 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 904 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 905 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 906 | memset( mac, 0, sizeof( mac ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 907 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | 25e65db | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 908 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 909 | |
| 910 | exit: |
| 911 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 912 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 913 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 914 | } |
| 915 | /* END_CASE */ |
| 916 | |
| 917 | /* BEGIN_CASE */ |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 918 | void cipher_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 919 | int policy_alg, |
| 920 | int key_type, |
| 921 | data_t *key_data, |
| 922 | int exercise_alg ) |
| 923 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 924 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 925 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 926 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 927 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 928 | psa_status_t status; |
| 929 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 930 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 931 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 932 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 933 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 934 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 935 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 936 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 937 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 938 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 939 | /* Check if no key usage flag implication is done */ |
| 940 | TEST_EQUAL( policy_usage, |
| 941 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 942 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 943 | status = psa_cipher_encrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 944 | if( policy_alg == exercise_alg && |
| 945 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 946 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 947 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 948 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 949 | psa_cipher_abort( &operation ); |
| 950 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 951 | status = psa_cipher_decrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 952 | if( policy_alg == exercise_alg && |
| 953 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 954 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 955 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 956 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 957 | |
| 958 | exit: |
| 959 | psa_cipher_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 960 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 961 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 962 | } |
| 963 | /* END_CASE */ |
| 964 | |
| 965 | /* BEGIN_CASE */ |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 966 | void aead_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 967 | int policy_alg, |
| 968 | int key_type, |
| 969 | data_t *key_data, |
| 970 | int nonce_length_arg, |
| 971 | int tag_length_arg, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 972 | int exercise_alg, |
| 973 | int expected_status_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 974 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 975 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 976 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 977 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 978 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 979 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 980 | unsigned char nonce[16] = {0}; |
| 981 | size_t nonce_length = nonce_length_arg; |
| 982 | unsigned char tag[16]; |
| 983 | size_t tag_length = tag_length_arg; |
| 984 | size_t output_length; |
| 985 | |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 986 | TEST_LE_U( nonce_length, sizeof( nonce ) ); |
| 987 | TEST_LE_U( tag_length, sizeof( tag ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 988 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 989 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 990 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 991 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 992 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 993 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 994 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 995 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 996 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 997 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 998 | /* Check if no key usage implication is done */ |
| 999 | TEST_EQUAL( policy_usage, |
| 1000 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1001 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1002 | status = psa_aead_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1003 | nonce, nonce_length, |
| 1004 | NULL, 0, |
| 1005 | NULL, 0, |
| 1006 | tag, tag_length, |
| 1007 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1008 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1009 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1010 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1011 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1012 | |
| 1013 | memset( tag, 0, sizeof( tag ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1014 | status = psa_aead_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1015 | nonce, nonce_length, |
| 1016 | NULL, 0, |
| 1017 | tag, tag_length, |
| 1018 | NULL, 0, |
| 1019 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1020 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 1021 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1022 | else if( expected_status == PSA_SUCCESS ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1023 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1024 | else |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1025 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1026 | |
| 1027 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1028 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1029 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1030 | } |
| 1031 | /* END_CASE */ |
| 1032 | |
| 1033 | /* BEGIN_CASE */ |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1034 | void asymmetric_encryption_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1035 | int policy_alg, |
| 1036 | int key_type, |
| 1037 | data_t *key_data, |
| 1038 | int exercise_alg ) |
| 1039 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1040 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1041 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1042 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1043 | psa_status_t status; |
| 1044 | size_t key_bits; |
| 1045 | size_t buffer_length; |
| 1046 | unsigned char *buffer = NULL; |
| 1047 | size_t output_length; |
| 1048 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1049 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1050 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1051 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1052 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1053 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1054 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1055 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1056 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1057 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1058 | /* Check if no key usage implication is done */ |
| 1059 | TEST_EQUAL( policy_usage, |
| 1060 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1061 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1062 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1063 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1064 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 1065 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1066 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1067 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1068 | status = psa_asymmetric_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1069 | NULL, 0, |
| 1070 | NULL, 0, |
| 1071 | buffer, buffer_length, |
| 1072 | &output_length ); |
| 1073 | if( policy_alg == exercise_alg && |
| 1074 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1075 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1076 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1077 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1078 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 1079 | if( buffer_length != 0 ) |
| 1080 | memset( buffer, 0, buffer_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1081 | status = psa_asymmetric_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1082 | buffer, buffer_length, |
| 1083 | NULL, 0, |
| 1084 | buffer, buffer_length, |
| 1085 | &output_length ); |
| 1086 | if( policy_alg == exercise_alg && |
| 1087 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1088 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1089 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1090 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1091 | |
| 1092 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1093 | /* |
| 1094 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1095 | * thus reset them as required. |
| 1096 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1097 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1098 | |
| 1099 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1100 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1101 | mbedtls_free( buffer ); |
| 1102 | } |
| 1103 | /* END_CASE */ |
| 1104 | |
| 1105 | /* BEGIN_CASE */ |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1106 | void asymmetric_signature_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1107 | int policy_alg, |
| 1108 | int key_type, |
| 1109 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1110 | int exercise_alg, |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1111 | int payload_length_arg, |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1112 | int expected_usage_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1113 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1114 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1115 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1116 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 1117 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1118 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1119 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 1120 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 1121 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 1122 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 1123 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 1124 | int compatible_alg = payload_length_arg > 0; |
| 1125 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1126 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1127 | size_t signature_length; |
| 1128 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1129 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1130 | in the expected usage flags. */ |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1131 | TEST_EQUAL( expected_usage, |
| 1132 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1133 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1134 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1135 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1136 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1137 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1138 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1139 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1140 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1141 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1142 | |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1143 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1144 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1145 | status = psa_sign_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1146 | payload, payload_length, |
| 1147 | signature, sizeof( signature ), |
| 1148 | &signature_length ); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1149 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1150 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1151 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1152 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1153 | |
| 1154 | memset( signature, 0, sizeof( signature ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1155 | status = psa_verify_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1156 | payload, payload_length, |
| 1157 | signature, sizeof( signature ) ); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1158 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1159 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1160 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1161 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1162 | |
Gilles Peskine | 8cb22c8 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 1163 | if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) && |
gabor-mezei-arm | 79df41d | 2021-06-28 14:53:49 +0200 | [diff] [blame] | 1164 | PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) ) |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1165 | { |
| 1166 | status = psa_sign_message( key, exercise_alg, |
| 1167 | payload, payload_length, |
| 1168 | signature, sizeof( signature ), |
| 1169 | &signature_length ); |
| 1170 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 ) |
| 1171 | PSA_ASSERT( status ); |
| 1172 | else |
| 1173 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1174 | |
| 1175 | memset( signature, 0, sizeof( signature ) ); |
| 1176 | status = psa_verify_message( key, exercise_alg, |
| 1177 | payload, payload_length, |
| 1178 | signature, sizeof( signature ) ); |
| 1179 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 ) |
| 1180 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 1181 | else |
| 1182 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1183 | } |
| 1184 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1185 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1186 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1187 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1188 | } |
| 1189 | /* END_CASE */ |
| 1190 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1191 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1192 | void derive_key_policy( int policy_usage, |
| 1193 | int policy_alg, |
| 1194 | int key_type, |
| 1195 | data_t *key_data, |
| 1196 | int exercise_alg ) |
| 1197 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1198 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1199 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1200 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1201 | psa_status_t status; |
| 1202 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1203 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1204 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1205 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1206 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1207 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1208 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1209 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1210 | &key ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1211 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1212 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 1213 | |
| 1214 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 1215 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1216 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1217 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 1218 | &operation, |
| 1219 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 1220 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1221 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1222 | |
| 1223 | status = psa_key_derivation_input_key( &operation, |
| 1224 | PSA_KEY_DERIVATION_INPUT_SECRET, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1225 | key ); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1226 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1227 | if( policy_alg == exercise_alg && |
| 1228 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1229 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1230 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1231 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1232 | |
| 1233 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1234 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1235 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1236 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1237 | } |
| 1238 | /* END_CASE */ |
| 1239 | |
| 1240 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1241 | void agreement_key_policy( int policy_usage, |
| 1242 | int policy_alg, |
| 1243 | int key_type_arg, |
| 1244 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1245 | int exercise_alg, |
| 1246 | int expected_status_arg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1247 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1248 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1249 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1250 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1251 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1252 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1253 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1254 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1255 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1256 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1257 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1258 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1259 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1260 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1261 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1262 | &key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1263 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1264 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1265 | status = mbedtls_test_psa_key_agreement_with_self( &operation, key ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1266 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1267 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1268 | |
| 1269 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1270 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1271 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1272 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1273 | } |
| 1274 | /* END_CASE */ |
| 1275 | |
| 1276 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1277 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 1278 | int usage_arg, int alg_arg, int alg2_arg ) |
| 1279 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1280 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1281 | psa_key_type_t key_type = key_type_arg; |
| 1282 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1283 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1284 | psa_key_usage_t usage = usage_arg; |
| 1285 | psa_algorithm_t alg = alg_arg; |
| 1286 | psa_algorithm_t alg2 = alg2_arg; |
| 1287 | |
| 1288 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1289 | |
| 1290 | psa_set_key_usage_flags( &attributes, usage ); |
| 1291 | psa_set_key_algorithm( &attributes, alg ); |
| 1292 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 1293 | psa_set_key_type( &attributes, key_type ); |
| 1294 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1295 | &key ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1296 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1297 | /* Update the usage flags to obtain implicit usage flags */ |
| 1298 | usage = mbedtls_test_update_key_usage_flags( usage ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1299 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1300 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 1301 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 1302 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 1303 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1304 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1305 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1306 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1307 | goto exit; |
| 1308 | |
| 1309 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1310 | /* |
| 1311 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1312 | * thus reset them as required. |
| 1313 | */ |
| 1314 | psa_reset_key_attributes( &got_attributes ); |
| 1315 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1316 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1317 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1318 | } |
| 1319 | /* END_CASE */ |
| 1320 | |
| 1321 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1322 | void raw_agreement_key_policy( int policy_usage, |
| 1323 | int policy_alg, |
| 1324 | int key_type_arg, |
| 1325 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1326 | int exercise_alg, |
| 1327 | int expected_status_arg ) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1328 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1329 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1330 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1331 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1332 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1333 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1334 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1335 | |
| 1336 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1337 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1338 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1339 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1340 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1341 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1342 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1343 | &key ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1344 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1345 | status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1346 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1347 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1348 | |
| 1349 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1350 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1351 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1352 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1353 | } |
| 1354 | /* END_CASE */ |
| 1355 | |
| 1356 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1357 | void copy_success( int source_usage_arg, |
| 1358 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1359 | int type_arg, data_t *material, |
| 1360 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1361 | int target_usage_arg, |
| 1362 | int target_alg_arg, int target_alg2_arg, |
| 1363 | int expected_usage_arg, |
| 1364 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1365 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1366 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1367 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1368 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 1369 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1370 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1371 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1372 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1373 | uint8_t *export_buffer = NULL; |
| 1374 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1375 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1376 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1377 | /* Prepare the source key. */ |
| 1378 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 1379 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1380 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1381 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1382 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 1383 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1384 | &source_key ) ); |
| 1385 | PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1386 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1387 | /* Prepare the target attributes. */ |
| 1388 | if( copy_attributes ) |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1389 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1390 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1391 | /* Set volatile lifetime to reset the key identifier to 0. */ |
| 1392 | psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE ); |
| 1393 | } |
| 1394 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1395 | if( target_usage_arg != -1 ) |
| 1396 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 1397 | if( target_alg_arg != -1 ) |
| 1398 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1399 | if( target_alg2_arg != -1 ) |
| 1400 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1401 | |
| 1402 | /* Copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1403 | PSA_ASSERT( psa_copy_key( source_key, |
| 1404 | &target_attributes, &target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1405 | |
| 1406 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1407 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1408 | |
| 1409 | /* Test that the target slot has the expected content and policy. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1410 | PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1411 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 1412 | psa_get_key_type( &target_attributes ) ); |
| 1413 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 1414 | psa_get_key_bits( &target_attributes ) ); |
| 1415 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 1416 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1417 | TEST_EQUAL( expected_alg2, |
| 1418 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1419 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 1420 | { |
| 1421 | size_t length; |
| 1422 | ASSERT_ALLOC( export_buffer, material->len ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1423 | PSA_ASSERT( psa_export_key( target_key, export_buffer, |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1424 | material->len, &length ) ); |
| 1425 | ASSERT_COMPARE( material->x, material->len, |
| 1426 | export_buffer, length ); |
| 1427 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1428 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1429 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1430 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1431 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) ) |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1432 | goto exit; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1433 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1434 | PSA_ASSERT( psa_destroy_key( target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1435 | |
| 1436 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1437 | /* |
| 1438 | * Source and target key attributes may have been returned by |
| 1439 | * psa_get_key_attributes() thus reset them as required. |
| 1440 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1441 | psa_reset_key_attributes( &source_attributes ); |
| 1442 | psa_reset_key_attributes( &target_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1443 | |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1444 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1445 | mbedtls_free( export_buffer ); |
| 1446 | } |
| 1447 | /* END_CASE */ |
| 1448 | |
| 1449 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1450 | void copy_fail( int source_usage_arg, |
| 1451 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1452 | int type_arg, data_t *material, |
| 1453 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1454 | int target_usage_arg, |
| 1455 | int target_alg_arg, int target_alg2_arg, |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 1456 | int target_id_arg, int target_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1457 | int expected_status_arg ) |
| 1458 | { |
| 1459 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1460 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1461 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1462 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 1463 | 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] | 1464 | |
| 1465 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1466 | |
| 1467 | /* Prepare the source key. */ |
| 1468 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 1469 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1470 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1471 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1472 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 1473 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1474 | &source_key ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1475 | |
| 1476 | /* Prepare the target attributes. */ |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 1477 | psa_set_key_id( &target_attributes, key_id ); |
| 1478 | psa_set_key_lifetime( &target_attributes, target_lifetime_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1479 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 1480 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 1481 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 1482 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1483 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1484 | |
| 1485 | /* Try to copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1486 | TEST_EQUAL( psa_copy_key( source_key, |
| 1487 | &target_attributes, &target_key ), |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1488 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1489 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1490 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1491 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1492 | exit: |
| 1493 | psa_reset_key_attributes( &source_attributes ); |
| 1494 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1495 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1496 | } |
| 1497 | /* END_CASE */ |
| 1498 | |
| 1499 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1500 | void hash_operation_init( ) |
| 1501 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1502 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1503 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1504 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1505 | * though it's OK by the C standard. We could test for this, but we'd need |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1506 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1507 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 1508 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 1509 | psa_hash_operation_t zero; |
| 1510 | |
| 1511 | memset( &zero, 0, sizeof( zero ) ); |
| 1512 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1513 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1514 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 1515 | PSA_ERROR_BAD_STATE ); |
| 1516 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 1517 | PSA_ERROR_BAD_STATE ); |
| 1518 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 1519 | PSA_ERROR_BAD_STATE ); |
| 1520 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1521 | /* A default hash operation should be abortable without error. */ |
| 1522 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 1523 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 1524 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1525 | } |
| 1526 | /* END_CASE */ |
| 1527 | |
| 1528 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1529 | void hash_setup( int alg_arg, |
| 1530 | int expected_status_arg ) |
| 1531 | { |
| 1532 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1533 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1534 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1535 | psa_status_t status; |
| 1536 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1537 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1538 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1539 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1540 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1541 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 1542 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 1543 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1544 | |
| 1545 | /* If setup failed, reproduce the failure, so as to |
| 1546 | * test the resulting state of the operation object. */ |
| 1547 | if( status != PSA_SUCCESS ) |
| 1548 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 1549 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1550 | /* Now the operation object should be reusable. */ |
| 1551 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 1552 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 1553 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1554 | #endif |
| 1555 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1556 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1557 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1558 | } |
| 1559 | /* END_CASE */ |
| 1560 | |
| 1561 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1562 | void hash_compute_fail( int alg_arg, data_t *input, |
| 1563 | int output_size_arg, int expected_status_arg ) |
| 1564 | { |
| 1565 | psa_algorithm_t alg = alg_arg; |
| 1566 | uint8_t *output = NULL; |
| 1567 | size_t output_size = output_size_arg; |
| 1568 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 1569 | psa_status_t expected_status = expected_status_arg; |
| 1570 | psa_status_t status; |
| 1571 | |
| 1572 | ASSERT_ALLOC( output, output_size ); |
| 1573 | |
| 1574 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1575 | |
| 1576 | status = psa_hash_compute( alg, input->x, input->len, |
| 1577 | output, output_size, &output_length ); |
| 1578 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1579 | TEST_LE_U( output_length, output_size ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1580 | |
| 1581 | exit: |
| 1582 | mbedtls_free( output ); |
| 1583 | PSA_DONE( ); |
| 1584 | } |
| 1585 | /* END_CASE */ |
| 1586 | |
| 1587 | /* BEGIN_CASE */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 1588 | void hash_compare_fail( int alg_arg, data_t *input, |
| 1589 | data_t *reference_hash, |
| 1590 | int expected_status_arg ) |
| 1591 | { |
| 1592 | psa_algorithm_t alg = alg_arg; |
| 1593 | psa_status_t expected_status = expected_status_arg; |
| 1594 | psa_status_t status; |
| 1595 | |
| 1596 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1597 | |
| 1598 | status = psa_hash_compare( alg, input->x, input->len, |
| 1599 | reference_hash->x, reference_hash->len ); |
| 1600 | TEST_EQUAL( status, expected_status ); |
| 1601 | |
| 1602 | exit: |
| 1603 | PSA_DONE( ); |
| 1604 | } |
| 1605 | /* END_CASE */ |
| 1606 | |
| 1607 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1608 | void hash_compute_compare( int alg_arg, data_t *input, |
| 1609 | data_t *expected_output ) |
| 1610 | { |
| 1611 | psa_algorithm_t alg = alg_arg; |
| 1612 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 1613 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 1614 | size_t i; |
| 1615 | |
| 1616 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1617 | |
| 1618 | /* Compute with tight buffer */ |
| 1619 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1620 | output, PSA_HASH_LENGTH( alg ), |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1621 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1622 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1623 | ASSERT_COMPARE( output, output_length, |
| 1624 | expected_output->x, expected_output->len ); |
| 1625 | |
| 1626 | /* Compute with larger buffer */ |
| 1627 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
| 1628 | output, sizeof( output ), |
| 1629 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1630 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1631 | ASSERT_COMPARE( output, output_length, |
| 1632 | expected_output->x, expected_output->len ); |
| 1633 | |
| 1634 | /* Compare with correct hash */ |
| 1635 | PSA_ASSERT( psa_hash_compare( alg, input->x, input->len, |
| 1636 | output, output_length ) ); |
| 1637 | |
| 1638 | /* Compare with trailing garbage */ |
| 1639 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 1640 | output, output_length + 1 ), |
| 1641 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1642 | |
| 1643 | /* Compare with truncated hash */ |
| 1644 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 1645 | output, output_length - 1 ), |
| 1646 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1647 | |
| 1648 | /* Compare with corrupted value */ |
| 1649 | for( i = 0; i < output_length; i++ ) |
| 1650 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 1651 | mbedtls_test_set_step( i ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1652 | output[i] ^= 1; |
| 1653 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 1654 | output, output_length ), |
| 1655 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1656 | output[i] ^= 1; |
| 1657 | } |
| 1658 | |
| 1659 | exit: |
| 1660 | PSA_DONE( ); |
| 1661 | } |
| 1662 | /* END_CASE */ |
| 1663 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 1664 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1665 | void hash_bad_order( ) |
| 1666 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1667 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1668 | unsigned char input[] = ""; |
| 1669 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1670 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1671 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 1672 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 1673 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1674 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1675 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1676 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1677 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1678 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1679 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1680 | /* Call setup twice in a row. */ |
| 1681 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1682 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1683 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 1684 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1685 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1686 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1687 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1688 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1689 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1690 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1691 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1692 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1693 | |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1694 | /* Check that update calls abort on error. */ |
| 1695 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 54f7351 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 1696 | operation.id = UINT_MAX; |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1697 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
| 1698 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
| 1699 | PSA_ERROR_BAD_STATE ); |
| 1700 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 1701 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1702 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 1703 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1704 | /* Call update 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_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1709 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1710 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1711 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1712 | /* Call verify without calling setup beforehand. */ |
| 1713 | TEST_EQUAL( psa_hash_verify( &operation, |
| 1714 | valid_hash, sizeof( valid_hash ) ), |
| 1715 | PSA_ERROR_BAD_STATE ); |
| 1716 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1717 | |
| 1718 | /* Call verify after finish. */ |
| 1719 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1720 | PSA_ASSERT( psa_hash_finish( &operation, |
| 1721 | hash, sizeof( hash ), &hash_len ) ); |
| 1722 | TEST_EQUAL( psa_hash_verify( &operation, |
| 1723 | valid_hash, sizeof( valid_hash ) ), |
| 1724 | PSA_ERROR_BAD_STATE ); |
| 1725 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1726 | |
| 1727 | /* Call verify twice in a row. */ |
| 1728 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1729 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1730 | PSA_ASSERT( psa_hash_verify( &operation, |
| 1731 | valid_hash, sizeof( valid_hash ) ) ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1732 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1733 | TEST_EQUAL( psa_hash_verify( &operation, |
| 1734 | valid_hash, sizeof( valid_hash ) ), |
| 1735 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1736 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1737 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1738 | |
| 1739 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1740 | TEST_EQUAL( psa_hash_finish( &operation, |
| 1741 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1742 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1743 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1744 | |
| 1745 | /* Call finish twice in a row. */ |
| 1746 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1747 | PSA_ASSERT( psa_hash_finish( &operation, |
| 1748 | hash, sizeof( hash ), &hash_len ) ); |
| 1749 | TEST_EQUAL( psa_hash_finish( &operation, |
| 1750 | hash, sizeof( hash ), &hash_len ), |
| 1751 | PSA_ERROR_BAD_STATE ); |
| 1752 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1753 | |
| 1754 | /* Call finish after calling verify. */ |
| 1755 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1756 | PSA_ASSERT( psa_hash_verify( &operation, |
| 1757 | valid_hash, sizeof( valid_hash ) ) ); |
| 1758 | TEST_EQUAL( psa_hash_finish( &operation, |
| 1759 | hash, sizeof( hash ), &hash_len ), |
| 1760 | PSA_ERROR_BAD_STATE ); |
| 1761 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1762 | |
| 1763 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1764 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1765 | } |
| 1766 | /* END_CASE */ |
| 1767 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 1768 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1769 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1770 | { |
| 1771 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1772 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 1773 | * appended to it */ |
| 1774 | unsigned char hash[] = { |
| 1775 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 1776 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 1777 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1778 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1779 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1780 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1781 | PSA_ASSERT( psa_crypto_init( ) ); |
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 smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1784 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1785 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1786 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1787 | PSA_ERROR_INVALID_SIGNATURE ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1788 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 1789 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1790 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1791 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1792 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1793 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1794 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1795 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1796 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1797 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1798 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1799 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1800 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 1801 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1802 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1803 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1804 | } |
| 1805 | /* END_CASE */ |
| 1806 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 1807 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 1808 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1809 | { |
| 1810 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 1811 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1812 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1813 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1814 | size_t hash_len; |
| 1815 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1816 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1817 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1818 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1819 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1820 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1821 | hash, expected_size - 1, &hash_len ), |
| 1822 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1823 | |
| 1824 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1825 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1826 | } |
| 1827 | /* END_CASE */ |
| 1828 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 1829 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1830 | void hash_clone_source_state( ) |
| 1831 | { |
| 1832 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 1833 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 1834 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 1835 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 1836 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 1837 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 1838 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 1839 | size_t hash_len; |
| 1840 | |
| 1841 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1842 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 1843 | |
| 1844 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 1845 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 1846 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 1847 | hash, sizeof( hash ), &hash_len ) ); |
| 1848 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 1849 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 1850 | |
| 1851 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 1852 | PSA_ERROR_BAD_STATE ); |
| 1853 | |
| 1854 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 1855 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 1856 | hash, sizeof( hash ), &hash_len ) ); |
| 1857 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 1858 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 1859 | hash, sizeof( hash ), &hash_len ) ); |
| 1860 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 1861 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 1862 | hash, sizeof( hash ), &hash_len ) ); |
| 1863 | |
| 1864 | exit: |
| 1865 | psa_hash_abort( &op_source ); |
| 1866 | psa_hash_abort( &op_init ); |
| 1867 | psa_hash_abort( &op_setup ); |
| 1868 | psa_hash_abort( &op_finished ); |
| 1869 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1870 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1871 | } |
| 1872 | /* END_CASE */ |
| 1873 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 1874 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1875 | void hash_clone_target_state( ) |
| 1876 | { |
| 1877 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 1878 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 1879 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 1880 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 1881 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 1882 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 1883 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 1884 | size_t hash_len; |
| 1885 | |
| 1886 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1887 | |
| 1888 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 1889 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 1890 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 1891 | hash, sizeof( hash ), &hash_len ) ); |
| 1892 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 1893 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 1894 | |
| 1895 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 1896 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 1897 | hash, sizeof( hash ), &hash_len ) ); |
| 1898 | |
| 1899 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 1900 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 1901 | PSA_ERROR_BAD_STATE ); |
| 1902 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 1903 | PSA_ERROR_BAD_STATE ); |
| 1904 | |
| 1905 | exit: |
| 1906 | psa_hash_abort( &op_target ); |
| 1907 | psa_hash_abort( &op_init ); |
| 1908 | psa_hash_abort( &op_setup ); |
| 1909 | psa_hash_abort( &op_finished ); |
| 1910 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1911 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1912 | } |
| 1913 | /* END_CASE */ |
| 1914 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1915 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1916 | void mac_operation_init( ) |
| 1917 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1918 | const uint8_t input[1] = { 0 }; |
| 1919 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1920 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1921 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1922 | * though it's OK by the C standard. We could test for this, but we'd need |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1923 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1924 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 1925 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 1926 | psa_mac_operation_t zero; |
| 1927 | |
| 1928 | memset( &zero, 0, sizeof( zero ) ); |
| 1929 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1930 | /* A freshly-initialized MAC operation should not be usable. */ |
| 1931 | TEST_EQUAL( psa_mac_update( &func, |
| 1932 | input, sizeof( input ) ), |
| 1933 | PSA_ERROR_BAD_STATE ); |
| 1934 | TEST_EQUAL( psa_mac_update( &init, |
| 1935 | input, sizeof( input ) ), |
| 1936 | PSA_ERROR_BAD_STATE ); |
| 1937 | TEST_EQUAL( psa_mac_update( &zero, |
| 1938 | input, sizeof( input ) ), |
| 1939 | PSA_ERROR_BAD_STATE ); |
| 1940 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1941 | /* A default MAC operation should be abortable without error. */ |
| 1942 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 1943 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 1944 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1945 | } |
| 1946 | /* END_CASE */ |
| 1947 | |
| 1948 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1949 | void mac_setup( int key_type_arg, |
| 1950 | data_t *key, |
| 1951 | int alg_arg, |
| 1952 | int expected_status_arg ) |
| 1953 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1954 | psa_key_type_t key_type = key_type_arg; |
| 1955 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1956 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1957 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1958 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 1959 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 1960 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 1961 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1962 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1963 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1964 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1965 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 1966 | &operation, &status ) ) |
| 1967 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1968 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1969 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1970 | /* The operation object should be reusable. */ |
| 1971 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 1972 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 1973 | smoke_test_key_data, |
| 1974 | sizeof( smoke_test_key_data ), |
| 1975 | KNOWN_SUPPORTED_MAC_ALG, |
| 1976 | &operation, &status ) ) |
| 1977 | goto exit; |
| 1978 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1979 | #endif |
| 1980 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1981 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1982 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1983 | } |
| 1984 | /* END_CASE */ |
| 1985 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 1986 | /* 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] | 1987 | void mac_bad_order( ) |
| 1988 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1989 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1990 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 1991 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1992 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1993 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 1994 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 1995 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1996 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1997 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 1998 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 1999 | size_t sign_mac_length = 0; |
| 2000 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 2001 | const uint8_t verify_mac[] = { |
| 2002 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 2003 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 2004 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 2005 | |
| 2006 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2007 | 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] | 2008 | psa_set_key_algorithm( &attributes, alg ); |
| 2009 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2010 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2011 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 2012 | &key ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2013 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2014 | /* Call update without calling setup beforehand. */ |
| 2015 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2016 | PSA_ERROR_BAD_STATE ); |
| 2017 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2018 | |
| 2019 | /* Call sign finish without calling setup beforehand. */ |
| 2020 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 2021 | &sign_mac_length), |
| 2022 | PSA_ERROR_BAD_STATE ); |
| 2023 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2024 | |
| 2025 | /* Call verify finish without calling setup beforehand. */ |
| 2026 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2027 | verify_mac, sizeof( verify_mac ) ), |
| 2028 | PSA_ERROR_BAD_STATE ); |
| 2029 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2030 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2031 | /* Call setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2032 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2033 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2034 | TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2035 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2036 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2037 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2038 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2039 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2040 | /* Call update after sign finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2041 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2042 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2043 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2044 | sign_mac, sizeof( sign_mac ), |
| 2045 | &sign_mac_length ) ); |
| 2046 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2047 | PSA_ERROR_BAD_STATE ); |
| 2048 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2049 | |
| 2050 | /* Call update after verify finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2051 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2052 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2053 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2054 | verify_mac, sizeof( verify_mac ) ) ); |
| 2055 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2056 | PSA_ERROR_BAD_STATE ); |
| 2057 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2058 | |
| 2059 | /* Call sign finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2060 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2061 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2062 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2063 | sign_mac, sizeof( sign_mac ), |
| 2064 | &sign_mac_length ) ); |
| 2065 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2066 | sign_mac, sizeof( sign_mac ), |
| 2067 | &sign_mac_length ), |
| 2068 | PSA_ERROR_BAD_STATE ); |
| 2069 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2070 | |
| 2071 | /* Call verify finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2072 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2073 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2074 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2075 | verify_mac, sizeof( verify_mac ) ) ); |
| 2076 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2077 | verify_mac, sizeof( verify_mac ) ), |
| 2078 | PSA_ERROR_BAD_STATE ); |
| 2079 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2080 | |
| 2081 | /* Setup sign but try verify. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2082 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2083 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2084 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2085 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2086 | verify_mac, sizeof( verify_mac ) ), |
| 2087 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2088 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2089 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2090 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2091 | |
| 2092 | /* Setup verify but try sign. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2093 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2094 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2095 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2096 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2097 | sign_mac, sizeof( sign_mac ), |
| 2098 | &sign_mac_length ), |
| 2099 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2100 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2101 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2102 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2103 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2104 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2105 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2106 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2107 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2108 | } |
| 2109 | /* END_CASE */ |
| 2110 | |
| 2111 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2112 | void mac_sign( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2113 | data_t *key_data, |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2114 | int alg_arg, |
| 2115 | data_t *input, |
| 2116 | data_t *expected_mac ) |
| 2117 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2118 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2119 | psa_key_type_t key_type = key_type_arg; |
| 2120 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2121 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2122 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2123 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2124 | size_t mac_buffer_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2125 | 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] | 2126 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2127 | const size_t output_sizes_to_test[] = { |
| 2128 | 0, |
| 2129 | 1, |
| 2130 | expected_mac->len - 1, |
| 2131 | expected_mac->len, |
| 2132 | expected_mac->len + 1, |
| 2133 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2134 | |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2135 | TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2136 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 3d404d6 | 2020-08-25 23:47:36 +0200 | [diff] [blame] | 2137 | TEST_ASSERT( expected_mac->len == mac_buffer_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2138 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2139 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2140 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2141 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2142 | psa_set_key_algorithm( &attributes, alg ); |
| 2143 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2144 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2145 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2146 | &key ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2147 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2148 | for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ ) |
| 2149 | { |
| 2150 | const size_t output_size = output_sizes_to_test[i]; |
| 2151 | psa_status_t expected_status = |
| 2152 | ( output_size >= expected_mac->len ? PSA_SUCCESS : |
| 2153 | PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2154 | |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2155 | mbedtls_test_set_step( output_size ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2156 | ASSERT_ALLOC( actual_mac, output_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2157 | |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2158 | /* Calculate the MAC, one-shot case. */ |
| 2159 | TEST_EQUAL( psa_mac_compute( key, alg, |
| 2160 | input->x, input->len, |
| 2161 | actual_mac, output_size, &mac_length ), |
| 2162 | expected_status ); |
| 2163 | if( expected_status == PSA_SUCCESS ) |
| 2164 | { |
| 2165 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 2166 | actual_mac, mac_length ); |
| 2167 | } |
| 2168 | |
| 2169 | if( output_size > 0 ) |
| 2170 | memset( actual_mac, 0, output_size ); |
| 2171 | |
| 2172 | /* Calculate the MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2173 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2174 | PSA_ASSERT( psa_mac_update( &operation, |
| 2175 | input->x, input->len ) ); |
| 2176 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2177 | actual_mac, output_size, |
| 2178 | &mac_length ), |
| 2179 | expected_status ); |
| 2180 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2181 | |
| 2182 | if( expected_status == PSA_SUCCESS ) |
| 2183 | { |
| 2184 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 2185 | actual_mac, mac_length ); |
| 2186 | } |
| 2187 | mbedtls_free( actual_mac ); |
| 2188 | actual_mac = NULL; |
| 2189 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2190 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2191 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2192 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2193 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2194 | PSA_DONE( ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2195 | mbedtls_free( actual_mac ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2196 | } |
| 2197 | /* END_CASE */ |
| 2198 | |
| 2199 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2200 | void mac_verify( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2201 | data_t *key_data, |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2202 | int alg_arg, |
| 2203 | data_t *input, |
| 2204 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2205 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2206 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2207 | psa_key_type_t key_type = key_type_arg; |
| 2208 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2209 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2210 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2211 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2212 | |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2213 | TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 2214 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2215 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2216 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2217 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2218 | psa_set_key_algorithm( &attributes, alg ); |
| 2219 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2220 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2221 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2222 | &key ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2223 | |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2224 | /* Verify correct MAC, one-shot case. */ |
| 2225 | PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len, |
| 2226 | expected_mac->x, expected_mac->len ) ); |
| 2227 | |
| 2228 | /* Verify correct MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2229 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2230 | PSA_ASSERT( psa_mac_update( &operation, |
| 2231 | input->x, input->len ) ); |
| 2232 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2233 | expected_mac->x, |
| 2234 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2235 | |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2236 | /* Test a MAC that's too short, one-shot case. */ |
| 2237 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 2238 | input->x, input->len, |
| 2239 | expected_mac->x, |
| 2240 | expected_mac->len - 1 ), |
| 2241 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2242 | |
| 2243 | /* Test a MAC that's too short, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2244 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2245 | PSA_ASSERT( psa_mac_update( &operation, |
| 2246 | input->x, input->len ) ); |
| 2247 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2248 | expected_mac->x, |
| 2249 | expected_mac->len - 1 ), |
| 2250 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2251 | |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2252 | /* Test a MAC that's too long, one-shot case. */ |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2253 | ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 ); |
| 2254 | memcpy( perturbed_mac, expected_mac->x, expected_mac->len ); |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2255 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 2256 | input->x, input->len, |
| 2257 | perturbed_mac, expected_mac->len + 1 ), |
| 2258 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2259 | |
| 2260 | /* Test a MAC that's too long, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2261 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2262 | PSA_ASSERT( psa_mac_update( &operation, |
| 2263 | input->x, input->len ) ); |
| 2264 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2265 | perturbed_mac, |
| 2266 | expected_mac->len + 1 ), |
| 2267 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2268 | |
| 2269 | /* Test changing one byte. */ |
| 2270 | for( size_t i = 0; i < expected_mac->len; i++ ) |
| 2271 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2272 | mbedtls_test_set_step( i ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2273 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2274 | |
| 2275 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 2276 | input->x, input->len, |
| 2277 | perturbed_mac, expected_mac->len ), |
| 2278 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2279 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2280 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2281 | PSA_ASSERT( psa_mac_update( &operation, |
| 2282 | input->x, input->len ) ); |
| 2283 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2284 | perturbed_mac, |
| 2285 | expected_mac->len ), |
| 2286 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2287 | perturbed_mac[i] ^= 1; |
| 2288 | } |
| 2289 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2290 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2291 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2292 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2293 | PSA_DONE( ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2294 | mbedtls_free( perturbed_mac ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2295 | } |
| 2296 | /* END_CASE */ |
| 2297 | |
| 2298 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2299 | void cipher_operation_init( ) |
| 2300 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2301 | const uint8_t input[1] = { 0 }; |
| 2302 | unsigned char output[1] = { 0 }; |
| 2303 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2304 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2305 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2306 | * though it's OK by the C standard. We could test for this, but we'd need |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 2307 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2308 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 2309 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 2310 | psa_cipher_operation_t zero; |
| 2311 | |
| 2312 | memset( &zero, 0, sizeof( zero ) ); |
| 2313 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2314 | /* A freshly-initialized cipher operation should not be usable. */ |
| 2315 | TEST_EQUAL( psa_cipher_update( &func, |
| 2316 | input, sizeof( input ), |
| 2317 | output, sizeof( output ), |
| 2318 | &output_length ), |
| 2319 | PSA_ERROR_BAD_STATE ); |
| 2320 | TEST_EQUAL( psa_cipher_update( &init, |
| 2321 | input, sizeof( input ), |
| 2322 | output, sizeof( output ), |
| 2323 | &output_length ), |
| 2324 | PSA_ERROR_BAD_STATE ); |
| 2325 | TEST_EQUAL( psa_cipher_update( &zero, |
| 2326 | input, sizeof( input ), |
| 2327 | output, sizeof( output ), |
| 2328 | &output_length ), |
| 2329 | PSA_ERROR_BAD_STATE ); |
| 2330 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2331 | /* A default cipher operation should be abortable without error. */ |
| 2332 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 2333 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 2334 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2335 | } |
| 2336 | /* END_CASE */ |
| 2337 | |
| 2338 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2339 | void cipher_setup( int key_type_arg, |
| 2340 | data_t *key, |
| 2341 | int alg_arg, |
| 2342 | int expected_status_arg ) |
| 2343 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2344 | psa_key_type_t key_type = key_type_arg; |
| 2345 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2346 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2347 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2348 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 2349 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2350 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2351 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2352 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2353 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2354 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2355 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 2356 | &operation, &status ) ) |
| 2357 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2358 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2359 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2360 | /* The operation object should be reusable. */ |
| 2361 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 2362 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 2363 | smoke_test_key_data, |
| 2364 | sizeof( smoke_test_key_data ), |
| 2365 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 2366 | &operation, &status ) ) |
| 2367 | goto exit; |
| 2368 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2369 | #endif |
| 2370 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2371 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2372 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2373 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2374 | } |
| 2375 | /* END_CASE */ |
| 2376 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2377 | /* 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] | 2378 | void cipher_bad_order( ) |
| 2379 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2380 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2381 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 2382 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2383 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2384 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2385 | 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] | 2386 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2387 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2388 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 2389 | const uint8_t text[] = { |
| 2390 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 2391 | 0xbb, 0xbb, 0xbb, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2392 | 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] | 2393 | size_t length = 0; |
| 2394 | |
| 2395 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2396 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 2397 | psa_set_key_algorithm( &attributes, alg ); |
| 2398 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2399 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 2400 | &key ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2401 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2402 | /* Call encrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2403 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2404 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2405 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2406 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2407 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2408 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2409 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2410 | |
| 2411 | /* Call decrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2412 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2413 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2414 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2415 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2416 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2417 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | ff8d52b | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2418 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2419 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2420 | /* Generate an IV without calling setup beforehand. */ |
| 2421 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2422 | buffer, sizeof( buffer ), |
| 2423 | &length ), |
| 2424 | PSA_ERROR_BAD_STATE ); |
| 2425 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2426 | |
| 2427 | /* Generate an IV twice in a row. */ |
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_generate_iv( &operation, |
| 2430 | buffer, sizeof( buffer ), |
| 2431 | &length ) ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2432 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2433 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2434 | buffer, sizeof( buffer ), |
| 2435 | &length ), |
| 2436 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2437 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2438 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2439 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2440 | |
| 2441 | /* Generate an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2442 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2443 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2444 | iv, sizeof( iv ) ) ); |
| 2445 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2446 | buffer, sizeof( buffer ), |
| 2447 | &length ), |
| 2448 | PSA_ERROR_BAD_STATE ); |
| 2449 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2450 | |
| 2451 | /* Set an IV without calling setup beforehand. */ |
| 2452 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2453 | iv, sizeof( iv ) ), |
| 2454 | PSA_ERROR_BAD_STATE ); |
| 2455 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2456 | |
| 2457 | /* Set an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2458 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2459 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2460 | iv, sizeof( iv ) ) ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2461 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2462 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2463 | iv, sizeof( iv ) ), |
| 2464 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2465 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2466 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2467 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2468 | |
| 2469 | /* Set an IV after it's already generated. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2470 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2471 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 2472 | buffer, sizeof( buffer ), |
| 2473 | &length ) ); |
| 2474 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2475 | iv, sizeof( iv ) ), |
| 2476 | PSA_ERROR_BAD_STATE ); |
| 2477 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2478 | |
| 2479 | /* Call update without calling setup beforehand. */ |
| 2480 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2481 | text, sizeof( text ), |
| 2482 | buffer, sizeof( buffer ), |
| 2483 | &length ), |
| 2484 | PSA_ERROR_BAD_STATE ); |
| 2485 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2486 | |
| 2487 | /* Call update without an IV where an IV is required. */ |
Dave Rodgman | 33b58ee | 2021-06-23 12:48:52 +0100 | [diff] [blame] | 2488 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2489 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2490 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2491 | text, sizeof( text ), |
| 2492 | buffer, sizeof( buffer ), |
| 2493 | &length ), |
| 2494 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2495 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2496 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2497 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2498 | |
| 2499 | /* Call update after finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2500 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2501 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2502 | iv, sizeof( iv ) ) ); |
| 2503 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2504 | buffer, sizeof( buffer ), &length ) ); |
| 2505 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2506 | text, sizeof( text ), |
| 2507 | buffer, sizeof( buffer ), |
| 2508 | &length ), |
| 2509 | PSA_ERROR_BAD_STATE ); |
| 2510 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2511 | |
| 2512 | /* Call finish without calling setup beforehand. */ |
| 2513 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2514 | buffer, sizeof( buffer ), &length ), |
| 2515 | PSA_ERROR_BAD_STATE ); |
| 2516 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2517 | |
| 2518 | /* Call finish without an IV where an IV is required. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2519 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2520 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 2521 | * for cipher modes with padding. */ |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2522 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2523 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2524 | buffer, sizeof( buffer ), &length ), |
| 2525 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2526 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2527 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 34b147d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2528 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2529 | |
| 2530 | /* Call finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2531 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2532 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2533 | iv, sizeof( iv ) ) ); |
| 2534 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2535 | buffer, sizeof( buffer ), &length ) ); |
| 2536 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2537 | buffer, sizeof( buffer ), &length ), |
| 2538 | PSA_ERROR_BAD_STATE ); |
| 2539 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2540 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2541 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2542 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2543 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2544 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2545 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2546 | } |
| 2547 | /* END_CASE */ |
| 2548 | |
| 2549 | /* BEGIN_CASE */ |
gabor-mezei-arm | 43611b0 | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 2550 | void cipher_encrypt_fail( int alg_arg, |
| 2551 | int key_type_arg, |
| 2552 | data_t *key_data, |
| 2553 | data_t *input, |
| 2554 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2555 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2556 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2557 | psa_status_t status; |
| 2558 | psa_key_type_t key_type = key_type_arg; |
| 2559 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2560 | psa_status_t expected_status = expected_status_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2561 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2562 | size_t output_buffer_size = 0; |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2563 | size_t output_length = 0; |
| 2564 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2565 | |
| 2566 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 2567 | { |
| 2568 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2569 | |
| 2570 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2571 | psa_set_key_algorithm( &attributes, alg ); |
| 2572 | psa_set_key_type( &attributes, key_type ); |
| 2573 | |
| 2574 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 2575 | input->len ); |
| 2576 | ASSERT_ALLOC( output, output_buffer_size ); |
| 2577 | |
| 2578 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2579 | &key ) ); |
| 2580 | } |
| 2581 | |
| 2582 | status = psa_cipher_encrypt( key, alg, input->x, input->len, output, |
| 2583 | output_buffer_size, &output_length ); |
| 2584 | |
| 2585 | TEST_EQUAL( status, expected_status ); |
| 2586 | |
| 2587 | exit: |
| 2588 | mbedtls_free( output ); |
| 2589 | psa_destroy_key( key ); |
| 2590 | PSA_DONE( ); |
| 2591 | } |
| 2592 | /* END_CASE */ |
| 2593 | |
| 2594 | /* BEGIN_CASE */ |
Gilles Peskine | 69d9817 | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 2595 | void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data, |
| 2596 | data_t *plaintext, data_t *ciphertext ) |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2597 | { |
| 2598 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2599 | psa_key_type_t key_type = key_type_arg; |
| 2600 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 33c6968 | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 2601 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 2602 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2603 | unsigned char *output = NULL; |
| 2604 | size_t output_buffer_size = 0; |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2605 | size_t output_length, length; |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2606 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2607 | |
| 2608 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2609 | |
Gilles Peskine | 5f50420 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 2610 | /* Validate size macros */ |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2611 | TEST_LE_U( ciphertext->len, |
| 2612 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) ); |
| 2613 | TEST_LE_U( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ), |
Gilles Peskine | 69d9817 | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 2614 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2615 | TEST_LE_U( plaintext->len, |
| 2616 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) ); |
| 2617 | TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ), |
| 2618 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) ); |
Gilles Peskine | 69d9817 | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 2619 | |
Gilles Peskine | 5f50420 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 2620 | |
| 2621 | /* Set up key and output buffer */ |
Gilles Peskine | 69d9817 | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 2622 | psa_set_key_usage_flags( &attributes, |
| 2623 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2624 | psa_set_key_algorithm( &attributes, alg ); |
| 2625 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 5f50420 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 2626 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2627 | &key ) ); |
Gilles Peskine | 69d9817 | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 2628 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 2629 | plaintext->len ); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2630 | ASSERT_ALLOC( output, output_buffer_size ); |
| 2631 | |
Gilles Peskine | 5f50420 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 2632 | /* set_iv() is not allowed */ |
Ronald Cron | 33c6968 | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 2633 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 2634 | TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ), |
| 2635 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 69d9817 | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 2636 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 2637 | TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ), |
| 2638 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 5f50420 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 2639 | |
| 2640 | /* generate_iv() is not allowed */ |
Ronald Cron | 33c6968 | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 2641 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 2642 | TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ), |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2643 | &length ), |
Ronald Cron | 33c6968 | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 2644 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 69d9817 | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 2645 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 2646 | TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ), |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2647 | &length ), |
Gilles Peskine | 69d9817 | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 2648 | PSA_ERROR_BAD_STATE ); |
Ronald Cron | 33c6968 | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 2649 | |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2650 | /* Multipart encryption */ |
| 2651 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 2652 | output_length = 0; |
| 2653 | length = ~0; |
| 2654 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2655 | plaintext->x, plaintext->len, |
| 2656 | output, output_buffer_size, |
| 2657 | &length ) ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2658 | TEST_LE_U( length, output_buffer_size ); |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2659 | output_length += length; |
| 2660 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2661 | output + output_length, |
| 2662 | output_buffer_size - output_length, |
| 2663 | &length ) ); |
| 2664 | output_length += length; |
| 2665 | ASSERT_COMPARE( ciphertext->x, ciphertext->len, |
| 2666 | output, output_length ); |
| 2667 | |
| 2668 | /* Multipart encryption */ |
| 2669 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 2670 | output_length = 0; |
| 2671 | length = ~0; |
| 2672 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2673 | ciphertext->x, ciphertext->len, |
| 2674 | output, output_buffer_size, |
| 2675 | &length ) ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2676 | TEST_LE_U( length, output_buffer_size ); |
Gilles Peskine | 4da5a85 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 2677 | output_length += length; |
| 2678 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2679 | output + output_length, |
| 2680 | output_buffer_size - output_length, |
| 2681 | &length ) ); |
| 2682 | output_length += length; |
| 2683 | ASSERT_COMPARE( plaintext->x, plaintext->len, |
| 2684 | output, output_length ); |
| 2685 | |
Gilles Peskine | 5f50420 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 2686 | /* One-shot encryption */ |
Gilles Peskine | 69d9817 | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 2687 | output_length = ~0; |
| 2688 | PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len, |
| 2689 | output, output_buffer_size, |
| 2690 | &output_length ) ); |
| 2691 | ASSERT_COMPARE( ciphertext->x, ciphertext->len, |
| 2692 | output, output_length ); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2693 | |
Gilles Peskine | 69d9817 | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 2694 | /* One-shot decryption */ |
| 2695 | output_length = ~0; |
| 2696 | PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len, |
| 2697 | output, output_buffer_size, |
| 2698 | &output_length ) ); |
| 2699 | ASSERT_COMPARE( plaintext->x, plaintext->len, |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2700 | output, output_length ); |
Gilles Peskine | 5f50420 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 2701 | |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2702 | exit: |
| 2703 | mbedtls_free( output ); |
Gilles Peskine | 5f50420 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 2704 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2705 | psa_destroy_key( key ); |
| 2706 | PSA_DONE( ); |
| 2707 | } |
| 2708 | /* END_CASE */ |
| 2709 | |
| 2710 | /* BEGIN_CASE */ |
Paul Elliott | ed33ef1 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 2711 | void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data ) |
| 2712 | { |
| 2713 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2714 | psa_algorithm_t alg = alg_arg; |
| 2715 | psa_key_type_t key_type = key_type_arg; |
| 2716 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2717 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 2718 | psa_status_t status; |
| 2719 | |
| 2720 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2721 | |
| 2722 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2723 | psa_set_key_algorithm( &attributes, alg ); |
| 2724 | psa_set_key_type( &attributes, key_type ); |
| 2725 | |
| 2726 | /* Usage of either of these two size macros would cause divide by zero |
| 2727 | * with incorrect key types previously. Input length should be irrelevant |
| 2728 | * here. */ |
| 2729 | TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ), |
| 2730 | 0 ); |
| 2731 | TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 ); |
| 2732 | |
| 2733 | |
| 2734 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2735 | &key ) ); |
| 2736 | |
| 2737 | /* Should fail due to invalid alg type (to support invalid key type). |
| 2738 | * Encrypt or decrypt will end up in the same place. */ |
| 2739 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 2740 | |
| 2741 | TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT ); |
| 2742 | |
| 2743 | exit: |
| 2744 | psa_cipher_abort( &operation ); |
| 2745 | psa_destroy_key( key ); |
| 2746 | PSA_DONE( ); |
| 2747 | } |
| 2748 | /* END_CASE */ |
| 2749 | |
| 2750 | /* BEGIN_CASE */ |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2751 | void cipher_encrypt_validation( int alg_arg, |
| 2752 | int key_type_arg, |
| 2753 | data_t *key_data, |
| 2754 | data_t *input ) |
| 2755 | { |
| 2756 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2757 | psa_key_type_t key_type = key_type_arg; |
| 2758 | psa_algorithm_t alg = alg_arg; |
| 2759 | size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg ); |
| 2760 | unsigned char *output1 = NULL; |
| 2761 | size_t output1_buffer_size = 0; |
| 2762 | size_t output1_length = 0; |
| 2763 | unsigned char *output2 = NULL; |
| 2764 | size_t output2_buffer_size = 0; |
| 2765 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2766 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2767 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2768 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2769 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2770 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2771 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2772 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2773 | psa_set_key_algorithm( &attributes, alg ); |
| 2774 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2775 | |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2776 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 2777 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 2778 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 2779 | ASSERT_ALLOC( output1, output1_buffer_size ); |
| 2780 | ASSERT_ALLOC( output2, output2_buffer_size ); |
| 2781 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2782 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2783 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2784 | |
gabor-mezei-arm | 7aa1efd | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 2785 | /* The one-shot cipher encryption uses generated iv so validating |
| 2786 | the output is not possible. Validating with multipart encryption. */ |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2787 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1, |
| 2788 | output1_buffer_size, &output1_length ) ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2789 | TEST_LE_U( output1_length, |
| 2790 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 2791 | TEST_LE_U( output1_length, |
| 2792 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2793 | |
| 2794 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 2795 | PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2796 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2797 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2798 | input->x, input->len, |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2799 | output2, output2_buffer_size, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2800 | &function_output_length ) ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2801 | TEST_LE_U( function_output_length, |
| 2802 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 2803 | TEST_LE_U( function_output_length, |
| 2804 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2805 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2806 | |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2807 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2808 | output2 + output2_length, |
| 2809 | output2_buffer_size - output2_length, |
| 2810 | &function_output_length ) ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2811 | TEST_LE_U( function_output_length, |
| 2812 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 2813 | TEST_LE_U( function_output_length, |
| 2814 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2815 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2816 | |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2817 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2818 | ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size, |
| 2819 | output2, output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2820 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2821 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2822 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2823 | mbedtls_free( output1 ); |
| 2824 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2825 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2826 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2827 | } |
| 2828 | /* END_CASE */ |
| 2829 | |
| 2830 | /* BEGIN_CASE */ |
| 2831 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2832 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2833 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2834 | int first_part_size_arg, |
| 2835 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2836 | data_t *expected_output, |
| 2837 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2838 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2839 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2840 | psa_key_type_t key_type = key_type_arg; |
| 2841 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2842 | psa_status_t status; |
| 2843 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2844 | size_t first_part_size = first_part_size_arg; |
| 2845 | size_t output1_length = output1_length_arg; |
| 2846 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2847 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2848 | size_t output_buffer_size = 0; |
| 2849 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2850 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2851 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2852 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2853 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2854 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2855 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2856 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2857 | psa_set_key_algorithm( &attributes, alg ); |
| 2858 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2859 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2860 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2861 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2862 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2863 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2864 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2865 | if( iv->len > 0 ) |
| 2866 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 2867 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2868 | } |
| 2869 | |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2870 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 2871 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2872 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2873 | |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2874 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2875 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 2876 | output, output_buffer_size, |
| 2877 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2878 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2879 | TEST_LE_U( function_output_length, |
| 2880 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 2881 | TEST_LE_U( function_output_length, |
| 2882 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2883 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2884 | |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2885 | if( first_part_size < input->len ) |
| 2886 | { |
| 2887 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2888 | input->x + first_part_size, |
| 2889 | input->len - first_part_size, |
| 2890 | ( output_buffer_size == 0 ? NULL : |
| 2891 | output + total_output_length ), |
| 2892 | output_buffer_size - total_output_length, |
| 2893 | &function_output_length ) ); |
| 2894 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2895 | TEST_LE_U( function_output_length, |
| 2896 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 2897 | alg, |
| 2898 | input->len - first_part_size ) ); |
| 2899 | TEST_LE_U( function_output_length, |
| 2900 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2901 | total_output_length += function_output_length; |
| 2902 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2903 | |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2904 | status = psa_cipher_finish( &operation, |
| 2905 | ( output_buffer_size == 0 ? NULL : |
| 2906 | output + total_output_length ), |
| 2907 | output_buffer_size - total_output_length, |
| 2908 | &function_output_length ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2909 | TEST_LE_U( function_output_length, |
| 2910 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 2911 | TEST_LE_U( function_output_length, |
| 2912 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2913 | total_output_length += function_output_length; |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2914 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2915 | |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2916 | if( expected_status == PSA_SUCCESS ) |
| 2917 | { |
| 2918 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2919 | |
| 2920 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2921 | output, total_output_length ); |
| 2922 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2923 | |
| 2924 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2925 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2926 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2927 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2928 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2929 | } |
| 2930 | /* END_CASE */ |
| 2931 | |
| 2932 | /* BEGIN_CASE */ |
| 2933 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2934 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2935 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2936 | int first_part_size_arg, |
| 2937 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2938 | data_t *expected_output, |
| 2939 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2940 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2941 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2942 | psa_key_type_t key_type = key_type_arg; |
| 2943 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2944 | psa_status_t status; |
| 2945 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2946 | size_t first_part_size = first_part_size_arg; |
| 2947 | size_t output1_length = output1_length_arg; |
| 2948 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2949 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2950 | size_t output_buffer_size = 0; |
| 2951 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2952 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2953 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2954 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2955 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2956 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2957 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2958 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 2959 | psa_set_key_algorithm( &attributes, alg ); |
| 2960 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2961 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2962 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2963 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2964 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2965 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2966 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 2967 | if( iv->len > 0 ) |
| 2968 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 2969 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2970 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2971 | |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2972 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 2973 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2974 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2975 | |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2976 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2977 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2978 | input->x, first_part_size, |
| 2979 | output, output_buffer_size, |
| 2980 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2981 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2982 | TEST_LE_U( function_output_length, |
| 2983 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 2984 | TEST_LE_U( function_output_length, |
| 2985 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2986 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2987 | |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2988 | if( first_part_size < input->len ) |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 2989 | { |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2990 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2991 | input->x + first_part_size, |
| 2992 | input->len - first_part_size, |
| 2993 | ( output_buffer_size == 0 ? NULL : |
| 2994 | output + total_output_length ), |
| 2995 | output_buffer_size - total_output_length, |
| 2996 | &function_output_length ) ); |
| 2997 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2998 | TEST_LE_U( function_output_length, |
| 2999 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3000 | alg, |
| 3001 | input->len - first_part_size ) ); |
| 3002 | TEST_LE_U( function_output_length, |
| 3003 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3004 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3005 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3006 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3007 | status = psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 3008 | ( output_buffer_size == 0 ? NULL : |
| 3009 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3010 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3011 | &function_output_length ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3012 | TEST_LE_U( function_output_length, |
| 3013 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3014 | TEST_LE_U( function_output_length, |
| 3015 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3016 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3017 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3018 | |
| 3019 | if( expected_status == PSA_SUCCESS ) |
| 3020 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3021 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | 9ac4847 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3022 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3023 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3024 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3025 | } |
| 3026 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3027 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3028 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3029 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3030 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3031 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3032 | } |
| 3033 | /* END_CASE */ |
| 3034 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3035 | /* BEGIN_CASE */ |
gabor-mezei-arm | 43611b0 | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 3036 | void cipher_decrypt_fail( int alg_arg, |
| 3037 | int key_type_arg, |
| 3038 | data_t *key_data, |
| 3039 | data_t *iv, |
| 3040 | data_t *input_arg, |
| 3041 | int expected_status_arg ) |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3042 | { |
| 3043 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3044 | psa_status_t status; |
| 3045 | psa_key_type_t key_type = key_type_arg; |
| 3046 | psa_algorithm_t alg = alg_arg; |
| 3047 | psa_status_t expected_status = expected_status_arg; |
| 3048 | unsigned char *input = NULL; |
| 3049 | size_t input_buffer_size = 0; |
| 3050 | unsigned char *output = NULL; |
| 3051 | size_t output_buffer_size = 0; |
| 3052 | size_t output_length = 0; |
| 3053 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3054 | |
| 3055 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 3056 | { |
| 3057 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3058 | |
| 3059 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3060 | psa_set_key_algorithm( &attributes, alg ); |
| 3061 | psa_set_key_type( &attributes, key_type ); |
| 3062 | |
| 3063 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3064 | &key ) ); |
| 3065 | } |
| 3066 | |
| 3067 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 3068 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 3069 | if ( input_buffer_size > 0 ) |
| 3070 | { |
| 3071 | ASSERT_ALLOC( input, input_buffer_size ); |
| 3072 | memcpy( input, iv->x, iv->len ); |
| 3073 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 3074 | } |
| 3075 | |
| 3076 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 3077 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3078 | |
| 3079 | status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 3080 | output_buffer_size, &output_length ); |
| 3081 | TEST_EQUAL( status, expected_status ); |
| 3082 | |
| 3083 | exit: |
| 3084 | mbedtls_free( input ); |
| 3085 | mbedtls_free( output ); |
| 3086 | psa_destroy_key( key ); |
| 3087 | PSA_DONE( ); |
| 3088 | } |
| 3089 | /* END_CASE */ |
| 3090 | |
| 3091 | /* BEGIN_CASE */ |
| 3092 | void cipher_decrypt( int alg_arg, |
| 3093 | int key_type_arg, |
| 3094 | data_t *key_data, |
| 3095 | data_t *iv, |
| 3096 | data_t *input_arg, |
| 3097 | data_t *expected_output ) |
| 3098 | { |
| 3099 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3100 | psa_key_type_t key_type = key_type_arg; |
| 3101 | psa_algorithm_t alg = alg_arg; |
| 3102 | unsigned char *input = NULL; |
| 3103 | size_t input_buffer_size = 0; |
| 3104 | unsigned char *output = NULL; |
| 3105 | size_t output_buffer_size = 0; |
| 3106 | size_t output_length = 0; |
| 3107 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3108 | |
| 3109 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3110 | |
| 3111 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3112 | psa_set_key_algorithm( &attributes, alg ); |
| 3113 | psa_set_key_type( &attributes, key_type ); |
| 3114 | |
| 3115 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 3116 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 3117 | if ( input_buffer_size > 0 ) |
| 3118 | { |
| 3119 | ASSERT_ALLOC( input, input_buffer_size ); |
| 3120 | memcpy( input, iv->x, iv->len ); |
| 3121 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 3122 | } |
| 3123 | |
| 3124 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 3125 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3126 | |
| 3127 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3128 | &key ) ); |
| 3129 | |
| 3130 | PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 3131 | output_buffer_size, &output_length ) ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3132 | TEST_LE_U( output_length, |
| 3133 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) ); |
| 3134 | TEST_LE_U( output_length, |
| 3135 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) ); |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3136 | |
| 3137 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3138 | output, output_length ); |
| 3139 | exit: |
| 3140 | mbedtls_free( input ); |
| 3141 | mbedtls_free( output ); |
| 3142 | psa_destroy_key( key ); |
| 3143 | PSA_DONE( ); |
| 3144 | } |
| 3145 | /* END_CASE */ |
| 3146 | |
| 3147 | /* BEGIN_CASE */ |
| 3148 | void cipher_verify_output( int alg_arg, |
| 3149 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3150 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3151 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3152 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3153 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3154 | psa_key_type_t key_type = key_type_arg; |
| 3155 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3156 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3157 | size_t output1_size = 0; |
| 3158 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3159 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3160 | size_t output2_size = 0; |
| 3161 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3162 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3163 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3164 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3165 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3166 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3167 | psa_set_key_algorithm( &attributes, alg ); |
| 3168 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3169 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3170 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3171 | &key ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3172 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3173 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3174 | |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3175 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, |
| 3176 | output1, output1_size, |
| 3177 | &output1_length ) ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3178 | TEST_LE_U( output1_length, |
| 3179 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 3180 | TEST_LE_U( output1_length, |
| 3181 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3182 | |
| 3183 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3184 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3185 | |
gabor-mezei-arm | d086e6e | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3186 | PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length, |
| 3187 | output2, output2_size, |
| 3188 | &output2_length ) ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3189 | TEST_LE_U( output2_length, |
| 3190 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 3191 | TEST_LE_U( output2_length, |
| 3192 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3193 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3194 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3195 | |
| 3196 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3197 | mbedtls_free( output1 ); |
| 3198 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3199 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3200 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3201 | } |
| 3202 | /* END_CASE */ |
| 3203 | |
| 3204 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3205 | void cipher_verify_output_multipart( int alg_arg, |
| 3206 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3207 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3208 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3209 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3210 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3211 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3212 | psa_key_type_t key_type = key_type_arg; |
| 3213 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3214 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3215 | unsigned char iv[16] = {0}; |
| 3216 | size_t iv_size = 16; |
| 3217 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3218 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3219 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3220 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3221 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3222 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3223 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3224 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3225 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3226 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3227 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3228 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3229 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3230 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3231 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3232 | psa_set_key_algorithm( &attributes, alg ); |
| 3233 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3234 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3235 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3236 | &key ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3237 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3238 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 3239 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3240 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3241 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3242 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3243 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3244 | iv, iv_size, |
| 3245 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3246 | } |
| 3247 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3248 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3249 | TEST_LE_U( output1_buffer_size, |
| 3250 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3251 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3252 | |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3253 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3254 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3255 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 3256 | output1, output1_buffer_size, |
| 3257 | &function_output_length ) ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3258 | TEST_LE_U( function_output_length, |
| 3259 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3260 | TEST_LE_U( function_output_length, |
| 3261 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3262 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3263 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3264 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 3265 | input->x + first_part_size, |
| 3266 | input->len - first_part_size, |
| 3267 | output1, output1_buffer_size, |
| 3268 | &function_output_length ) ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3269 | TEST_LE_U( function_output_length, |
| 3270 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3271 | alg, |
| 3272 | input->len - first_part_size ) ); |
| 3273 | TEST_LE_U( function_output_length, |
| 3274 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3275 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3276 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3277 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 3278 | output1 + output1_length, |
| 3279 | output1_buffer_size - output1_length, |
| 3280 | &function_output_length ) ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3281 | TEST_LE_U( function_output_length, |
| 3282 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3283 | TEST_LE_U( function_output_length, |
| 3284 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3285 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3286 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3287 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3288 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3289 | output2_buffer_size = output1_length; |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3290 | TEST_LE_U( output2_buffer_size, |
| 3291 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 3292 | TEST_LE_U( output2_buffer_size, |
| 3293 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3294 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3295 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3296 | if( iv_length > 0 ) |
| 3297 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3298 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3299 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3300 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3301 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3302 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 3303 | output2, output2_buffer_size, |
| 3304 | &function_output_length ) ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3305 | TEST_LE_U( function_output_length, |
| 3306 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3307 | TEST_LE_U( function_output_length, |
| 3308 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3309 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3310 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3311 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 3312 | output1 + first_part_size, |
| 3313 | output1_length - first_part_size, |
| 3314 | output2, output2_buffer_size, |
| 3315 | &function_output_length ) ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3316 | TEST_LE_U( function_output_length, |
| 3317 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3318 | alg, |
| 3319 | output1_length - first_part_size ) ); |
| 3320 | TEST_LE_U( function_output_length, |
| 3321 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3322 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3323 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3324 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3325 | output2 + output2_length, |
| 3326 | output2_buffer_size - output2_length, |
| 3327 | &function_output_length ) ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3328 | TEST_LE_U( function_output_length, |
| 3329 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3330 | TEST_LE_U( function_output_length, |
| 3331 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3332 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3333 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3334 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3335 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3336 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3337 | |
| 3338 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3339 | psa_cipher_abort( &operation1 ); |
| 3340 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3341 | mbedtls_free( output1 ); |
| 3342 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3343 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3344 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3345 | } |
| 3346 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 3347 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3348 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3349 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3350 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3351 | data_t *nonce, |
| 3352 | data_t *additional_data, |
| 3353 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3354 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3355 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3356 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3357 | psa_key_type_t key_type = key_type_arg; |
| 3358 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3359 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3360 | unsigned char *output_data = NULL; |
| 3361 | size_t output_size = 0; |
| 3362 | size_t output_length = 0; |
| 3363 | unsigned char *output_data2 = NULL; |
| 3364 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 3365 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3366 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3367 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3368 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3369 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3370 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3371 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3372 | psa_set_key_algorithm( &attributes, alg ); |
| 3373 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3374 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3375 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3376 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3377 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3378 | key_bits = psa_get_key_bits( &attributes ); |
| 3379 | |
| 3380 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3381 | alg ); |
| 3382 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3383 | * should be exact. */ |
| 3384 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 3385 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 3386 | { |
| 3387 | TEST_EQUAL( output_size, |
| 3388 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3389 | TEST_ASSERT( output_size <= |
| 3390 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3391 | } |
| 3392 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3393 | |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 3394 | status = psa_aead_encrypt( key, alg, |
| 3395 | nonce->x, nonce->len, |
| 3396 | additional_data->x, |
| 3397 | additional_data->len, |
| 3398 | input_data->x, input_data->len, |
| 3399 | output_data, output_size, |
| 3400 | &output_length ); |
| 3401 | |
| 3402 | /* If the operation is not supported, just skip and not fail in case the |
| 3403 | * encryption involves a common limitation of cryptography hardwares and |
| 3404 | * an alternative implementation. */ |
| 3405 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 3406 | { |
| 3407 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3408 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 3409 | } |
| 3410 | |
| 3411 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3412 | |
| 3413 | if( PSA_SUCCESS == expected_result ) |
| 3414 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3415 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3416 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3417 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3418 | * should be exact. */ |
| 3419 | TEST_EQUAL( input_data->len, |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3420 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) ); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3421 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3422 | TEST_ASSERT( input_data->len <= |
| 3423 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) ); |
| 3424 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3425 | TEST_EQUAL( psa_aead_decrypt( key, alg, |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3426 | nonce->x, nonce->len, |
| 3427 | additional_data->x, |
| 3428 | additional_data->len, |
| 3429 | output_data, output_length, |
| 3430 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3431 | &output_length2 ), |
| 3432 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3433 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3434 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 3435 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3436 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3437 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3438 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3439 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3440 | mbedtls_free( output_data ); |
| 3441 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3442 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3443 | } |
| 3444 | /* END_CASE */ |
| 3445 | |
| 3446 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3447 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 3448 | int alg_arg, |
| 3449 | data_t *nonce, |
| 3450 | data_t *additional_data, |
| 3451 | data_t *input_data, |
| 3452 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3453 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3454 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3455 | psa_key_type_t key_type = key_type_arg; |
| 3456 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3457 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3458 | unsigned char *output_data = NULL; |
| 3459 | size_t output_size = 0; |
| 3460 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3461 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3462 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3463 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3464 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3465 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3466 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3467 | psa_set_key_algorithm( &attributes, alg ); |
| 3468 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3469 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3470 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3471 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3472 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3473 | key_bits = psa_get_key_bits( &attributes ); |
| 3474 | |
| 3475 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3476 | alg ); |
| 3477 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3478 | * should be exact. */ |
| 3479 | TEST_EQUAL( output_size, |
| 3480 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3481 | TEST_ASSERT( output_size <= |
| 3482 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3483 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3484 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3485 | status = psa_aead_encrypt( key, alg, |
| 3486 | nonce->x, nonce->len, |
| 3487 | additional_data->x, additional_data->len, |
| 3488 | input_data->x, input_data->len, |
| 3489 | output_data, output_size, |
| 3490 | &output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3491 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3492 | /* If the operation is not supported, just skip and not fail in case the |
| 3493 | * encryption involves a common limitation of cryptography hardwares and |
| 3494 | * an alternative implementation. */ |
| 3495 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3496 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3497 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3498 | 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] | 3499 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3500 | |
| 3501 | PSA_ASSERT( status ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3502 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 3503 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3504 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3505 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3506 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3507 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3508 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3509 | } |
| 3510 | /* END_CASE */ |
| 3511 | |
| 3512 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3513 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 3514 | int alg_arg, |
| 3515 | data_t *nonce, |
| 3516 | data_t *additional_data, |
| 3517 | data_t *input_data, |
| 3518 | data_t *expected_data, |
| 3519 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3520 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3521 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3522 | psa_key_type_t key_type = key_type_arg; |
| 3523 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3524 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3525 | unsigned char *output_data = NULL; |
| 3526 | size_t output_size = 0; |
| 3527 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3528 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3529 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3530 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3531 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3532 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3533 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3534 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3535 | psa_set_key_algorithm( &attributes, alg ); |
| 3536 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3537 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3538 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3539 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3540 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3541 | key_bits = psa_get_key_bits( &attributes ); |
| 3542 | |
| 3543 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3544 | alg ); |
| 3545 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 3546 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 3547 | { |
| 3548 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3549 | * should be exact. */ |
| 3550 | TEST_EQUAL( output_size, |
| 3551 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3552 | TEST_ASSERT( output_size <= |
| 3553 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3554 | } |
| 3555 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3556 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3557 | status = psa_aead_decrypt( key, alg, |
| 3558 | nonce->x, nonce->len, |
| 3559 | additional_data->x, |
| 3560 | additional_data->len, |
| 3561 | input_data->x, input_data->len, |
| 3562 | output_data, output_size, |
| 3563 | &output_length ); |
| 3564 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3565 | /* If the operation is not supported, just skip and not fail in case the |
| 3566 | * decryption involves a common limitation of cryptography hardwares and |
| 3567 | * an alternative implementation. */ |
| 3568 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3569 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3570 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3571 | 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] | 3572 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3573 | |
| 3574 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3575 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3576 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3577 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 3578 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3579 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3580 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3581 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3582 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3583 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3584 | } |
| 3585 | /* END_CASE */ |
| 3586 | |
| 3587 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3588 | void signature_size( int type_arg, |
| 3589 | int bits, |
| 3590 | int alg_arg, |
| 3591 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3592 | { |
| 3593 | psa_key_type_t type = type_arg; |
| 3594 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3595 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 3596 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3597 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 3598 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 3599 | TEST_EQUAL( actual_size, |
| 3600 | PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) ); |
| 3601 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3602 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3603 | exit: |
| 3604 | ; |
| 3605 | } |
| 3606 | /* END_CASE */ |
| 3607 | |
| 3608 | /* BEGIN_CASE */ |
gabor-mezei-arm | dc76df4 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 3609 | void sign_hash_deterministic( int key_type_arg, data_t *key_data, |
| 3610 | int alg_arg, data_t *input_data, |
| 3611 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3612 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3613 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3614 | psa_key_type_t key_type = key_type_arg; |
| 3615 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3616 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3617 | unsigned char *signature = NULL; |
| 3618 | size_t signature_size; |
| 3619 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3620 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3621 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3622 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3623 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3624 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3625 | psa_set_key_algorithm( &attributes, alg ); |
| 3626 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 3627 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3628 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3629 | &key ) ); |
| 3630 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3631 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3632 | |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 3633 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3634 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3635 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3636 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3637 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3638 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3639 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3640 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3641 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3642 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3643 | input_data->x, input_data->len, |
| 3644 | signature, signature_size, |
| 3645 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3646 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3647 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 3648 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3649 | |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 3650 | #if defined(MBEDTLS_TEST_DEPRECATED) |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3651 | memset( signature, 0, signature_size ); |
| 3652 | signature_length = INVALID_EXPORT_LENGTH; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3653 | PSA_ASSERT( psa_asymmetric_sign( key, alg, |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 3654 | input_data->x, input_data->len, |
| 3655 | signature, signature_size, |
| 3656 | &signature_length ) ); |
| 3657 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 3658 | signature, signature_length ); |
| 3659 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3660 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3661 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3662 | /* |
| 3663 | * Key attributes may have been returned by psa_get_key_attributes() |
| 3664 | * thus reset them as required. |
| 3665 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3666 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3667 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3668 | psa_destroy_key( key ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 3669 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3670 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3671 | } |
| 3672 | /* END_CASE */ |
| 3673 | |
| 3674 | /* BEGIN_CASE */ |
gabor-mezei-arm | dc76df4 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 3675 | void sign_hash_fail( int key_type_arg, data_t *key_data, |
| 3676 | int alg_arg, data_t *input_data, |
| 3677 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3678 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3679 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3680 | psa_key_type_t key_type = key_type_arg; |
| 3681 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3682 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3683 | psa_status_t actual_status; |
| 3684 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 3685 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 3686 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3687 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3688 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3689 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3690 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3691 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3692 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3693 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3694 | psa_set_key_algorithm( &attributes, alg ); |
| 3695 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 3696 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3697 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3698 | &key ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3699 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3700 | actual_status = psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3701 | input_data->x, input_data->len, |
| 3702 | signature, signature_size, |
| 3703 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3704 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3705 | /* The value of *signature_length is unspecified on error, but |
| 3706 | * whatever it is, it should be less than signature_size, so that |
| 3707 | * if the caller tries to read *signature_length bytes without |
| 3708 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3709 | TEST_LE_U( signature_length, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3710 | |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3711 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 3712 | signature_length = INVALID_EXPORT_LENGTH; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3713 | TEST_EQUAL( psa_asymmetric_sign( key, alg, |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3714 | input_data->x, input_data->len, |
| 3715 | signature, signature_size, |
| 3716 | &signature_length ), |
| 3717 | expected_status ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3718 | TEST_LE_U( signature_length, signature_size ); |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3719 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3720 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3721 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3722 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3723 | psa_destroy_key( key ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3724 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3725 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3726 | } |
| 3727 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3728 | |
| 3729 | /* BEGIN_CASE */ |
gabor-mezei-arm | dc76df4 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 3730 | void sign_verify_hash( int key_type_arg, data_t *key_data, |
| 3731 | int alg_arg, data_t *input_data ) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3732 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3733 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3734 | psa_key_type_t key_type = key_type_arg; |
| 3735 | psa_algorithm_t alg = alg_arg; |
| 3736 | size_t key_bits; |
| 3737 | unsigned char *signature = NULL; |
| 3738 | size_t signature_size; |
| 3739 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3740 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3741 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3742 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3743 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3744 | 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] | 3745 | psa_set_key_algorithm( &attributes, alg ); |
| 3746 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3747 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3748 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3749 | &key ) ); |
| 3750 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3751 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3752 | |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 3753 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3754 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3755 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3756 | key_bits, alg ); |
| 3757 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3758 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3759 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3760 | |
| 3761 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3762 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3763 | input_data->x, input_data->len, |
| 3764 | signature, signature_size, |
| 3765 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3766 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3767 | TEST_LE_U( signature_length, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3768 | TEST_ASSERT( signature_length > 0 ); |
| 3769 | |
| 3770 | /* Use the library to verify that the signature is correct. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3771 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3772 | input_data->x, input_data->len, |
| 3773 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3774 | |
| 3775 | if( input_data->len != 0 ) |
| 3776 | { |
| 3777 | /* Flip a bit in the input and verify that the signature is now |
| 3778 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 3779 | * because ECDSA may ignore the last few bits of the input. */ |
| 3780 | input_data->x[0] ^= 1; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3781 | TEST_EQUAL( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3782 | input_data->x, input_data->len, |
| 3783 | signature, signature_length ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3784 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3785 | } |
| 3786 | |
| 3787 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3788 | /* |
| 3789 | * Key attributes may have been returned by psa_get_key_attributes() |
| 3790 | * thus reset them as required. |
| 3791 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3792 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3793 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3794 | psa_destroy_key( key ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3795 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3796 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3797 | } |
| 3798 | /* END_CASE */ |
| 3799 | |
| 3800 | /* BEGIN_CASE */ |
gabor-mezei-arm | dc76df4 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 3801 | void verify_hash( int key_type_arg, data_t *key_data, |
| 3802 | int alg_arg, data_t *hash_data, |
| 3803 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3804 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3805 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3806 | psa_key_type_t key_type = key_type_arg; |
| 3807 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3808 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3809 | |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3810 | TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3811 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3812 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3813 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3814 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3815 | psa_set_key_algorithm( &attributes, alg ); |
| 3816 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3817 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3818 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3819 | &key ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3820 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3821 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3822 | hash_data->x, hash_data->len, |
| 3823 | signature_data->x, signature_data->len ) ); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 3824 | |
| 3825 | #if defined(MBEDTLS_TEST_DEPRECATED) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3826 | PSA_ASSERT( psa_asymmetric_verify( key, alg, |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 3827 | hash_data->x, hash_data->len, |
| 3828 | signature_data->x, |
| 3829 | signature_data->len ) ); |
| 3830 | |
| 3831 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3832 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3833 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3834 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3835 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3836 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3837 | } |
| 3838 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3839 | |
| 3840 | /* BEGIN_CASE */ |
gabor-mezei-arm | dc76df4 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 3841 | void verify_hash_fail( int key_type_arg, data_t *key_data, |
| 3842 | int alg_arg, data_t *hash_data, |
| 3843 | data_t *signature_data, |
| 3844 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3845 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3846 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3847 | psa_key_type_t key_type = key_type_arg; |
| 3848 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3849 | psa_status_t actual_status; |
| 3850 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3851 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3852 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3853 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3854 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3855 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3856 | psa_set_key_algorithm( &attributes, alg ); |
| 3857 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3858 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3859 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3860 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3861 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3862 | actual_status = psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3863 | hash_data->x, hash_data->len, |
| 3864 | signature_data->x, signature_data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3865 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3866 | |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3867 | #if defined(MBEDTLS_TEST_DEPRECATED) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3868 | TEST_EQUAL( psa_asymmetric_verify( key, alg, |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3869 | hash_data->x, hash_data->len, |
| 3870 | signature_data->x, signature_data->len ), |
| 3871 | expected_status ); |
| 3872 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3873 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3874 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3875 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3876 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3877 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3878 | } |
| 3879 | /* END_CASE */ |
| 3880 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3881 | /* BEGIN_CASE */ |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3882 | void sign_message_deterministic( int key_type_arg, |
| 3883 | data_t *key_data, |
| 3884 | int alg_arg, |
| 3885 | data_t *input_data, |
| 3886 | data_t *output_data ) |
| 3887 | { |
| 3888 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3889 | psa_key_type_t key_type = key_type_arg; |
| 3890 | psa_algorithm_t alg = alg_arg; |
| 3891 | size_t key_bits; |
| 3892 | unsigned char *signature = NULL; |
| 3893 | size_t signature_size; |
| 3894 | size_t signature_length = 0xdeadbeef; |
| 3895 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3896 | |
| 3897 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3898 | |
| 3899 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 3900 | psa_set_key_algorithm( &attributes, alg ); |
| 3901 | psa_set_key_type( &attributes, key_type ); |
| 3902 | |
| 3903 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3904 | &key ) ); |
| 3905 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3906 | key_bits = psa_get_key_bits( &attributes ); |
| 3907 | |
| 3908 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 3909 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3910 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3911 | ASSERT_ALLOC( signature, signature_size ); |
| 3912 | |
| 3913 | PSA_ASSERT( psa_sign_message( key, alg, |
| 3914 | input_data->x, input_data->len, |
| 3915 | signature, signature_size, |
| 3916 | &signature_length ) ); |
| 3917 | |
| 3918 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 3919 | signature, signature_length ); |
| 3920 | |
| 3921 | exit: |
| 3922 | psa_reset_key_attributes( &attributes ); |
| 3923 | |
| 3924 | psa_destroy_key( key ); |
| 3925 | mbedtls_free( signature ); |
| 3926 | PSA_DONE( ); |
| 3927 | |
| 3928 | } |
| 3929 | /* END_CASE */ |
| 3930 | |
| 3931 | /* BEGIN_CASE */ |
| 3932 | void sign_message_fail( int key_type_arg, |
| 3933 | data_t *key_data, |
| 3934 | int alg_arg, |
| 3935 | data_t *input_data, |
| 3936 | int signature_size_arg, |
| 3937 | int expected_status_arg ) |
| 3938 | { |
| 3939 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3940 | psa_key_type_t key_type = key_type_arg; |
| 3941 | psa_algorithm_t alg = alg_arg; |
| 3942 | size_t signature_size = signature_size_arg; |
| 3943 | psa_status_t actual_status; |
| 3944 | psa_status_t expected_status = expected_status_arg; |
| 3945 | unsigned char *signature = NULL; |
| 3946 | size_t signature_length = 0xdeadbeef; |
| 3947 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3948 | |
| 3949 | ASSERT_ALLOC( signature, signature_size ); |
| 3950 | |
| 3951 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3952 | |
| 3953 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 3954 | psa_set_key_algorithm( &attributes, alg ); |
| 3955 | psa_set_key_type( &attributes, key_type ); |
| 3956 | |
| 3957 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3958 | &key ) ); |
| 3959 | |
| 3960 | actual_status = psa_sign_message( key, alg, |
| 3961 | input_data->x, input_data->len, |
| 3962 | signature, signature_size, |
| 3963 | &signature_length ); |
| 3964 | TEST_EQUAL( actual_status, expected_status ); |
| 3965 | /* The value of *signature_length is unspecified on error, but |
| 3966 | * whatever it is, it should be less than signature_size, so that |
| 3967 | * if the caller tries to read *signature_length bytes without |
| 3968 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3969 | TEST_LE_U( signature_length, signature_size ); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3970 | |
| 3971 | exit: |
| 3972 | psa_reset_key_attributes( &attributes ); |
| 3973 | psa_destroy_key( key ); |
| 3974 | mbedtls_free( signature ); |
| 3975 | PSA_DONE( ); |
| 3976 | } |
| 3977 | /* END_CASE */ |
| 3978 | |
| 3979 | /* BEGIN_CASE */ |
| 3980 | void sign_verify_message( int key_type_arg, |
| 3981 | data_t *key_data, |
| 3982 | int alg_arg, |
| 3983 | data_t *input_data ) |
| 3984 | { |
| 3985 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3986 | psa_key_type_t key_type = key_type_arg; |
| 3987 | psa_algorithm_t alg = alg_arg; |
| 3988 | size_t key_bits; |
| 3989 | unsigned char *signature = NULL; |
| 3990 | size_t signature_size; |
| 3991 | size_t signature_length = 0xdeadbeef; |
| 3992 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3993 | |
| 3994 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3995 | |
| 3996 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 3997 | PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 3998 | psa_set_key_algorithm( &attributes, alg ); |
| 3999 | psa_set_key_type( &attributes, key_type ); |
| 4000 | |
| 4001 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4002 | &key ) ); |
| 4003 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4004 | key_bits = psa_get_key_bits( &attributes ); |
| 4005 | |
| 4006 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 4007 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4008 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 4009 | ASSERT_ALLOC( signature, signature_size ); |
| 4010 | |
| 4011 | PSA_ASSERT( psa_sign_message( key, alg, |
| 4012 | input_data->x, input_data->len, |
| 4013 | signature, signature_size, |
| 4014 | &signature_length ) ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4015 | TEST_LE_U( signature_length, signature_size ); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 4016 | TEST_ASSERT( signature_length > 0 ); |
| 4017 | |
| 4018 | PSA_ASSERT( psa_verify_message( key, alg, |
| 4019 | input_data->x, input_data->len, |
| 4020 | signature, signature_length ) ); |
| 4021 | |
| 4022 | if( input_data->len != 0 ) |
| 4023 | { |
| 4024 | /* Flip a bit in the input and verify that the signature is now |
| 4025 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 4026 | * because ECDSA may ignore the last few bits of the input. */ |
| 4027 | input_data->x[0] ^= 1; |
| 4028 | TEST_EQUAL( psa_verify_message( key, alg, |
| 4029 | input_data->x, input_data->len, |
| 4030 | signature, signature_length ), |
| 4031 | PSA_ERROR_INVALID_SIGNATURE ); |
| 4032 | } |
| 4033 | |
| 4034 | exit: |
| 4035 | psa_reset_key_attributes( &attributes ); |
| 4036 | |
| 4037 | psa_destroy_key( key ); |
| 4038 | mbedtls_free( signature ); |
| 4039 | PSA_DONE( ); |
| 4040 | } |
| 4041 | /* END_CASE */ |
| 4042 | |
| 4043 | /* BEGIN_CASE */ |
| 4044 | void verify_message( int key_type_arg, |
| 4045 | data_t *key_data, |
| 4046 | int alg_arg, |
| 4047 | data_t *input_data, |
| 4048 | data_t *signature_data ) |
| 4049 | { |
| 4050 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4051 | psa_key_type_t key_type = key_type_arg; |
| 4052 | psa_algorithm_t alg = alg_arg; |
| 4053 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4054 | |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4055 | TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 4056 | |
| 4057 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4058 | |
| 4059 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 4060 | psa_set_key_algorithm( &attributes, alg ); |
| 4061 | psa_set_key_type( &attributes, key_type ); |
| 4062 | |
| 4063 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4064 | &key ) ); |
| 4065 | |
| 4066 | PSA_ASSERT( psa_verify_message( key, alg, |
| 4067 | input_data->x, input_data->len, |
| 4068 | signature_data->x, signature_data->len ) ); |
| 4069 | |
| 4070 | exit: |
| 4071 | psa_reset_key_attributes( &attributes ); |
| 4072 | psa_destroy_key( key ); |
| 4073 | PSA_DONE( ); |
| 4074 | } |
| 4075 | /* END_CASE */ |
| 4076 | |
| 4077 | /* BEGIN_CASE */ |
| 4078 | void verify_message_fail( int key_type_arg, |
| 4079 | data_t *key_data, |
| 4080 | int alg_arg, |
| 4081 | data_t *hash_data, |
| 4082 | data_t *signature_data, |
| 4083 | int expected_status_arg ) |
| 4084 | { |
| 4085 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4086 | psa_key_type_t key_type = key_type_arg; |
| 4087 | psa_algorithm_t alg = alg_arg; |
| 4088 | psa_status_t actual_status; |
| 4089 | psa_status_t expected_status = expected_status_arg; |
| 4090 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4091 | |
| 4092 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4093 | |
| 4094 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 4095 | psa_set_key_algorithm( &attributes, alg ); |
| 4096 | psa_set_key_type( &attributes, key_type ); |
| 4097 | |
| 4098 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4099 | &key ) ); |
| 4100 | |
| 4101 | actual_status = psa_verify_message( key, alg, |
| 4102 | hash_data->x, hash_data->len, |
| 4103 | signature_data->x, |
| 4104 | signature_data->len ); |
| 4105 | TEST_EQUAL( actual_status, expected_status ); |
| 4106 | |
| 4107 | exit: |
| 4108 | psa_reset_key_attributes( &attributes ); |
| 4109 | psa_destroy_key( key ); |
| 4110 | PSA_DONE( ); |
| 4111 | } |
| 4112 | /* END_CASE */ |
| 4113 | |
| 4114 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4115 | void asymmetric_encrypt( int key_type_arg, |
| 4116 | data_t *key_data, |
| 4117 | int alg_arg, |
| 4118 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4119 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4120 | int expected_output_length_arg, |
| 4121 | int expected_status_arg ) |
| 4122 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4123 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4124 | psa_key_type_t key_type = key_type_arg; |
| 4125 | psa_algorithm_t alg = alg_arg; |
| 4126 | size_t expected_output_length = expected_output_length_arg; |
| 4127 | size_t key_bits; |
| 4128 | unsigned char *output = NULL; |
| 4129 | size_t output_size; |
| 4130 | size_t output_length = ~0; |
| 4131 | psa_status_t actual_status; |
| 4132 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4133 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4134 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4135 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4136 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4137 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4138 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4139 | psa_set_key_algorithm( &attributes, alg ); |
| 4140 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4141 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4142 | &key ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4143 | |
| 4144 | /* Determine the maximum output length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4145 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4146 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4147 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4148 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4149 | TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4150 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4151 | |
| 4152 | /* Encrypt the input */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4153 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4154 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4155 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4156 | output, output_size, |
| 4157 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4158 | TEST_EQUAL( actual_status, expected_status ); |
| 4159 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4160 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4161 | /* If the label is empty, the test framework puts a non-null pointer |
| 4162 | * in label->x. Test that a null pointer works as well. */ |
| 4163 | if( label->len == 0 ) |
| 4164 | { |
| 4165 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4166 | if( output_size != 0 ) |
| 4167 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4168 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4169 | input_data->x, input_data->len, |
| 4170 | NULL, label->len, |
| 4171 | output, output_size, |
| 4172 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4173 | TEST_EQUAL( actual_status, expected_status ); |
| 4174 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4175 | } |
| 4176 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4177 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4178 | /* |
| 4179 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4180 | * thus reset them as required. |
| 4181 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4182 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4183 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4184 | psa_destroy_key( key ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4185 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4186 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4187 | } |
| 4188 | /* END_CASE */ |
| 4189 | |
| 4190 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4191 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 4192 | data_t *key_data, |
| 4193 | int alg_arg, |
| 4194 | data_t *input_data, |
| 4195 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4196 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4197 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4198 | psa_key_type_t key_type = key_type_arg; |
| 4199 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4200 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4201 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4202 | size_t output_size; |
| 4203 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4204 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4205 | size_t output2_size; |
| 4206 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4207 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4208 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4209 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4210 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4211 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4212 | psa_set_key_algorithm( &attributes, alg ); |
| 4213 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4214 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4215 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4216 | &key ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4217 | |
| 4218 | /* Determine the maximum ciphertext length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4219 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4220 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4221 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4222 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4223 | TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4224 | ASSERT_ALLOC( output, output_size ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4225 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4226 | output2_size = input_data->len; |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4227 | TEST_LE_U( output2_size, |
| 4228 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) ); |
| 4229 | TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4230 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4231 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 4232 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 4233 | * the original plaintext because of the non-optional random |
| 4234 | * part of encryption process which prevents using fixed vectors. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4235 | PSA_ASSERT( psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4236 | input_data->x, input_data->len, |
| 4237 | label->x, label->len, |
| 4238 | output, output_size, |
| 4239 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4240 | /* We don't know what ciphertext length to expect, but check that |
| 4241 | * it looks sensible. */ |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4242 | TEST_LE_U( output_length, output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4243 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4244 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4245 | output, output_length, |
| 4246 | label->x, label->len, |
| 4247 | output2, output2_size, |
| 4248 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4249 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 4250 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4251 | |
| 4252 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4253 | /* |
| 4254 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4255 | * thus reset them as required. |
| 4256 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4257 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4258 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4259 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4260 | mbedtls_free( output ); |
| 4261 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4262 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4263 | } |
| 4264 | /* END_CASE */ |
| 4265 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4266 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4267 | void asymmetric_decrypt( int key_type_arg, |
| 4268 | data_t *key_data, |
| 4269 | int alg_arg, |
| 4270 | data_t *input_data, |
| 4271 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 4272 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4273 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4274 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4275 | psa_key_type_t key_type = key_type_arg; |
| 4276 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4277 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4278 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 4279 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4280 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4281 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4282 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4283 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4284 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4285 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4286 | psa_set_key_algorithm( &attributes, alg ); |
| 4287 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4288 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4289 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4290 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4291 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4292 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4293 | key_bits = psa_get_key_bits( &attributes ); |
| 4294 | |
| 4295 | /* Determine the maximum ciphertext length */ |
| 4296 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4297 | TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4298 | ASSERT_ALLOC( output, output_size ); |
| 4299 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4300 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4301 | input_data->x, input_data->len, |
| 4302 | label->x, label->len, |
| 4303 | output, |
| 4304 | output_size, |
| 4305 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4306 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4307 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4308 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4309 | /* If the label is empty, the test framework puts a non-null pointer |
| 4310 | * in label->x. Test that a null pointer works as well. */ |
| 4311 | if( label->len == 0 ) |
| 4312 | { |
| 4313 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4314 | if( output_size != 0 ) |
| 4315 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4316 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4317 | input_data->x, input_data->len, |
| 4318 | NULL, label->len, |
| 4319 | output, |
| 4320 | output_size, |
| 4321 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4322 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4323 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4324 | } |
| 4325 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4326 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4327 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4328 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4329 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4330 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4331 | } |
| 4332 | /* END_CASE */ |
| 4333 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4334 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4335 | void asymmetric_decrypt_fail( int key_type_arg, |
| 4336 | data_t *key_data, |
| 4337 | int alg_arg, |
| 4338 | data_t *input_data, |
| 4339 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4340 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4341 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4342 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4343 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4344 | psa_key_type_t key_type = key_type_arg; |
| 4345 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4346 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4347 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4348 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4349 | psa_status_t actual_status; |
| 4350 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4351 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4352 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4353 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4354 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4355 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4356 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4357 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4358 | psa_set_key_algorithm( &attributes, alg ); |
| 4359 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4360 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4361 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4362 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4363 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4364 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4365 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4366 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4367 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4368 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4369 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4370 | TEST_LE_U( output_length, output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4371 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4372 | /* If the label is empty, the test framework puts a non-null pointer |
| 4373 | * in label->x. Test that a null pointer works as well. */ |
| 4374 | if( label->len == 0 ) |
| 4375 | { |
| 4376 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4377 | if( output_size != 0 ) |
| 4378 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4379 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4380 | input_data->x, input_data->len, |
| 4381 | NULL, label->len, |
| 4382 | output, output_size, |
| 4383 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4384 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4385 | TEST_LE_U( output_length, output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4386 | } |
| 4387 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4388 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4389 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4390 | psa_destroy_key( key ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4391 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4392 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4393 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4394 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4395 | |
| 4396 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4397 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4398 | { |
| 4399 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 4400 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 4401 | * though it's OK by the C standard. We could test for this, but we'd need |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 4402 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4403 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4404 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 4405 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4406 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4407 | |
| 4408 | memset( &zero, 0, sizeof( zero ) ); |
| 4409 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4410 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4411 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4412 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4413 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4414 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4415 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4416 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4417 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4418 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4419 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 4420 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 4421 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4422 | } |
| 4423 | /* END_CASE */ |
| 4424 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4425 | /* BEGIN_CASE */ |
| 4426 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4427 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4428 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4429 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4430 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4431 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4432 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4433 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4434 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4435 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4436 | |
| 4437 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4438 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4439 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4440 | } |
| 4441 | /* END_CASE */ |
| 4442 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4443 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4444 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 4445 | int expected_status_arg ) |
| 4446 | { |
| 4447 | psa_algorithm_t alg = alg_arg; |
| 4448 | size_t capacity = capacity_arg; |
| 4449 | psa_status_t expected_status = expected_status_arg; |
| 4450 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4451 | |
| 4452 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4453 | |
| 4454 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4455 | |
| 4456 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 4457 | expected_status ); |
| 4458 | |
| 4459 | exit: |
| 4460 | psa_key_derivation_abort( &operation ); |
| 4461 | PSA_DONE( ); |
| 4462 | } |
| 4463 | /* END_CASE */ |
| 4464 | |
| 4465 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4466 | void derive_input( int alg_arg, |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4467 | int step_arg1, int key_type_arg1, data_t *input1, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4468 | int expected_status_arg1, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 4469 | int step_arg2, int key_type_arg2, data_t *input2, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4470 | int expected_status_arg2, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 4471 | int step_arg3, int key_type_arg3, data_t *input3, |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4472 | int expected_status_arg3, |
| 4473 | int output_key_type_arg, int expected_output_status_arg ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4474 | { |
| 4475 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4476 | psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3}; |
| 4477 | 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] | 4478 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 4479 | expected_status_arg2, |
| 4480 | expected_status_arg3}; |
| 4481 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4482 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 4483 | MBEDTLS_SVC_KEY_ID_INIT, |
| 4484 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4485 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4486 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4487 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4488 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4489 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4490 | psa_status_t expected_output_status = expected_output_status_arg; |
| 4491 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4492 | |
| 4493 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4494 | |
| 4495 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4496 | psa_set_key_algorithm( &attributes, alg ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4497 | |
| 4498 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4499 | |
| 4500 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 4501 | { |
Gilles Peskine | f627931 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 4502 | mbedtls_test_set_step( i ); |
| 4503 | if( steps[i] == 0 ) |
| 4504 | { |
| 4505 | /* Skip this step */ |
| 4506 | } |
| 4507 | else if( key_types[i] != PSA_KEY_TYPE_NONE ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4508 | { |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4509 | psa_set_key_type( &attributes, key_types[i] ); |
| 4510 | PSA_ASSERT( psa_import_key( &attributes, |
| 4511 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4512 | &keys[i] ) ); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 4513 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) && |
| 4514 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 4515 | { |
| 4516 | // When taking a private key as secret input, use key agreement |
| 4517 | // to add the shared secret to the derivation |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 4518 | TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self( |
| 4519 | &operation, keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 4520 | expected_statuses[i] ); |
| 4521 | } |
| 4522 | else |
| 4523 | { |
| 4524 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4525 | keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 4526 | expected_statuses[i] ); |
| 4527 | } |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4528 | } |
| 4529 | else |
| 4530 | { |
| 4531 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 4532 | &operation, steps[i], |
| 4533 | inputs[i]->x, inputs[i]->len ), |
| 4534 | expected_statuses[i] ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4535 | } |
| 4536 | } |
| 4537 | |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4538 | if( output_key_type != PSA_KEY_TYPE_NONE ) |
| 4539 | { |
| 4540 | psa_reset_key_attributes( &attributes ); |
Dave Rodgman | dc4e4b7 | 2021-11-16 12:12:49 +0000 | [diff] [blame] | 4541 | psa_set_key_type( &attributes, output_key_type ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4542 | psa_set_key_bits( &attributes, 8 ); |
| 4543 | actual_output_status = |
| 4544 | psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4545 | &output_key ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4546 | } |
| 4547 | else |
| 4548 | { |
| 4549 | uint8_t buffer[1]; |
| 4550 | actual_output_status = |
| 4551 | psa_key_derivation_output_bytes( &operation, |
| 4552 | buffer, sizeof( buffer ) ); |
| 4553 | } |
| 4554 | TEST_EQUAL( actual_output_status, expected_output_status ); |
| 4555 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4556 | exit: |
| 4557 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4558 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 4559 | psa_destroy_key( keys[i] ); |
| 4560 | psa_destroy_key( output_key ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4561 | PSA_DONE( ); |
| 4562 | } |
| 4563 | /* END_CASE */ |
| 4564 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4565 | /* BEGIN_CASE */ |
Gilles Peskine | 0faba4e | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 4566 | void derive_over_capacity( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4567 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4568 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4569 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 4570 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4571 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4572 | unsigned char input1[] = "Input 1"; |
| 4573 | size_t input1_length = sizeof( input1 ); |
| 4574 | unsigned char input2[] = "Input 2"; |
| 4575 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4576 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 4577 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 4578 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4579 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4580 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4581 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4582 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4583 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4584 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4585 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4586 | psa_set_key_algorithm( &attributes, alg ); |
| 4587 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4588 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 4589 | PSA_ASSERT( psa_import_key( &attributes, |
| 4590 | key_data, sizeof( key_data ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4591 | &key ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4592 | |
| 4593 | /* valid key derivation */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 4594 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 4595 | input1, input1_length, |
| 4596 | input2, input2_length, |
| 4597 | capacity ) ) |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4598 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4599 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4600 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4601 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4602 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4603 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4604 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4605 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4606 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4607 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4608 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4609 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4610 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4611 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4612 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4613 | } |
| 4614 | /* END_CASE */ |
| 4615 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4616 | /* BEGIN_CASE */ |
Gilles Peskine | 0faba4e | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 4617 | void derive_actions_without_setup( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4618 | { |
| 4619 | uint8_t output_buffer[16]; |
| 4620 | size_t buffer_size = 16; |
| 4621 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4622 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4623 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4624 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 4625 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4626 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4627 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4628 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4629 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4630 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4631 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4632 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4633 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 4634 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4635 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4636 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4637 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4638 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4639 | |
| 4640 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4641 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4642 | } |
| 4643 | /* END_CASE */ |
| 4644 | |
| 4645 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4646 | void derive_output( int alg_arg, |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4647 | int step1_arg, data_t *input1, |
| 4648 | int step2_arg, data_t *input2, |
| 4649 | int step3_arg, data_t *input3, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4650 | int requested_capacity_arg, |
| 4651 | data_t *expected_output1, |
| 4652 | data_t *expected_output2 ) |
| 4653 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4654 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4655 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg}; |
| 4656 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4657 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 4658 | MBEDTLS_SVC_KEY_ID_INIT, |
| 4659 | MBEDTLS_SVC_KEY_ID_INIT }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4660 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4661 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4662 | uint8_t *expected_outputs[2] = |
| 4663 | {expected_output1->x, expected_output2->x}; |
| 4664 | size_t output_sizes[2] = |
| 4665 | {expected_output1->len, expected_output2->len}; |
| 4666 | size_t output_buffer_size = 0; |
| 4667 | uint8_t *output_buffer = NULL; |
| 4668 | size_t expected_capacity; |
| 4669 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4670 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4671 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4672 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4673 | |
| 4674 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 4675 | { |
| 4676 | if( output_sizes[i] > output_buffer_size ) |
| 4677 | output_buffer_size = output_sizes[i]; |
| 4678 | if( output_sizes[i] == 0 ) |
| 4679 | expected_outputs[i] = NULL; |
| 4680 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4681 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4682 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4683 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4684 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4685 | psa_set_key_algorithm( &attributes, alg ); |
| 4686 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4687 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4688 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4689 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4690 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 4691 | requested_capacity ) ); |
| 4692 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4693 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4694 | switch( steps[i] ) |
| 4695 | { |
| 4696 | case 0: |
| 4697 | break; |
| 4698 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 4699 | PSA_ASSERT( psa_import_key( &attributes, |
| 4700 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4701 | &keys[i] ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4702 | |
| 4703 | if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 4704 | { |
| 4705 | PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) ); |
| 4706 | TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <= |
| 4707 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ); |
| 4708 | } |
| 4709 | |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4710 | PSA_ASSERT( psa_key_derivation_input_key( |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4711 | &operation, steps[i], keys[i] ) ); |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4712 | break; |
| 4713 | default: |
| 4714 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 4715 | &operation, steps[i], |
| 4716 | inputs[i]->x, inputs[i]->len ) ); |
| 4717 | break; |
| 4718 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4719 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4720 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4721 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4722 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4723 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4724 | expected_capacity = requested_capacity; |
| 4725 | |
| 4726 | /* Expansion phase. */ |
| 4727 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 4728 | { |
| 4729 | /* Read some bytes. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4730 | status = psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4731 | output_buffer, output_sizes[i] ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4732 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 4733 | { |
| 4734 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 4735 | TEST_ASSERT( status == PSA_SUCCESS || |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4736 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4737 | continue; |
| 4738 | } |
| 4739 | else if( expected_capacity == 0 || |
| 4740 | output_sizes[i] > expected_capacity ) |
| 4741 | { |
| 4742 | /* Capacity exceeded. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4743 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4744 | expected_capacity = 0; |
| 4745 | continue; |
| 4746 | } |
| 4747 | /* Success. Check the read data. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4748 | PSA_ASSERT( status ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4749 | if( output_sizes[i] != 0 ) |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4750 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 4751 | expected_outputs[i], output_sizes[i] ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4752 | /* Check the operation status. */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4753 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4754 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4755 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4756 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4757 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4758 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4759 | |
| 4760 | exit: |
| 4761 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4762 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4763 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 4764 | psa_destroy_key( keys[i] ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4765 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4766 | } |
| 4767 | /* END_CASE */ |
| 4768 | |
| 4769 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4770 | void derive_full( int alg_arg, |
| 4771 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 4772 | data_t *input1, |
| 4773 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4774 | int requested_capacity_arg ) |
| 4775 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4776 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4777 | psa_algorithm_t alg = alg_arg; |
| 4778 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4779 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4780 | unsigned char output_buffer[16]; |
| 4781 | size_t expected_capacity = requested_capacity; |
| 4782 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4783 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4784 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4785 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4786 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4787 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4788 | psa_set_key_algorithm( &attributes, alg ); |
| 4789 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4790 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4791 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4792 | &key ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4793 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 4794 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 4795 | input1->x, input1->len, |
| 4796 | input2->x, input2->len, |
| 4797 | requested_capacity ) ) |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 4798 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 4799 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4800 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4801 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4802 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4803 | |
| 4804 | /* Expansion phase. */ |
| 4805 | while( current_capacity > 0 ) |
| 4806 | { |
| 4807 | size_t read_size = sizeof( output_buffer ); |
| 4808 | if( read_size > current_capacity ) |
| 4809 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4810 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4811 | output_buffer, |
| 4812 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4813 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4814 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4815 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4816 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4817 | } |
| 4818 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4819 | /* Check that the operation refuses to go over capacity. */ |
| 4820 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4821 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4822 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4823 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4824 | |
| 4825 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4826 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4827 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4828 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4829 | } |
| 4830 | /* END_CASE */ |
| 4831 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4832 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4833 | void derive_key_exercise( int alg_arg, |
| 4834 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4835 | data_t *input1, |
| 4836 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4837 | int derived_type_arg, |
| 4838 | int derived_bits_arg, |
| 4839 | int derived_usage_arg, |
| 4840 | int derived_alg_arg ) |
| 4841 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4842 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4843 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4844 | psa_algorithm_t alg = alg_arg; |
| 4845 | psa_key_type_t derived_type = derived_type_arg; |
| 4846 | size_t derived_bits = derived_bits_arg; |
| 4847 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 4848 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 4849 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4850 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4851 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4852 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4853 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4854 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4855 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4856 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4857 | psa_set_key_algorithm( &attributes, alg ); |
| 4858 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4859 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4860 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4861 | |
| 4862 | /* Derive a key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 4863 | if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 4864 | input1->x, input1->len, |
| 4865 | input2->x, input2->len, |
| 4866 | capacity ) ) |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4867 | goto exit; |
| 4868 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4869 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 4870 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 4871 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4872 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4873 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4874 | &derived_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4875 | |
| 4876 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4877 | PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4878 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 4879 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4880 | |
| 4881 | /* Exercise the derived key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 4882 | if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4883 | goto exit; |
| 4884 | |
| 4885 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4886 | /* |
| 4887 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4888 | * thus reset them as required. |
| 4889 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4890 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4891 | |
| 4892 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4893 | psa_destroy_key( base_key ); |
| 4894 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4895 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4896 | } |
| 4897 | /* END_CASE */ |
| 4898 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4899 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4900 | void derive_key_export( int alg_arg, |
| 4901 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4902 | data_t *input1, |
| 4903 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4904 | int bytes1_arg, |
| 4905 | int bytes2_arg ) |
| 4906 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4907 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4908 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4909 | psa_algorithm_t alg = alg_arg; |
| 4910 | size_t bytes1 = bytes1_arg; |
| 4911 | size_t bytes2 = bytes2_arg; |
| 4912 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4913 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4914 | uint8_t *output_buffer = NULL; |
| 4915 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4916 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4917 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4918 | size_t length; |
| 4919 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4920 | ASSERT_ALLOC( output_buffer, capacity ); |
| 4921 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4922 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4923 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4924 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 4925 | psa_set_key_algorithm( &base_attributes, alg ); |
| 4926 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4927 | 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] | 4928 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4929 | |
| 4930 | /* Derive some material and output it. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 4931 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 4932 | input1->x, input1->len, |
| 4933 | input2->x, input2->len, |
| 4934 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4935 | goto exit; |
| 4936 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4937 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4938 | output_buffer, |
| 4939 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4940 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4941 | |
| 4942 | /* 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] | 4943 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 4944 | input1->x, input1->len, |
| 4945 | input2->x, input2->len, |
| 4946 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4947 | goto exit; |
| 4948 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4949 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 4950 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 4951 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4952 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4953 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4954 | &derived_key ) ); |
| 4955 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4956 | export_buffer, bytes1, |
| 4957 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4958 | TEST_EQUAL( length, bytes1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4959 | PSA_ASSERT( psa_destroy_key( derived_key ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4960 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4961 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4962 | &derived_key ) ); |
| 4963 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4964 | export_buffer + bytes1, bytes2, |
| 4965 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4966 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4967 | |
| 4968 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4969 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 4970 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4971 | |
| 4972 | exit: |
| 4973 | mbedtls_free( output_buffer ); |
| 4974 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4975 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4976 | psa_destroy_key( base_key ); |
| 4977 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4978 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4979 | } |
| 4980 | /* END_CASE */ |
| 4981 | |
| 4982 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 4983 | void derive_key( int alg_arg, |
| 4984 | data_t *key_data, data_t *input1, data_t *input2, |
| 4985 | int type_arg, int bits_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 4986 | int expected_status_arg, |
| 4987 | int is_large_output ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4988 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4989 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4990 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4991 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 4992 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4993 | size_t bits = bits_arg; |
| 4994 | psa_status_t expected_status = expected_status_arg; |
| 4995 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4996 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4997 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4998 | |
| 4999 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5000 | |
| 5001 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 5002 | psa_set_key_algorithm( &base_attributes, alg ); |
| 5003 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 5004 | 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] | 5005 | &base_key ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5006 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5007 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 5008 | input1->x, input1->len, |
| 5009 | input2->x, input2->len, |
| 5010 | SIZE_MAX ) ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5011 | goto exit; |
| 5012 | |
| 5013 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 5014 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 5015 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5016 | psa_set_key_bits( &derived_attributes, bits ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 5017 | |
| 5018 | psa_status_t status = |
| 5019 | psa_key_derivation_output_key( &derived_attributes, |
| 5020 | &operation, |
| 5021 | &derived_key ); |
| 5022 | if( is_large_output > 0 ) |
| 5023 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5024 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5025 | |
| 5026 | exit: |
| 5027 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5028 | psa_destroy_key( base_key ); |
| 5029 | psa_destroy_key( derived_key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5030 | PSA_DONE( ); |
| 5031 | } |
| 5032 | /* END_CASE */ |
| 5033 | |
| 5034 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5035 | void key_agreement_setup( int alg_arg, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 5036 | int our_key_type_arg, int our_key_alg_arg, |
| 5037 | data_t *our_key_data, data_t *peer_key_data, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5038 | int expected_status_arg ) |
| 5039 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5040 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5041 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5042 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5043 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5044 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5045 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5046 | psa_status_t expected_status = expected_status_arg; |
| 5047 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5048 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5049 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5050 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5051 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5052 | psa_set_key_algorithm( &attributes, our_key_alg ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5053 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5054 | PSA_ASSERT( psa_import_key( &attributes, |
| 5055 | our_key_data->x, our_key_data->len, |
| 5056 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5057 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5058 | /* The tests currently include inputs that should fail at either step. |
| 5059 | * Test cases that fail at the setup step should be changed to call |
| 5060 | * key_derivation_setup instead, and this function should be renamed |
| 5061 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5062 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5063 | if( status == PSA_SUCCESS ) |
| 5064 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5065 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 5066 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 5067 | our_key, |
| 5068 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5069 | expected_status ); |
| 5070 | } |
| 5071 | else |
| 5072 | { |
| 5073 | TEST_ASSERT( status == expected_status ); |
| 5074 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5075 | |
| 5076 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5077 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5078 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5079 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5080 | } |
| 5081 | /* END_CASE */ |
| 5082 | |
| 5083 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5084 | void raw_key_agreement( int alg_arg, |
| 5085 | int our_key_type_arg, data_t *our_key_data, |
| 5086 | data_t *peer_key_data, |
| 5087 | data_t *expected_output ) |
| 5088 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5089 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5090 | psa_algorithm_t alg = alg_arg; |
| 5091 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5092 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5093 | unsigned char *output = NULL; |
| 5094 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5095 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5096 | |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5097 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5098 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5099 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5100 | psa_set_key_algorithm( &attributes, alg ); |
| 5101 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5102 | PSA_ASSERT( psa_import_key( &attributes, |
| 5103 | our_key_data->x, our_key_data->len, |
| 5104 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5105 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5106 | PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) ); |
| 5107 | key_bits = psa_get_key_bits( &attributes ); |
| 5108 | |
Gilles Peskine | 7d15029 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 5109 | /* Validate size macros */ |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5110 | TEST_LE_U( expected_output->len, |
| 5111 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) ); |
| 5112 | TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ), |
| 5113 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 7d15029 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 5114 | |
| 5115 | /* Good case with exact output size */ |
| 5116 | ASSERT_ALLOC( output, expected_output->len ); |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5117 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 5118 | peer_key_data->x, peer_key_data->len, |
| 5119 | output, expected_output->len, |
| 5120 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5121 | ASSERT_COMPARE( output, output_length, |
| 5122 | expected_output->x, expected_output->len ); |
Gilles Peskine | 7d15029 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 5123 | mbedtls_free( output ); |
| 5124 | output = NULL; |
| 5125 | output_length = ~0; |
| 5126 | |
| 5127 | /* Larger buffer */ |
| 5128 | ASSERT_ALLOC( output, expected_output->len + 1 ); |
| 5129 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 5130 | peer_key_data->x, peer_key_data->len, |
| 5131 | output, expected_output->len + 1, |
| 5132 | &output_length ) ); |
| 5133 | ASSERT_COMPARE( output, output_length, |
| 5134 | expected_output->x, expected_output->len ); |
| 5135 | mbedtls_free( output ); |
| 5136 | output = NULL; |
| 5137 | output_length = ~0; |
| 5138 | |
| 5139 | /* Buffer too small */ |
| 5140 | ASSERT_ALLOC( output, expected_output->len - 1 ); |
| 5141 | TEST_EQUAL( psa_raw_key_agreement( alg, our_key, |
| 5142 | peer_key_data->x, peer_key_data->len, |
| 5143 | output, expected_output->len - 1, |
| 5144 | &output_length ), |
| 5145 | PSA_ERROR_BUFFER_TOO_SMALL ); |
| 5146 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 47cfdfd | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5147 | TEST_LE_U( output_length, expected_output->len - 1 ); |
Gilles Peskine | 7d15029 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 5148 | mbedtls_free( output ); |
| 5149 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5150 | |
| 5151 | exit: |
| 5152 | mbedtls_free( output ); |
| 5153 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5154 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5155 | } |
| 5156 | /* END_CASE */ |
| 5157 | |
| 5158 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5159 | void key_agreement_capacity( int alg_arg, |
| 5160 | int our_key_type_arg, data_t *our_key_data, |
| 5161 | data_t *peer_key_data, |
| 5162 | int expected_capacity_arg ) |
| 5163 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5164 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5165 | psa_algorithm_t alg = alg_arg; |
| 5166 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5167 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5168 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5169 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5170 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5171 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5172 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5173 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5174 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5175 | psa_set_key_algorithm( &attributes, alg ); |
| 5176 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5177 | PSA_ASSERT( psa_import_key( &attributes, |
| 5178 | our_key_data->x, our_key_data->len, |
| 5179 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5180 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5181 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5182 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 5183 | &operation, |
| 5184 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 5185 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5186 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 5187 | { |
| 5188 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5189 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5190 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5191 | NULL, 0 ) ); |
| 5192 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5193 | |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 5194 | /* Test the advertised capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5195 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5196 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5197 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5198 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5199 | /* Test the actual capacity by reading the output. */ |
| 5200 | while( actual_capacity > sizeof( output ) ) |
| 5201 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5202 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5203 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5204 | actual_capacity -= sizeof( output ); |
| 5205 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5206 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5207 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5208 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5209 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5210 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5211 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5212 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5213 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5214 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5215 | } |
| 5216 | /* END_CASE */ |
| 5217 | |
| 5218 | /* BEGIN_CASE */ |
| 5219 | void key_agreement_output( int alg_arg, |
| 5220 | int our_key_type_arg, data_t *our_key_data, |
| 5221 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5222 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5223 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5224 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5225 | psa_algorithm_t alg = alg_arg; |
| 5226 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5227 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5228 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5229 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5230 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5231 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 5232 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5233 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5234 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5235 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5236 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5237 | psa_set_key_algorithm( &attributes, alg ); |
| 5238 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5239 | PSA_ASSERT( psa_import_key( &attributes, |
| 5240 | our_key_data->x, our_key_data->len, |
| 5241 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5242 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5243 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5244 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 5245 | &operation, |
| 5246 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 5247 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5248 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 5249 | { |
| 5250 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5251 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5252 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5253 | NULL, 0 ) ); |
| 5254 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5255 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5256 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5257 | actual_output, |
| 5258 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5259 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 5260 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5261 | if( expected_output2->len != 0 ) |
| 5262 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5263 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5264 | actual_output, |
| 5265 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5266 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 5267 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5268 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5269 | |
| 5270 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5271 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5272 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5273 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5274 | mbedtls_free( actual_output ); |
| 5275 | } |
| 5276 | /* END_CASE */ |
| 5277 | |
| 5278 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5279 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5280 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5281 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5282 | unsigned char *output = NULL; |
| 5283 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5284 | size_t i; |
| 5285 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5286 | |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 5287 | TEST_ASSERT( bytes_arg >= 0 ); |
| 5288 | |
Gilles Peskine | 9189202 | 2021-02-08 19:50:26 +0100 | [diff] [blame] | 5289 | ASSERT_ALLOC( output, bytes ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5290 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5291 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5292 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5293 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5294 | /* Run several times, to ensure that every output byte will be |
| 5295 | * nonzero at least once with overwhelming probability |
| 5296 | * (2^(-8*number_of_runs)). */ |
| 5297 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5298 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 5299 | if( bytes != 0 ) |
| 5300 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5301 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5302 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5303 | for( i = 0; i < bytes; i++ ) |
| 5304 | { |
| 5305 | if( output[i] != 0 ) |
| 5306 | ++changed[i]; |
| 5307 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5308 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5309 | |
| 5310 | /* Check that every byte was changed to nonzero at least once. This |
| 5311 | * validates that psa_generate_random is overwriting every byte of |
| 5312 | * the output buffer. */ |
| 5313 | for( i = 0; i < bytes; i++ ) |
| 5314 | { |
| 5315 | TEST_ASSERT( changed[i] != 0 ); |
| 5316 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5317 | |
| 5318 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5319 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5320 | mbedtls_free( output ); |
| 5321 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5322 | } |
| 5323 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5324 | |
| 5325 | /* BEGIN_CASE */ |
| 5326 | void generate_key( int type_arg, |
| 5327 | int bits_arg, |
| 5328 | int usage_arg, |
| 5329 | int alg_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 5330 | int expected_status_arg, |
| 5331 | int is_large_key ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5332 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5333 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5334 | psa_key_type_t type = type_arg; |
| 5335 | psa_key_usage_t usage = usage_arg; |
| 5336 | size_t bits = bits_arg; |
| 5337 | psa_algorithm_t alg = alg_arg; |
| 5338 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5339 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5340 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5341 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5342 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5343 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5344 | psa_set_key_usage_flags( &attributes, usage ); |
| 5345 | psa_set_key_algorithm( &attributes, alg ); |
| 5346 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5347 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5348 | |
| 5349 | /* Generate a key */ |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 5350 | psa_status_t status = psa_generate_key( &attributes, &key ); |
| 5351 | |
| 5352 | if( is_large_key > 0 ) |
| 5353 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5354 | TEST_EQUAL( status , expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5355 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5356 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5357 | |
| 5358 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5359 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5360 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 5361 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5362 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 5363 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5364 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 5365 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5366 | |
| 5367 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5368 | /* |
| 5369 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5370 | * thus reset them as required. |
| 5371 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5372 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5373 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5374 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5375 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5376 | } |
| 5377 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5378 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 5379 | /* 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] | 5380 | void generate_key_rsa( int bits_arg, |
| 5381 | data_t *e_arg, |
| 5382 | int expected_status_arg ) |
| 5383 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5384 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5385 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5386 | size_t bits = bits_arg; |
| 5387 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 5388 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 5389 | psa_status_t expected_status = expected_status_arg; |
| 5390 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5391 | uint8_t *exported = NULL; |
| 5392 | size_t exported_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5393 | PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5394 | size_t exported_length = SIZE_MAX; |
| 5395 | uint8_t *e_read_buffer = NULL; |
| 5396 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 5397 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5398 | size_t e_read_length = SIZE_MAX; |
| 5399 | |
| 5400 | if( e_arg->len == 0 || |
| 5401 | ( e_arg->len == 3 && |
| 5402 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 5403 | { |
| 5404 | is_default_public_exponent = 1; |
| 5405 | e_read_size = 0; |
| 5406 | } |
| 5407 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 5408 | ASSERT_ALLOC( exported, exported_size ); |
| 5409 | |
| 5410 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5411 | |
| 5412 | psa_set_key_usage_flags( &attributes, usage ); |
| 5413 | psa_set_key_algorithm( &attributes, alg ); |
| 5414 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 5415 | e_arg->x, e_arg->len ) ); |
| 5416 | psa_set_key_bits( &attributes, bits ); |
| 5417 | |
| 5418 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5419 | TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5420 | if( expected_status != PSA_SUCCESS ) |
| 5421 | goto exit; |
| 5422 | |
| 5423 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5424 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5425 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 5426 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 5427 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 5428 | e_read_buffer, e_read_size, |
| 5429 | &e_read_length ) ); |
| 5430 | if( is_default_public_exponent ) |
| 5431 | TEST_EQUAL( e_read_length, 0 ); |
| 5432 | else |
| 5433 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 5434 | |
| 5435 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5436 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5437 | goto exit; |
| 5438 | |
| 5439 | /* Export the key and check the public exponent. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5440 | PSA_ASSERT( psa_export_public_key( key, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5441 | exported, exported_size, |
| 5442 | &exported_length ) ); |
| 5443 | { |
| 5444 | uint8_t *p = exported; |
| 5445 | uint8_t *end = exported + exported_length; |
| 5446 | size_t len; |
| 5447 | /* RSAPublicKey ::= SEQUENCE { |
| 5448 | * modulus INTEGER, -- n |
| 5449 | * publicExponent INTEGER } -- e |
| 5450 | */ |
| 5451 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5452 | MBEDTLS_ASN1_SEQUENCE | |
| 5453 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 5454 | TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5455 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 5456 | MBEDTLS_ASN1_INTEGER ) ); |
| 5457 | if( len >= 1 && p[0] == 0 ) |
| 5458 | { |
| 5459 | ++p; |
| 5460 | --len; |
| 5461 | } |
| 5462 | if( e_arg->len == 0 ) |
| 5463 | { |
| 5464 | TEST_EQUAL( len, 3 ); |
| 5465 | TEST_EQUAL( p[0], 1 ); |
| 5466 | TEST_EQUAL( p[1], 0 ); |
| 5467 | TEST_EQUAL( p[2], 1 ); |
| 5468 | } |
| 5469 | else |
| 5470 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 5471 | } |
| 5472 | |
| 5473 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5474 | /* |
| 5475 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 5476 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 5477 | */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5478 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5479 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5480 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5481 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5482 | mbedtls_free( e_read_buffer ); |
| 5483 | mbedtls_free( exported ); |
| 5484 | } |
| 5485 | /* END_CASE */ |
| 5486 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5487 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5488 | void persistent_key_load_key_from_storage( data_t *data, |
| 5489 | int type_arg, int bits_arg, |
| 5490 | int usage_flags_arg, int alg_arg, |
| 5491 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5492 | { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 5493 | 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] | 5494 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5495 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5496 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5497 | psa_key_type_t type = type_arg; |
| 5498 | size_t bits = bits_arg; |
| 5499 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 5500 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5501 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5502 | unsigned char *first_export = NULL; |
| 5503 | unsigned char *second_export = NULL; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5504 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5505 | size_t first_exported_length; |
| 5506 | size_t second_exported_length; |
| 5507 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5508 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 5509 | { |
| 5510 | ASSERT_ALLOC( first_export, export_size ); |
| 5511 | ASSERT_ALLOC( second_export, export_size ); |
| 5512 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5513 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5514 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5515 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 5516 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5517 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 5518 | psa_set_key_algorithm( &attributes, alg ); |
| 5519 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5520 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5521 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5522 | switch( generation_method ) |
| 5523 | { |
| 5524 | case IMPORT_KEY: |
| 5525 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5526 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5527 | &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5528 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5529 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5530 | case GENERATE_KEY: |
| 5531 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5532 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5533 | break; |
| 5534 | |
| 5535 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 5536 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5537 | { |
| 5538 | /* Create base key */ |
| 5539 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 5540 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5541 | psa_set_key_usage_flags( &base_attributes, |
| 5542 | PSA_KEY_USAGE_DERIVE ); |
| 5543 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 5544 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5545 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 5546 | data->x, data->len, |
| 5547 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5548 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5549 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5550 | PSA_ASSERT( psa_key_derivation_input_key( |
| 5551 | &operation, |
| 5552 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5553 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5554 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5555 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5556 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 5557 | &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5558 | &key ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5559 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5560 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5561 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5562 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 5563 | #else |
| 5564 | TEST_ASSUME( ! "KDF not supported in this configuration" ); |
| 5565 | #endif |
| 5566 | break; |
| 5567 | |
| 5568 | default: |
| 5569 | TEST_ASSERT( ! "generation_method not implemented in test" ); |
| 5570 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5571 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5572 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5573 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5574 | /* Export the key if permitted by the key policy. */ |
| 5575 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 5576 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5577 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5578 | first_export, export_size, |
| 5579 | &first_exported_length ) ); |
| 5580 | if( generation_method == IMPORT_KEY ) |
| 5581 | ASSERT_COMPARE( data->x, data->len, |
| 5582 | first_export, first_exported_length ); |
| 5583 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5584 | |
| 5585 | /* Shutdown and restart */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5586 | PSA_ASSERT( psa_purge_key( key ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5587 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5588 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5589 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5590 | /* Check key slot still contains key data */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5591 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 5592 | TEST_ASSERT( mbedtls_svc_key_id_equal( |
| 5593 | psa_get_key_id( &attributes ), key_id ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5594 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 5595 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 5596 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 5597 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
gabor-mezei-arm | 4d9009e | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 5598 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 5599 | mbedtls_test_update_key_usage_flags( usage_flags ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5600 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5601 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5602 | /* Export the key again if permitted by the key policy. */ |
| 5603 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5604 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5605 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5606 | second_export, export_size, |
| 5607 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5608 | ASSERT_COMPARE( first_export, first_exported_length, |
| 5609 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5610 | } |
| 5611 | |
| 5612 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5613 | if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5614 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5615 | |
| 5616 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5617 | /* |
| 5618 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5619 | * thus reset them as required. |
| 5620 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5621 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5622 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5623 | mbedtls_free( first_export ); |
| 5624 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5625 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5626 | psa_destroy_key( base_key ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5627 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5628 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5629 | } |
| 5630 | /* END_CASE */ |