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 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 22 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 23 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 24 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 25 | /** Test if a buffer contains a constant byte value. |
| 26 | * |
| 27 | * `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] | 28 | * |
| 29 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 30 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 31 | * \param size Size of the buffer in bytes. |
| 32 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 33 | * \return 1 if the buffer is all-bits-zero. |
| 34 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 35 | */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 36 | 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] | 37 | { |
| 38 | size_t i; |
| 39 | for( i = 0; i < size; i++ ) |
| 40 | { |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 41 | if( ( (unsigned char *) buffer )[i] != c ) |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 42 | return( 0 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 43 | } |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 44 | return( 1 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 45 | } |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 46 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 47 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
| 48 | static int asn1_write_10x( unsigned char **p, |
| 49 | unsigned char *start, |
| 50 | size_t bits, |
| 51 | unsigned char x ) |
| 52 | { |
| 53 | int ret; |
| 54 | int len = bits / 8 + 1; |
Gilles Peskine | 480416a | 2018-06-28 19:04:07 +0200 | [diff] [blame] | 55 | if( bits == 0 ) |
| 56 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
| 57 | if( bits <= 8 && x >= 1 << ( bits - 1 ) ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 58 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 59 | if( *p < start || *p - start < (ptrdiff_t) len ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 60 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 61 | *p -= len; |
| 62 | ( *p )[len-1] = x; |
| 63 | if( bits % 8 == 0 ) |
| 64 | ( *p )[1] |= 1; |
| 65 | else |
| 66 | ( *p )[0] |= 1 << ( bits % 8 ); |
| 67 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 68 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
| 69 | MBEDTLS_ASN1_INTEGER ) ); |
| 70 | return( len ); |
| 71 | } |
| 72 | |
| 73 | static int construct_fake_rsa_key( unsigned char *buffer, |
| 74 | size_t buffer_size, |
| 75 | unsigned char **p, |
| 76 | size_t bits, |
| 77 | int keypair ) |
| 78 | { |
| 79 | size_t half_bits = ( bits + 1 ) / 2; |
| 80 | int ret; |
| 81 | int len = 0; |
| 82 | /* Construct something that looks like a DER encoding of |
| 83 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 84 | * RSAPrivateKey ::= SEQUENCE { |
| 85 | * version Version, |
| 86 | * modulus INTEGER, -- n |
| 87 | * publicExponent INTEGER, -- e |
| 88 | * privateExponent INTEGER, -- d |
| 89 | * prime1 INTEGER, -- p |
| 90 | * prime2 INTEGER, -- q |
| 91 | * exponent1 INTEGER, -- d mod (p-1) |
| 92 | * exponent2 INTEGER, -- d mod (q-1) |
| 93 | * coefficient INTEGER, -- (inverse of q) mod p |
| 94 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 95 | * } |
| 96 | * Or, for a public key, the same structure with only |
| 97 | * version, modulus and publicExponent. |
| 98 | */ |
| 99 | *p = buffer + buffer_size; |
| 100 | if( keypair ) |
| 101 | { |
| 102 | MBEDTLS_ASN1_CHK_ADD( len, /* pq */ |
| 103 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 104 | MBEDTLS_ASN1_CHK_ADD( len, /* dq */ |
| 105 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 106 | MBEDTLS_ASN1_CHK_ADD( len, /* dp */ |
| 107 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 108 | MBEDTLS_ASN1_CHK_ADD( len, /* q */ |
| 109 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 110 | MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */ |
| 111 | asn1_write_10x( p, buffer, half_bits, 3 ) ); |
| 112 | MBEDTLS_ASN1_CHK_ADD( len, /* d */ |
| 113 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 114 | } |
| 115 | MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */ |
| 116 | asn1_write_10x( p, buffer, 17, 1 ) ); |
| 117 | MBEDTLS_ASN1_CHK_ADD( len, /* n */ |
| 118 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 119 | if( keypair ) |
| 120 | MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */ |
| 121 | mbedtls_asn1_write_int( p, buffer, 0 ) ); |
| 122 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) ); |
| 123 | { |
| 124 | const unsigned char tag = |
| 125 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
| 126 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) ); |
| 127 | } |
| 128 | return( len ); |
| 129 | } |
| 130 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 131 | int exercise_mac_setup( psa_key_type_t key_type, |
| 132 | const unsigned char *key_bytes, |
| 133 | size_t key_length, |
| 134 | psa_algorithm_t alg, |
| 135 | psa_mac_operation_t *operation, |
| 136 | psa_status_t *status ) |
| 137 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 138 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 139 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 140 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 141 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 142 | psa_set_key_algorithm( &attributes, alg ); |
| 143 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 144 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 145 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 146 | *status = psa_mac_sign_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 147 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 148 | PSA_ASSERT( psa_mac_abort( operation ) ); |
| 149 | /* If setup failed, reproduce the failure, so that the caller can |
| 150 | * test the resulting state of the operation object. */ |
| 151 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 152 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 153 | TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 156 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 157 | return( 1 ); |
| 158 | |
| 159 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 160 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 161 | return( 0 ); |
| 162 | } |
| 163 | |
| 164 | int exercise_cipher_setup( psa_key_type_t key_type, |
| 165 | const unsigned char *key_bytes, |
| 166 | size_t key_length, |
| 167 | psa_algorithm_t alg, |
| 168 | psa_cipher_operation_t *operation, |
| 169 | psa_status_t *status ) |
| 170 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 171 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 172 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 173 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 174 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 175 | psa_set_key_algorithm( &attributes, alg ); |
| 176 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 177 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 178 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 179 | *status = psa_cipher_encrypt_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 180 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 181 | PSA_ASSERT( psa_cipher_abort( operation ) ); |
| 182 | /* If setup failed, reproduce the failure, so that the caller can |
| 183 | * test the resulting state of the operation object. */ |
| 184 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 185 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 186 | TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ), |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 187 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 188 | } |
| 189 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 190 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 191 | return( 1 ); |
| 192 | |
| 193 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 194 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 195 | return( 0 ); |
| 196 | } |
| 197 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 198 | 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] | 199 | { |
| 200 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 201 | 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] | 202 | uint8_t buffer[1]; |
| 203 | size_t length; |
| 204 | int ok = 0; |
| 205 | |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 206 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 207 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 208 | psa_set_key_algorithm( &attributes, PSA_ALG_CTR ); |
| 209 | psa_set_key_type( &attributes, PSA_KEY_TYPE_AES ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 210 | TEST_EQUAL( psa_get_key_attributes( key, &attributes ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 211 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 212 | TEST_EQUAL( |
| 213 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 ); |
| 214 | TEST_EQUAL( |
| 215 | 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] | 216 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 217 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 218 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 219 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
| 220 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
| 221 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 222 | TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 223 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 224 | TEST_EQUAL( psa_export_public_key( key, |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 225 | buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 226 | PSA_ERROR_INVALID_HANDLE ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 227 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 228 | ok = 1; |
| 229 | |
| 230 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 231 | /* |
| 232 | * Key attributes may have been returned by psa_get_key_attributes() |
| 233 | * thus reset them as required. |
| 234 | */ |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 235 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 236 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 237 | return( ok ); |
| 238 | } |
| 239 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 240 | /* Assert that a key isn't reported as having a slot number. */ |
| 241 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 242 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 243 | do \ |
| 244 | { \ |
| 245 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
| 246 | TEST_EQUAL( psa_get_key_slot_number( \ |
| 247 | attributes, \ |
| 248 | &ASSERT_NO_SLOT_NUMBER_slot_number ), \ |
| 249 | PSA_ERROR_INVALID_ARGUMENT ); \ |
| 250 | } \ |
| 251 | while( 0 ) |
| 252 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 253 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 254 | ( (void) 0 ) |
| 255 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 256 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 257 | /* An overapproximation of the amount of storage needed for a key of the |
| 258 | * given type and with the given content. The API doesn't make it easy |
| 259 | * to find a good value for the size. The current implementation doesn't |
| 260 | * care about the value anyway. */ |
| 261 | #define KEY_BITS_FROM_DATA( type, data ) \ |
| 262 | ( data )->len |
| 263 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 264 | typedef enum { |
| 265 | IMPORT_KEY = 0, |
| 266 | GENERATE_KEY = 1, |
| 267 | DERIVE_KEY = 2 |
| 268 | } generate_method; |
| 269 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 270 | /* END_HEADER */ |
| 271 | |
| 272 | /* BEGIN_DEPENDENCIES |
| 273 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 274 | * END_DEPENDENCIES |
| 275 | */ |
| 276 | |
| 277 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 278 | void static_checks( ) |
| 279 | { |
| 280 | size_t max_truncated_mac_size = |
| 281 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 282 | |
| 283 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 284 | * encoding. The shifted mask is the maximum truncated value. The |
| 285 | * untruncated algorithm may be one byte larger. */ |
| 286 | TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 287 | |
| 288 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 289 | /* Check deprecated constants. */ |
| 290 | TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR ); |
| 291 | TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS ); |
| 292 | TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST ); |
| 293 | TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA ); |
| 294 | TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED ); |
| 295 | TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH ); |
| 296 | TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH ); |
| 297 | TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 298 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 299 | TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 300 | TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 301 | TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 302 | TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 303 | TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 304 | TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 305 | TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 306 | TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 307 | TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 308 | TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 309 | TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 ); |
| 310 | TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 311 | TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 312 | TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 313 | TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 314 | TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 315 | TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 316 | TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 317 | TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 318 | TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 319 | TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 320 | TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 321 | TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 322 | TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 ); |
| 323 | TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 ); |
| 324 | TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 ); |
| 325 | TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 ); |
| 326 | TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 ); |
| 327 | TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY ); |
| 328 | TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY ); |
| 329 | |
| 330 | TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 331 | TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 332 | TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 ); |
| 333 | TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 334 | TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 335 | TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 ); |
| 336 | TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 ); |
| 337 | TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY ); |
Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 338 | |
Paul Elliott | 75e2703 | 2020-06-03 15:17:39 +0100 | [diff] [blame] | 339 | TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 ); |
| 340 | TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 ); |
| 341 | TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 ); |
| 342 | TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 ); |
| 343 | TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 ); |
| 344 | |
| 345 | TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 ); |
| 346 | TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM ); |
Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 347 | #endif |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 348 | } |
| 349 | /* END_CASE */ |
| 350 | |
| 351 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 352 | void import_with_policy( int type_arg, |
| 353 | int usage_arg, int alg_arg, |
| 354 | int expected_status_arg ) |
| 355 | { |
| 356 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 357 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 358 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 359 | psa_key_type_t type = type_arg; |
| 360 | psa_key_usage_t usage = usage_arg; |
| 361 | psa_algorithm_t alg = alg_arg; |
| 362 | psa_status_t expected_status = expected_status_arg; |
| 363 | const uint8_t key_material[16] = {0}; |
| 364 | psa_status_t status; |
| 365 | |
| 366 | PSA_ASSERT( psa_crypto_init( ) ); |
| 367 | |
| 368 | psa_set_key_type( &attributes, type ); |
| 369 | psa_set_key_usage_flags( &attributes, usage ); |
| 370 | psa_set_key_algorithm( &attributes, alg ); |
| 371 | |
| 372 | status = psa_import_key( &attributes, |
| 373 | key_material, sizeof( key_material ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 374 | &key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 375 | TEST_EQUAL( status, expected_status ); |
| 376 | if( status != PSA_SUCCESS ) |
| 377 | goto exit; |
| 378 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 379 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 380 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
gabor-mezei-arm | 4d9009e | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 381 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame^] | 382 | mbedtls_test_update_key_usage_flags( usage ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 383 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 384 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 385 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 386 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 387 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 388 | |
| 389 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 390 | /* |
| 391 | * Key attributes may have been returned by psa_get_key_attributes() |
| 392 | * thus reset them as required. |
| 393 | */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 394 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 395 | |
| 396 | psa_destroy_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 397 | PSA_DONE( ); |
| 398 | } |
| 399 | /* END_CASE */ |
| 400 | |
| 401 | /* BEGIN_CASE */ |
| 402 | void import_with_data( data_t *data, int type_arg, |
| 403 | int attr_bits_arg, |
| 404 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 405 | { |
| 406 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 407 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 408 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 409 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 410 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 411 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 412 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 413 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 414 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 415 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 416 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 417 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 418 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 419 | status = psa_import_key( &attributes, data->x, data->len, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 420 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 421 | if( status != PSA_SUCCESS ) |
| 422 | goto exit; |
| 423 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 424 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 425 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 426 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 427 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 428 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 429 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 430 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 431 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 432 | |
| 433 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 434 | /* |
| 435 | * Key attributes may have been returned by psa_get_key_attributes() |
| 436 | * thus reset them as required. |
| 437 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 438 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 439 | |
| 440 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 441 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 442 | } |
| 443 | /* END_CASE */ |
| 444 | |
| 445 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 446 | void import_large_key( int type_arg, int byte_size_arg, |
| 447 | int expected_status_arg ) |
| 448 | { |
| 449 | psa_key_type_t type = type_arg; |
| 450 | size_t byte_size = byte_size_arg; |
| 451 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 452 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 453 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 454 | psa_status_t status; |
| 455 | uint8_t *buffer = NULL; |
| 456 | size_t buffer_size = byte_size + 1; |
| 457 | size_t n; |
| 458 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 459 | /* Skip the test case if the target running the test cannot |
| 460 | * accomodate large keys due to heap size constraints */ |
| 461 | ASSERT_ALLOC_WEAK( buffer, buffer_size ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 462 | memset( buffer, 'K', byte_size ); |
| 463 | |
| 464 | PSA_ASSERT( psa_crypto_init( ) ); |
| 465 | |
| 466 | /* Try importing the key */ |
| 467 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 468 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 469 | status = psa_import_key( &attributes, buffer, byte_size, &key ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 470 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 471 | TEST_EQUAL( status, expected_status ); |
| 472 | |
| 473 | if( status == PSA_SUCCESS ) |
| 474 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 475 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 476 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 477 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 478 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 479 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 480 | memset( buffer, 0, byte_size + 1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 481 | PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 482 | for( n = 0; n < byte_size; n++ ) |
| 483 | TEST_EQUAL( buffer[n], 'K' ); |
| 484 | for( n = byte_size; n < buffer_size; n++ ) |
| 485 | TEST_EQUAL( buffer[n], 0 ); |
| 486 | } |
| 487 | |
| 488 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 489 | /* |
| 490 | * Key attributes may have been returned by psa_get_key_attributes() |
| 491 | * thus reset them as required. |
| 492 | */ |
| 493 | psa_reset_key_attributes( &attributes ); |
| 494 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 495 | psa_destroy_key( key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 496 | PSA_DONE( ); |
| 497 | mbedtls_free( buffer ); |
| 498 | } |
| 499 | /* END_CASE */ |
| 500 | |
| 501 | /* BEGIN_CASE */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 502 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 503 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 504 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 505 | size_t bits = bits_arg; |
| 506 | psa_status_t expected_status = expected_status_arg; |
| 507 | psa_status_t status; |
| 508 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 509 | 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] | 510 | size_t buffer_size = /* Slight overapproximations */ |
| 511 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 512 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 513 | unsigned char *p; |
| 514 | int ret; |
| 515 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 516 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 517 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 518 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 519 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 520 | |
| 521 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 522 | bits, keypair ) ) >= 0 ); |
| 523 | length = ret; |
| 524 | |
| 525 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 526 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 527 | status = psa_import_key( &attributes, p, length, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 528 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 529 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 530 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 531 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 532 | |
| 533 | exit: |
| 534 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 535 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 536 | } |
| 537 | /* END_CASE */ |
| 538 | |
| 539 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 540 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 541 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 542 | int usage_arg, int alg_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 543 | int expected_bits, |
| 544 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 545 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 546 | int canonical_input ) |
| 547 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 548 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 549 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 550 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 551 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 552 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 553 | unsigned char *exported = NULL; |
| 554 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 555 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 556 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 557 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 558 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 559 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 560 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 561 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 562 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 563 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 564 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 565 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 566 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 567 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 568 | psa_set_key_algorithm( &attributes, alg ); |
| 569 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 570 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 571 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 572 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 573 | |
| 574 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 575 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 576 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 577 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 578 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 579 | |
| 580 | /* Export the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 581 | status = psa_export_key( key, exported, export_size, &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 582 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 583 | |
| 584 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 585 | * and export_size. On errors, the exported length must be 0. */ |
| 586 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 587 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
| 588 | TEST_ASSERT( exported_length <= export_size ); |
| 589 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 590 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 591 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 592 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 593 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 594 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 595 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 596 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 597 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 598 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 599 | * this validates the canonical representations. For canonical inputs, |
| 600 | * this doesn't directly validate the implementation, but it still helps |
| 601 | * by cross-validating the test data with the sanity check code. */ |
| 602 | if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) ) |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 603 | goto exit; |
| 604 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 605 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 606 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 607 | else |
| 608 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 609 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 610 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 611 | &key2 ) ); |
| 612 | PSA_ASSERT( psa_export_key( key2, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 613 | reexported, |
| 614 | export_size, |
| 615 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 616 | ASSERT_COMPARE( exported, exported_length, |
| 617 | reexported, reexported_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 618 | PSA_ASSERT( psa_destroy_key( key2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 619 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 620 | TEST_ASSERT( exported_length <= |
| 621 | PSA_EXPORT_KEY_OUTPUT_SIZE( type, |
| 622 | psa_get_key_bits( &got_attributes ) ) ); |
| 623 | TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 624 | |
| 625 | destroy: |
| 626 | /* Destroy the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 627 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 628 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 629 | |
| 630 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 631 | /* |
| 632 | * Key attributes may have been returned by psa_get_key_attributes() |
| 633 | * thus reset them as required. |
| 634 | */ |
| 635 | psa_reset_key_attributes( &got_attributes ); |
| 636 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 637 | mbedtls_free( exported ); |
| 638 | mbedtls_free( reexported ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 639 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 640 | } |
| 641 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 642 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 643 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 644 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 645 | int type_arg, |
| 646 | int alg_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 647 | int export_size_delta, |
| 648 | int expected_export_status_arg, |
| 649 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 650 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 651 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 652 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 653 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 654 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 655 | psa_status_t status; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 656 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 657 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 658 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 659 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 660 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 661 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 662 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 663 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 664 | psa_set_key_algorithm( &attributes, alg ); |
| 665 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 666 | |
| 667 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 668 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 669 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 670 | /* Export the public key */ |
| 671 | ASSERT_ALLOC( exported, export_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 672 | status = psa_export_public_key( key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 673 | exported, export_size, |
| 674 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 675 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 676 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 677 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 678 | 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] | 679 | size_t bits; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 680 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 681 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 682 | TEST_ASSERT( expected_public_key->len <= |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 683 | PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 684 | TEST_ASSERT( expected_public_key->len <= |
| 685 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 686 | TEST_ASSERT( expected_public_key->len <= |
| 687 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 688 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 689 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 690 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 691 | |
| 692 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 693 | /* |
| 694 | * Key attributes may have been returned by psa_get_key_attributes() |
| 695 | * thus reset them as required. |
| 696 | */ |
| 697 | psa_reset_key_attributes( &attributes ); |
| 698 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 699 | mbedtls_free( exported ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 700 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 701 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 702 | } |
| 703 | /* END_CASE */ |
| 704 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 705 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 706 | void import_and_exercise_key( data_t *data, |
| 707 | int type_arg, |
| 708 | int bits_arg, |
| 709 | int alg_arg ) |
| 710 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 711 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 712 | psa_key_type_t type = type_arg; |
| 713 | size_t bits = bits_arg; |
| 714 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 715 | 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] | 716 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 717 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 718 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 719 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 720 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 721 | psa_set_key_usage_flags( &attributes, usage ); |
| 722 | psa_set_key_algorithm( &attributes, alg ); |
| 723 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 724 | |
| 725 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 726 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 727 | |
| 728 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 729 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 730 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 731 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 732 | |
| 733 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 734 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 735 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 736 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 737 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 738 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 739 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 740 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 741 | /* |
| 742 | * Key attributes may have been returned by psa_get_key_attributes() |
| 743 | * thus reset them as required. |
| 744 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 745 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 746 | |
| 747 | psa_reset_key_attributes( &attributes ); |
| 748 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 749 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 750 | } |
| 751 | /* END_CASE */ |
| 752 | |
| 753 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 754 | void effective_key_attributes( int type_arg, int expected_type_arg, |
| 755 | int bits_arg, int expected_bits_arg, |
| 756 | int usage_arg, int expected_usage_arg, |
| 757 | int alg_arg, int expected_alg_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 758 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 759 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 760 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 761 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 762 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 763 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 764 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 765 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 766 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 767 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 768 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 769 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame^] | 770 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 771 | in the expected usage flags. */ |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame^] | 772 | TEST_EQUAL( expected_usage, mbedtls_test_update_key_usage_flags( usage ) ); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 773 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 774 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 775 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 776 | psa_set_key_usage_flags( &attributes, usage ); |
| 777 | psa_set_key_algorithm( &attributes, alg ); |
| 778 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 779 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 780 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 781 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 782 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 783 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 784 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 785 | TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type ); |
| 786 | TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits ); |
| 787 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 788 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 789 | |
| 790 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 791 | /* |
| 792 | * Key attributes may have been returned by psa_get_key_attributes() |
| 793 | * thus reset them as required. |
| 794 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 795 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 796 | |
| 797 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 798 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 799 | } |
| 800 | /* END_CASE */ |
| 801 | |
| 802 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 803 | void check_key_policy( int type_arg, int bits_arg, |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 804 | int usage_arg, int expected_usage_arg, int alg_arg ) |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 805 | { |
| 806 | test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg, |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 807 | usage_arg, expected_usage_arg, |
| 808 | alg_arg, alg_arg ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 809 | goto exit; |
| 810 | } |
| 811 | /* END_CASE */ |
| 812 | |
| 813 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 814 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 815 | { |
| 816 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 817 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 818 | * though it's OK by the C standard. We could test for this, but we'd need |
| 819 | * to supress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 820 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 821 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 822 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 823 | |
| 824 | memset( &zero, 0, sizeof( zero ) ); |
| 825 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 826 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 827 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 828 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 829 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 830 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 831 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 832 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 833 | |
| 834 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 835 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 836 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 837 | |
| 838 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 839 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 840 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 841 | |
| 842 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 843 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 844 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 845 | } |
| 846 | /* END_CASE */ |
| 847 | |
| 848 | /* BEGIN_CASE */ |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 849 | void mac_key_policy( int policy_usage_arg, |
| 850 | int policy_alg_arg, |
| 851 | int key_type_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 852 | data_t *key_data, |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 853 | int exercise_alg_arg, |
| 854 | int expected_usage_arg, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 855 | int expected_status_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 856 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 857 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 858 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 859 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 860 | psa_key_type_t key_type = key_type_arg; |
| 861 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 862 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 863 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 864 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 865 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 866 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 867 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 868 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame^] | 869 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 870 | in the expected usage flags. */ |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame^] | 871 | TEST_EQUAL( expected_usage, |
| 872 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 873 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 874 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 875 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 876 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 877 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 878 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 879 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 880 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 881 | &key ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 882 | |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 883 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 884 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 885 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 886 | if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 887 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 888 | else |
| 889 | TEST_EQUAL( status, expected_status ); |
| 890 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 891 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 892 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 893 | memset( mac, 0, sizeof( mac ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 894 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 895 | if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 896 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 897 | else |
| 898 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 899 | |
| 900 | exit: |
| 901 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 902 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 903 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 904 | } |
| 905 | /* END_CASE */ |
| 906 | |
| 907 | /* BEGIN_CASE */ |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 908 | void cipher_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 909 | int policy_alg, |
| 910 | int key_type, |
| 911 | data_t *key_data, |
| 912 | int exercise_alg ) |
| 913 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 914 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 915 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 916 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 917 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 918 | psa_status_t status; |
| 919 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 920 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 921 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 922 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 923 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 924 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 925 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 926 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 927 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 928 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame^] | 929 | /* Check if no key usage flag implication is done */ |
| 930 | TEST_EQUAL( policy_usage, |
| 931 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 932 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 933 | status = psa_cipher_encrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 934 | if( policy_alg == exercise_alg && |
| 935 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 936 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 937 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 938 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 939 | psa_cipher_abort( &operation ); |
| 940 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 941 | status = psa_cipher_decrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 942 | if( policy_alg == exercise_alg && |
| 943 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 944 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 945 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 946 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 947 | |
| 948 | exit: |
| 949 | psa_cipher_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 950 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 951 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 952 | } |
| 953 | /* END_CASE */ |
| 954 | |
| 955 | /* BEGIN_CASE */ |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 956 | void aead_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 957 | int policy_alg, |
| 958 | int key_type, |
| 959 | data_t *key_data, |
| 960 | int nonce_length_arg, |
| 961 | int tag_length_arg, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 962 | int exercise_alg, |
| 963 | int expected_status_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 964 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 965 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 966 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 967 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 968 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 969 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 970 | unsigned char nonce[16] = {0}; |
| 971 | size_t nonce_length = nonce_length_arg; |
| 972 | unsigned char tag[16]; |
| 973 | size_t tag_length = tag_length_arg; |
| 974 | size_t output_length; |
| 975 | |
| 976 | TEST_ASSERT( nonce_length <= sizeof( nonce ) ); |
| 977 | TEST_ASSERT( tag_length <= sizeof( tag ) ); |
| 978 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 979 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 980 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 981 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 982 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 983 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 984 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 985 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 986 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 987 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame^] | 988 | /* Check if no key usage implication is done */ |
| 989 | TEST_EQUAL( policy_usage, |
| 990 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 991 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 992 | status = psa_aead_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 993 | nonce, nonce_length, |
| 994 | NULL, 0, |
| 995 | NULL, 0, |
| 996 | tag, tag_length, |
| 997 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 998 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 999 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1000 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1001 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1002 | |
| 1003 | memset( tag, 0, sizeof( tag ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1004 | status = psa_aead_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1005 | nonce, nonce_length, |
| 1006 | NULL, 0, |
| 1007 | tag, tag_length, |
| 1008 | NULL, 0, |
| 1009 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1010 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 1011 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1012 | else if( expected_status == PSA_SUCCESS ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1013 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1014 | else |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1015 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1016 | |
| 1017 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1018 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1019 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1020 | } |
| 1021 | /* END_CASE */ |
| 1022 | |
| 1023 | /* BEGIN_CASE */ |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1024 | void asymmetric_encryption_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1025 | int policy_alg, |
| 1026 | int key_type, |
| 1027 | data_t *key_data, |
| 1028 | int exercise_alg ) |
| 1029 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1030 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1031 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1032 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1033 | psa_status_t status; |
| 1034 | size_t key_bits; |
| 1035 | size_t buffer_length; |
| 1036 | unsigned char *buffer = NULL; |
| 1037 | size_t output_length; |
| 1038 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1039 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1040 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1041 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1042 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1043 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1044 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1045 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1046 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1047 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame^] | 1048 | /* Check if no key usage implication is done */ |
| 1049 | TEST_EQUAL( policy_usage, |
| 1050 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1051 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1052 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1053 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1054 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 1055 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1056 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1057 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1058 | status = psa_asymmetric_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1059 | NULL, 0, |
| 1060 | NULL, 0, |
| 1061 | buffer, buffer_length, |
| 1062 | &output_length ); |
| 1063 | if( policy_alg == exercise_alg && |
| 1064 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1065 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1066 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1067 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1068 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 1069 | if( buffer_length != 0 ) |
| 1070 | memset( buffer, 0, buffer_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1071 | status = psa_asymmetric_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1072 | buffer, buffer_length, |
| 1073 | NULL, 0, |
| 1074 | buffer, buffer_length, |
| 1075 | &output_length ); |
| 1076 | if( policy_alg == exercise_alg && |
| 1077 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1078 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1079 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1080 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1081 | |
| 1082 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1083 | /* |
| 1084 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1085 | * thus reset them as required. |
| 1086 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1087 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1088 | |
| 1089 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1090 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1091 | mbedtls_free( buffer ); |
| 1092 | } |
| 1093 | /* END_CASE */ |
| 1094 | |
| 1095 | /* BEGIN_CASE */ |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1096 | void asymmetric_signature_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1097 | int policy_alg, |
| 1098 | int key_type, |
| 1099 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1100 | int exercise_alg, |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1101 | int payload_length_arg, |
| 1102 | int hashing_permitted, |
| 1103 | int expected_usage_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1104 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1105 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1106 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1107 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 1108 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1109 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1110 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 1111 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 1112 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 1113 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 1114 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 1115 | int compatible_alg = payload_length_arg > 0; |
| 1116 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1117 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1118 | size_t signature_length; |
| 1119 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame^] | 1120 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1121 | in the expected usage flags. */ |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame^] | 1122 | TEST_EQUAL( expected_usage, |
| 1123 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1124 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1125 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1126 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1127 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1128 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1129 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1130 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1131 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1132 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1133 | |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1134 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1135 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1136 | status = psa_sign_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1137 | payload, payload_length, |
| 1138 | signature, sizeof( signature ), |
| 1139 | &signature_length ); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1140 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1141 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1142 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1143 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1144 | |
| 1145 | memset( signature, 0, sizeof( signature ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1146 | status = psa_verify_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1147 | payload, payload_length, |
| 1148 | signature, sizeof( signature ) ); |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1149 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1150 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
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 | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1153 | |
gabor-mezei-arm | 3e5f6cd | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1154 | if( hashing_permitted ) |
| 1155 | { |
| 1156 | status = psa_sign_message( key, exercise_alg, |
| 1157 | payload, payload_length, |
| 1158 | signature, sizeof( signature ), |
| 1159 | &signature_length ); |
| 1160 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 ) |
| 1161 | PSA_ASSERT( status ); |
| 1162 | else |
| 1163 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1164 | |
| 1165 | memset( signature, 0, sizeof( signature ) ); |
| 1166 | status = psa_verify_message( key, exercise_alg, |
| 1167 | payload, payload_length, |
| 1168 | signature, sizeof( signature ) ); |
| 1169 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 ) |
| 1170 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 1171 | else |
| 1172 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1173 | } |
| 1174 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1175 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1176 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1177 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1178 | } |
| 1179 | /* END_CASE */ |
| 1180 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1181 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1182 | void derive_key_policy( int policy_usage, |
| 1183 | int policy_alg, |
| 1184 | int key_type, |
| 1185 | data_t *key_data, |
| 1186 | int exercise_alg ) |
| 1187 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1188 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1189 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1190 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1191 | psa_status_t status; |
| 1192 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1193 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1194 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1195 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1196 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1197 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1198 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1199 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1200 | &key ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1201 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1202 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 1203 | |
| 1204 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 1205 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1206 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1207 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 1208 | &operation, |
| 1209 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 1210 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1211 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1212 | |
| 1213 | status = psa_key_derivation_input_key( &operation, |
| 1214 | PSA_KEY_DERIVATION_INPUT_SECRET, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1215 | key ); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1216 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1217 | if( policy_alg == exercise_alg && |
| 1218 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1219 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1220 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1221 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1222 | |
| 1223 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1224 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1225 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1226 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1227 | } |
| 1228 | /* END_CASE */ |
| 1229 | |
| 1230 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1231 | void agreement_key_policy( int policy_usage, |
| 1232 | int policy_alg, |
| 1233 | int key_type_arg, |
| 1234 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1235 | int exercise_alg, |
| 1236 | int expected_status_arg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1237 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1238 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1239 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1240 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1241 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1242 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1243 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1244 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1245 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1246 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1247 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1248 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1249 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1250 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1251 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1252 | &key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1253 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1254 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1255 | status = mbedtls_test_psa_key_agreement_with_self( &operation, key ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1256 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1257 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1258 | |
| 1259 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1260 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1261 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1262 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1263 | } |
| 1264 | /* END_CASE */ |
| 1265 | |
| 1266 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1267 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 1268 | int usage_arg, int alg_arg, int alg2_arg ) |
| 1269 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1270 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1271 | psa_key_type_t key_type = key_type_arg; |
| 1272 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1273 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1274 | psa_key_usage_t usage = usage_arg; |
| 1275 | psa_algorithm_t alg = alg_arg; |
| 1276 | psa_algorithm_t alg2 = alg2_arg; |
| 1277 | |
| 1278 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1279 | |
| 1280 | psa_set_key_usage_flags( &attributes, usage ); |
| 1281 | psa_set_key_algorithm( &attributes, alg ); |
| 1282 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 1283 | psa_set_key_type( &attributes, key_type ); |
| 1284 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1285 | &key ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1286 | |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame^] | 1287 | /* Update the usage flags to obtain implicit usage flags */ |
| 1288 | usage = mbedtls_test_update_key_usage_flags( usage ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1289 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1290 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 1291 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 1292 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 1293 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1294 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1295 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1296 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1297 | goto exit; |
| 1298 | |
| 1299 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1300 | /* |
| 1301 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1302 | * thus reset them as required. |
| 1303 | */ |
| 1304 | psa_reset_key_attributes( &got_attributes ); |
| 1305 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1306 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1307 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1308 | } |
| 1309 | /* END_CASE */ |
| 1310 | |
| 1311 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1312 | void raw_agreement_key_policy( int policy_usage, |
| 1313 | int policy_alg, |
| 1314 | int key_type_arg, |
| 1315 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1316 | int exercise_alg, |
| 1317 | int expected_status_arg ) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1318 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1319 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1320 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1321 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1322 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1323 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1324 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1325 | |
| 1326 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1327 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1328 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1329 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1330 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1331 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1332 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1333 | &key ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1334 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1335 | status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1336 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1337 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1338 | |
| 1339 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1340 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1341 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1342 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1343 | } |
| 1344 | /* END_CASE */ |
| 1345 | |
| 1346 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1347 | void copy_success( int source_usage_arg, |
| 1348 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1349 | int type_arg, data_t *material, |
| 1350 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1351 | int target_usage_arg, |
| 1352 | int target_alg_arg, int target_alg2_arg, |
| 1353 | int expected_usage_arg, |
| 1354 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1355 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1356 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1357 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1358 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 1359 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1360 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1361 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1362 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1363 | uint8_t *export_buffer = NULL; |
| 1364 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1365 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1366 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1367 | /* Prepare the source key. */ |
| 1368 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 1369 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1370 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1371 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1372 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 1373 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1374 | &source_key ) ); |
| 1375 | PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1376 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1377 | /* Prepare the target attributes. */ |
| 1378 | if( copy_attributes ) |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1379 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1380 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1381 | /* Set volatile lifetime to reset the key identifier to 0. */ |
| 1382 | psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE ); |
| 1383 | } |
| 1384 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1385 | if( target_usage_arg != -1 ) |
| 1386 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 1387 | if( target_alg_arg != -1 ) |
| 1388 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1389 | if( target_alg2_arg != -1 ) |
| 1390 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1391 | |
| 1392 | /* Copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1393 | PSA_ASSERT( psa_copy_key( source_key, |
| 1394 | &target_attributes, &target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1395 | |
| 1396 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1397 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1398 | |
| 1399 | /* Test that the target slot has the expected content and policy. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1400 | PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1401 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 1402 | psa_get_key_type( &target_attributes ) ); |
| 1403 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 1404 | psa_get_key_bits( &target_attributes ) ); |
| 1405 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 1406 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1407 | TEST_EQUAL( expected_alg2, |
| 1408 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1409 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 1410 | { |
| 1411 | size_t length; |
| 1412 | ASSERT_ALLOC( export_buffer, material->len ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1413 | PSA_ASSERT( psa_export_key( target_key, export_buffer, |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1414 | material->len, &length ) ); |
| 1415 | ASSERT_COMPARE( material->x, material->len, |
| 1416 | export_buffer, length ); |
| 1417 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1418 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1419 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1420 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1421 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) ) |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1422 | goto exit; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1423 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1424 | PSA_ASSERT( psa_destroy_key( target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1425 | |
| 1426 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1427 | /* |
| 1428 | * Source and target key attributes may have been returned by |
| 1429 | * psa_get_key_attributes() thus reset them as required. |
| 1430 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1431 | psa_reset_key_attributes( &source_attributes ); |
| 1432 | psa_reset_key_attributes( &target_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1433 | |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1434 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1435 | mbedtls_free( export_buffer ); |
| 1436 | } |
| 1437 | /* END_CASE */ |
| 1438 | |
| 1439 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1440 | void copy_fail( int source_usage_arg, |
| 1441 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1442 | int type_arg, data_t *material, |
| 1443 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1444 | int target_usage_arg, |
| 1445 | int target_alg_arg, int target_alg2_arg, |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 1446 | int target_id_arg, int target_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1447 | int expected_status_arg ) |
| 1448 | { |
| 1449 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1450 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1451 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1452 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 1453 | 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] | 1454 | |
| 1455 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1456 | |
| 1457 | /* Prepare the source key. */ |
| 1458 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 1459 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1460 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1461 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1462 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 1463 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1464 | &source_key ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1465 | |
| 1466 | /* Prepare the target attributes. */ |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 1467 | psa_set_key_id( &target_attributes, key_id ); |
| 1468 | psa_set_key_lifetime( &target_attributes, target_lifetime_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1469 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 1470 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 1471 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 1472 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1473 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1474 | |
| 1475 | /* Try to copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1476 | TEST_EQUAL( psa_copy_key( source_key, |
| 1477 | &target_attributes, &target_key ), |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1478 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1479 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1480 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1481 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1482 | exit: |
| 1483 | psa_reset_key_attributes( &source_attributes ); |
| 1484 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1485 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1486 | } |
| 1487 | /* END_CASE */ |
| 1488 | |
| 1489 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1490 | void hash_operation_init( ) |
| 1491 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1492 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1493 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1494 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1495 | * though it's OK by the C standard. We could test for this, but we'd need |
| 1496 | * to supress the Clang warning for the test. */ |
| 1497 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 1498 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 1499 | psa_hash_operation_t zero; |
| 1500 | |
| 1501 | memset( &zero, 0, sizeof( zero ) ); |
| 1502 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1503 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1504 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 1505 | PSA_ERROR_BAD_STATE ); |
| 1506 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 1507 | PSA_ERROR_BAD_STATE ); |
| 1508 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 1509 | PSA_ERROR_BAD_STATE ); |
| 1510 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1511 | /* A default hash operation should be abortable without error. */ |
| 1512 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 1513 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 1514 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1515 | } |
| 1516 | /* END_CASE */ |
| 1517 | |
| 1518 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1519 | void hash_setup( int alg_arg, |
| 1520 | int expected_status_arg ) |
| 1521 | { |
| 1522 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1523 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1524 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1525 | psa_status_t status; |
| 1526 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1527 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1528 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1529 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1530 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1531 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 1532 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 1533 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1534 | |
| 1535 | /* If setup failed, reproduce the failure, so as to |
| 1536 | * test the resulting state of the operation object. */ |
| 1537 | if( status != PSA_SUCCESS ) |
| 1538 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 1539 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1540 | /* Now the operation object should be reusable. */ |
| 1541 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 1542 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 1543 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1544 | #endif |
| 1545 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1546 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1547 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1548 | } |
| 1549 | /* END_CASE */ |
| 1550 | |
| 1551 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1552 | void hash_compute_fail( int alg_arg, data_t *input, |
| 1553 | int output_size_arg, int expected_status_arg ) |
| 1554 | { |
| 1555 | psa_algorithm_t alg = alg_arg; |
| 1556 | uint8_t *output = NULL; |
| 1557 | size_t output_size = output_size_arg; |
| 1558 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 1559 | psa_status_t expected_status = expected_status_arg; |
| 1560 | psa_status_t status; |
| 1561 | |
| 1562 | ASSERT_ALLOC( output, output_size ); |
| 1563 | |
| 1564 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1565 | |
| 1566 | status = psa_hash_compute( alg, input->x, input->len, |
| 1567 | output, output_size, &output_length ); |
| 1568 | TEST_EQUAL( status, expected_status ); |
| 1569 | TEST_ASSERT( output_length <= output_size ); |
| 1570 | |
| 1571 | exit: |
| 1572 | mbedtls_free( output ); |
| 1573 | PSA_DONE( ); |
| 1574 | } |
| 1575 | /* END_CASE */ |
| 1576 | |
| 1577 | /* BEGIN_CASE */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 1578 | void hash_compare_fail( int alg_arg, data_t *input, |
| 1579 | data_t *reference_hash, |
| 1580 | int expected_status_arg ) |
| 1581 | { |
| 1582 | psa_algorithm_t alg = alg_arg; |
| 1583 | psa_status_t expected_status = expected_status_arg; |
| 1584 | psa_status_t status; |
| 1585 | |
| 1586 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1587 | |
| 1588 | status = psa_hash_compare( alg, input->x, input->len, |
| 1589 | reference_hash->x, reference_hash->len ); |
| 1590 | TEST_EQUAL( status, expected_status ); |
| 1591 | |
| 1592 | exit: |
| 1593 | PSA_DONE( ); |
| 1594 | } |
| 1595 | /* END_CASE */ |
| 1596 | |
| 1597 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1598 | void hash_compute_compare( int alg_arg, data_t *input, |
| 1599 | data_t *expected_output ) |
| 1600 | { |
| 1601 | psa_algorithm_t alg = alg_arg; |
| 1602 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 1603 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 1604 | size_t i; |
| 1605 | |
| 1606 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1607 | |
| 1608 | /* Compute with tight buffer */ |
| 1609 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1610 | output, PSA_HASH_LENGTH( alg ), |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1611 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1612 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1613 | ASSERT_COMPARE( output, output_length, |
| 1614 | expected_output->x, expected_output->len ); |
| 1615 | |
| 1616 | /* Compute with larger buffer */ |
| 1617 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
| 1618 | output, sizeof( output ), |
| 1619 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1620 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1621 | ASSERT_COMPARE( output, output_length, |
| 1622 | expected_output->x, expected_output->len ); |
| 1623 | |
| 1624 | /* Compare with correct hash */ |
| 1625 | PSA_ASSERT( psa_hash_compare( alg, input->x, input->len, |
| 1626 | output, output_length ) ); |
| 1627 | |
| 1628 | /* Compare with trailing garbage */ |
| 1629 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 1630 | output, output_length + 1 ), |
| 1631 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1632 | |
| 1633 | /* Compare with truncated hash */ |
| 1634 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 1635 | output, output_length - 1 ), |
| 1636 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1637 | |
| 1638 | /* Compare with corrupted value */ |
| 1639 | for( i = 0; i < output_length; i++ ) |
| 1640 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 1641 | mbedtls_test_set_step( i ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1642 | output[i] ^= 1; |
| 1643 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 1644 | output, output_length ), |
| 1645 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1646 | output[i] ^= 1; |
| 1647 | } |
| 1648 | |
| 1649 | exit: |
| 1650 | PSA_DONE( ); |
| 1651 | } |
| 1652 | /* END_CASE */ |
| 1653 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 1654 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1655 | void hash_bad_order( ) |
| 1656 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1657 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1658 | unsigned char input[] = ""; |
| 1659 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1660 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1661 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 1662 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 1663 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1664 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1665 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1666 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1667 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1668 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1669 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1670 | /* Call setup twice in a row. */ |
| 1671 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1672 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 1673 | PSA_ERROR_BAD_STATE ); |
| 1674 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1675 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1676 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1677 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1678 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1679 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1680 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1681 | /* Call update after finish. */ |
| 1682 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1683 | PSA_ASSERT( psa_hash_finish( &operation, |
| 1684 | hash, sizeof( hash ), &hash_len ) ); |
| 1685 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1686 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1687 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1688 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1689 | /* Call verify without calling setup beforehand. */ |
| 1690 | TEST_EQUAL( psa_hash_verify( &operation, |
| 1691 | valid_hash, sizeof( valid_hash ) ), |
| 1692 | PSA_ERROR_BAD_STATE ); |
| 1693 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1694 | |
| 1695 | /* Call verify after finish. */ |
| 1696 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1697 | PSA_ASSERT( psa_hash_finish( &operation, |
| 1698 | hash, sizeof( hash ), &hash_len ) ); |
| 1699 | TEST_EQUAL( psa_hash_verify( &operation, |
| 1700 | valid_hash, sizeof( valid_hash ) ), |
| 1701 | PSA_ERROR_BAD_STATE ); |
| 1702 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1703 | |
| 1704 | /* Call verify twice in a row. */ |
| 1705 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1706 | PSA_ASSERT( psa_hash_verify( &operation, |
| 1707 | valid_hash, sizeof( valid_hash ) ) ); |
| 1708 | TEST_EQUAL( psa_hash_verify( &operation, |
| 1709 | valid_hash, sizeof( valid_hash ) ), |
| 1710 | PSA_ERROR_BAD_STATE ); |
| 1711 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1712 | |
| 1713 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1714 | TEST_EQUAL( psa_hash_finish( &operation, |
| 1715 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1716 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1717 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1718 | |
| 1719 | /* Call finish twice in a row. */ |
| 1720 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1721 | PSA_ASSERT( psa_hash_finish( &operation, |
| 1722 | hash, sizeof( hash ), &hash_len ) ); |
| 1723 | TEST_EQUAL( psa_hash_finish( &operation, |
| 1724 | hash, sizeof( hash ), &hash_len ), |
| 1725 | PSA_ERROR_BAD_STATE ); |
| 1726 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1727 | |
| 1728 | /* Call finish after calling verify. */ |
| 1729 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1730 | PSA_ASSERT( psa_hash_verify( &operation, |
| 1731 | valid_hash, sizeof( valid_hash ) ) ); |
| 1732 | TEST_EQUAL( psa_hash_finish( &operation, |
| 1733 | hash, sizeof( hash ), &hash_len ), |
| 1734 | PSA_ERROR_BAD_STATE ); |
| 1735 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1736 | |
| 1737 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1738 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1739 | } |
| 1740 | /* END_CASE */ |
| 1741 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 1742 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1743 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1744 | { |
| 1745 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1746 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 1747 | * appended to it */ |
| 1748 | unsigned char hash[] = { |
| 1749 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 1750 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 1751 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1752 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1753 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1754 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1755 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1756 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1757 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1758 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1759 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1760 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1761 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1762 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1763 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1764 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1765 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1766 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1767 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1768 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1769 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1770 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 1771 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1772 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1773 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1774 | } |
| 1775 | /* END_CASE */ |
| 1776 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 1777 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 1778 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1779 | { |
| 1780 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 1781 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1782 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1783 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1784 | size_t hash_len; |
| 1785 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1786 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1787 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1788 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1789 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1790 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1791 | hash, expected_size - 1, &hash_len ), |
| 1792 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1793 | |
| 1794 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1795 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1796 | } |
| 1797 | /* END_CASE */ |
| 1798 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 1799 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1800 | void hash_clone_source_state( ) |
| 1801 | { |
| 1802 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 1803 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 1804 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 1805 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 1806 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 1807 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 1808 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 1809 | size_t hash_len; |
| 1810 | |
| 1811 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1812 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 1813 | |
| 1814 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 1815 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 1816 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 1817 | hash, sizeof( hash ), &hash_len ) ); |
| 1818 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 1819 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 1820 | |
| 1821 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 1822 | PSA_ERROR_BAD_STATE ); |
| 1823 | |
| 1824 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 1825 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 1826 | hash, sizeof( hash ), &hash_len ) ); |
| 1827 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 1828 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 1829 | hash, sizeof( hash ), &hash_len ) ); |
| 1830 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 1831 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 1832 | hash, sizeof( hash ), &hash_len ) ); |
| 1833 | |
| 1834 | exit: |
| 1835 | psa_hash_abort( &op_source ); |
| 1836 | psa_hash_abort( &op_init ); |
| 1837 | psa_hash_abort( &op_setup ); |
| 1838 | psa_hash_abort( &op_finished ); |
| 1839 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1840 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1841 | } |
| 1842 | /* END_CASE */ |
| 1843 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 1844 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1845 | void hash_clone_target_state( ) |
| 1846 | { |
| 1847 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 1848 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 1849 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 1850 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 1851 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 1852 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 1853 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 1854 | size_t hash_len; |
| 1855 | |
| 1856 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1857 | |
| 1858 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 1859 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 1860 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 1861 | hash, sizeof( hash ), &hash_len ) ); |
| 1862 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 1863 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 1864 | |
| 1865 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 1866 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 1867 | hash, sizeof( hash ), &hash_len ) ); |
| 1868 | |
| 1869 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 1870 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 1871 | PSA_ERROR_BAD_STATE ); |
| 1872 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 1873 | PSA_ERROR_BAD_STATE ); |
| 1874 | |
| 1875 | exit: |
| 1876 | psa_hash_abort( &op_target ); |
| 1877 | psa_hash_abort( &op_init ); |
| 1878 | psa_hash_abort( &op_setup ); |
| 1879 | psa_hash_abort( &op_finished ); |
| 1880 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1881 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1882 | } |
| 1883 | /* END_CASE */ |
| 1884 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1885 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1886 | void mac_operation_init( ) |
| 1887 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1888 | const uint8_t input[1] = { 0 }; |
| 1889 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1890 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1891 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1892 | * though it's OK by the C standard. We could test for this, but we'd need |
| 1893 | * to supress the Clang warning for the test. */ |
| 1894 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 1895 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 1896 | psa_mac_operation_t zero; |
| 1897 | |
| 1898 | memset( &zero, 0, sizeof( zero ) ); |
| 1899 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1900 | /* A freshly-initialized MAC operation should not be usable. */ |
| 1901 | TEST_EQUAL( psa_mac_update( &func, |
| 1902 | input, sizeof( input ) ), |
| 1903 | PSA_ERROR_BAD_STATE ); |
| 1904 | TEST_EQUAL( psa_mac_update( &init, |
| 1905 | input, sizeof( input ) ), |
| 1906 | PSA_ERROR_BAD_STATE ); |
| 1907 | TEST_EQUAL( psa_mac_update( &zero, |
| 1908 | input, sizeof( input ) ), |
| 1909 | PSA_ERROR_BAD_STATE ); |
| 1910 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1911 | /* A default MAC operation should be abortable without error. */ |
| 1912 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 1913 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 1914 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1915 | } |
| 1916 | /* END_CASE */ |
| 1917 | |
| 1918 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1919 | void mac_setup( int key_type_arg, |
| 1920 | data_t *key, |
| 1921 | int alg_arg, |
| 1922 | int expected_status_arg ) |
| 1923 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1924 | psa_key_type_t key_type = key_type_arg; |
| 1925 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1926 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1927 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1928 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 1929 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 1930 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 1931 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1932 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1933 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1934 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1935 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 1936 | &operation, &status ) ) |
| 1937 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1938 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1939 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1940 | /* The operation object should be reusable. */ |
| 1941 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 1942 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 1943 | smoke_test_key_data, |
| 1944 | sizeof( smoke_test_key_data ), |
| 1945 | KNOWN_SUPPORTED_MAC_ALG, |
| 1946 | &operation, &status ) ) |
| 1947 | goto exit; |
| 1948 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1949 | #endif |
| 1950 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1951 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1952 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1953 | } |
| 1954 | /* END_CASE */ |
| 1955 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 1956 | /* 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] | 1957 | void mac_bad_order( ) |
| 1958 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1959 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1960 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 1961 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1962 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1963 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 1964 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 1965 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1966 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1967 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 1968 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 1969 | size_t sign_mac_length = 0; |
| 1970 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 1971 | const uint8_t verify_mac[] = { |
| 1972 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 1973 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 1974 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 1975 | |
| 1976 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1977 | 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] | 1978 | psa_set_key_algorithm( &attributes, alg ); |
| 1979 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1980 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1981 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 1982 | &key ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1983 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1984 | /* Call update without calling setup beforehand. */ |
| 1985 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 1986 | PSA_ERROR_BAD_STATE ); |
| 1987 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1988 | |
| 1989 | /* Call sign finish without calling setup beforehand. */ |
| 1990 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 1991 | &sign_mac_length), |
| 1992 | PSA_ERROR_BAD_STATE ); |
| 1993 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1994 | |
| 1995 | /* Call verify finish without calling setup beforehand. */ |
| 1996 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 1997 | verify_mac, sizeof( verify_mac ) ), |
| 1998 | PSA_ERROR_BAD_STATE ); |
| 1999 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2000 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2001 | /* Call setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2002 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
| 2003 | TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2004 | PSA_ERROR_BAD_STATE ); |
| 2005 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2006 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2007 | /* Call update after sign finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2008 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2009 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2010 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2011 | sign_mac, sizeof( sign_mac ), |
| 2012 | &sign_mac_length ) ); |
| 2013 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2014 | PSA_ERROR_BAD_STATE ); |
| 2015 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2016 | |
| 2017 | /* Call update after verify finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2018 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2019 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2020 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2021 | verify_mac, sizeof( verify_mac ) ) ); |
| 2022 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2023 | PSA_ERROR_BAD_STATE ); |
| 2024 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2025 | |
| 2026 | /* Call sign finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2027 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2028 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2029 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2030 | sign_mac, sizeof( sign_mac ), |
| 2031 | &sign_mac_length ) ); |
| 2032 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2033 | sign_mac, sizeof( sign_mac ), |
| 2034 | &sign_mac_length ), |
| 2035 | PSA_ERROR_BAD_STATE ); |
| 2036 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2037 | |
| 2038 | /* Call verify finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2039 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2040 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2041 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2042 | verify_mac, sizeof( verify_mac ) ) ); |
| 2043 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2044 | verify_mac, sizeof( verify_mac ) ), |
| 2045 | PSA_ERROR_BAD_STATE ); |
| 2046 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2047 | |
| 2048 | /* Setup sign but try verify. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2049 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2050 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2051 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2052 | verify_mac, sizeof( verify_mac ) ), |
| 2053 | PSA_ERROR_BAD_STATE ); |
| 2054 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2055 | |
| 2056 | /* Setup verify but try sign. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2057 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2058 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2059 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2060 | sign_mac, sizeof( sign_mac ), |
| 2061 | &sign_mac_length ), |
| 2062 | PSA_ERROR_BAD_STATE ); |
| 2063 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2064 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2065 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2066 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2067 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2068 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2069 | } |
| 2070 | /* END_CASE */ |
| 2071 | |
| 2072 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2073 | void mac_sign( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2074 | data_t *key_data, |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2075 | int alg_arg, |
| 2076 | data_t *input, |
| 2077 | data_t *expected_mac ) |
| 2078 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2079 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2080 | psa_key_type_t key_type = key_type_arg; |
| 2081 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2082 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2083 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2084 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2085 | size_t mac_buffer_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2086 | 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] | 2087 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2088 | const size_t output_sizes_to_test[] = { |
| 2089 | 0, |
| 2090 | 1, |
| 2091 | expected_mac->len - 1, |
| 2092 | expected_mac->len, |
| 2093 | expected_mac->len + 1, |
| 2094 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2095 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2096 | TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2097 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 3d404d6 | 2020-08-25 23:47:36 +0200 | [diff] [blame] | 2098 | TEST_ASSERT( expected_mac->len == mac_buffer_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2099 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2100 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2101 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2102 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2103 | psa_set_key_algorithm( &attributes, alg ); |
| 2104 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2105 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2106 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2107 | &key ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2108 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2109 | for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ ) |
| 2110 | { |
| 2111 | const size_t output_size = output_sizes_to_test[i]; |
| 2112 | psa_status_t expected_status = |
| 2113 | ( output_size >= expected_mac->len ? PSA_SUCCESS : |
| 2114 | PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2115 | |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2116 | mbedtls_test_set_step( output_size ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2117 | ASSERT_ALLOC( actual_mac, output_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2118 | |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2119 | /* Calculate the MAC, one-shot case. */ |
| 2120 | TEST_EQUAL( psa_mac_compute( key, alg, |
| 2121 | input->x, input->len, |
| 2122 | actual_mac, output_size, &mac_length ), |
| 2123 | expected_status ); |
| 2124 | if( expected_status == PSA_SUCCESS ) |
| 2125 | { |
| 2126 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 2127 | actual_mac, mac_length ); |
| 2128 | } |
| 2129 | |
| 2130 | if( output_size > 0 ) |
| 2131 | memset( actual_mac, 0, output_size ); |
| 2132 | |
| 2133 | /* Calculate the MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2134 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2135 | PSA_ASSERT( psa_mac_update( &operation, |
| 2136 | input->x, input->len ) ); |
| 2137 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2138 | actual_mac, output_size, |
| 2139 | &mac_length ), |
| 2140 | expected_status ); |
| 2141 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2142 | |
| 2143 | if( expected_status == PSA_SUCCESS ) |
| 2144 | { |
| 2145 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 2146 | actual_mac, mac_length ); |
| 2147 | } |
| 2148 | mbedtls_free( actual_mac ); |
| 2149 | actual_mac = NULL; |
| 2150 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2151 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2152 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2153 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2154 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2155 | PSA_DONE( ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2156 | mbedtls_free( actual_mac ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2157 | } |
| 2158 | /* END_CASE */ |
| 2159 | |
| 2160 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2161 | void mac_verify( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2162 | data_t *key_data, |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2163 | int alg_arg, |
| 2164 | data_t *input, |
| 2165 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2166 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2167 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2168 | psa_key_type_t key_type = key_type_arg; |
| 2169 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2170 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2171 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2172 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2173 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 2174 | TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE ); |
| 2175 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2176 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2177 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2178 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2179 | psa_set_key_algorithm( &attributes, alg ); |
| 2180 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2181 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2182 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2183 | &key ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2184 | |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2185 | /* Verify correct MAC, one-shot case. */ |
| 2186 | PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len, |
| 2187 | expected_mac->x, expected_mac->len ) ); |
| 2188 | |
| 2189 | /* Verify correct MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2190 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2191 | PSA_ASSERT( psa_mac_update( &operation, |
| 2192 | input->x, input->len ) ); |
| 2193 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2194 | expected_mac->x, |
| 2195 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2196 | |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2197 | /* Test a MAC that's too short, one-shot case. */ |
| 2198 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 2199 | input->x, input->len, |
| 2200 | expected_mac->x, |
| 2201 | expected_mac->len - 1 ), |
| 2202 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2203 | |
| 2204 | /* Test a MAC that's too short, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2205 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2206 | PSA_ASSERT( psa_mac_update( &operation, |
| 2207 | input->x, input->len ) ); |
| 2208 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2209 | expected_mac->x, |
| 2210 | expected_mac->len - 1 ), |
| 2211 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2212 | |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2213 | /* Test a MAC that's too long, one-shot case. */ |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2214 | ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 ); |
| 2215 | memcpy( perturbed_mac, expected_mac->x, expected_mac->len ); |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2216 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 2217 | input->x, input->len, |
| 2218 | perturbed_mac, expected_mac->len + 1 ), |
| 2219 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2220 | |
| 2221 | /* Test a MAC that's too long, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2222 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2223 | PSA_ASSERT( psa_mac_update( &operation, |
| 2224 | input->x, input->len ) ); |
| 2225 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2226 | perturbed_mac, |
| 2227 | expected_mac->len + 1 ), |
| 2228 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2229 | |
| 2230 | /* Test changing one byte. */ |
| 2231 | for( size_t i = 0; i < expected_mac->len; i++ ) |
| 2232 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2233 | mbedtls_test_set_step( i ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2234 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | a93e423 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2235 | |
| 2236 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 2237 | input->x, input->len, |
| 2238 | perturbed_mac, expected_mac->len ), |
| 2239 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2240 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2241 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2242 | PSA_ASSERT( psa_mac_update( &operation, |
| 2243 | input->x, input->len ) ); |
| 2244 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2245 | perturbed_mac, |
| 2246 | expected_mac->len ), |
| 2247 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2248 | perturbed_mac[i] ^= 1; |
| 2249 | } |
| 2250 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2251 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2252 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2253 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2254 | PSA_DONE( ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2255 | mbedtls_free( perturbed_mac ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2256 | } |
| 2257 | /* END_CASE */ |
| 2258 | |
| 2259 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2260 | void cipher_operation_init( ) |
| 2261 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2262 | const uint8_t input[1] = { 0 }; |
| 2263 | unsigned char output[1] = { 0 }; |
| 2264 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2265 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2266 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2267 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2268 | * to supress the Clang warning for the test. */ |
| 2269 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 2270 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 2271 | psa_cipher_operation_t zero; |
| 2272 | |
| 2273 | memset( &zero, 0, sizeof( zero ) ); |
| 2274 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2275 | /* A freshly-initialized cipher operation should not be usable. */ |
| 2276 | TEST_EQUAL( psa_cipher_update( &func, |
| 2277 | input, sizeof( input ), |
| 2278 | output, sizeof( output ), |
| 2279 | &output_length ), |
| 2280 | PSA_ERROR_BAD_STATE ); |
| 2281 | TEST_EQUAL( psa_cipher_update( &init, |
| 2282 | input, sizeof( input ), |
| 2283 | output, sizeof( output ), |
| 2284 | &output_length ), |
| 2285 | PSA_ERROR_BAD_STATE ); |
| 2286 | TEST_EQUAL( psa_cipher_update( &zero, |
| 2287 | input, sizeof( input ), |
| 2288 | output, sizeof( output ), |
| 2289 | &output_length ), |
| 2290 | PSA_ERROR_BAD_STATE ); |
| 2291 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2292 | /* A default cipher operation should be abortable without error. */ |
| 2293 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 2294 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 2295 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2296 | } |
| 2297 | /* END_CASE */ |
| 2298 | |
| 2299 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2300 | void cipher_setup( int key_type_arg, |
| 2301 | data_t *key, |
| 2302 | int alg_arg, |
| 2303 | int expected_status_arg ) |
| 2304 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2305 | psa_key_type_t key_type = key_type_arg; |
| 2306 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2307 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2308 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2309 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 2310 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2311 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2312 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2313 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2314 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2315 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2316 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 2317 | &operation, &status ) ) |
| 2318 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2319 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2320 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2321 | /* The operation object should be reusable. */ |
| 2322 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 2323 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 2324 | smoke_test_key_data, |
| 2325 | sizeof( smoke_test_key_data ), |
| 2326 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 2327 | &operation, &status ) ) |
| 2328 | goto exit; |
| 2329 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2330 | #endif |
| 2331 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2332 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2333 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2334 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2335 | } |
| 2336 | /* END_CASE */ |
| 2337 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2338 | /* 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] | 2339 | void cipher_bad_order( ) |
| 2340 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2341 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2342 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 2343 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2344 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2345 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2346 | 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] | 2347 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2348 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2349 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 2350 | const uint8_t text[] = { |
| 2351 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 2352 | 0xbb, 0xbb, 0xbb, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2353 | 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] | 2354 | size_t length = 0; |
| 2355 | |
| 2356 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2357 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 2358 | psa_set_key_algorithm( &attributes, alg ); |
| 2359 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2360 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 2361 | &key ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2362 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2363 | /* Call encrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2364 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 2365 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2366 | PSA_ERROR_BAD_STATE ); |
| 2367 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2368 | |
| 2369 | /* Call decrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2370 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 2371 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2372 | PSA_ERROR_BAD_STATE ); |
| 2373 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2374 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2375 | /* Generate an IV without calling setup beforehand. */ |
| 2376 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2377 | buffer, sizeof( buffer ), |
| 2378 | &length ), |
| 2379 | PSA_ERROR_BAD_STATE ); |
| 2380 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2381 | |
| 2382 | /* Generate an IV twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2383 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2384 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 2385 | buffer, sizeof( buffer ), |
| 2386 | &length ) ); |
| 2387 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2388 | buffer, sizeof( buffer ), |
| 2389 | &length ), |
| 2390 | PSA_ERROR_BAD_STATE ); |
| 2391 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2392 | |
| 2393 | /* Generate an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2394 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2395 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2396 | iv, sizeof( iv ) ) ); |
| 2397 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2398 | buffer, sizeof( buffer ), |
| 2399 | &length ), |
| 2400 | PSA_ERROR_BAD_STATE ); |
| 2401 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2402 | |
| 2403 | /* Set an IV without calling setup beforehand. */ |
| 2404 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2405 | iv, sizeof( iv ) ), |
| 2406 | PSA_ERROR_BAD_STATE ); |
| 2407 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2408 | |
| 2409 | /* Set an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2410 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2411 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2412 | iv, sizeof( iv ) ) ); |
| 2413 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2414 | iv, sizeof( iv ) ), |
| 2415 | PSA_ERROR_BAD_STATE ); |
| 2416 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2417 | |
| 2418 | /* Set an IV after it's already generated. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2419 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2420 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 2421 | buffer, sizeof( buffer ), |
| 2422 | &length ) ); |
| 2423 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2424 | iv, sizeof( iv ) ), |
| 2425 | PSA_ERROR_BAD_STATE ); |
| 2426 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2427 | |
| 2428 | /* Call update without calling setup beforehand. */ |
| 2429 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2430 | text, sizeof( text ), |
| 2431 | buffer, sizeof( buffer ), |
| 2432 | &length ), |
| 2433 | PSA_ERROR_BAD_STATE ); |
| 2434 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2435 | |
| 2436 | /* Call update without an IV where an IV is required. */ |
| 2437 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2438 | text, sizeof( text ), |
| 2439 | buffer, sizeof( buffer ), |
| 2440 | &length ), |
| 2441 | PSA_ERROR_BAD_STATE ); |
| 2442 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2443 | |
| 2444 | /* Call update after finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2445 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2446 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2447 | iv, sizeof( iv ) ) ); |
| 2448 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2449 | buffer, sizeof( buffer ), &length ) ); |
| 2450 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2451 | text, sizeof( text ), |
| 2452 | buffer, sizeof( buffer ), |
| 2453 | &length ), |
| 2454 | PSA_ERROR_BAD_STATE ); |
| 2455 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2456 | |
| 2457 | /* Call finish without calling setup beforehand. */ |
| 2458 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2459 | buffer, sizeof( buffer ), &length ), |
| 2460 | PSA_ERROR_BAD_STATE ); |
| 2461 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2462 | |
| 2463 | /* Call finish without an IV where an IV is required. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2464 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2465 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 2466 | * for cipher modes with padding. */ |
| 2467 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2468 | buffer, sizeof( buffer ), &length ), |
| 2469 | PSA_ERROR_BAD_STATE ); |
| 2470 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2471 | |
| 2472 | /* Call finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2473 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2474 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2475 | iv, sizeof( iv ) ) ); |
| 2476 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2477 | buffer, sizeof( buffer ), &length ) ); |
| 2478 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2479 | buffer, sizeof( buffer ), &length ), |
| 2480 | PSA_ERROR_BAD_STATE ); |
| 2481 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2482 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2483 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2484 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2485 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2486 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2487 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2488 | } |
| 2489 | /* END_CASE */ |
| 2490 | |
| 2491 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2492 | void cipher_encrypt( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2493 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2494 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2495 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2496 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2497 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2498 | psa_status_t status; |
| 2499 | psa_key_type_t key_type = key_type_arg; |
| 2500 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2501 | psa_status_t expected_status = expected_status_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2502 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2503 | size_t output_buffer_size = 0; |
| 2504 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2505 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2506 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2507 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2508 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2509 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2510 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2511 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2512 | psa_set_key_algorithm( &attributes, alg ); |
| 2513 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2514 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2515 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2516 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2517 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2518 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2519 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2520 | if( iv->len > 0 ) |
| 2521 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 2522 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2523 | } |
| 2524 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2525 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 2526 | TEST_ASSERT( output_buffer_size <= |
| 2527 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2528 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2529 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2530 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2531 | input->x, input->len, |
| 2532 | output, output_buffer_size, |
| 2533 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2534 | TEST_ASSERT( function_output_length <= |
| 2535 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 2536 | TEST_ASSERT( function_output_length <= |
| 2537 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2538 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2539 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2540 | status = psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 2541 | ( output_buffer_size == 0 ? NULL : |
| 2542 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2543 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2544 | &function_output_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2545 | TEST_ASSERT( function_output_length <= |
| 2546 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 2547 | TEST_ASSERT( function_output_length <= |
| 2548 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2549 | total_output_length += function_output_length; |
| 2550 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2551 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2552 | if( expected_status == PSA_SUCCESS ) |
| 2553 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2554 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2555 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2556 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2557 | } |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2558 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2559 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2560 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2561 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2562 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2563 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2564 | } |
| 2565 | /* END_CASE */ |
| 2566 | |
| 2567 | /* BEGIN_CASE */ |
| 2568 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2569 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2570 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2571 | int first_part_size_arg, |
| 2572 | int output1_length_arg, int output2_length_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2573 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2574 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2575 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2576 | psa_key_type_t key_type = key_type_arg; |
| 2577 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2578 | size_t first_part_size = first_part_size_arg; |
| 2579 | size_t output1_length = output1_length_arg; |
| 2580 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2581 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2582 | size_t output_buffer_size = 0; |
| 2583 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2584 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2585 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2586 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2587 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2588 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2589 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2590 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2591 | psa_set_key_algorithm( &attributes, alg ); |
| 2592 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2593 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2594 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2595 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2596 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2597 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2598 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2599 | if( iv->len > 0 ) |
| 2600 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 2601 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2602 | } |
| 2603 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2604 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 2605 | TEST_ASSERT( output_buffer_size <= |
| 2606 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2607 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2608 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2609 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2610 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 2611 | output, output_buffer_size, |
| 2612 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2613 | TEST_ASSERT( function_output_length == output1_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2614 | TEST_ASSERT( function_output_length <= |
| 2615 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 2616 | TEST_ASSERT( function_output_length <= |
| 2617 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2618 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2619 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2620 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2621 | input->x + first_part_size, |
| 2622 | input->len - first_part_size, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 2623 | ( output_buffer_size == 0 ? NULL : |
| 2624 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2625 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2626 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2627 | TEST_ASSERT( function_output_length == output2_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2628 | TEST_ASSERT( function_output_length <= |
| 2629 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 2630 | alg, |
| 2631 | input->len - first_part_size ) ); |
| 2632 | TEST_ASSERT( function_output_length <= |
| 2633 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2634 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2635 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2636 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 2637 | ( output_buffer_size == 0 ? NULL : |
| 2638 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2639 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2640 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2641 | TEST_ASSERT( function_output_length <= |
| 2642 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 2643 | TEST_ASSERT( function_output_length <= |
| 2644 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2645 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2646 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2647 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2648 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2649 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2650 | |
| 2651 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2652 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2653 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2654 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2655 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2656 | } |
| 2657 | /* END_CASE */ |
| 2658 | |
| 2659 | /* BEGIN_CASE */ |
| 2660 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2661 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2662 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2663 | int first_part_size_arg, |
| 2664 | int output1_length_arg, int output2_length_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2665 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2666 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2667 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2668 | psa_key_type_t key_type = key_type_arg; |
| 2669 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2670 | size_t first_part_size = first_part_size_arg; |
| 2671 | size_t output1_length = output1_length_arg; |
| 2672 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2673 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2674 | size_t output_buffer_size = 0; |
| 2675 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2676 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2677 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2678 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2679 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2680 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2681 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2682 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 2683 | psa_set_key_algorithm( &attributes, alg ); |
| 2684 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2685 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2686 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2687 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2688 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2689 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2690 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 2691 | if( iv->len > 0 ) |
| 2692 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 2693 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2694 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2695 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2696 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 2697 | TEST_ASSERT( output_buffer_size <= |
| 2698 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2699 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2700 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2701 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2702 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2703 | input->x, first_part_size, |
| 2704 | output, output_buffer_size, |
| 2705 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2706 | TEST_ASSERT( function_output_length == output1_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2707 | TEST_ASSERT( function_output_length <= |
| 2708 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 2709 | TEST_ASSERT( function_output_length <= |
| 2710 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2711 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2712 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2713 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2714 | input->x + first_part_size, |
| 2715 | input->len - first_part_size, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 2716 | ( output_buffer_size == 0 ? NULL : |
| 2717 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2718 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2719 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2720 | TEST_ASSERT( function_output_length == output2_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2721 | TEST_ASSERT( function_output_length <= |
| 2722 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 2723 | alg, |
| 2724 | input->len - first_part_size ) ); |
| 2725 | TEST_ASSERT( function_output_length <= |
| 2726 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2727 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2728 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2729 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 2730 | ( output_buffer_size == 0 ? NULL : |
| 2731 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2732 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2733 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2734 | TEST_ASSERT( function_output_length <= |
| 2735 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 2736 | TEST_ASSERT( function_output_length <= |
| 2737 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2738 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2739 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2740 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2741 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2742 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2743 | |
| 2744 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2745 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2746 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2747 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2748 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2749 | } |
| 2750 | /* END_CASE */ |
| 2751 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2752 | /* BEGIN_CASE */ |
| 2753 | void cipher_decrypt( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2754 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2755 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2756 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2757 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2758 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2759 | psa_status_t status; |
| 2760 | psa_key_type_t key_type = key_type_arg; |
| 2761 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2762 | psa_status_t expected_status = expected_status_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2763 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2764 | size_t output_buffer_size = 0; |
| 2765 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2766 | size_t total_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_DECRYPT ); |
| 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 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2776 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2777 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2778 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2779 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2780 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 2781 | if( iv->len > 0 ) |
| 2782 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 2783 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2784 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2785 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2786 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 2787 | TEST_ASSERT( output_buffer_size <= |
| 2788 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2789 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2790 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2791 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2792 | input->x, input->len, |
| 2793 | output, output_buffer_size, |
| 2794 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2795 | TEST_ASSERT( function_output_length <= |
| 2796 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 2797 | TEST_ASSERT( function_output_length <= |
| 2798 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2799 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2800 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2801 | status = psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 2802 | ( output_buffer_size == 0 ? NULL : |
| 2803 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2804 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2805 | &function_output_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2806 | TEST_ASSERT( function_output_length <= |
| 2807 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 2808 | TEST_ASSERT( function_output_length <= |
| 2809 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2810 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2811 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2812 | |
| 2813 | if( expected_status == PSA_SUCCESS ) |
| 2814 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2815 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2816 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2817 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2818 | } |
| 2819 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2820 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2821 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2822 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2823 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2824 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2825 | } |
| 2826 | /* END_CASE */ |
| 2827 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2828 | /* BEGIN_CASE */ |
| 2829 | void cipher_verify_output( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2830 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2831 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2832 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2833 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2834 | psa_key_type_t key_type = key_type_arg; |
| 2835 | psa_algorithm_t alg = alg_arg; |
mohammad1603 | e6b67a1 | 2018-03-12 10:38:49 -0700 | [diff] [blame] | 2836 | unsigned char iv[16] = {0}; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2837 | size_t iv_size = 16; |
| 2838 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2839 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2840 | size_t output1_size = 0; |
| 2841 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2842 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2843 | size_t output2_size = 0; |
| 2844 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2845 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2846 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 2847 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2848 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2849 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2850 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2851 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2852 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 2853 | psa_set_key_algorithm( &attributes, alg ); |
| 2854 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2855 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2856 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2857 | &key ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2858 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2859 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 2860 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2861 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 2862 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 2863 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 2864 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 2865 | iv, iv_size, |
| 2866 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2867 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2868 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 2869 | TEST_ASSERT( output1_size <= |
| 2870 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2871 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2872 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2873 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len, |
| 2874 | output1, output1_size, |
| 2875 | &output1_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2876 | TEST_ASSERT( output1_length <= |
| 2877 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 2878 | TEST_ASSERT( output1_length <= |
| 2879 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
| 2880 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2881 | PSA_ASSERT( psa_cipher_finish( &operation1, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2882 | output1 + output1_length, |
| 2883 | output1_size - output1_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2884 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2885 | TEST_ASSERT( function_output_length <= |
| 2886 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 2887 | TEST_ASSERT( function_output_length <= |
| 2888 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 2889 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2890 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2891 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2892 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2893 | |
| 2894 | output2_size = output1_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2895 | TEST_ASSERT( output2_size <= |
| 2896 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 2897 | TEST_ASSERT( output2_size <= |
| 2898 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2899 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2900 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 2901 | if( iv_length > 0 ) |
| 2902 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 2903 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 2904 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2905 | } |
| 2906 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2907 | PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length, |
| 2908 | output2, output2_size, |
| 2909 | &output2_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2910 | TEST_ASSERT( output2_length <= |
| 2911 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 2912 | TEST_ASSERT( output2_length <= |
| 2913 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) ); |
| 2914 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2915 | function_output_length = 0; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2916 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 2917 | output2 + output2_length, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2918 | output2_size - output2_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2919 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2920 | TEST_ASSERT( function_output_length <= |
| 2921 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 2922 | TEST_ASSERT( function_output_length <= |
| 2923 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2924 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2925 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 2926 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2927 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2928 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2929 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2930 | |
| 2931 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2932 | psa_cipher_abort( &operation1 ); |
| 2933 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2934 | mbedtls_free( output1 ); |
| 2935 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2936 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2937 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2938 | } |
| 2939 | /* END_CASE */ |
| 2940 | |
| 2941 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2942 | void cipher_verify_output_multipart( int alg_arg, |
| 2943 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2944 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2945 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2946 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2947 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2948 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2949 | psa_key_type_t key_type = key_type_arg; |
| 2950 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2951 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2952 | unsigned char iv[16] = {0}; |
| 2953 | size_t iv_size = 16; |
| 2954 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2955 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2956 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2957 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2958 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2959 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2960 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2961 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2962 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 2963 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2964 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2965 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2966 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2967 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2968 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 2969 | psa_set_key_algorithm( &attributes, alg ); |
| 2970 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2971 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2972 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2973 | &key ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2974 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2975 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 2976 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2977 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 2978 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 2979 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 2980 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 2981 | iv, iv_size, |
| 2982 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2983 | } |
| 2984 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2985 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 2986 | TEST_ASSERT( output1_buffer_size <= |
| 2987 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2988 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2989 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2990 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 2991 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2992 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 2993 | output1, output1_buffer_size, |
| 2994 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2995 | TEST_ASSERT( function_output_length <= |
| 2996 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 2997 | TEST_ASSERT( function_output_length <= |
| 2998 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2999 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3000 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3001 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 3002 | input->x + first_part_size, |
| 3003 | input->len - first_part_size, |
| 3004 | output1, output1_buffer_size, |
| 3005 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3006 | TEST_ASSERT( function_output_length <= |
| 3007 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3008 | alg, |
| 3009 | input->len - first_part_size ) ); |
| 3010 | TEST_ASSERT( function_output_length <= |
| 3011 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3012 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3013 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3014 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 3015 | output1 + output1_length, |
| 3016 | output1_buffer_size - output1_length, |
| 3017 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3018 | TEST_ASSERT( function_output_length <= |
| 3019 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3020 | TEST_ASSERT( function_output_length <= |
| 3021 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3022 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3023 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3024 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3025 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3026 | output2_buffer_size = output1_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3027 | TEST_ASSERT( output2_buffer_size <= |
| 3028 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 3029 | TEST_ASSERT( output2_buffer_size <= |
| 3030 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3031 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3032 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3033 | if( iv_length > 0 ) |
| 3034 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3035 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3036 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3037 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3038 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3039 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 3040 | output2, output2_buffer_size, |
| 3041 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3042 | TEST_ASSERT( function_output_length <= |
| 3043 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3044 | TEST_ASSERT( function_output_length <= |
| 3045 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3046 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3047 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3048 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 3049 | output1 + first_part_size, |
| 3050 | output1_length - first_part_size, |
| 3051 | output2, output2_buffer_size, |
| 3052 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3053 | TEST_ASSERT( function_output_length <= |
| 3054 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3055 | alg, |
| 3056 | output1_length - first_part_size ) ); |
| 3057 | TEST_ASSERT( function_output_length <= |
| 3058 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3059 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3060 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3061 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3062 | output2 + output2_length, |
| 3063 | output2_buffer_size - output2_length, |
| 3064 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3065 | TEST_ASSERT( function_output_length <= |
| 3066 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3067 | TEST_ASSERT( function_output_length <= |
| 3068 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3069 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3070 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3071 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3072 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3073 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3074 | |
| 3075 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3076 | psa_cipher_abort( &operation1 ); |
| 3077 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3078 | mbedtls_free( output1 ); |
| 3079 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3080 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3081 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3082 | } |
| 3083 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 3084 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3085 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3086 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3087 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3088 | data_t *nonce, |
| 3089 | data_t *additional_data, |
| 3090 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3091 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3092 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3093 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3094 | psa_key_type_t key_type = key_type_arg; |
| 3095 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3096 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3097 | unsigned char *output_data = NULL; |
| 3098 | size_t output_size = 0; |
| 3099 | size_t output_length = 0; |
| 3100 | unsigned char *output_data2 = NULL; |
| 3101 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 3102 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3103 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3104 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3105 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3106 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3107 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3108 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3109 | psa_set_key_algorithm( &attributes, alg ); |
| 3110 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3111 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3112 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3113 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3114 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3115 | key_bits = psa_get_key_bits( &attributes ); |
| 3116 | |
| 3117 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3118 | alg ); |
| 3119 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3120 | * should be exact. */ |
| 3121 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 3122 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 3123 | { |
| 3124 | TEST_EQUAL( output_size, |
| 3125 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3126 | TEST_ASSERT( output_size <= |
| 3127 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3128 | } |
| 3129 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3130 | |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 3131 | status = psa_aead_encrypt( key, alg, |
| 3132 | nonce->x, nonce->len, |
| 3133 | additional_data->x, |
| 3134 | additional_data->len, |
| 3135 | input_data->x, input_data->len, |
| 3136 | output_data, output_size, |
| 3137 | &output_length ); |
| 3138 | |
| 3139 | /* If the operation is not supported, just skip and not fail in case the |
| 3140 | * encryption involves a common limitation of cryptography hardwares and |
| 3141 | * an alternative implementation. */ |
| 3142 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 3143 | { |
| 3144 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3145 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 3146 | } |
| 3147 | |
| 3148 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3149 | |
| 3150 | if( PSA_SUCCESS == expected_result ) |
| 3151 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3152 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3153 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3154 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3155 | * should be exact. */ |
| 3156 | TEST_EQUAL( input_data->len, |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3157 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) ); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3158 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3159 | TEST_ASSERT( input_data->len <= |
| 3160 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) ); |
| 3161 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3162 | TEST_EQUAL( psa_aead_decrypt( key, alg, |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3163 | nonce->x, nonce->len, |
| 3164 | additional_data->x, |
| 3165 | additional_data->len, |
| 3166 | output_data, output_length, |
| 3167 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3168 | &output_length2 ), |
| 3169 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3170 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3171 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 3172 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3173 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3174 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3175 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3176 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3177 | mbedtls_free( output_data ); |
| 3178 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3179 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3180 | } |
| 3181 | /* END_CASE */ |
| 3182 | |
| 3183 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3184 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 3185 | int alg_arg, |
| 3186 | data_t *nonce, |
| 3187 | data_t *additional_data, |
| 3188 | data_t *input_data, |
| 3189 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3190 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3191 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3192 | psa_key_type_t key_type = key_type_arg; |
| 3193 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3194 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3195 | unsigned char *output_data = NULL; |
| 3196 | size_t output_size = 0; |
| 3197 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3198 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3199 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3200 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3201 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3202 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3203 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3204 | psa_set_key_algorithm( &attributes, alg ); |
| 3205 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3206 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3207 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3208 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3209 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3210 | key_bits = psa_get_key_bits( &attributes ); |
| 3211 | |
| 3212 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3213 | alg ); |
| 3214 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3215 | * should be exact. */ |
| 3216 | TEST_EQUAL( output_size, |
| 3217 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3218 | TEST_ASSERT( output_size <= |
| 3219 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3220 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3221 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3222 | status = psa_aead_encrypt( key, alg, |
| 3223 | nonce->x, nonce->len, |
| 3224 | additional_data->x, additional_data->len, |
| 3225 | input_data->x, input_data->len, |
| 3226 | output_data, output_size, |
| 3227 | &output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3228 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3229 | /* If the operation is not supported, just skip and not fail in case the |
| 3230 | * encryption involves a common limitation of cryptography hardwares and |
| 3231 | * an alternative implementation. */ |
| 3232 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3233 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3234 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3235 | 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] | 3236 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3237 | |
| 3238 | PSA_ASSERT( status ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3239 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 3240 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3241 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3242 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3243 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3244 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3245 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3246 | } |
| 3247 | /* END_CASE */ |
| 3248 | |
| 3249 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3250 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 3251 | int alg_arg, |
| 3252 | data_t *nonce, |
| 3253 | data_t *additional_data, |
| 3254 | data_t *input_data, |
| 3255 | data_t *expected_data, |
| 3256 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3257 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3258 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3259 | psa_key_type_t key_type = key_type_arg; |
| 3260 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3261 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3262 | unsigned char *output_data = NULL; |
| 3263 | size_t output_size = 0; |
| 3264 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3265 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3266 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3267 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3268 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3269 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3270 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3271 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3272 | psa_set_key_algorithm( &attributes, alg ); |
| 3273 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3274 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3275 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3276 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3277 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3278 | key_bits = psa_get_key_bits( &attributes ); |
| 3279 | |
| 3280 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3281 | alg ); |
| 3282 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 3283 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 3284 | { |
| 3285 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3286 | * should be exact. */ |
| 3287 | TEST_EQUAL( output_size, |
| 3288 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3289 | TEST_ASSERT( output_size <= |
| 3290 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3291 | } |
| 3292 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3293 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3294 | status = psa_aead_decrypt( key, alg, |
| 3295 | nonce->x, nonce->len, |
| 3296 | additional_data->x, |
| 3297 | additional_data->len, |
| 3298 | input_data->x, input_data->len, |
| 3299 | output_data, output_size, |
| 3300 | &output_length ); |
| 3301 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3302 | /* If the operation is not supported, just skip and not fail in case the |
| 3303 | * decryption involves a common limitation of cryptography hardwares and |
| 3304 | * an alternative implementation. */ |
| 3305 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3306 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3307 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3308 | 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] | 3309 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3310 | |
| 3311 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3312 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3313 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3314 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 3315 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3316 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3317 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3318 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3319 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3320 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3321 | } |
| 3322 | /* END_CASE */ |
| 3323 | |
| 3324 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3325 | void signature_size( int type_arg, |
| 3326 | int bits, |
| 3327 | int alg_arg, |
| 3328 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3329 | { |
| 3330 | psa_key_type_t type = type_arg; |
| 3331 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3332 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 3333 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3334 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 3335 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 3336 | TEST_EQUAL( actual_size, |
| 3337 | PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) ); |
| 3338 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3339 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3340 | exit: |
| 3341 | ; |
| 3342 | } |
| 3343 | /* END_CASE */ |
| 3344 | |
| 3345 | /* BEGIN_CASE */ |
gabor-mezei-arm | dc76df4 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 3346 | void sign_hash_deterministic( int key_type_arg, data_t *key_data, |
| 3347 | int alg_arg, data_t *input_data, |
| 3348 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3349 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3350 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3351 | psa_key_type_t key_type = key_type_arg; |
| 3352 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3353 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3354 | unsigned char *signature = NULL; |
| 3355 | size_t signature_size; |
| 3356 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3357 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3358 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3359 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3360 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3361 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3362 | psa_set_key_algorithm( &attributes, alg ); |
| 3363 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 3364 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3365 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3366 | &key ) ); |
| 3367 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3368 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3369 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3370 | /* Allocate a buffer which has the size advertized by the |
| 3371 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3372 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3373 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3374 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3375 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3376 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3377 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3378 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3379 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3380 | input_data->x, input_data->len, |
| 3381 | signature, signature_size, |
| 3382 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3383 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3384 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 3385 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3386 | |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 3387 | #if defined(MBEDTLS_TEST_DEPRECATED) |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3388 | memset( signature, 0, signature_size ); |
| 3389 | signature_length = INVALID_EXPORT_LENGTH; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3390 | PSA_ASSERT( psa_asymmetric_sign( key, alg, |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 3391 | input_data->x, input_data->len, |
| 3392 | signature, signature_size, |
| 3393 | &signature_length ) ); |
| 3394 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 3395 | signature, signature_length ); |
| 3396 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3397 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3398 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3399 | /* |
| 3400 | * Key attributes may have been returned by psa_get_key_attributes() |
| 3401 | * thus reset them as required. |
| 3402 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3403 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3404 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3405 | psa_destroy_key( key ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 3406 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3407 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3408 | } |
| 3409 | /* END_CASE */ |
| 3410 | |
| 3411 | /* BEGIN_CASE */ |
gabor-mezei-arm | dc76df4 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 3412 | void sign_hash_fail( int key_type_arg, data_t *key_data, |
| 3413 | int alg_arg, data_t *input_data, |
| 3414 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3415 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3416 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3417 | psa_key_type_t key_type = key_type_arg; |
| 3418 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3419 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3420 | psa_status_t actual_status; |
| 3421 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 3422 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 3423 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3424 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3425 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3426 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3427 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3428 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3429 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3430 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3431 | psa_set_key_algorithm( &attributes, alg ); |
| 3432 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 3433 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3434 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3435 | &key ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3436 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3437 | actual_status = psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3438 | input_data->x, input_data->len, |
| 3439 | signature, signature_size, |
| 3440 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3441 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3442 | /* The value of *signature_length is unspecified on error, but |
| 3443 | * whatever it is, it should be less than signature_size, so that |
| 3444 | * if the caller tries to read *signature_length bytes without |
| 3445 | * checking the error code then they don't overflow a buffer. */ |
| 3446 | TEST_ASSERT( signature_length <= signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3447 | |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3448 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 3449 | signature_length = INVALID_EXPORT_LENGTH; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3450 | TEST_EQUAL( psa_asymmetric_sign( key, alg, |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3451 | input_data->x, input_data->len, |
| 3452 | signature, signature_size, |
| 3453 | &signature_length ), |
| 3454 | expected_status ); |
| 3455 | TEST_ASSERT( signature_length <= signature_size ); |
| 3456 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3457 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3458 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3459 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3460 | psa_destroy_key( key ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3461 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3462 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3463 | } |
| 3464 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3465 | |
| 3466 | /* BEGIN_CASE */ |
gabor-mezei-arm | dc76df4 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 3467 | void sign_verify_hash( int key_type_arg, data_t *key_data, |
| 3468 | int alg_arg, data_t *input_data ) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3469 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3470 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3471 | psa_key_type_t key_type = key_type_arg; |
| 3472 | psa_algorithm_t alg = alg_arg; |
| 3473 | size_t key_bits; |
| 3474 | unsigned char *signature = NULL; |
| 3475 | size_t signature_size; |
| 3476 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3477 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3478 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3479 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3480 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3481 | 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] | 3482 | psa_set_key_algorithm( &attributes, alg ); |
| 3483 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3484 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3485 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3486 | &key ) ); |
| 3487 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3488 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3489 | |
| 3490 | /* Allocate a buffer which has the size advertized by the |
| 3491 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3492 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3493 | key_bits, alg ); |
| 3494 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3495 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3496 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3497 | |
| 3498 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3499 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3500 | input_data->x, input_data->len, |
| 3501 | signature, signature_size, |
| 3502 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3503 | /* Check that the signature length looks sensible. */ |
| 3504 | TEST_ASSERT( signature_length <= signature_size ); |
| 3505 | TEST_ASSERT( signature_length > 0 ); |
| 3506 | |
| 3507 | /* Use the library to verify that the signature is correct. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3508 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3509 | input_data->x, input_data->len, |
| 3510 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3511 | |
| 3512 | if( input_data->len != 0 ) |
| 3513 | { |
| 3514 | /* Flip a bit in the input and verify that the signature is now |
| 3515 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 3516 | * because ECDSA may ignore the last few bits of the input. */ |
| 3517 | input_data->x[0] ^= 1; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3518 | TEST_EQUAL( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3519 | input_data->x, input_data->len, |
| 3520 | signature, signature_length ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3521 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3522 | } |
| 3523 | |
| 3524 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3525 | /* |
| 3526 | * Key attributes may have been returned by psa_get_key_attributes() |
| 3527 | * thus reset them as required. |
| 3528 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3529 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3530 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3531 | psa_destroy_key( key ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3532 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3533 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3534 | } |
| 3535 | /* END_CASE */ |
| 3536 | |
| 3537 | /* BEGIN_CASE */ |
gabor-mezei-arm | dc76df4 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 3538 | void verify_hash( int key_type_arg, data_t *key_data, |
| 3539 | int alg_arg, data_t *hash_data, |
| 3540 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3541 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3542 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3543 | psa_key_type_t key_type = key_type_arg; |
| 3544 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3545 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3546 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3547 | TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3548 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3549 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3550 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3551 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3552 | psa_set_key_algorithm( &attributes, alg ); |
| 3553 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3554 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3555 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3556 | &key ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3557 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3558 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3559 | hash_data->x, hash_data->len, |
| 3560 | signature_data->x, signature_data->len ) ); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 3561 | |
| 3562 | #if defined(MBEDTLS_TEST_DEPRECATED) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3563 | PSA_ASSERT( psa_asymmetric_verify( key, alg, |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 3564 | hash_data->x, hash_data->len, |
| 3565 | signature_data->x, |
| 3566 | signature_data->len ) ); |
| 3567 | |
| 3568 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3569 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3570 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3571 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3572 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3573 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3574 | } |
| 3575 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3576 | |
| 3577 | /* BEGIN_CASE */ |
gabor-mezei-arm | dc76df4 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 3578 | void verify_hash_fail( int key_type_arg, data_t *key_data, |
| 3579 | int alg_arg, data_t *hash_data, |
| 3580 | data_t *signature_data, |
| 3581 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3582 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3583 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3584 | psa_key_type_t key_type = key_type_arg; |
| 3585 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3586 | psa_status_t actual_status; |
| 3587 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3588 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3589 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3590 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3591 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3592 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3593 | psa_set_key_algorithm( &attributes, alg ); |
| 3594 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3595 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3596 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3597 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3598 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3599 | actual_status = psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3600 | hash_data->x, hash_data->len, |
| 3601 | signature_data->x, signature_data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3602 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3603 | |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3604 | #if defined(MBEDTLS_TEST_DEPRECATED) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3605 | TEST_EQUAL( psa_asymmetric_verify( key, alg, |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 3606 | hash_data->x, hash_data->len, |
| 3607 | signature_data->x, signature_data->len ), |
| 3608 | expected_status ); |
| 3609 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 3610 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3611 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3612 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3613 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3614 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3615 | } |
| 3616 | /* END_CASE */ |
| 3617 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3618 | /* BEGIN_CASE */ |
gabor-mezei-arm | abd7258 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3619 | void sign_message_deterministic( int key_type_arg, |
| 3620 | data_t *key_data, |
| 3621 | int alg_arg, |
| 3622 | data_t *input_data, |
| 3623 | data_t *output_data ) |
| 3624 | { |
| 3625 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3626 | psa_key_type_t key_type = key_type_arg; |
| 3627 | psa_algorithm_t alg = alg_arg; |
| 3628 | size_t key_bits; |
| 3629 | unsigned char *signature = NULL; |
| 3630 | size_t signature_size; |
| 3631 | size_t signature_length = 0xdeadbeef; |
| 3632 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3633 | |
| 3634 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3635 | |
| 3636 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 3637 | psa_set_key_algorithm( &attributes, alg ); |
| 3638 | psa_set_key_type( &attributes, key_type ); |
| 3639 | |
| 3640 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3641 | &key ) ); |
| 3642 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3643 | key_bits = psa_get_key_bits( &attributes ); |
| 3644 | |
| 3645 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 3646 | TEST_ASSERT( signature_size != 0 ); |
| 3647 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
| 3648 | ASSERT_ALLOC( signature, signature_size ); |
| 3649 | |
| 3650 | PSA_ASSERT( psa_sign_message( key, alg, |
| 3651 | input_data->x, input_data->len, |
| 3652 | signature, signature_size, |
| 3653 | &signature_length ) ); |
| 3654 | |
| 3655 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 3656 | signature, signature_length ); |
| 3657 | |
| 3658 | exit: |
| 3659 | psa_reset_key_attributes( &attributes ); |
| 3660 | |
| 3661 | psa_destroy_key( key ); |
| 3662 | mbedtls_free( signature ); |
| 3663 | PSA_DONE( ); |
| 3664 | |
| 3665 | } |
| 3666 | /* END_CASE */ |
| 3667 | |
| 3668 | /* BEGIN_CASE */ |
| 3669 | void sign_message_fail( int key_type_arg, |
| 3670 | data_t *key_data, |
| 3671 | int alg_arg, |
| 3672 | data_t *input_data, |
| 3673 | int signature_size_arg, |
| 3674 | int expected_status_arg ) |
| 3675 | { |
| 3676 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3677 | psa_key_type_t key_type = key_type_arg; |
| 3678 | psa_algorithm_t alg = alg_arg; |
| 3679 | size_t signature_size = signature_size_arg; |
| 3680 | psa_status_t actual_status; |
| 3681 | psa_status_t expected_status = expected_status_arg; |
| 3682 | unsigned char *signature = NULL; |
| 3683 | size_t signature_length = 0xdeadbeef; |
| 3684 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3685 | |
| 3686 | ASSERT_ALLOC( signature, signature_size ); |
| 3687 | |
| 3688 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3689 | |
| 3690 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 3691 | psa_set_key_algorithm( &attributes, alg ); |
| 3692 | psa_set_key_type( &attributes, key_type ); |
| 3693 | |
| 3694 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3695 | &key ) ); |
| 3696 | |
| 3697 | actual_status = psa_sign_message( key, alg, |
| 3698 | input_data->x, input_data->len, |
| 3699 | signature, signature_size, |
| 3700 | &signature_length ); |
| 3701 | TEST_EQUAL( actual_status, expected_status ); |
| 3702 | /* The value of *signature_length is unspecified on error, but |
| 3703 | * whatever it is, it should be less than signature_size, so that |
| 3704 | * if the caller tries to read *signature_length bytes without |
| 3705 | * checking the error code then they don't overflow a buffer. */ |
| 3706 | TEST_ASSERT( signature_length <= signature_size ); |
| 3707 | |
| 3708 | exit: |
| 3709 | psa_reset_key_attributes( &attributes ); |
| 3710 | psa_destroy_key( key ); |
| 3711 | mbedtls_free( signature ); |
| 3712 | PSA_DONE( ); |
| 3713 | } |
| 3714 | /* END_CASE */ |
| 3715 | |
| 3716 | /* BEGIN_CASE */ |
| 3717 | void sign_verify_message( int key_type_arg, |
| 3718 | data_t *key_data, |
| 3719 | int alg_arg, |
| 3720 | data_t *input_data ) |
| 3721 | { |
| 3722 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3723 | psa_key_type_t key_type = key_type_arg; |
| 3724 | psa_algorithm_t alg = alg_arg; |
| 3725 | size_t key_bits; |
| 3726 | unsigned char *signature = NULL; |
| 3727 | size_t signature_size; |
| 3728 | size_t signature_length = 0xdeadbeef; |
| 3729 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3730 | |
| 3731 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3732 | |
| 3733 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 3734 | PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 3735 | psa_set_key_algorithm( &attributes, alg ); |
| 3736 | psa_set_key_type( &attributes, key_type ); |
| 3737 | |
| 3738 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3739 | &key ) ); |
| 3740 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3741 | key_bits = psa_get_key_bits( &attributes ); |
| 3742 | |
| 3743 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 3744 | TEST_ASSERT( signature_size != 0 ); |
| 3745 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
| 3746 | ASSERT_ALLOC( signature, signature_size ); |
| 3747 | |
| 3748 | PSA_ASSERT( psa_sign_message( key, alg, |
| 3749 | input_data->x, input_data->len, |
| 3750 | signature, signature_size, |
| 3751 | &signature_length ) ); |
| 3752 | TEST_ASSERT( signature_length <= signature_size ); |
| 3753 | TEST_ASSERT( signature_length > 0 ); |
| 3754 | |
| 3755 | PSA_ASSERT( psa_verify_message( key, alg, |
| 3756 | input_data->x, input_data->len, |
| 3757 | signature, signature_length ) ); |
| 3758 | |
| 3759 | if( input_data->len != 0 ) |
| 3760 | { |
| 3761 | /* Flip a bit in the input and verify that the signature is now |
| 3762 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 3763 | * because ECDSA may ignore the last few bits of the input. */ |
| 3764 | input_data->x[0] ^= 1; |
| 3765 | TEST_EQUAL( psa_verify_message( key, alg, |
| 3766 | input_data->x, input_data->len, |
| 3767 | signature, signature_length ), |
| 3768 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3769 | } |
| 3770 | |
| 3771 | exit: |
| 3772 | psa_reset_key_attributes( &attributes ); |
| 3773 | |
| 3774 | psa_destroy_key( key ); |
| 3775 | mbedtls_free( signature ); |
| 3776 | PSA_DONE( ); |
| 3777 | } |
| 3778 | /* END_CASE */ |
| 3779 | |
| 3780 | /* BEGIN_CASE */ |
| 3781 | void verify_message( int key_type_arg, |
| 3782 | data_t *key_data, |
| 3783 | int alg_arg, |
| 3784 | data_t *input_data, |
| 3785 | data_t *signature_data ) |
| 3786 | { |
| 3787 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3788 | psa_key_type_t key_type = key_type_arg; |
| 3789 | psa_algorithm_t alg = alg_arg; |
| 3790 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3791 | |
| 3792 | TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE ); |
| 3793 | |
| 3794 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3795 | |
| 3796 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 3797 | psa_set_key_algorithm( &attributes, alg ); |
| 3798 | psa_set_key_type( &attributes, key_type ); |
| 3799 | |
| 3800 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3801 | &key ) ); |
| 3802 | |
| 3803 | PSA_ASSERT( psa_verify_message( key, alg, |
| 3804 | input_data->x, input_data->len, |
| 3805 | signature_data->x, signature_data->len ) ); |
| 3806 | |
| 3807 | exit: |
| 3808 | psa_reset_key_attributes( &attributes ); |
| 3809 | psa_destroy_key( key ); |
| 3810 | PSA_DONE( ); |
| 3811 | } |
| 3812 | /* END_CASE */ |
| 3813 | |
| 3814 | /* BEGIN_CASE */ |
| 3815 | void verify_message_fail( int key_type_arg, |
| 3816 | data_t *key_data, |
| 3817 | int alg_arg, |
| 3818 | data_t *hash_data, |
| 3819 | data_t *signature_data, |
| 3820 | int expected_status_arg ) |
| 3821 | { |
| 3822 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3823 | psa_key_type_t key_type = key_type_arg; |
| 3824 | psa_algorithm_t alg = alg_arg; |
| 3825 | psa_status_t actual_status; |
| 3826 | psa_status_t expected_status = expected_status_arg; |
| 3827 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3828 | |
| 3829 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3830 | |
| 3831 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 3832 | psa_set_key_algorithm( &attributes, alg ); |
| 3833 | psa_set_key_type( &attributes, key_type ); |
| 3834 | |
| 3835 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3836 | &key ) ); |
| 3837 | |
| 3838 | actual_status = psa_verify_message( key, alg, |
| 3839 | hash_data->x, hash_data->len, |
| 3840 | signature_data->x, |
| 3841 | signature_data->len ); |
| 3842 | TEST_EQUAL( actual_status, expected_status ); |
| 3843 | |
| 3844 | exit: |
| 3845 | psa_reset_key_attributes( &attributes ); |
| 3846 | psa_destroy_key( key ); |
| 3847 | PSA_DONE( ); |
| 3848 | } |
| 3849 | /* END_CASE */ |
| 3850 | |
| 3851 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3852 | void asymmetric_encrypt( int key_type_arg, |
| 3853 | data_t *key_data, |
| 3854 | int alg_arg, |
| 3855 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3856 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3857 | int expected_output_length_arg, |
| 3858 | int expected_status_arg ) |
| 3859 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3860 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3861 | psa_key_type_t key_type = key_type_arg; |
| 3862 | psa_algorithm_t alg = alg_arg; |
| 3863 | size_t expected_output_length = expected_output_length_arg; |
| 3864 | size_t key_bits; |
| 3865 | unsigned char *output = NULL; |
| 3866 | size_t output_size; |
| 3867 | size_t output_length = ~0; |
| 3868 | psa_status_t actual_status; |
| 3869 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3870 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3871 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3872 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3873 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3874 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3875 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3876 | psa_set_key_algorithm( &attributes, alg ); |
| 3877 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3878 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3879 | &key ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3880 | |
| 3881 | /* Determine the maximum output length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3882 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3883 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3884 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3885 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3886 | TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3887 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3888 | |
| 3889 | /* Encrypt the input */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3890 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3891 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3892 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3893 | output, output_size, |
| 3894 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3895 | TEST_EQUAL( actual_status, expected_status ); |
| 3896 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3897 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3898 | /* If the label is empty, the test framework puts a non-null pointer |
| 3899 | * in label->x. Test that a null pointer works as well. */ |
| 3900 | if( label->len == 0 ) |
| 3901 | { |
| 3902 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 3903 | if( output_size != 0 ) |
| 3904 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3905 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3906 | input_data->x, input_data->len, |
| 3907 | NULL, label->len, |
| 3908 | output, output_size, |
| 3909 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3910 | TEST_EQUAL( actual_status, expected_status ); |
| 3911 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3912 | } |
| 3913 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3914 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3915 | /* |
| 3916 | * Key attributes may have been returned by psa_get_key_attributes() |
| 3917 | * thus reset them as required. |
| 3918 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3919 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3920 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3921 | psa_destroy_key( key ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3922 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3923 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3924 | } |
| 3925 | /* END_CASE */ |
| 3926 | |
| 3927 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3928 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 3929 | data_t *key_data, |
| 3930 | int alg_arg, |
| 3931 | data_t *input_data, |
| 3932 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3933 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3934 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3935 | psa_key_type_t key_type = key_type_arg; |
| 3936 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3937 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3938 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3939 | size_t output_size; |
| 3940 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 3941 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3942 | size_t output2_size; |
| 3943 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3944 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3945 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3946 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3947 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3948 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3949 | psa_set_key_algorithm( &attributes, alg ); |
| 3950 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3951 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3952 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3953 | &key ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3954 | |
| 3955 | /* Determine the maximum ciphertext length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3956 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3957 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3958 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3959 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3960 | TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3961 | ASSERT_ALLOC( output, output_size ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3962 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3963 | output2_size = input_data->len; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3964 | TEST_ASSERT( output2_size <= |
| 3965 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) ); |
| 3966 | TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3967 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3968 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 3969 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 3970 | * the original plaintext because of the non-optional random |
| 3971 | * part of encryption process which prevents using fixed vectors. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3972 | PSA_ASSERT( psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3973 | input_data->x, input_data->len, |
| 3974 | label->x, label->len, |
| 3975 | output, output_size, |
| 3976 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3977 | /* We don't know what ciphertext length to expect, but check that |
| 3978 | * it looks sensible. */ |
| 3979 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 3980 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3981 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3982 | output, output_length, |
| 3983 | label->x, label->len, |
| 3984 | output2, output2_size, |
| 3985 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3986 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 3987 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3988 | |
| 3989 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3990 | /* |
| 3991 | * Key attributes may have been returned by psa_get_key_attributes() |
| 3992 | * thus reset them as required. |
| 3993 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3994 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3995 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3996 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3997 | mbedtls_free( output ); |
| 3998 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3999 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4000 | } |
| 4001 | /* END_CASE */ |
| 4002 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4003 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4004 | void asymmetric_decrypt( int key_type_arg, |
| 4005 | data_t *key_data, |
| 4006 | int alg_arg, |
| 4007 | data_t *input_data, |
| 4008 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 4009 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4010 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4011 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4012 | psa_key_type_t key_type = key_type_arg; |
| 4013 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4014 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4015 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 4016 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4017 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4018 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4019 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4020 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4021 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4022 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4023 | psa_set_key_algorithm( &attributes, alg ); |
| 4024 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4025 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4026 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4027 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4028 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4029 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4030 | key_bits = psa_get_key_bits( &attributes ); |
| 4031 | |
| 4032 | /* Determine the maximum ciphertext length */ |
| 4033 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 4034 | TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
| 4035 | ASSERT_ALLOC( output, output_size ); |
| 4036 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4037 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4038 | input_data->x, input_data->len, |
| 4039 | label->x, label->len, |
| 4040 | output, |
| 4041 | output_size, |
| 4042 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4043 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4044 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4045 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4046 | /* If the label is empty, the test framework puts a non-null pointer |
| 4047 | * in label->x. Test that a null pointer works as well. */ |
| 4048 | if( label->len == 0 ) |
| 4049 | { |
| 4050 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4051 | if( output_size != 0 ) |
| 4052 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4053 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4054 | input_data->x, input_data->len, |
| 4055 | NULL, label->len, |
| 4056 | output, |
| 4057 | output_size, |
| 4058 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4059 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4060 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4061 | } |
| 4062 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4063 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4064 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4065 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4066 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4067 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4068 | } |
| 4069 | /* END_CASE */ |
| 4070 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4071 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4072 | void asymmetric_decrypt_fail( int key_type_arg, |
| 4073 | data_t *key_data, |
| 4074 | int alg_arg, |
| 4075 | data_t *input_data, |
| 4076 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4077 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4078 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4079 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4080 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4081 | psa_key_type_t key_type = key_type_arg; |
| 4082 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4083 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4084 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4085 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4086 | psa_status_t actual_status; |
| 4087 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4088 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4089 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4090 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4091 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4092 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4093 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4094 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4095 | psa_set_key_algorithm( &attributes, alg ); |
| 4096 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4097 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4098 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4099 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4100 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4101 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4102 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4103 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4104 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4105 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4106 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4107 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4108 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4109 | /* If the label is empty, the test framework puts a non-null pointer |
| 4110 | * in label->x. Test that a null pointer works as well. */ |
| 4111 | if( label->len == 0 ) |
| 4112 | { |
| 4113 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4114 | if( output_size != 0 ) |
| 4115 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4116 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4117 | input_data->x, input_data->len, |
| 4118 | NULL, label->len, |
| 4119 | output, output_size, |
| 4120 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4121 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4122 | TEST_ASSERT( output_length <= output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4123 | } |
| 4124 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4125 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4126 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4127 | psa_destroy_key( key ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4128 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4129 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4130 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4131 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4132 | |
| 4133 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4134 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4135 | { |
| 4136 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 4137 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 4138 | * though it's OK by the C standard. We could test for this, but we'd need |
| 4139 | * to supress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4140 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4141 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 4142 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4143 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4144 | |
| 4145 | memset( &zero, 0, sizeof( zero ) ); |
| 4146 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4147 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4148 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4149 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4150 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4151 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4152 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4153 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4154 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4155 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4156 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 4157 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 4158 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4159 | } |
| 4160 | /* END_CASE */ |
| 4161 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4162 | /* BEGIN_CASE */ |
| 4163 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4164 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4165 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4166 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4167 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4168 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4169 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4170 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4171 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4172 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4173 | |
| 4174 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4175 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4176 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4177 | } |
| 4178 | /* END_CASE */ |
| 4179 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4180 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4181 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 4182 | int expected_status_arg ) |
| 4183 | { |
| 4184 | psa_algorithm_t alg = alg_arg; |
| 4185 | size_t capacity = capacity_arg; |
| 4186 | psa_status_t expected_status = expected_status_arg; |
| 4187 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4188 | |
| 4189 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4190 | |
| 4191 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4192 | |
| 4193 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 4194 | expected_status ); |
| 4195 | |
| 4196 | exit: |
| 4197 | psa_key_derivation_abort( &operation ); |
| 4198 | PSA_DONE( ); |
| 4199 | } |
| 4200 | /* END_CASE */ |
| 4201 | |
| 4202 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4203 | void derive_input( int alg_arg, |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4204 | int step_arg1, int key_type_arg1, data_t *input1, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4205 | int expected_status_arg1, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 4206 | int step_arg2, int key_type_arg2, data_t *input2, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4207 | int expected_status_arg2, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 4208 | int step_arg3, int key_type_arg3, data_t *input3, |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4209 | int expected_status_arg3, |
| 4210 | int output_key_type_arg, int expected_output_status_arg ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4211 | { |
| 4212 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4213 | psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3}; |
| 4214 | 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] | 4215 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 4216 | expected_status_arg2, |
| 4217 | expected_status_arg3}; |
| 4218 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4219 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 4220 | MBEDTLS_SVC_KEY_ID_INIT, |
| 4221 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4222 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4223 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4224 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4225 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4226 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4227 | psa_status_t expected_output_status = expected_output_status_arg; |
| 4228 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4229 | |
| 4230 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4231 | |
| 4232 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4233 | psa_set_key_algorithm( &attributes, alg ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4234 | |
| 4235 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4236 | |
| 4237 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 4238 | { |
Gilles Peskine | f627931 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 4239 | mbedtls_test_set_step( i ); |
| 4240 | if( steps[i] == 0 ) |
| 4241 | { |
| 4242 | /* Skip this step */ |
| 4243 | } |
| 4244 | else if( key_types[i] != PSA_KEY_TYPE_NONE ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4245 | { |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4246 | psa_set_key_type( &attributes, key_types[i] ); |
| 4247 | PSA_ASSERT( psa_import_key( &attributes, |
| 4248 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4249 | &keys[i] ) ); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 4250 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) && |
| 4251 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 4252 | { |
| 4253 | // When taking a private key as secret input, use key agreement |
| 4254 | // to add the shared secret to the derivation |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 4255 | TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self( |
| 4256 | &operation, keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 4257 | expected_statuses[i] ); |
| 4258 | } |
| 4259 | else |
| 4260 | { |
| 4261 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4262 | keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 4263 | expected_statuses[i] ); |
| 4264 | } |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4265 | } |
| 4266 | else |
| 4267 | { |
| 4268 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 4269 | &operation, steps[i], |
| 4270 | inputs[i]->x, inputs[i]->len ), |
| 4271 | expected_statuses[i] ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4272 | } |
| 4273 | } |
| 4274 | |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4275 | if( output_key_type != PSA_KEY_TYPE_NONE ) |
| 4276 | { |
| 4277 | psa_reset_key_attributes( &attributes ); |
| 4278 | psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA ); |
| 4279 | psa_set_key_bits( &attributes, 8 ); |
| 4280 | actual_output_status = |
| 4281 | psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4282 | &output_key ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4283 | } |
| 4284 | else |
| 4285 | { |
| 4286 | uint8_t buffer[1]; |
| 4287 | actual_output_status = |
| 4288 | psa_key_derivation_output_bytes( &operation, |
| 4289 | buffer, sizeof( buffer ) ); |
| 4290 | } |
| 4291 | TEST_EQUAL( actual_output_status, expected_output_status ); |
| 4292 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4293 | exit: |
| 4294 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4295 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 4296 | psa_destroy_key( keys[i] ); |
| 4297 | psa_destroy_key( output_key ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4298 | PSA_DONE( ); |
| 4299 | } |
| 4300 | /* END_CASE */ |
| 4301 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4302 | /* BEGIN_CASE */ |
Gilles Peskine | 0faba4e | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 4303 | void derive_over_capacity( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4304 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4305 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4306 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 4307 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4308 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4309 | unsigned char input1[] = "Input 1"; |
| 4310 | size_t input1_length = sizeof( input1 ); |
| 4311 | unsigned char input2[] = "Input 2"; |
| 4312 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4313 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 4314 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 4315 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4316 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4317 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4318 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4319 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4320 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4321 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4322 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4323 | psa_set_key_algorithm( &attributes, alg ); |
| 4324 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4325 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 4326 | PSA_ASSERT( psa_import_key( &attributes, |
| 4327 | key_data, sizeof( key_data ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4328 | &key ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4329 | |
| 4330 | /* valid key derivation */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 4331 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 4332 | input1, input1_length, |
| 4333 | input2, input2_length, |
| 4334 | capacity ) ) |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4335 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4336 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4337 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4338 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4339 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4340 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4341 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4342 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4343 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4344 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4345 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4346 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4347 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4348 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4349 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4350 | } |
| 4351 | /* END_CASE */ |
| 4352 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4353 | /* BEGIN_CASE */ |
Gilles Peskine | 0faba4e | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 4354 | void derive_actions_without_setup( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4355 | { |
| 4356 | uint8_t output_buffer[16]; |
| 4357 | size_t buffer_size = 16; |
| 4358 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4359 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4360 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4361 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 4362 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4363 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4364 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4365 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4366 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4367 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4368 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4369 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4370 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 4371 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4372 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4373 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4374 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4375 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4376 | |
| 4377 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4378 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4379 | } |
| 4380 | /* END_CASE */ |
| 4381 | |
| 4382 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4383 | void derive_output( int alg_arg, |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4384 | int step1_arg, data_t *input1, |
| 4385 | int step2_arg, data_t *input2, |
| 4386 | int step3_arg, data_t *input3, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4387 | int requested_capacity_arg, |
| 4388 | data_t *expected_output1, |
| 4389 | data_t *expected_output2 ) |
| 4390 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4391 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4392 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg}; |
| 4393 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4394 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 4395 | MBEDTLS_SVC_KEY_ID_INIT, |
| 4396 | MBEDTLS_SVC_KEY_ID_INIT }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4397 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4398 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4399 | uint8_t *expected_outputs[2] = |
| 4400 | {expected_output1->x, expected_output2->x}; |
| 4401 | size_t output_sizes[2] = |
| 4402 | {expected_output1->len, expected_output2->len}; |
| 4403 | size_t output_buffer_size = 0; |
| 4404 | uint8_t *output_buffer = NULL; |
| 4405 | size_t expected_capacity; |
| 4406 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4407 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4408 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4409 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4410 | |
| 4411 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 4412 | { |
| 4413 | if( output_sizes[i] > output_buffer_size ) |
| 4414 | output_buffer_size = output_sizes[i]; |
| 4415 | if( output_sizes[i] == 0 ) |
| 4416 | expected_outputs[i] = NULL; |
| 4417 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4418 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4419 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4420 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4421 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4422 | psa_set_key_algorithm( &attributes, alg ); |
| 4423 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4424 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4425 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4426 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4427 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 4428 | requested_capacity ) ); |
| 4429 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4430 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4431 | switch( steps[i] ) |
| 4432 | { |
| 4433 | case 0: |
| 4434 | break; |
| 4435 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 4436 | PSA_ASSERT( psa_import_key( &attributes, |
| 4437 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4438 | &keys[i] ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4439 | |
| 4440 | if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 4441 | { |
| 4442 | PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) ); |
| 4443 | TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <= |
| 4444 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ); |
| 4445 | } |
| 4446 | |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4447 | PSA_ASSERT( psa_key_derivation_input_key( |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4448 | &operation, steps[i], keys[i] ) ); |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4449 | break; |
| 4450 | default: |
| 4451 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 4452 | &operation, steps[i], |
| 4453 | inputs[i]->x, inputs[i]->len ) ); |
| 4454 | break; |
| 4455 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4456 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4457 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4458 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4459 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4460 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4461 | expected_capacity = requested_capacity; |
| 4462 | |
| 4463 | /* Expansion phase. */ |
| 4464 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 4465 | { |
| 4466 | /* Read some bytes. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4467 | status = psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4468 | output_buffer, output_sizes[i] ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4469 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 4470 | { |
| 4471 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 4472 | TEST_ASSERT( status == PSA_SUCCESS || |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4473 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4474 | continue; |
| 4475 | } |
| 4476 | else if( expected_capacity == 0 || |
| 4477 | output_sizes[i] > expected_capacity ) |
| 4478 | { |
| 4479 | /* Capacity exceeded. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4480 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4481 | expected_capacity = 0; |
| 4482 | continue; |
| 4483 | } |
| 4484 | /* Success. Check the read data. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4485 | PSA_ASSERT( status ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4486 | if( output_sizes[i] != 0 ) |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4487 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 4488 | expected_outputs[i], output_sizes[i] ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4489 | /* Check the operation status. */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4490 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4491 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4492 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4493 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4494 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4495 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4496 | |
| 4497 | exit: |
| 4498 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4499 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4500 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 4501 | psa_destroy_key( keys[i] ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4502 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4503 | } |
| 4504 | /* END_CASE */ |
| 4505 | |
| 4506 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4507 | void derive_full( int alg_arg, |
| 4508 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 4509 | data_t *input1, |
| 4510 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4511 | int requested_capacity_arg ) |
| 4512 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4513 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4514 | psa_algorithm_t alg = alg_arg; |
| 4515 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4516 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4517 | unsigned char output_buffer[16]; |
| 4518 | size_t expected_capacity = requested_capacity; |
| 4519 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4520 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4521 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4522 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4523 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4524 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4525 | psa_set_key_algorithm( &attributes, alg ); |
| 4526 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4527 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4528 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4529 | &key ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4530 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 4531 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 4532 | input1->x, input1->len, |
| 4533 | input2->x, input2->len, |
| 4534 | requested_capacity ) ) |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 4535 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 4536 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4537 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4538 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4539 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4540 | |
| 4541 | /* Expansion phase. */ |
| 4542 | while( current_capacity > 0 ) |
| 4543 | { |
| 4544 | size_t read_size = sizeof( output_buffer ); |
| 4545 | if( read_size > current_capacity ) |
| 4546 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4547 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4548 | output_buffer, |
| 4549 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4550 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4551 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4552 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4553 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4554 | } |
| 4555 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4556 | /* Check that the operation refuses to go over capacity. */ |
| 4557 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4558 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4559 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4560 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4561 | |
| 4562 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4563 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4564 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4565 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4566 | } |
| 4567 | /* END_CASE */ |
| 4568 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4569 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4570 | void derive_key_exercise( int alg_arg, |
| 4571 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4572 | data_t *input1, |
| 4573 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4574 | int derived_type_arg, |
| 4575 | int derived_bits_arg, |
| 4576 | int derived_usage_arg, |
| 4577 | int derived_alg_arg ) |
| 4578 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4579 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4580 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4581 | psa_algorithm_t alg = alg_arg; |
| 4582 | psa_key_type_t derived_type = derived_type_arg; |
| 4583 | size_t derived_bits = derived_bits_arg; |
| 4584 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 4585 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 4586 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4587 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4588 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4589 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4590 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4591 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4592 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4593 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4594 | psa_set_key_algorithm( &attributes, alg ); |
| 4595 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4596 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4597 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4598 | |
| 4599 | /* Derive a key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 4600 | if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 4601 | input1->x, input1->len, |
| 4602 | input2->x, input2->len, |
| 4603 | capacity ) ) |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4604 | goto exit; |
| 4605 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4606 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 4607 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 4608 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4609 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4610 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4611 | &derived_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4612 | |
| 4613 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4614 | PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4615 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 4616 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4617 | |
| 4618 | /* Exercise the derived key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 4619 | if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4620 | goto exit; |
| 4621 | |
| 4622 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4623 | /* |
| 4624 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4625 | * thus reset them as required. |
| 4626 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4627 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4628 | |
| 4629 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4630 | psa_destroy_key( base_key ); |
| 4631 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4632 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4633 | } |
| 4634 | /* END_CASE */ |
| 4635 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4636 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4637 | void derive_key_export( int alg_arg, |
| 4638 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4639 | data_t *input1, |
| 4640 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4641 | int bytes1_arg, |
| 4642 | int bytes2_arg ) |
| 4643 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4644 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4645 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4646 | psa_algorithm_t alg = alg_arg; |
| 4647 | size_t bytes1 = bytes1_arg; |
| 4648 | size_t bytes2 = bytes2_arg; |
| 4649 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4650 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4651 | uint8_t *output_buffer = NULL; |
| 4652 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4653 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4654 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4655 | size_t length; |
| 4656 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4657 | ASSERT_ALLOC( output_buffer, capacity ); |
| 4658 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4659 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4660 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4661 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 4662 | psa_set_key_algorithm( &base_attributes, alg ); |
| 4663 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4664 | 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] | 4665 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4666 | |
| 4667 | /* Derive some material and output it. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 4668 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 4669 | input1->x, input1->len, |
| 4670 | input2->x, input2->len, |
| 4671 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4672 | goto exit; |
| 4673 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4674 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4675 | output_buffer, |
| 4676 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4677 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4678 | |
| 4679 | /* 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] | 4680 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 4681 | input1->x, input1->len, |
| 4682 | input2->x, input2->len, |
| 4683 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4684 | goto exit; |
| 4685 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4686 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 4687 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 4688 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4689 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4690 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4691 | &derived_key ) ); |
| 4692 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4693 | export_buffer, bytes1, |
| 4694 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4695 | TEST_EQUAL( length, bytes1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4696 | PSA_ASSERT( psa_destroy_key( derived_key ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4697 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4698 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4699 | &derived_key ) ); |
| 4700 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4701 | export_buffer + bytes1, bytes2, |
| 4702 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4703 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4704 | |
| 4705 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4706 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 4707 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4708 | |
| 4709 | exit: |
| 4710 | mbedtls_free( output_buffer ); |
| 4711 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4712 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4713 | psa_destroy_key( base_key ); |
| 4714 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4715 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4716 | } |
| 4717 | /* END_CASE */ |
| 4718 | |
| 4719 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 4720 | void derive_key( int alg_arg, |
| 4721 | data_t *key_data, data_t *input1, data_t *input2, |
| 4722 | int type_arg, int bits_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 4723 | int expected_status_arg, |
| 4724 | int is_large_output ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4725 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4726 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4727 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4728 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 4729 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4730 | size_t bits = bits_arg; |
| 4731 | psa_status_t expected_status = expected_status_arg; |
| 4732 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4733 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4734 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4735 | |
| 4736 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4737 | |
| 4738 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 4739 | psa_set_key_algorithm( &base_attributes, alg ); |
| 4740 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 4741 | 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] | 4742 | &base_key ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4743 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 4744 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 4745 | input1->x, input1->len, |
| 4746 | input2->x, input2->len, |
| 4747 | SIZE_MAX ) ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4748 | goto exit; |
| 4749 | |
| 4750 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 4751 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 4752 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4753 | psa_set_key_bits( &derived_attributes, bits ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 4754 | |
| 4755 | psa_status_t status = |
| 4756 | psa_key_derivation_output_key( &derived_attributes, |
| 4757 | &operation, |
| 4758 | &derived_key ); |
| 4759 | if( is_large_output > 0 ) |
| 4760 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4761 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4762 | |
| 4763 | exit: |
| 4764 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4765 | psa_destroy_key( base_key ); |
| 4766 | psa_destroy_key( derived_key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4767 | PSA_DONE( ); |
| 4768 | } |
| 4769 | /* END_CASE */ |
| 4770 | |
| 4771 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4772 | void key_agreement_setup( int alg_arg, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 4773 | int our_key_type_arg, int our_key_alg_arg, |
| 4774 | data_t *our_key_data, data_t *peer_key_data, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4775 | int expected_status_arg ) |
| 4776 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4777 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4778 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 4779 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4780 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4781 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4782 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4783 | psa_status_t expected_status = expected_status_arg; |
| 4784 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4785 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4786 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4787 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4788 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 4789 | psa_set_key_algorithm( &attributes, our_key_alg ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4790 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4791 | PSA_ASSERT( psa_import_key( &attributes, |
| 4792 | our_key_data->x, our_key_data->len, |
| 4793 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4794 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4795 | /* The tests currently include inputs that should fail at either step. |
| 4796 | * Test cases that fail at the setup step should be changed to call |
| 4797 | * key_derivation_setup instead, and this function should be renamed |
| 4798 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4799 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4800 | if( status == PSA_SUCCESS ) |
| 4801 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4802 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 4803 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 4804 | our_key, |
| 4805 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4806 | expected_status ); |
| 4807 | } |
| 4808 | else |
| 4809 | { |
| 4810 | TEST_ASSERT( status == expected_status ); |
| 4811 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4812 | |
| 4813 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4814 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4815 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4816 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4817 | } |
| 4818 | /* END_CASE */ |
| 4819 | |
| 4820 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4821 | void raw_key_agreement( int alg_arg, |
| 4822 | int our_key_type_arg, data_t *our_key_data, |
| 4823 | data_t *peer_key_data, |
| 4824 | data_t *expected_output ) |
| 4825 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4826 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4827 | psa_algorithm_t alg = alg_arg; |
| 4828 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4829 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4830 | unsigned char *output = NULL; |
| 4831 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4832 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4833 | |
| 4834 | ASSERT_ALLOC( output, expected_output->len ); |
| 4835 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4836 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4837 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4838 | psa_set_key_algorithm( &attributes, alg ); |
| 4839 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4840 | PSA_ASSERT( psa_import_key( &attributes, |
| 4841 | our_key_data->x, our_key_data->len, |
| 4842 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4843 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4844 | PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) ); |
| 4845 | key_bits = psa_get_key_bits( &attributes ); |
| 4846 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 4847 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 4848 | peer_key_data->x, peer_key_data->len, |
| 4849 | output, expected_output->len, |
| 4850 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4851 | ASSERT_COMPARE( output, output_length, |
| 4852 | expected_output->x, expected_output->len ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4853 | TEST_ASSERT( output_length <= |
| 4854 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) ); |
| 4855 | TEST_ASSERT( output_length <= |
| 4856 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4857 | |
| 4858 | exit: |
| 4859 | mbedtls_free( output ); |
| 4860 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4861 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4862 | } |
| 4863 | /* END_CASE */ |
| 4864 | |
| 4865 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4866 | void key_agreement_capacity( int alg_arg, |
| 4867 | int our_key_type_arg, data_t *our_key_data, |
| 4868 | data_t *peer_key_data, |
| 4869 | int expected_capacity_arg ) |
| 4870 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4871 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4872 | psa_algorithm_t alg = alg_arg; |
| 4873 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4874 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4875 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4876 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4877 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4878 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4879 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4880 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4881 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4882 | psa_set_key_algorithm( &attributes, alg ); |
| 4883 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4884 | PSA_ASSERT( psa_import_key( &attributes, |
| 4885 | our_key_data->x, our_key_data->len, |
| 4886 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4887 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4888 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4889 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 4890 | &operation, |
| 4891 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 4892 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 4893 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 4894 | { |
| 4895 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4896 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4897 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 4898 | NULL, 0 ) ); |
| 4899 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4900 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4901 | /* Test the advertized capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4902 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4903 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4904 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4905 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4906 | /* Test the actual capacity by reading the output. */ |
| 4907 | while( actual_capacity > sizeof( output ) ) |
| 4908 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4909 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4910 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4911 | actual_capacity -= sizeof( output ); |
| 4912 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4913 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4914 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4915 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4916 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4917 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4918 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4919 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4920 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4921 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4922 | } |
| 4923 | /* END_CASE */ |
| 4924 | |
| 4925 | /* BEGIN_CASE */ |
| 4926 | void key_agreement_output( int alg_arg, |
| 4927 | int our_key_type_arg, data_t *our_key_data, |
| 4928 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4929 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4930 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4931 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4932 | psa_algorithm_t alg = alg_arg; |
| 4933 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4934 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4935 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4936 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4937 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4938 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 4939 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4940 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4941 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4942 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4943 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4944 | psa_set_key_algorithm( &attributes, alg ); |
| 4945 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4946 | PSA_ASSERT( psa_import_key( &attributes, |
| 4947 | our_key_data->x, our_key_data->len, |
| 4948 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4949 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4950 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4951 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 4952 | &operation, |
| 4953 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 4954 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 4955 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 4956 | { |
| 4957 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4958 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4959 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 4960 | NULL, 0 ) ); |
| 4961 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4962 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4963 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4964 | actual_output, |
| 4965 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4966 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 4967 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4968 | if( expected_output2->len != 0 ) |
| 4969 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4970 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4971 | actual_output, |
| 4972 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4973 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 4974 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 4975 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4976 | |
| 4977 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4978 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4979 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4980 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4981 | mbedtls_free( actual_output ); |
| 4982 | } |
| 4983 | /* END_CASE */ |
| 4984 | |
| 4985 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4986 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4987 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4988 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4989 | unsigned char *output = NULL; |
| 4990 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4991 | size_t i; |
| 4992 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4993 | |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 4994 | TEST_ASSERT( bytes_arg >= 0 ); |
| 4995 | |
Gilles Peskine | 9189202 | 2021-02-08 19:50:26 +0100 | [diff] [blame] | 4996 | ASSERT_ALLOC( output, bytes ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4997 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4998 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4999 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5000 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5001 | /* Run several times, to ensure that every output byte will be |
| 5002 | * nonzero at least once with overwhelming probability |
| 5003 | * (2^(-8*number_of_runs)). */ |
| 5004 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5005 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 5006 | if( bytes != 0 ) |
| 5007 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5008 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5009 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5010 | for( i = 0; i < bytes; i++ ) |
| 5011 | { |
| 5012 | if( output[i] != 0 ) |
| 5013 | ++changed[i]; |
| 5014 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5015 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5016 | |
| 5017 | /* Check that every byte was changed to nonzero at least once. This |
| 5018 | * validates that psa_generate_random is overwriting every byte of |
| 5019 | * the output buffer. */ |
| 5020 | for( i = 0; i < bytes; i++ ) |
| 5021 | { |
| 5022 | TEST_ASSERT( changed[i] != 0 ); |
| 5023 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5024 | |
| 5025 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5026 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5027 | mbedtls_free( output ); |
| 5028 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5029 | } |
| 5030 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5031 | |
| 5032 | /* BEGIN_CASE */ |
| 5033 | void generate_key( int type_arg, |
| 5034 | int bits_arg, |
| 5035 | int usage_arg, |
| 5036 | int alg_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 5037 | int expected_status_arg, |
| 5038 | int is_large_key ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5039 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5040 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5041 | psa_key_type_t type = type_arg; |
| 5042 | psa_key_usage_t usage = usage_arg; |
| 5043 | size_t bits = bits_arg; |
| 5044 | psa_algorithm_t alg = alg_arg; |
| 5045 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5046 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5047 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5048 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5049 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5050 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5051 | psa_set_key_usage_flags( &attributes, usage ); |
| 5052 | psa_set_key_algorithm( &attributes, alg ); |
| 5053 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5054 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5055 | |
| 5056 | /* Generate a key */ |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 5057 | psa_status_t status = psa_generate_key( &attributes, &key ); |
| 5058 | |
| 5059 | if( is_large_key > 0 ) |
| 5060 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5061 | TEST_EQUAL( status , expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5062 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5063 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5064 | |
| 5065 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5066 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5067 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 5068 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5069 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 5070 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5071 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 5072 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5073 | |
| 5074 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5075 | /* |
| 5076 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5077 | * thus reset them as required. |
| 5078 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5079 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5080 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5081 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5082 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5083 | } |
| 5084 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5085 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 5086 | /* 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] | 5087 | void generate_key_rsa( int bits_arg, |
| 5088 | data_t *e_arg, |
| 5089 | int expected_status_arg ) |
| 5090 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5091 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5092 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5093 | size_t bits = bits_arg; |
| 5094 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 5095 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 5096 | psa_status_t expected_status = expected_status_arg; |
| 5097 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5098 | uint8_t *exported = NULL; |
| 5099 | size_t exported_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5100 | PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5101 | size_t exported_length = SIZE_MAX; |
| 5102 | uint8_t *e_read_buffer = NULL; |
| 5103 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 5104 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5105 | size_t e_read_length = SIZE_MAX; |
| 5106 | |
| 5107 | if( e_arg->len == 0 || |
| 5108 | ( e_arg->len == 3 && |
| 5109 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 5110 | { |
| 5111 | is_default_public_exponent = 1; |
| 5112 | e_read_size = 0; |
| 5113 | } |
| 5114 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 5115 | ASSERT_ALLOC( exported, exported_size ); |
| 5116 | |
| 5117 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5118 | |
| 5119 | psa_set_key_usage_flags( &attributes, usage ); |
| 5120 | psa_set_key_algorithm( &attributes, alg ); |
| 5121 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 5122 | e_arg->x, e_arg->len ) ); |
| 5123 | psa_set_key_bits( &attributes, bits ); |
| 5124 | |
| 5125 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5126 | TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5127 | if( expected_status != PSA_SUCCESS ) |
| 5128 | goto exit; |
| 5129 | |
| 5130 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5131 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5132 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 5133 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 5134 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 5135 | e_read_buffer, e_read_size, |
| 5136 | &e_read_length ) ); |
| 5137 | if( is_default_public_exponent ) |
| 5138 | TEST_EQUAL( e_read_length, 0 ); |
| 5139 | else |
| 5140 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 5141 | |
| 5142 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5143 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5144 | goto exit; |
| 5145 | |
| 5146 | /* Export the key and check the public exponent. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5147 | PSA_ASSERT( psa_export_public_key( key, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5148 | exported, exported_size, |
| 5149 | &exported_length ) ); |
| 5150 | { |
| 5151 | uint8_t *p = exported; |
| 5152 | uint8_t *end = exported + exported_length; |
| 5153 | size_t len; |
| 5154 | /* RSAPublicKey ::= SEQUENCE { |
| 5155 | * modulus INTEGER, -- n |
| 5156 | * publicExponent INTEGER } -- e |
| 5157 | */ |
| 5158 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5159 | MBEDTLS_ASN1_SEQUENCE | |
| 5160 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 5161 | TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5162 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 5163 | MBEDTLS_ASN1_INTEGER ) ); |
| 5164 | if( len >= 1 && p[0] == 0 ) |
| 5165 | { |
| 5166 | ++p; |
| 5167 | --len; |
| 5168 | } |
| 5169 | if( e_arg->len == 0 ) |
| 5170 | { |
| 5171 | TEST_EQUAL( len, 3 ); |
| 5172 | TEST_EQUAL( p[0], 1 ); |
| 5173 | TEST_EQUAL( p[1], 0 ); |
| 5174 | TEST_EQUAL( p[2], 1 ); |
| 5175 | } |
| 5176 | else |
| 5177 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 5178 | } |
| 5179 | |
| 5180 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5181 | /* |
| 5182 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 5183 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 5184 | */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5185 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5186 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5187 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5188 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5189 | mbedtls_free( e_read_buffer ); |
| 5190 | mbedtls_free( exported ); |
| 5191 | } |
| 5192 | /* END_CASE */ |
| 5193 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5194 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5195 | void persistent_key_load_key_from_storage( data_t *data, |
| 5196 | int type_arg, int bits_arg, |
| 5197 | int usage_flags_arg, int alg_arg, |
| 5198 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5199 | { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 5200 | 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] | 5201 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5202 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5203 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5204 | psa_key_type_t type = type_arg; |
| 5205 | size_t bits = bits_arg; |
| 5206 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 5207 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5208 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5209 | unsigned char *first_export = NULL; |
| 5210 | unsigned char *second_export = NULL; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5211 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5212 | size_t first_exported_length; |
| 5213 | size_t second_exported_length; |
| 5214 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5215 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 5216 | { |
| 5217 | ASSERT_ALLOC( first_export, export_size ); |
| 5218 | ASSERT_ALLOC( second_export, export_size ); |
| 5219 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5220 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5221 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5222 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 5223 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5224 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 5225 | psa_set_key_algorithm( &attributes, alg ); |
| 5226 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5227 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5228 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5229 | switch( generation_method ) |
| 5230 | { |
| 5231 | case IMPORT_KEY: |
| 5232 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5233 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5234 | &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5235 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5236 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5237 | case GENERATE_KEY: |
| 5238 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5239 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5240 | break; |
| 5241 | |
| 5242 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 5243 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5244 | { |
| 5245 | /* Create base key */ |
| 5246 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 5247 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5248 | psa_set_key_usage_flags( &base_attributes, |
| 5249 | PSA_KEY_USAGE_DERIVE ); |
| 5250 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 5251 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5252 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 5253 | data->x, data->len, |
| 5254 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5255 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5256 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5257 | PSA_ASSERT( psa_key_derivation_input_key( |
| 5258 | &operation, |
| 5259 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5260 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5261 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5262 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5263 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 5264 | &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5265 | &key ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5266 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5267 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5268 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5269 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 5270 | #else |
| 5271 | TEST_ASSUME( ! "KDF not supported in this configuration" ); |
| 5272 | #endif |
| 5273 | break; |
| 5274 | |
| 5275 | default: |
| 5276 | TEST_ASSERT( ! "generation_method not implemented in test" ); |
| 5277 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5278 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5279 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5280 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5281 | /* Export the key if permitted by the key policy. */ |
| 5282 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 5283 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5284 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5285 | first_export, export_size, |
| 5286 | &first_exported_length ) ); |
| 5287 | if( generation_method == IMPORT_KEY ) |
| 5288 | ASSERT_COMPARE( data->x, data->len, |
| 5289 | first_export, first_exported_length ); |
| 5290 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5291 | |
| 5292 | /* Shutdown and restart */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5293 | PSA_ASSERT( psa_purge_key( key ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5294 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5295 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5296 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5297 | /* Check key slot still contains key data */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5298 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 5299 | TEST_ASSERT( mbedtls_svc_key_id_equal( |
| 5300 | psa_get_key_id( &attributes ), key_id ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5301 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 5302 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 5303 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 5304 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
gabor-mezei-arm | 4d9009e | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 5305 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
gabor-mezei-arm | ff03fd6 | 2021-06-28 14:05:00 +0200 | [diff] [blame^] | 5306 | mbedtls_test_update_key_usage_flags( usage_flags ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5307 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5308 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5309 | /* Export the key again if permitted by the key policy. */ |
| 5310 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5311 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5312 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5313 | second_export, export_size, |
| 5314 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5315 | ASSERT_COMPARE( first_export, first_exported_length, |
| 5316 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5317 | } |
| 5318 | |
| 5319 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5320 | if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5321 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5322 | |
| 5323 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5324 | /* |
| 5325 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5326 | * thus reset them as required. |
| 5327 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5328 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5329 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5330 | mbedtls_free( first_export ); |
| 5331 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5332 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5333 | psa_destroy_key( base_key ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5334 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5335 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5336 | } |
| 5337 | /* END_CASE */ |