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 | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 19 | /* If this comes up, it's a bug in the test code or in the test data. */ |
| 20 | #define UNUSED 0xdeadbeef |
| 21 | |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 22 | /* Assert that an operation is (not) active. |
| 23 | * This serves as a proxy for checking if the operation is aborted. */ |
| 24 | #define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 ) |
| 25 | #define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 ) |
| 26 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 27 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 28 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 29 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 30 | /** Test if a buffer contains a constant byte value. |
| 31 | * |
| 32 | * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 33 | * |
| 34 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 35 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 36 | * \param size Size of the buffer in bytes. |
| 37 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 38 | * \return 1 if the buffer is all-bits-zero. |
| 39 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 40 | */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 41 | static int mem_is_char( void *buffer, unsigned char c, size_t size ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 42 | { |
| 43 | size_t i; |
| 44 | for( i = 0; i < size; i++ ) |
| 45 | { |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 46 | if( ( (unsigned char *) buffer )[i] != c ) |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 47 | return( 0 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 48 | } |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 49 | return( 1 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 50 | } |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 51 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 52 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
| 53 | static int asn1_write_10x( unsigned char **p, |
| 54 | unsigned char *start, |
| 55 | size_t bits, |
| 56 | unsigned char x ) |
| 57 | { |
| 58 | int ret; |
| 59 | int len = bits / 8 + 1; |
Gilles Peskine | 480416a | 2018-06-28 19:04:07 +0200 | [diff] [blame] | 60 | if( bits == 0 ) |
| 61 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
| 62 | if( bits <= 8 && x >= 1 << ( bits - 1 ) ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 63 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 64 | if( *p < start || *p - start < (ptrdiff_t) len ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 65 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 66 | *p -= len; |
| 67 | ( *p )[len-1] = x; |
| 68 | if( bits % 8 == 0 ) |
| 69 | ( *p )[1] |= 1; |
| 70 | else |
| 71 | ( *p )[0] |= 1 << ( bits % 8 ); |
| 72 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 73 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
| 74 | MBEDTLS_ASN1_INTEGER ) ); |
| 75 | return( len ); |
| 76 | } |
| 77 | |
| 78 | static int construct_fake_rsa_key( unsigned char *buffer, |
| 79 | size_t buffer_size, |
| 80 | unsigned char **p, |
| 81 | size_t bits, |
| 82 | int keypair ) |
| 83 | { |
| 84 | size_t half_bits = ( bits + 1 ) / 2; |
| 85 | int ret; |
| 86 | int len = 0; |
| 87 | /* Construct something that looks like a DER encoding of |
| 88 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 89 | * RSAPrivateKey ::= SEQUENCE { |
| 90 | * version Version, |
| 91 | * modulus INTEGER, -- n |
| 92 | * publicExponent INTEGER, -- e |
| 93 | * privateExponent INTEGER, -- d |
| 94 | * prime1 INTEGER, -- p |
| 95 | * prime2 INTEGER, -- q |
| 96 | * exponent1 INTEGER, -- d mod (p-1) |
| 97 | * exponent2 INTEGER, -- d mod (q-1) |
| 98 | * coefficient INTEGER, -- (inverse of q) mod p |
| 99 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 100 | * } |
| 101 | * Or, for a public key, the same structure with only |
| 102 | * version, modulus and publicExponent. |
| 103 | */ |
| 104 | *p = buffer + buffer_size; |
| 105 | if( keypair ) |
| 106 | { |
| 107 | MBEDTLS_ASN1_CHK_ADD( len, /* pq */ |
| 108 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 109 | MBEDTLS_ASN1_CHK_ADD( len, /* dq */ |
| 110 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 111 | MBEDTLS_ASN1_CHK_ADD( len, /* dp */ |
| 112 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 113 | MBEDTLS_ASN1_CHK_ADD( len, /* q */ |
| 114 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 115 | MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */ |
| 116 | asn1_write_10x( p, buffer, half_bits, 3 ) ); |
| 117 | MBEDTLS_ASN1_CHK_ADD( len, /* d */ |
| 118 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 119 | } |
| 120 | MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */ |
| 121 | asn1_write_10x( p, buffer, 17, 1 ) ); |
| 122 | MBEDTLS_ASN1_CHK_ADD( len, /* n */ |
| 123 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 124 | if( keypair ) |
| 125 | MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */ |
| 126 | mbedtls_asn1_write_int( p, buffer, 0 ) ); |
| 127 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) ); |
| 128 | { |
| 129 | const unsigned char tag = |
| 130 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
| 131 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) ); |
| 132 | } |
| 133 | return( len ); |
| 134 | } |
| 135 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 136 | int exercise_mac_setup( psa_key_type_t key_type, |
| 137 | const unsigned char *key_bytes, |
| 138 | size_t key_length, |
| 139 | psa_algorithm_t alg, |
| 140 | psa_mac_operation_t *operation, |
| 141 | psa_status_t *status ) |
| 142 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 143 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 144 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 145 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 146 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 147 | psa_set_key_algorithm( &attributes, alg ); |
| 148 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 149 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 150 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 151 | *status = psa_mac_sign_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 152 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 153 | PSA_ASSERT( psa_mac_abort( operation ) ); |
| 154 | /* If setup failed, reproduce the failure, so that the caller can |
| 155 | * test the resulting state of the operation object. */ |
| 156 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 157 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 158 | TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 159 | } |
| 160 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 161 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 162 | return( 1 ); |
| 163 | |
| 164 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 165 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 166 | return( 0 ); |
| 167 | } |
| 168 | |
| 169 | int exercise_cipher_setup( psa_key_type_t key_type, |
| 170 | const unsigned char *key_bytes, |
| 171 | size_t key_length, |
| 172 | psa_algorithm_t alg, |
| 173 | psa_cipher_operation_t *operation, |
| 174 | psa_status_t *status ) |
| 175 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 176 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 177 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 178 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 179 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 180 | psa_set_key_algorithm( &attributes, alg ); |
| 181 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 182 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 183 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 184 | *status = psa_cipher_encrypt_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 185 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 186 | PSA_ASSERT( psa_cipher_abort( operation ) ); |
| 187 | /* If setup failed, reproduce the failure, so that the caller can |
| 188 | * test the resulting state of the operation object. */ |
| 189 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 190 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 191 | TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ), |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 192 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 195 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 196 | return( 1 ); |
| 197 | |
| 198 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 199 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 200 | return( 0 ); |
| 201 | } |
| 202 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 203 | static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key ) |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 204 | { |
| 205 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 206 | mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 207 | uint8_t buffer[1]; |
| 208 | size_t length; |
| 209 | int ok = 0; |
| 210 | |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 211 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 212 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 213 | psa_set_key_algorithm( &attributes, PSA_ALG_CTR ); |
| 214 | psa_set_key_type( &attributes, PSA_KEY_TYPE_AES ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 215 | TEST_EQUAL( psa_get_key_attributes( key, &attributes ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 216 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 217 | TEST_EQUAL( |
| 218 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 ); |
| 219 | TEST_EQUAL( |
| 220 | MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 221 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 222 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 223 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 224 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
| 225 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
| 226 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 227 | TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 228 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 229 | TEST_EQUAL( psa_export_public_key( key, |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 230 | buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 231 | PSA_ERROR_INVALID_HANDLE ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 232 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 233 | ok = 1; |
| 234 | |
| 235 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 236 | /* |
| 237 | * Key attributes may have been returned by psa_get_key_attributes() |
| 238 | * thus reset them as required. |
| 239 | */ |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 240 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 241 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 242 | return( ok ); |
| 243 | } |
| 244 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 245 | /* Assert that a key isn't reported as having a slot number. */ |
| 246 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 247 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 248 | do \ |
| 249 | { \ |
| 250 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
| 251 | TEST_EQUAL( psa_get_key_slot_number( \ |
| 252 | attributes, \ |
| 253 | &ASSERT_NO_SLOT_NUMBER_slot_number ), \ |
| 254 | PSA_ERROR_INVALID_ARGUMENT ); \ |
| 255 | } \ |
| 256 | while( 0 ) |
| 257 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 258 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 259 | ( (void) 0 ) |
| 260 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 261 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 262 | /* An overapproximation of the amount of storage needed for a key of the |
| 263 | * given type and with the given content. The API doesn't make it easy |
| 264 | * to find a good value for the size. The current implementation doesn't |
| 265 | * care about the value anyway. */ |
| 266 | #define KEY_BITS_FROM_DATA( type, data ) \ |
| 267 | ( data )->len |
| 268 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 269 | typedef enum { |
| 270 | IMPORT_KEY = 0, |
| 271 | GENERATE_KEY = 1, |
| 272 | DERIVE_KEY = 2 |
| 273 | } generate_method; |
| 274 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 275 | /* END_HEADER */ |
| 276 | |
| 277 | /* BEGIN_DEPENDENCIES |
| 278 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 279 | * END_DEPENDENCIES |
| 280 | */ |
| 281 | |
| 282 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 283 | void static_checks( ) |
| 284 | { |
| 285 | size_t max_truncated_mac_size = |
| 286 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 287 | |
| 288 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 289 | * encoding. The shifted mask is the maximum truncated value. The |
| 290 | * untruncated algorithm may be one byte larger. */ |
| 291 | TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size ); |
| 292 | } |
| 293 | /* END_CASE */ |
| 294 | |
| 295 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 296 | void import_with_policy( int type_arg, |
| 297 | int usage_arg, int alg_arg, |
| 298 | int expected_status_arg ) |
| 299 | { |
| 300 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 301 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 302 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 303 | psa_key_type_t type = type_arg; |
| 304 | psa_key_usage_t usage = usage_arg; |
| 305 | psa_algorithm_t alg = alg_arg; |
| 306 | psa_status_t expected_status = expected_status_arg; |
| 307 | const uint8_t key_material[16] = {0}; |
| 308 | psa_status_t status; |
| 309 | |
| 310 | PSA_ASSERT( psa_crypto_init( ) ); |
| 311 | |
| 312 | psa_set_key_type( &attributes, type ); |
| 313 | psa_set_key_usage_flags( &attributes, usage ); |
| 314 | psa_set_key_algorithm( &attributes, alg ); |
| 315 | |
| 316 | status = psa_import_key( &attributes, |
| 317 | key_material, sizeof( key_material ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 318 | &key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 319 | TEST_EQUAL( status, expected_status ); |
| 320 | if( status != PSA_SUCCESS ) |
| 321 | goto exit; |
| 322 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 323 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 324 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 325 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 326 | mbedtls_test_update_key_usage_flags( usage ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 327 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 328 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 329 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 330 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 331 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 332 | |
| 333 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 334 | /* |
| 335 | * Key attributes may have been returned by psa_get_key_attributes() |
| 336 | * thus reset them as required. |
| 337 | */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 338 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 339 | |
| 340 | psa_destroy_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 341 | PSA_DONE( ); |
| 342 | } |
| 343 | /* END_CASE */ |
| 344 | |
| 345 | /* BEGIN_CASE */ |
| 346 | void import_with_data( data_t *data, int type_arg, |
| 347 | int attr_bits_arg, |
| 348 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 349 | { |
| 350 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 351 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 352 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 353 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 354 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 355 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 356 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 357 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 358 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 359 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 360 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 361 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 362 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 363 | status = psa_import_key( &attributes, data->x, data->len, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 364 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 365 | if( status != PSA_SUCCESS ) |
| 366 | goto exit; |
| 367 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 368 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 369 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 370 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 371 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 372 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 373 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 374 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 375 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 376 | |
| 377 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 378 | /* |
| 379 | * Key attributes may have been returned by psa_get_key_attributes() |
| 380 | * thus reset them as required. |
| 381 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 382 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 383 | |
| 384 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 385 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 386 | } |
| 387 | /* END_CASE */ |
| 388 | |
| 389 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 390 | void import_large_key( int type_arg, int byte_size_arg, |
| 391 | int expected_status_arg ) |
| 392 | { |
| 393 | psa_key_type_t type = type_arg; |
| 394 | size_t byte_size = byte_size_arg; |
| 395 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 396 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 397 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 398 | psa_status_t status; |
| 399 | uint8_t *buffer = NULL; |
| 400 | size_t buffer_size = byte_size + 1; |
| 401 | size_t n; |
| 402 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 403 | /* Skip the test case if the target running the test cannot |
| 404 | * accomodate large keys due to heap size constraints */ |
| 405 | ASSERT_ALLOC_WEAK( buffer, buffer_size ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 406 | memset( buffer, 'K', byte_size ); |
| 407 | |
| 408 | PSA_ASSERT( psa_crypto_init( ) ); |
| 409 | |
| 410 | /* Try importing the key */ |
| 411 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 412 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 413 | status = psa_import_key( &attributes, buffer, byte_size, &key ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 414 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 415 | TEST_EQUAL( status, expected_status ); |
| 416 | |
| 417 | if( status == PSA_SUCCESS ) |
| 418 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 419 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 420 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 421 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 422 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 423 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 424 | memset( buffer, 0, byte_size + 1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 425 | PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 426 | for( n = 0; n < byte_size; n++ ) |
| 427 | TEST_EQUAL( buffer[n], 'K' ); |
| 428 | for( n = byte_size; n < buffer_size; n++ ) |
| 429 | TEST_EQUAL( buffer[n], 0 ); |
| 430 | } |
| 431 | |
| 432 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 433 | /* |
| 434 | * Key attributes may have been returned by psa_get_key_attributes() |
| 435 | * thus reset them as required. |
| 436 | */ |
| 437 | psa_reset_key_attributes( &attributes ); |
| 438 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 439 | psa_destroy_key( key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 440 | PSA_DONE( ); |
| 441 | mbedtls_free( buffer ); |
| 442 | } |
| 443 | /* END_CASE */ |
| 444 | |
| 445 | /* BEGIN_CASE */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 446 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 447 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 448 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 449 | size_t bits = bits_arg; |
| 450 | psa_status_t expected_status = expected_status_arg; |
| 451 | psa_status_t status; |
| 452 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 453 | 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] | 454 | size_t buffer_size = /* Slight overapproximations */ |
| 455 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 456 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 457 | unsigned char *p; |
| 458 | int ret; |
| 459 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 460 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 461 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 462 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 463 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 464 | |
| 465 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 466 | bits, keypair ) ) >= 0 ); |
| 467 | length = ret; |
| 468 | |
| 469 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 470 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 471 | status = psa_import_key( &attributes, p, length, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 472 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 473 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 474 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 475 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 476 | |
| 477 | exit: |
| 478 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 479 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 480 | } |
| 481 | /* END_CASE */ |
| 482 | |
| 483 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 484 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 485 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 486 | int usage_arg, int alg_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 487 | int expected_bits, |
| 488 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 489 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 490 | int canonical_input ) |
| 491 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 492 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 493 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 494 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 495 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 496 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 497 | unsigned char *exported = NULL; |
| 498 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 499 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 500 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 501 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 502 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 503 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 504 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 505 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 506 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 507 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 508 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 509 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 510 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 511 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 512 | psa_set_key_algorithm( &attributes, alg ); |
| 513 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 514 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 515 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 516 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 517 | |
| 518 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 519 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 520 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 521 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 522 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 523 | |
| 524 | /* Export the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 525 | status = psa_export_key( key, exported, export_size, &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 526 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 527 | |
| 528 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 529 | * and export_size. On errors, the exported length must be 0. */ |
| 530 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 531 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
| 532 | TEST_ASSERT( exported_length <= export_size ); |
| 533 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 534 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 535 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 536 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 537 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 538 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 539 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 540 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 541 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 542 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 543 | * this validates the canonical representations. For canonical inputs, |
| 544 | * this doesn't directly validate the implementation, but it still helps |
| 545 | * by cross-validating the test data with the sanity check code. */ |
| 546 | if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) ) |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 547 | goto exit; |
| 548 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 549 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 550 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 551 | else |
| 552 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 553 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 554 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 555 | &key2 ) ); |
| 556 | PSA_ASSERT( psa_export_key( key2, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 557 | reexported, |
| 558 | export_size, |
| 559 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 560 | ASSERT_COMPARE( exported, exported_length, |
| 561 | reexported, reexported_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 562 | PSA_ASSERT( psa_destroy_key( key2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 563 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 564 | TEST_ASSERT( exported_length <= |
| 565 | PSA_EXPORT_KEY_OUTPUT_SIZE( type, |
| 566 | psa_get_key_bits( &got_attributes ) ) ); |
| 567 | TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 568 | |
| 569 | destroy: |
| 570 | /* Destroy the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 571 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 572 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 573 | |
| 574 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 575 | /* |
| 576 | * Key attributes may have been returned by psa_get_key_attributes() |
| 577 | * thus reset them as required. |
| 578 | */ |
| 579 | psa_reset_key_attributes( &got_attributes ); |
| 580 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 581 | mbedtls_free( exported ); |
| 582 | mbedtls_free( reexported ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 583 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 584 | } |
| 585 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 586 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 587 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 588 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 589 | int type_arg, |
| 590 | int alg_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 591 | int export_size_delta, |
| 592 | int expected_export_status_arg, |
| 593 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 594 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 595 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 596 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 597 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 598 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 599 | psa_status_t status; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 600 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 601 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 602 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 603 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 604 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 605 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 606 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 607 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 608 | psa_set_key_algorithm( &attributes, alg ); |
| 609 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 610 | |
| 611 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 612 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 613 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 614 | /* Export the public key */ |
| 615 | ASSERT_ALLOC( exported, export_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 616 | status = psa_export_public_key( key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 617 | exported, export_size, |
| 618 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 619 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 620 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 621 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 622 | 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] | 623 | size_t bits; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 624 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 625 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 626 | TEST_ASSERT( expected_public_key->len <= |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 627 | PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 628 | TEST_ASSERT( expected_public_key->len <= |
| 629 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 630 | TEST_ASSERT( expected_public_key->len <= |
| 631 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 632 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 633 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 634 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 635 | |
| 636 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 637 | /* |
| 638 | * Key attributes may have been returned by psa_get_key_attributes() |
| 639 | * thus reset them as required. |
| 640 | */ |
| 641 | psa_reset_key_attributes( &attributes ); |
| 642 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 643 | mbedtls_free( exported ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 644 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 645 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 646 | } |
| 647 | /* END_CASE */ |
| 648 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 649 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 650 | void import_and_exercise_key( data_t *data, |
| 651 | int type_arg, |
| 652 | int bits_arg, |
| 653 | int alg_arg ) |
| 654 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 655 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 656 | psa_key_type_t type = type_arg; |
| 657 | size_t bits = bits_arg; |
| 658 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 659 | 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] | 660 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 661 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 662 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 663 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 664 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 665 | psa_set_key_usage_flags( &attributes, usage ); |
| 666 | psa_set_key_algorithm( &attributes, alg ); |
| 667 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 668 | |
| 669 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 670 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 671 | |
| 672 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 673 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 674 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 675 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 676 | |
| 677 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 678 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 679 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 680 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 681 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 682 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 683 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 684 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 685 | /* |
| 686 | * Key attributes may have been returned by psa_get_key_attributes() |
| 687 | * thus reset them as required. |
| 688 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 689 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 690 | |
| 691 | psa_reset_key_attributes( &attributes ); |
| 692 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 693 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 694 | } |
| 695 | /* END_CASE */ |
| 696 | |
| 697 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 698 | void effective_key_attributes( int type_arg, int expected_type_arg, |
| 699 | int bits_arg, int expected_bits_arg, |
| 700 | int usage_arg, int expected_usage_arg, |
| 701 | int alg_arg, int expected_alg_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 702 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 703 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 704 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 705 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 706 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 707 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 708 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 709 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 710 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 711 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 712 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 713 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 714 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 715 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 716 | psa_set_key_usage_flags( &attributes, usage ); |
| 717 | psa_set_key_algorithm( &attributes, alg ); |
| 718 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 719 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 720 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 721 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 722 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 723 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 724 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 725 | TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type ); |
| 726 | TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits ); |
| 727 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 728 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 729 | |
| 730 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 731 | /* |
| 732 | * Key attributes may have been returned by psa_get_key_attributes() |
| 733 | * thus reset them as required. |
| 734 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 735 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 736 | |
| 737 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 738 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 739 | } |
| 740 | /* END_CASE */ |
| 741 | |
| 742 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 743 | void check_key_policy( int type_arg, int bits_arg, |
gabor-mezei-arm | ff8264c | 2021-06-28 14:36:03 +0200 | [diff] [blame] | 744 | int usage_arg, int alg_arg ) |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 745 | { |
| 746 | test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg, |
gabor-mezei-arm | ff8264c | 2021-06-28 14:36:03 +0200 | [diff] [blame] | 747 | usage_arg, |
| 748 | mbedtls_test_update_key_usage_flags( usage_arg ), |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 749 | alg_arg, alg_arg ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 750 | goto exit; |
| 751 | } |
| 752 | /* END_CASE */ |
| 753 | |
| 754 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 755 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 756 | { |
| 757 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 758 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 759 | * though it's OK by the C standard. We could test for this, but we'd need |
| 760 | * to supress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 761 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 762 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 763 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 764 | |
| 765 | memset( &zero, 0, sizeof( zero ) ); |
| 766 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 767 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 768 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 769 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 770 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 771 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 772 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 773 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 774 | |
| 775 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 776 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 777 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 778 | |
| 779 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 780 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 781 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 782 | |
| 783 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 784 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 785 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 786 | } |
| 787 | /* END_CASE */ |
| 788 | |
| 789 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 790 | void mac_key_policy( int policy_usage_arg, |
| 791 | int policy_alg_arg, |
| 792 | int key_type_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 793 | data_t *key_data, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 794 | int exercise_alg_arg, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 795 | int expected_status_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 796 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 797 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 798 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 799 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 800 | psa_key_type_t key_type = key_type_arg; |
| 801 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 802 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 803 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 804 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 805 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 806 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 807 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 808 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 809 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 810 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 811 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 812 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 813 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 814 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 815 | &key ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 816 | |
gabor-mezei-arm | 40d5cd8 | 2021-06-29 11:06:16 +0200 | [diff] [blame] | 817 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
| 818 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 819 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 820 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 821 | if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 822 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 823 | else |
| 824 | TEST_EQUAL( status, expected_status ); |
| 825 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 826 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 827 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 828 | memset( mac, 0, sizeof( mac ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 829 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 830 | if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 831 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 832 | else |
| 833 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 834 | |
| 835 | exit: |
| 836 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 837 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 838 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 839 | } |
| 840 | /* END_CASE */ |
| 841 | |
| 842 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 843 | void cipher_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 844 | int policy_alg, |
| 845 | int key_type, |
| 846 | data_t *key_data, |
| 847 | int exercise_alg ) |
| 848 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 849 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 850 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 851 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 852 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 853 | psa_status_t status; |
| 854 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 855 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 856 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 857 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 858 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 859 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 860 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 861 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 862 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 863 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 864 | /* Check if no key usage flag implication is done */ |
| 865 | TEST_EQUAL( policy_usage, |
| 866 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 867 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 868 | status = psa_cipher_encrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 869 | if( policy_alg == exercise_alg && |
| 870 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 871 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 872 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 873 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 874 | psa_cipher_abort( &operation ); |
| 875 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 876 | status = psa_cipher_decrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 877 | if( policy_alg == exercise_alg && |
| 878 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 879 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 880 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 881 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 882 | |
| 883 | exit: |
| 884 | psa_cipher_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 885 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 886 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 887 | } |
| 888 | /* END_CASE */ |
| 889 | |
| 890 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 891 | void aead_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 892 | int policy_alg, |
| 893 | int key_type, |
| 894 | data_t *key_data, |
| 895 | int nonce_length_arg, |
| 896 | int tag_length_arg, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 897 | int exercise_alg, |
| 898 | int expected_status_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 899 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 900 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 901 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 902 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 903 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 904 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 905 | unsigned char nonce[16] = {0}; |
| 906 | size_t nonce_length = nonce_length_arg; |
| 907 | unsigned char tag[16]; |
| 908 | size_t tag_length = tag_length_arg; |
| 909 | size_t output_length; |
| 910 | |
| 911 | TEST_ASSERT( nonce_length <= sizeof( nonce ) ); |
| 912 | TEST_ASSERT( tag_length <= sizeof( tag ) ); |
| 913 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 914 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 915 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 916 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 917 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 918 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 919 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 920 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 921 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 922 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 923 | /* Check if no key usage implication is done */ |
| 924 | TEST_EQUAL( policy_usage, |
| 925 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 926 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 927 | status = psa_aead_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 928 | nonce, nonce_length, |
| 929 | NULL, 0, |
| 930 | NULL, 0, |
| 931 | tag, tag_length, |
| 932 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 933 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 934 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 935 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 936 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 937 | |
| 938 | memset( tag, 0, sizeof( tag ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 939 | status = psa_aead_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 940 | nonce, nonce_length, |
| 941 | NULL, 0, |
| 942 | tag, tag_length, |
| 943 | NULL, 0, |
| 944 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 945 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 946 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 947 | else if( expected_status == PSA_SUCCESS ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 948 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 949 | else |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 950 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 951 | |
| 952 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 953 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 954 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 955 | } |
| 956 | /* END_CASE */ |
| 957 | |
| 958 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 959 | void asymmetric_encryption_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 960 | int policy_alg, |
| 961 | int key_type, |
| 962 | data_t *key_data, |
| 963 | int exercise_alg ) |
| 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 | edf2df8 | 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; |
| 969 | size_t key_bits; |
| 970 | size_t buffer_length; |
| 971 | unsigned char *buffer = NULL; |
| 972 | size_t output_length; |
| 973 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 974 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 975 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 976 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 977 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 978 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 979 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 980 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 981 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 982 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 983 | /* Check if no key usage implication is done */ |
| 984 | TEST_EQUAL( policy_usage, |
| 985 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 986 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 987 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 988 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 989 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 990 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 991 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 992 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 993 | status = psa_asymmetric_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 994 | NULL, 0, |
| 995 | NULL, 0, |
| 996 | buffer, buffer_length, |
| 997 | &output_length ); |
| 998 | if( policy_alg == exercise_alg && |
| 999 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1000 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1001 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1002 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1003 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 1004 | if( buffer_length != 0 ) |
| 1005 | memset( buffer, 0, buffer_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1006 | status = psa_asymmetric_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1007 | buffer, buffer_length, |
| 1008 | NULL, 0, |
| 1009 | buffer, buffer_length, |
| 1010 | &output_length ); |
| 1011 | if( policy_alg == exercise_alg && |
| 1012 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1013 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1014 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1015 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1016 | |
| 1017 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1018 | /* |
| 1019 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1020 | * thus reset them as required. |
| 1021 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1022 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1023 | |
| 1024 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1025 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1026 | mbedtls_free( buffer ); |
| 1027 | } |
| 1028 | /* END_CASE */ |
| 1029 | |
| 1030 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1031 | void asymmetric_signature_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1032 | int policy_alg, |
| 1033 | int key_type, |
| 1034 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1035 | int exercise_alg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1036 | int payload_length_arg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1037 | int expected_usage_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1038 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1039 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1040 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1041 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 1042 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1043 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1044 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 1045 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 1046 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 1047 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 1048 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 1049 | int compatible_alg = payload_length_arg > 0; |
| 1050 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1051 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1052 | size_t signature_length; |
| 1053 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1054 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1055 | in the expected usage flags. */ |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1056 | TEST_EQUAL( expected_usage, |
| 1057 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1058 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1059 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1060 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1061 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1062 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1063 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1064 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1065 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1066 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1067 | |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1068 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1069 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1070 | status = psa_sign_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1071 | payload, payload_length, |
| 1072 | signature, sizeof( signature ), |
| 1073 | &signature_length ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1074 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1075 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1076 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1077 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1078 | |
| 1079 | memset( signature, 0, sizeof( signature ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1080 | status = psa_verify_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1081 | payload, payload_length, |
| 1082 | signature, sizeof( signature ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1083 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1084 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1085 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1086 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1087 | |
gabor-mezei-arm | d851d68 | 2021-06-28 14:53:49 +0200 | [diff] [blame] | 1088 | if( PSA_ALG_IS_HASH_AND_SIGN( exercise_alg ) && |
| 1089 | PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) ) |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1090 | { |
| 1091 | status = psa_sign_message( key, exercise_alg, |
| 1092 | payload, payload_length, |
| 1093 | signature, sizeof( signature ), |
| 1094 | &signature_length ); |
| 1095 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 ) |
| 1096 | PSA_ASSERT( status ); |
| 1097 | else |
| 1098 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1099 | |
| 1100 | memset( signature, 0, sizeof( signature ) ); |
| 1101 | status = psa_verify_message( key, exercise_alg, |
| 1102 | payload, payload_length, |
| 1103 | signature, sizeof( signature ) ); |
| 1104 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 ) |
| 1105 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 1106 | else |
| 1107 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1108 | } |
| 1109 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1110 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1111 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1112 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1113 | } |
| 1114 | /* END_CASE */ |
| 1115 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1116 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1117 | void derive_key_policy( int policy_usage, |
| 1118 | int policy_alg, |
| 1119 | int key_type, |
| 1120 | data_t *key_data, |
| 1121 | int exercise_alg ) |
| 1122 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1123 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1124 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1125 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1126 | psa_status_t status; |
| 1127 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1128 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1129 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1130 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1131 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1132 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1133 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1134 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1135 | &key ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1136 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1137 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 1138 | |
| 1139 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 1140 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1141 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1142 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 1143 | &operation, |
| 1144 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 1145 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1146 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1147 | |
| 1148 | status = psa_key_derivation_input_key( &operation, |
| 1149 | PSA_KEY_DERIVATION_INPUT_SECRET, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1150 | key ); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1151 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1152 | if( policy_alg == exercise_alg && |
| 1153 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1154 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1155 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1156 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1157 | |
| 1158 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1159 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1160 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1161 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1162 | } |
| 1163 | /* END_CASE */ |
| 1164 | |
| 1165 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1166 | void agreement_key_policy( int policy_usage, |
| 1167 | int policy_alg, |
| 1168 | int key_type_arg, |
| 1169 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1170 | int exercise_alg, |
| 1171 | int expected_status_arg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1172 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1173 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1174 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1175 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1176 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1177 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1178 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1179 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1180 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1181 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1182 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1183 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1184 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1185 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1186 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1187 | &key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1188 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1189 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1190 | status = mbedtls_test_psa_key_agreement_with_self( &operation, key ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1191 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1192 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1193 | |
| 1194 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1195 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1196 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1197 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1198 | } |
| 1199 | /* END_CASE */ |
| 1200 | |
| 1201 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1202 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 1203 | int usage_arg, int alg_arg, int alg2_arg ) |
| 1204 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1205 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1206 | psa_key_type_t key_type = key_type_arg; |
| 1207 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1208 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1209 | psa_key_usage_t usage = usage_arg; |
| 1210 | psa_algorithm_t alg = alg_arg; |
| 1211 | psa_algorithm_t alg2 = alg2_arg; |
| 1212 | |
| 1213 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1214 | |
| 1215 | psa_set_key_usage_flags( &attributes, usage ); |
| 1216 | psa_set_key_algorithm( &attributes, alg ); |
| 1217 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 1218 | psa_set_key_type( &attributes, key_type ); |
| 1219 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1220 | &key ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1221 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1222 | /* Update the usage flags to obtain implicit usage flags */ |
| 1223 | usage = mbedtls_test_update_key_usage_flags( usage ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1224 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1225 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 1226 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 1227 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 1228 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1229 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1230 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1231 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1232 | goto exit; |
| 1233 | |
| 1234 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1235 | /* |
| 1236 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1237 | * thus reset them as required. |
| 1238 | */ |
| 1239 | psa_reset_key_attributes( &got_attributes ); |
| 1240 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1241 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1242 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1243 | } |
| 1244 | /* END_CASE */ |
| 1245 | |
| 1246 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1247 | void raw_agreement_key_policy( int policy_usage, |
| 1248 | int policy_alg, |
| 1249 | int key_type_arg, |
| 1250 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1251 | int exercise_alg, |
| 1252 | int expected_status_arg ) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1253 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1254 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1255 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1256 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1257 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1258 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1259 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1260 | |
| 1261 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1262 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1263 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1264 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1265 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1266 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1267 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1268 | &key ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1269 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1270 | status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1271 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1272 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1273 | |
| 1274 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1275 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1276 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1277 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1278 | } |
| 1279 | /* END_CASE */ |
| 1280 | |
| 1281 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1282 | void copy_success( int source_usage_arg, |
| 1283 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1284 | int type_arg, data_t *material, |
| 1285 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1286 | int target_usage_arg, |
| 1287 | int target_alg_arg, int target_alg2_arg, |
| 1288 | int expected_usage_arg, |
| 1289 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1290 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1291 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1292 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1293 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 1294 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1295 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1296 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1297 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1298 | uint8_t *export_buffer = NULL; |
| 1299 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1300 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1301 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1302 | /* Prepare the source key. */ |
| 1303 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 1304 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1305 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1306 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1307 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 1308 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1309 | &source_key ) ); |
| 1310 | PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1311 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1312 | /* Prepare the target attributes. */ |
| 1313 | if( copy_attributes ) |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1314 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1315 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1316 | /* Set volatile lifetime to reset the key identifier to 0. */ |
| 1317 | psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE ); |
| 1318 | } |
| 1319 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1320 | if( target_usage_arg != -1 ) |
| 1321 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 1322 | if( target_alg_arg != -1 ) |
| 1323 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1324 | if( target_alg2_arg != -1 ) |
| 1325 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1326 | |
| 1327 | /* Copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1328 | PSA_ASSERT( psa_copy_key( source_key, |
| 1329 | &target_attributes, &target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1330 | |
| 1331 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1332 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1333 | |
| 1334 | /* Test that the target slot has the expected content and policy. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1335 | PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1336 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 1337 | psa_get_key_type( &target_attributes ) ); |
| 1338 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 1339 | psa_get_key_bits( &target_attributes ) ); |
| 1340 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 1341 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1342 | TEST_EQUAL( expected_alg2, |
| 1343 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1344 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 1345 | { |
| 1346 | size_t length; |
| 1347 | ASSERT_ALLOC( export_buffer, material->len ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1348 | PSA_ASSERT( psa_export_key( target_key, export_buffer, |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1349 | material->len, &length ) ); |
| 1350 | ASSERT_COMPARE( material->x, material->len, |
| 1351 | export_buffer, length ); |
| 1352 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1353 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1354 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1355 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1356 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) ) |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1357 | goto exit; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1358 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1359 | PSA_ASSERT( psa_destroy_key( target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1360 | |
| 1361 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1362 | /* |
| 1363 | * Source and target key attributes may have been returned by |
| 1364 | * psa_get_key_attributes() thus reset them as required. |
| 1365 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1366 | psa_reset_key_attributes( &source_attributes ); |
| 1367 | psa_reset_key_attributes( &target_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1368 | |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1369 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1370 | mbedtls_free( export_buffer ); |
| 1371 | } |
| 1372 | /* END_CASE */ |
| 1373 | |
| 1374 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1375 | void copy_fail( int source_usage_arg, |
| 1376 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1377 | int type_arg, data_t *material, |
| 1378 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1379 | int target_usage_arg, |
| 1380 | int target_alg_arg, int target_alg2_arg, |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 1381 | int target_id_arg, int target_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1382 | int expected_status_arg ) |
| 1383 | { |
| 1384 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1385 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1386 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1387 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 1388 | 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] | 1389 | |
| 1390 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1391 | |
| 1392 | /* Prepare the source key. */ |
| 1393 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 1394 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1395 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1396 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1397 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 1398 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1399 | &source_key ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1400 | |
| 1401 | /* Prepare the target attributes. */ |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 1402 | psa_set_key_id( &target_attributes, key_id ); |
| 1403 | psa_set_key_lifetime( &target_attributes, target_lifetime_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1404 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 1405 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 1406 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 1407 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1408 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1409 | |
| 1410 | /* Try to copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1411 | TEST_EQUAL( psa_copy_key( source_key, |
| 1412 | &target_attributes, &target_key ), |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1413 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1414 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1415 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1416 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1417 | exit: |
| 1418 | psa_reset_key_attributes( &source_attributes ); |
| 1419 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1420 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1421 | } |
| 1422 | /* END_CASE */ |
| 1423 | |
| 1424 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1425 | void hash_operation_init( ) |
| 1426 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1427 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1428 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1429 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1430 | * though it's OK by the C standard. We could test for this, but we'd need |
| 1431 | * to supress the Clang warning for the test. */ |
| 1432 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 1433 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 1434 | psa_hash_operation_t zero; |
| 1435 | |
| 1436 | memset( &zero, 0, sizeof( zero ) ); |
| 1437 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1438 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1439 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 1440 | PSA_ERROR_BAD_STATE ); |
| 1441 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 1442 | PSA_ERROR_BAD_STATE ); |
| 1443 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 1444 | PSA_ERROR_BAD_STATE ); |
| 1445 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1446 | /* A default hash operation should be abortable without error. */ |
| 1447 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 1448 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 1449 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1450 | } |
| 1451 | /* END_CASE */ |
| 1452 | |
| 1453 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1454 | void hash_setup( int alg_arg, |
| 1455 | int expected_status_arg ) |
| 1456 | { |
| 1457 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1458 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1459 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1460 | psa_status_t status; |
| 1461 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1462 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1463 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1464 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1465 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1466 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 1467 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 1468 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1469 | |
| 1470 | /* If setup failed, reproduce the failure, so as to |
| 1471 | * test the resulting state of the operation object. */ |
| 1472 | if( status != PSA_SUCCESS ) |
| 1473 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 1474 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1475 | /* Now the operation object should be reusable. */ |
| 1476 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 1477 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 1478 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1479 | #endif |
| 1480 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1481 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1482 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1483 | } |
| 1484 | /* END_CASE */ |
| 1485 | |
| 1486 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1487 | void hash_compute_fail( int alg_arg, data_t *input, |
| 1488 | int output_size_arg, int expected_status_arg ) |
| 1489 | { |
| 1490 | psa_algorithm_t alg = alg_arg; |
| 1491 | uint8_t *output = NULL; |
| 1492 | size_t output_size = output_size_arg; |
| 1493 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 1494 | psa_status_t expected_status = expected_status_arg; |
| 1495 | psa_status_t status; |
| 1496 | |
| 1497 | ASSERT_ALLOC( output, output_size ); |
| 1498 | |
| 1499 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1500 | |
| 1501 | status = psa_hash_compute( alg, input->x, input->len, |
| 1502 | output, output_size, &output_length ); |
| 1503 | TEST_EQUAL( status, expected_status ); |
| 1504 | TEST_ASSERT( output_length <= output_size ); |
| 1505 | |
| 1506 | exit: |
| 1507 | mbedtls_free( output ); |
| 1508 | PSA_DONE( ); |
| 1509 | } |
| 1510 | /* END_CASE */ |
| 1511 | |
| 1512 | /* BEGIN_CASE */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 1513 | void hash_compare_fail( int alg_arg, data_t *input, |
| 1514 | data_t *reference_hash, |
| 1515 | int expected_status_arg ) |
| 1516 | { |
| 1517 | psa_algorithm_t alg = alg_arg; |
| 1518 | psa_status_t expected_status = expected_status_arg; |
| 1519 | psa_status_t status; |
| 1520 | |
| 1521 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1522 | |
| 1523 | status = psa_hash_compare( alg, input->x, input->len, |
| 1524 | reference_hash->x, reference_hash->len ); |
| 1525 | TEST_EQUAL( status, expected_status ); |
| 1526 | |
| 1527 | exit: |
| 1528 | PSA_DONE( ); |
| 1529 | } |
| 1530 | /* END_CASE */ |
| 1531 | |
| 1532 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1533 | void hash_compute_compare( int alg_arg, data_t *input, |
| 1534 | data_t *expected_output ) |
| 1535 | { |
| 1536 | psa_algorithm_t alg = alg_arg; |
| 1537 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 1538 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 1539 | size_t i; |
| 1540 | |
| 1541 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1542 | |
| 1543 | /* Compute with tight buffer */ |
| 1544 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1545 | output, PSA_HASH_LENGTH( alg ), |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1546 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1547 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1548 | ASSERT_COMPARE( output, output_length, |
| 1549 | expected_output->x, expected_output->len ); |
| 1550 | |
| 1551 | /* Compute with larger buffer */ |
| 1552 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
| 1553 | output, sizeof( output ), |
| 1554 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1555 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1556 | ASSERT_COMPARE( output, output_length, |
| 1557 | expected_output->x, expected_output->len ); |
| 1558 | |
| 1559 | /* Compare with correct hash */ |
| 1560 | PSA_ASSERT( psa_hash_compare( alg, input->x, input->len, |
| 1561 | output, output_length ) ); |
| 1562 | |
| 1563 | /* Compare with trailing garbage */ |
| 1564 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 1565 | output, output_length + 1 ), |
| 1566 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1567 | |
| 1568 | /* Compare with truncated hash */ |
| 1569 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 1570 | output, output_length - 1 ), |
| 1571 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1572 | |
| 1573 | /* Compare with corrupted value */ |
| 1574 | for( i = 0; i < output_length; i++ ) |
| 1575 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 1576 | mbedtls_test_set_step( i ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1577 | output[i] ^= 1; |
| 1578 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 1579 | output, output_length ), |
| 1580 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1581 | output[i] ^= 1; |
| 1582 | } |
| 1583 | |
| 1584 | exit: |
| 1585 | PSA_DONE( ); |
| 1586 | } |
| 1587 | /* END_CASE */ |
| 1588 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 1589 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1590 | void hash_bad_order( ) |
| 1591 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1592 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1593 | unsigned char input[] = ""; |
| 1594 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1595 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1596 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 1597 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 1598 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1599 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1600 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1601 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1602 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1603 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1604 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1605 | /* Call setup twice in a row. */ |
| 1606 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1607 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1608 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 1609 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1610 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1611 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1612 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1613 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1614 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1615 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1616 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1617 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1618 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1619 | /* Check that update calls abort on error. */ |
| 1620 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 1621 | operation.id = UINT_MAX; |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1622 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
| 1623 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
| 1624 | PSA_ERROR_BAD_STATE ); |
| 1625 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 1626 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1627 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 1628 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1629 | /* Call update after finish. */ |
| 1630 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1631 | PSA_ASSERT( psa_hash_finish( &operation, |
| 1632 | hash, sizeof( hash ), &hash_len ) ); |
| 1633 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1634 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1635 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1636 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1637 | /* Call verify without calling setup beforehand. */ |
| 1638 | TEST_EQUAL( psa_hash_verify( &operation, |
| 1639 | valid_hash, sizeof( valid_hash ) ), |
| 1640 | PSA_ERROR_BAD_STATE ); |
| 1641 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1642 | |
| 1643 | /* Call verify after finish. */ |
| 1644 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1645 | PSA_ASSERT( psa_hash_finish( &operation, |
| 1646 | hash, sizeof( hash ), &hash_len ) ); |
| 1647 | TEST_EQUAL( psa_hash_verify( &operation, |
| 1648 | valid_hash, sizeof( valid_hash ) ), |
| 1649 | PSA_ERROR_BAD_STATE ); |
| 1650 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1651 | |
| 1652 | /* Call verify twice in a row. */ |
| 1653 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1654 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1655 | PSA_ASSERT( psa_hash_verify( &operation, |
| 1656 | valid_hash, sizeof( valid_hash ) ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1657 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1658 | TEST_EQUAL( psa_hash_verify( &operation, |
| 1659 | valid_hash, sizeof( valid_hash ) ), |
| 1660 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1661 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1662 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1663 | |
| 1664 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1665 | TEST_EQUAL( psa_hash_finish( &operation, |
| 1666 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1667 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1668 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1669 | |
| 1670 | /* Call finish twice in a row. */ |
| 1671 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1672 | PSA_ASSERT( psa_hash_finish( &operation, |
| 1673 | hash, sizeof( hash ), &hash_len ) ); |
| 1674 | TEST_EQUAL( psa_hash_finish( &operation, |
| 1675 | hash, sizeof( hash ), &hash_len ), |
| 1676 | PSA_ERROR_BAD_STATE ); |
| 1677 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1678 | |
| 1679 | /* Call finish after calling verify. */ |
| 1680 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1681 | PSA_ASSERT( psa_hash_verify( &operation, |
| 1682 | valid_hash, sizeof( valid_hash ) ) ); |
| 1683 | TEST_EQUAL( psa_hash_finish( &operation, |
| 1684 | hash, sizeof( hash ), &hash_len ), |
| 1685 | PSA_ERROR_BAD_STATE ); |
| 1686 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1687 | |
| 1688 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1689 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1690 | } |
| 1691 | /* END_CASE */ |
| 1692 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 1693 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1694 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1695 | { |
| 1696 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1697 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 1698 | * appended to it */ |
| 1699 | unsigned char hash[] = { |
| 1700 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 1701 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 1702 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1703 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1704 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1705 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1706 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1707 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1708 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1709 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1710 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1711 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1712 | PSA_ERROR_INVALID_SIGNATURE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1713 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 1714 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1715 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1716 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1717 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1718 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1719 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1720 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1721 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1722 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1723 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1724 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1725 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 1726 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1727 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1728 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1729 | } |
| 1730 | /* END_CASE */ |
| 1731 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 1732 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 1733 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1734 | { |
| 1735 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 1736 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1737 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1738 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1739 | size_t hash_len; |
| 1740 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1741 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1742 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1743 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1744 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1745 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1746 | hash, expected_size - 1, &hash_len ), |
| 1747 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1748 | |
| 1749 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1750 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1751 | } |
| 1752 | /* END_CASE */ |
| 1753 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 1754 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1755 | void hash_clone_source_state( ) |
| 1756 | { |
| 1757 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 1758 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 1759 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 1760 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 1761 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 1762 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 1763 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 1764 | size_t hash_len; |
| 1765 | |
| 1766 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1767 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 1768 | |
| 1769 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 1770 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 1771 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 1772 | hash, sizeof( hash ), &hash_len ) ); |
| 1773 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 1774 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 1775 | |
| 1776 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 1777 | PSA_ERROR_BAD_STATE ); |
| 1778 | |
| 1779 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 1780 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 1781 | hash, sizeof( hash ), &hash_len ) ); |
| 1782 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 1783 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 1784 | hash, sizeof( hash ), &hash_len ) ); |
| 1785 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 1786 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 1787 | hash, sizeof( hash ), &hash_len ) ); |
| 1788 | |
| 1789 | exit: |
| 1790 | psa_hash_abort( &op_source ); |
| 1791 | psa_hash_abort( &op_init ); |
| 1792 | psa_hash_abort( &op_setup ); |
| 1793 | psa_hash_abort( &op_finished ); |
| 1794 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1795 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [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_target_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_init = PSA_HASH_OPERATION_INIT; |
| 1805 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 1806 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 1807 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 1808 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 1809 | size_t hash_len; |
| 1810 | |
| 1811 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1812 | |
| 1813 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 1814 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 1815 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 1816 | hash, sizeof( hash ), &hash_len ) ); |
| 1817 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 1818 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 1819 | |
| 1820 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 1821 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 1822 | hash, sizeof( hash ), &hash_len ) ); |
| 1823 | |
| 1824 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 1825 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 1826 | PSA_ERROR_BAD_STATE ); |
| 1827 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 1828 | PSA_ERROR_BAD_STATE ); |
| 1829 | |
| 1830 | exit: |
| 1831 | psa_hash_abort( &op_target ); |
| 1832 | psa_hash_abort( &op_init ); |
| 1833 | psa_hash_abort( &op_setup ); |
| 1834 | psa_hash_abort( &op_finished ); |
| 1835 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1836 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1837 | } |
| 1838 | /* END_CASE */ |
| 1839 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1840 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1841 | void mac_operation_init( ) |
| 1842 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1843 | const uint8_t input[1] = { 0 }; |
| 1844 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1845 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1846 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1847 | * though it's OK by the C standard. We could test for this, but we'd need |
| 1848 | * to supress the Clang warning for the test. */ |
| 1849 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 1850 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 1851 | psa_mac_operation_t zero; |
| 1852 | |
| 1853 | memset( &zero, 0, sizeof( zero ) ); |
| 1854 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1855 | /* A freshly-initialized MAC operation should not be usable. */ |
| 1856 | TEST_EQUAL( psa_mac_update( &func, |
| 1857 | input, sizeof( input ) ), |
| 1858 | PSA_ERROR_BAD_STATE ); |
| 1859 | TEST_EQUAL( psa_mac_update( &init, |
| 1860 | input, sizeof( input ) ), |
| 1861 | PSA_ERROR_BAD_STATE ); |
| 1862 | TEST_EQUAL( psa_mac_update( &zero, |
| 1863 | input, sizeof( input ) ), |
| 1864 | PSA_ERROR_BAD_STATE ); |
| 1865 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1866 | /* A default MAC operation should be abortable without error. */ |
| 1867 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 1868 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 1869 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1870 | } |
| 1871 | /* END_CASE */ |
| 1872 | |
| 1873 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1874 | void mac_setup( int key_type_arg, |
| 1875 | data_t *key, |
| 1876 | int alg_arg, |
| 1877 | int expected_status_arg ) |
| 1878 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1879 | psa_key_type_t key_type = key_type_arg; |
| 1880 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1881 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1882 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1883 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 1884 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 1885 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 1886 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1887 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1888 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1889 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1890 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 1891 | &operation, &status ) ) |
| 1892 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1893 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1894 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1895 | /* The operation object should be reusable. */ |
| 1896 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 1897 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 1898 | smoke_test_key_data, |
| 1899 | sizeof( smoke_test_key_data ), |
| 1900 | KNOWN_SUPPORTED_MAC_ALG, |
| 1901 | &operation, &status ) ) |
| 1902 | goto exit; |
| 1903 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1904 | #endif |
| 1905 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1906 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1907 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1908 | } |
| 1909 | /* END_CASE */ |
| 1910 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 1911 | /* 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] | 1912 | void mac_bad_order( ) |
| 1913 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1914 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1915 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 1916 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1917 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1918 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 1919 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 1920 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1921 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1922 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 1923 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 1924 | size_t sign_mac_length = 0; |
| 1925 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 1926 | const uint8_t verify_mac[] = { |
| 1927 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 1928 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 1929 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 1930 | |
| 1931 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1932 | 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] | 1933 | psa_set_key_algorithm( &attributes, alg ); |
| 1934 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1935 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1936 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 1937 | &key ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1938 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1939 | /* Call update without calling setup beforehand. */ |
| 1940 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 1941 | PSA_ERROR_BAD_STATE ); |
| 1942 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1943 | |
| 1944 | /* Call sign finish without calling setup beforehand. */ |
| 1945 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 1946 | &sign_mac_length), |
| 1947 | PSA_ERROR_BAD_STATE ); |
| 1948 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1949 | |
| 1950 | /* Call verify finish without calling setup beforehand. */ |
| 1951 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 1952 | verify_mac, sizeof( verify_mac ) ), |
| 1953 | PSA_ERROR_BAD_STATE ); |
| 1954 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1955 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1956 | /* Call setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1957 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1958 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1959 | TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1960 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1961 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1962 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1963 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1964 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1965 | /* Call update after sign finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1966 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1967 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 1968 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 1969 | sign_mac, sizeof( sign_mac ), |
| 1970 | &sign_mac_length ) ); |
| 1971 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 1972 | PSA_ERROR_BAD_STATE ); |
| 1973 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1974 | |
| 1975 | /* Call update after verify finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1976 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1977 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 1978 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 1979 | verify_mac, sizeof( verify_mac ) ) ); |
| 1980 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 1981 | PSA_ERROR_BAD_STATE ); |
| 1982 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1983 | |
| 1984 | /* Call sign finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1985 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1986 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 1987 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 1988 | sign_mac, sizeof( sign_mac ), |
| 1989 | &sign_mac_length ) ); |
| 1990 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 1991 | sign_mac, sizeof( sign_mac ), |
| 1992 | &sign_mac_length ), |
| 1993 | PSA_ERROR_BAD_STATE ); |
| 1994 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1995 | |
| 1996 | /* Call verify finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1997 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 1998 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 1999 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2000 | verify_mac, sizeof( verify_mac ) ) ); |
| 2001 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2002 | verify_mac, sizeof( verify_mac ) ), |
| 2003 | PSA_ERROR_BAD_STATE ); |
| 2004 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2005 | |
| 2006 | /* Setup sign but try verify. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2007 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2008 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2009 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2010 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2011 | verify_mac, sizeof( verify_mac ) ), |
| 2012 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2013 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2014 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2015 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2016 | |
| 2017 | /* Setup verify but try sign. */ |
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 ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2020 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2021 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2022 | sign_mac, sizeof( sign_mac ), |
| 2023 | &sign_mac_length ), |
| 2024 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2025 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2026 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2027 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2028 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2029 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2030 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2031 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2032 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2033 | } |
| 2034 | /* END_CASE */ |
| 2035 | |
| 2036 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2037 | void mac_sign( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2038 | data_t *key_data, |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2039 | int alg_arg, |
| 2040 | data_t *input, |
| 2041 | data_t *expected_mac ) |
| 2042 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2043 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2044 | psa_key_type_t key_type = key_type_arg; |
| 2045 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2046 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2047 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2048 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2049 | size_t mac_buffer_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2050 | 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] | 2051 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2052 | const size_t output_sizes_to_test[] = { |
| 2053 | 0, |
| 2054 | 1, |
| 2055 | expected_mac->len - 1, |
| 2056 | expected_mac->len, |
| 2057 | expected_mac->len + 1, |
| 2058 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2059 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2060 | TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2061 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 3d404d6 | 2020-08-25 23:47:36 +0200 | [diff] [blame] | 2062 | TEST_ASSERT( expected_mac->len == mac_buffer_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2063 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2064 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2065 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2066 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2067 | psa_set_key_algorithm( &attributes, alg ); |
| 2068 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2069 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2070 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2071 | &key ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2072 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2073 | for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ ) |
| 2074 | { |
| 2075 | const size_t output_size = output_sizes_to_test[i]; |
| 2076 | psa_status_t expected_status = |
| 2077 | ( output_size >= expected_mac->len ? PSA_SUCCESS : |
| 2078 | PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2079 | |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2080 | mbedtls_test_set_step( output_size ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2081 | ASSERT_ALLOC( actual_mac, output_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2082 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2083 | /* Calculate the MAC, one-shot case. */ |
| 2084 | TEST_EQUAL( psa_mac_compute( key, alg, |
| 2085 | input->x, input->len, |
| 2086 | actual_mac, output_size, &mac_length ), |
| 2087 | expected_status ); |
| 2088 | if( expected_status == PSA_SUCCESS ) |
| 2089 | { |
| 2090 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 2091 | actual_mac, mac_length ); |
| 2092 | } |
| 2093 | |
| 2094 | if( output_size > 0 ) |
| 2095 | memset( actual_mac, 0, output_size ); |
| 2096 | |
| 2097 | /* Calculate the MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2098 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2099 | PSA_ASSERT( psa_mac_update( &operation, |
| 2100 | input->x, input->len ) ); |
| 2101 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2102 | actual_mac, output_size, |
| 2103 | &mac_length ), |
| 2104 | expected_status ); |
| 2105 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2106 | |
| 2107 | if( expected_status == PSA_SUCCESS ) |
| 2108 | { |
| 2109 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 2110 | actual_mac, mac_length ); |
| 2111 | } |
| 2112 | mbedtls_free( actual_mac ); |
| 2113 | actual_mac = NULL; |
| 2114 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2115 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2116 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2117 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2118 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2119 | PSA_DONE( ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2120 | mbedtls_free( actual_mac ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2121 | } |
| 2122 | /* END_CASE */ |
| 2123 | |
| 2124 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2125 | void mac_verify( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2126 | data_t *key_data, |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2127 | int alg_arg, |
| 2128 | data_t *input, |
| 2129 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2130 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2131 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2132 | psa_key_type_t key_type = key_type_arg; |
| 2133 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2134 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2135 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2136 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2137 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 2138 | TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE ); |
| 2139 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2140 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2141 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2142 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2143 | psa_set_key_algorithm( &attributes, alg ); |
| 2144 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2145 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2146 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2147 | &key ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2148 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2149 | /* Verify correct MAC, one-shot case. */ |
| 2150 | PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len, |
| 2151 | expected_mac->x, expected_mac->len ) ); |
| 2152 | |
| 2153 | /* Verify correct MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2154 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2155 | PSA_ASSERT( psa_mac_update( &operation, |
| 2156 | input->x, input->len ) ); |
| 2157 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2158 | expected_mac->x, |
| 2159 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2160 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2161 | /* Test a MAC that's too short, one-shot case. */ |
| 2162 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 2163 | input->x, input->len, |
| 2164 | expected_mac->x, |
| 2165 | expected_mac->len - 1 ), |
| 2166 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2167 | |
| 2168 | /* Test a MAC that's too short, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2169 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2170 | PSA_ASSERT( psa_mac_update( &operation, |
| 2171 | input->x, input->len ) ); |
| 2172 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2173 | expected_mac->x, |
| 2174 | expected_mac->len - 1 ), |
| 2175 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2176 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2177 | /* Test a MAC that's too long, one-shot case. */ |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2178 | ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 ); |
| 2179 | memcpy( perturbed_mac, expected_mac->x, expected_mac->len ); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2180 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 2181 | input->x, input->len, |
| 2182 | perturbed_mac, expected_mac->len + 1 ), |
| 2183 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2184 | |
| 2185 | /* Test a MAC that's too long, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2186 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2187 | PSA_ASSERT( psa_mac_update( &operation, |
| 2188 | input->x, input->len ) ); |
| 2189 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2190 | perturbed_mac, |
| 2191 | expected_mac->len + 1 ), |
| 2192 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2193 | |
| 2194 | /* Test changing one byte. */ |
| 2195 | for( size_t i = 0; i < expected_mac->len; i++ ) |
| 2196 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2197 | mbedtls_test_set_step( i ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2198 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2199 | |
| 2200 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 2201 | input->x, input->len, |
| 2202 | perturbed_mac, expected_mac->len ), |
| 2203 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2204 | |
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 | perturbed_mac, |
| 2210 | expected_mac->len ), |
| 2211 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2212 | perturbed_mac[i] ^= 1; |
| 2213 | } |
| 2214 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2215 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2216 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2217 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2218 | PSA_DONE( ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2219 | mbedtls_free( perturbed_mac ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2220 | } |
| 2221 | /* END_CASE */ |
| 2222 | |
| 2223 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2224 | void cipher_operation_init( ) |
| 2225 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2226 | const uint8_t input[1] = { 0 }; |
| 2227 | unsigned char output[1] = { 0 }; |
| 2228 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2229 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2230 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2231 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2232 | * to supress the Clang warning for the test. */ |
| 2233 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 2234 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 2235 | psa_cipher_operation_t zero; |
| 2236 | |
| 2237 | memset( &zero, 0, sizeof( zero ) ); |
| 2238 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2239 | /* A freshly-initialized cipher operation should not be usable. */ |
| 2240 | TEST_EQUAL( psa_cipher_update( &func, |
| 2241 | input, sizeof( input ), |
| 2242 | output, sizeof( output ), |
| 2243 | &output_length ), |
| 2244 | PSA_ERROR_BAD_STATE ); |
| 2245 | TEST_EQUAL( psa_cipher_update( &init, |
| 2246 | input, sizeof( input ), |
| 2247 | output, sizeof( output ), |
| 2248 | &output_length ), |
| 2249 | PSA_ERROR_BAD_STATE ); |
| 2250 | TEST_EQUAL( psa_cipher_update( &zero, |
| 2251 | input, sizeof( input ), |
| 2252 | output, sizeof( output ), |
| 2253 | &output_length ), |
| 2254 | PSA_ERROR_BAD_STATE ); |
| 2255 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2256 | /* A default cipher operation should be abortable without error. */ |
| 2257 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 2258 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 2259 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2260 | } |
| 2261 | /* END_CASE */ |
| 2262 | |
| 2263 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2264 | void cipher_setup( int key_type_arg, |
| 2265 | data_t *key, |
| 2266 | int alg_arg, |
| 2267 | int expected_status_arg ) |
| 2268 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2269 | psa_key_type_t key_type = key_type_arg; |
| 2270 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2271 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2272 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2273 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 2274 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2275 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2276 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2277 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2278 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2279 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2280 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 2281 | &operation, &status ) ) |
| 2282 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2283 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2284 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2285 | /* The operation object should be reusable. */ |
| 2286 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 2287 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 2288 | smoke_test_key_data, |
| 2289 | sizeof( smoke_test_key_data ), |
| 2290 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 2291 | &operation, &status ) ) |
| 2292 | goto exit; |
| 2293 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2294 | #endif |
| 2295 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2296 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2297 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2298 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2299 | } |
| 2300 | /* END_CASE */ |
| 2301 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2302 | /* 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] | 2303 | void cipher_bad_order( ) |
| 2304 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2305 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2306 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 2307 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2308 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2309 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2310 | 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] | 2311 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2312 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2313 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 2314 | const uint8_t text[] = { |
| 2315 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 2316 | 0xbb, 0xbb, 0xbb, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2317 | 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] | 2318 | size_t length = 0; |
| 2319 | |
| 2320 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2321 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 2322 | psa_set_key_algorithm( &attributes, alg ); |
| 2323 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2324 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 2325 | &key ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2326 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2327 | /* Call encrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2328 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2329 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2330 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2331 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2332 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2333 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2334 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2335 | |
| 2336 | /* Call decrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2337 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2338 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2339 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2340 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2341 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2342 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2343 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2344 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2345 | /* Generate an IV without calling setup beforehand. */ |
| 2346 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2347 | buffer, sizeof( buffer ), |
| 2348 | &length ), |
| 2349 | PSA_ERROR_BAD_STATE ); |
| 2350 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2351 | |
| 2352 | /* Generate an IV twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2353 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2354 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 2355 | buffer, sizeof( buffer ), |
| 2356 | &length ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2357 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2358 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2359 | buffer, sizeof( buffer ), |
| 2360 | &length ), |
| 2361 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2362 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2363 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2364 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2365 | |
| 2366 | /* Generate an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2367 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2368 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2369 | iv, sizeof( iv ) ) ); |
| 2370 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2371 | buffer, sizeof( buffer ), |
| 2372 | &length ), |
| 2373 | PSA_ERROR_BAD_STATE ); |
| 2374 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2375 | |
| 2376 | /* Set an IV without calling setup beforehand. */ |
| 2377 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2378 | iv, sizeof( iv ) ), |
| 2379 | PSA_ERROR_BAD_STATE ); |
| 2380 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2381 | |
| 2382 | /* Set an IV after it's already set. */ |
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_set_iv( &operation, |
| 2385 | iv, sizeof( iv ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2386 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2387 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2388 | iv, sizeof( iv ) ), |
| 2389 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2390 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2391 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2392 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2393 | |
| 2394 | /* Set an IV after it's already generated. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2395 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2396 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 2397 | buffer, sizeof( buffer ), |
| 2398 | &length ) ); |
| 2399 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2400 | iv, sizeof( iv ) ), |
| 2401 | PSA_ERROR_BAD_STATE ); |
| 2402 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2403 | |
| 2404 | /* Call update without calling setup beforehand. */ |
| 2405 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2406 | text, sizeof( text ), |
| 2407 | buffer, sizeof( buffer ), |
| 2408 | &length ), |
| 2409 | PSA_ERROR_BAD_STATE ); |
| 2410 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2411 | |
| 2412 | /* Call update without an IV where an IV is required. */ |
Dave Rodgman | 095dadc | 2021-06-23 12:48:52 +0100 | [diff] [blame] | 2413 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2414 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2415 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2416 | text, sizeof( text ), |
| 2417 | buffer, sizeof( buffer ), |
| 2418 | &length ), |
| 2419 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2420 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2421 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2422 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2423 | |
| 2424 | /* Call update after finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2425 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2426 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2427 | iv, sizeof( iv ) ) ); |
| 2428 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2429 | buffer, sizeof( buffer ), &length ) ); |
| 2430 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2431 | text, sizeof( text ), |
| 2432 | buffer, sizeof( buffer ), |
| 2433 | &length ), |
| 2434 | PSA_ERROR_BAD_STATE ); |
| 2435 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2436 | |
| 2437 | /* Call finish without calling setup beforehand. */ |
| 2438 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2439 | buffer, sizeof( buffer ), &length ), |
| 2440 | PSA_ERROR_BAD_STATE ); |
| 2441 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2442 | |
| 2443 | /* Call finish without an IV where an IV is required. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2444 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2445 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 2446 | * for cipher modes with padding. */ |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2447 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2448 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2449 | buffer, sizeof( buffer ), &length ), |
| 2450 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2451 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2452 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2453 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2454 | |
| 2455 | /* Call finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2456 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2457 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2458 | iv, sizeof( iv ) ) ); |
| 2459 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2460 | buffer, sizeof( buffer ), &length ) ); |
| 2461 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2462 | buffer, sizeof( buffer ), &length ), |
| 2463 | PSA_ERROR_BAD_STATE ); |
| 2464 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2465 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2466 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2467 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2468 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2469 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2470 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2471 | } |
| 2472 | /* END_CASE */ |
| 2473 | |
| 2474 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 2475 | void cipher_encrypt_fail( int alg_arg, |
| 2476 | int key_type_arg, |
| 2477 | data_t *key_data, |
| 2478 | data_t *input, |
| 2479 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2480 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2481 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2482 | psa_status_t status; |
| 2483 | psa_key_type_t key_type = key_type_arg; |
| 2484 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2485 | psa_status_t expected_status = expected_status_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2486 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2487 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2488 | size_t output_length = 0; |
| 2489 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2490 | |
| 2491 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 2492 | { |
| 2493 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2494 | |
| 2495 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2496 | psa_set_key_algorithm( &attributes, alg ); |
| 2497 | psa_set_key_type( &attributes, key_type ); |
| 2498 | |
| 2499 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 2500 | input->len ); |
| 2501 | ASSERT_ALLOC( output, output_buffer_size ); |
| 2502 | |
| 2503 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2504 | &key ) ); |
| 2505 | } |
| 2506 | |
| 2507 | status = psa_cipher_encrypt( key, alg, input->x, input->len, output, |
| 2508 | output_buffer_size, &output_length ); |
| 2509 | |
| 2510 | TEST_EQUAL( status, expected_status ); |
| 2511 | |
| 2512 | exit: |
| 2513 | mbedtls_free( output ); |
| 2514 | psa_destroy_key( key ); |
| 2515 | PSA_DONE( ); |
| 2516 | } |
| 2517 | /* END_CASE */ |
| 2518 | |
| 2519 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 2520 | void cipher_encrypt_alg_without_iv( int alg_arg, |
| 2521 | int key_type_arg, |
| 2522 | data_t *key_data, |
| 2523 | data_t *input, |
| 2524 | data_t *expected_output ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2525 | { |
| 2526 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2527 | psa_key_type_t key_type = key_type_arg; |
| 2528 | psa_algorithm_t alg = alg_arg; |
| 2529 | unsigned char *output = NULL; |
| 2530 | size_t output_buffer_size = 0; |
| 2531 | size_t output_length = 0; |
| 2532 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2533 | |
| 2534 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2535 | |
| 2536 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2537 | psa_set_key_algorithm( &attributes, alg ); |
| 2538 | psa_set_key_type( &attributes, key_type ); |
| 2539 | |
| 2540 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 2541 | ASSERT_ALLOC( output, output_buffer_size ); |
| 2542 | |
| 2543 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2544 | &key ) ); |
| 2545 | |
| 2546 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output, |
| 2547 | output_buffer_size, &output_length ) ); |
| 2548 | TEST_ASSERT( output_length <= |
| 2549 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 2550 | TEST_ASSERT( output_length <= |
| 2551 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
| 2552 | |
| 2553 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2554 | output, output_length ); |
| 2555 | exit: |
| 2556 | mbedtls_free( output ); |
| 2557 | psa_destroy_key( key ); |
| 2558 | PSA_DONE( ); |
| 2559 | } |
| 2560 | /* END_CASE */ |
| 2561 | |
| 2562 | /* BEGIN_CASE */ |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 2563 | void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data ) |
| 2564 | { |
| 2565 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2566 | psa_algorithm_t alg = alg_arg; |
| 2567 | psa_key_type_t key_type = key_type_arg; |
| 2568 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2569 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 2570 | psa_status_t status; |
| 2571 | |
| 2572 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2573 | |
| 2574 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2575 | psa_set_key_algorithm( &attributes, alg ); |
| 2576 | psa_set_key_type( &attributes, key_type ); |
| 2577 | |
| 2578 | /* Usage of either of these two size macros would cause divide by zero |
| 2579 | * with incorrect key types previously. Input length should be irrelevant |
| 2580 | * here. */ |
| 2581 | TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ), |
| 2582 | 0 ); |
| 2583 | TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 ); |
| 2584 | |
| 2585 | |
| 2586 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2587 | &key ) ); |
| 2588 | |
| 2589 | /* Should fail due to invalid alg type (to support invalid key type). |
| 2590 | * Encrypt or decrypt will end up in the same place. */ |
| 2591 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 2592 | |
| 2593 | TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT ); |
| 2594 | |
| 2595 | exit: |
| 2596 | psa_cipher_abort( &operation ); |
| 2597 | psa_destroy_key( key ); |
| 2598 | PSA_DONE( ); |
| 2599 | } |
| 2600 | /* END_CASE */ |
| 2601 | |
| 2602 | /* BEGIN_CASE */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2603 | void cipher_encrypt_validation( int alg_arg, |
| 2604 | int key_type_arg, |
| 2605 | data_t *key_data, |
| 2606 | data_t *input ) |
| 2607 | { |
| 2608 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2609 | psa_key_type_t key_type = key_type_arg; |
| 2610 | psa_algorithm_t alg = alg_arg; |
| 2611 | size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg ); |
| 2612 | unsigned char *output1 = NULL; |
| 2613 | size_t output1_buffer_size = 0; |
| 2614 | size_t output1_length = 0; |
| 2615 | unsigned char *output2 = NULL; |
| 2616 | size_t output2_buffer_size = 0; |
| 2617 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2618 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2619 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2620 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2621 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2622 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2623 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2624 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2625 | psa_set_key_algorithm( &attributes, alg ); |
| 2626 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2627 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2628 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 2629 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 2630 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 2631 | ASSERT_ALLOC( output1, output1_buffer_size ); |
| 2632 | ASSERT_ALLOC( output2, output2_buffer_size ); |
| 2633 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2634 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2635 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2636 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 2637 | /* The one-shot cipher encryption uses generated iv so validating |
| 2638 | the output is not possible. Validating with multipart encryption. */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2639 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1, |
| 2640 | output1_buffer_size, &output1_length ) ); |
| 2641 | TEST_ASSERT( output1_length <= |
| 2642 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 2643 | TEST_ASSERT( output1_length <= |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2644 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2645 | |
| 2646 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 2647 | PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2648 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2649 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2650 | input->x, input->len, |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2651 | output2, output2_buffer_size, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2652 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2653 | TEST_ASSERT( function_output_length <= |
| 2654 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 2655 | TEST_ASSERT( function_output_length <= |
| 2656 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2657 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2658 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2659 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2660 | output2 + output2_length, |
| 2661 | output2_buffer_size - output2_length, |
| 2662 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2663 | TEST_ASSERT( function_output_length <= |
| 2664 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 2665 | TEST_ASSERT( function_output_length <= |
| 2666 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2667 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2668 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2669 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2670 | ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size, |
| 2671 | output2, output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2672 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2673 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2674 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2675 | mbedtls_free( output1 ); |
| 2676 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2677 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2678 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2679 | } |
| 2680 | /* END_CASE */ |
| 2681 | |
| 2682 | /* BEGIN_CASE */ |
| 2683 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2684 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2685 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2686 | int first_part_size_arg, |
| 2687 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2688 | data_t *expected_output, |
| 2689 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2690 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2691 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2692 | psa_key_type_t key_type = key_type_arg; |
| 2693 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2694 | psa_status_t status; |
| 2695 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2696 | size_t first_part_size = first_part_size_arg; |
| 2697 | size_t output1_length = output1_length_arg; |
| 2698 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2699 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2700 | size_t output_buffer_size = 0; |
| 2701 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2702 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2703 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2704 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2705 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2706 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2707 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2708 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2709 | psa_set_key_algorithm( &attributes, alg ); |
| 2710 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2711 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2712 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2713 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2714 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2715 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2716 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2717 | if( iv->len > 0 ) |
| 2718 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 2719 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2720 | } |
| 2721 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2722 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 2723 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2724 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2725 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2726 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2727 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 2728 | output, output_buffer_size, |
| 2729 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2730 | TEST_ASSERT( function_output_length == output1_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2731 | TEST_ASSERT( function_output_length <= |
| 2732 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 2733 | TEST_ASSERT( function_output_length <= |
| 2734 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2735 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2736 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2737 | if( first_part_size < input->len ) |
| 2738 | { |
| 2739 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2740 | input->x + first_part_size, |
| 2741 | input->len - first_part_size, |
| 2742 | ( output_buffer_size == 0 ? NULL : |
| 2743 | output + total_output_length ), |
| 2744 | output_buffer_size - total_output_length, |
| 2745 | &function_output_length ) ); |
| 2746 | TEST_ASSERT( function_output_length == output2_length ); |
| 2747 | TEST_ASSERT( function_output_length <= |
| 2748 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 2749 | alg, |
| 2750 | input->len - first_part_size ) ); |
| 2751 | TEST_ASSERT( function_output_length <= |
| 2752 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
| 2753 | total_output_length += function_output_length; |
| 2754 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2755 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2756 | status = psa_cipher_finish( &operation, |
| 2757 | ( output_buffer_size == 0 ? NULL : |
| 2758 | output + total_output_length ), |
| 2759 | output_buffer_size - total_output_length, |
| 2760 | &function_output_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2761 | TEST_ASSERT( function_output_length <= |
| 2762 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 2763 | TEST_ASSERT( function_output_length <= |
| 2764 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2765 | total_output_length += function_output_length; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2766 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2767 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2768 | if( expected_status == PSA_SUCCESS ) |
| 2769 | { |
| 2770 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2771 | |
| 2772 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2773 | output, total_output_length ); |
| 2774 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2775 | |
| 2776 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2777 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2778 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2779 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2780 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2781 | } |
| 2782 | /* END_CASE */ |
| 2783 | |
| 2784 | /* BEGIN_CASE */ |
| 2785 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2786 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2787 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2788 | int first_part_size_arg, |
| 2789 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2790 | data_t *expected_output, |
| 2791 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2792 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2793 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2794 | psa_key_type_t key_type = key_type_arg; |
| 2795 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2796 | psa_status_t status; |
| 2797 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2798 | size_t first_part_size = first_part_size_arg; |
| 2799 | size_t output1_length = output1_length_arg; |
| 2800 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2801 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2802 | size_t output_buffer_size = 0; |
| 2803 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2804 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2805 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2806 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2807 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2808 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2809 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2810 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 2811 | psa_set_key_algorithm( &attributes, alg ); |
| 2812 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2813 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2814 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2815 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2816 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2817 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2818 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 2819 | if( iv->len > 0 ) |
| 2820 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 2821 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2822 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2823 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2824 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 2825 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2826 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2827 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2828 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2829 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2830 | input->x, first_part_size, |
| 2831 | output, output_buffer_size, |
| 2832 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2833 | TEST_ASSERT( function_output_length == output1_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2834 | TEST_ASSERT( function_output_length <= |
| 2835 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 2836 | TEST_ASSERT( function_output_length <= |
| 2837 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2838 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2839 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2840 | if( first_part_size < input->len ) |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 2841 | { |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2842 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2843 | input->x + first_part_size, |
| 2844 | input->len - first_part_size, |
| 2845 | ( output_buffer_size == 0 ? NULL : |
| 2846 | output + total_output_length ), |
| 2847 | output_buffer_size - total_output_length, |
| 2848 | &function_output_length ) ); |
| 2849 | TEST_ASSERT( function_output_length == output2_length ); |
| 2850 | TEST_ASSERT( function_output_length <= |
| 2851 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 2852 | alg, |
| 2853 | input->len - first_part_size ) ); |
| 2854 | TEST_ASSERT( function_output_length <= |
| 2855 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
| 2856 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2857 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2858 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2859 | status = psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 2860 | ( output_buffer_size == 0 ? NULL : |
| 2861 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2862 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2863 | &function_output_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2864 | TEST_ASSERT( function_output_length <= |
| 2865 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 2866 | TEST_ASSERT( function_output_length <= |
| 2867 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2868 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2869 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2870 | |
| 2871 | if( expected_status == PSA_SUCCESS ) |
| 2872 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2873 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 2874 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2875 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2876 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2877 | } |
| 2878 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2879 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2880 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2881 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2882 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2883 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2884 | } |
| 2885 | /* END_CASE */ |
| 2886 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2887 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 2888 | void cipher_decrypt_fail( int alg_arg, |
| 2889 | int key_type_arg, |
| 2890 | data_t *key_data, |
| 2891 | data_t *iv, |
| 2892 | data_t *input_arg, |
| 2893 | int expected_status_arg ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2894 | { |
| 2895 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2896 | psa_status_t status; |
| 2897 | psa_key_type_t key_type = key_type_arg; |
| 2898 | psa_algorithm_t alg = alg_arg; |
| 2899 | psa_status_t expected_status = expected_status_arg; |
| 2900 | unsigned char *input = NULL; |
| 2901 | size_t input_buffer_size = 0; |
| 2902 | unsigned char *output = NULL; |
| 2903 | size_t output_buffer_size = 0; |
| 2904 | size_t output_length = 0; |
| 2905 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2906 | |
| 2907 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 2908 | { |
| 2909 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2910 | |
| 2911 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 2912 | psa_set_key_algorithm( &attributes, alg ); |
| 2913 | psa_set_key_type( &attributes, key_type ); |
| 2914 | |
| 2915 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2916 | &key ) ); |
| 2917 | } |
| 2918 | |
| 2919 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 2920 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 2921 | if ( input_buffer_size > 0 ) |
| 2922 | { |
| 2923 | ASSERT_ALLOC( input, input_buffer_size ); |
| 2924 | memcpy( input, iv->x, iv->len ); |
| 2925 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 2926 | } |
| 2927 | |
| 2928 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 2929 | ASSERT_ALLOC( output, output_buffer_size ); |
| 2930 | |
| 2931 | status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 2932 | output_buffer_size, &output_length ); |
| 2933 | TEST_EQUAL( status, expected_status ); |
| 2934 | |
| 2935 | exit: |
| 2936 | mbedtls_free( input ); |
| 2937 | mbedtls_free( output ); |
| 2938 | psa_destroy_key( key ); |
| 2939 | PSA_DONE( ); |
| 2940 | } |
| 2941 | /* END_CASE */ |
| 2942 | |
| 2943 | /* BEGIN_CASE */ |
| 2944 | void cipher_decrypt( int alg_arg, |
| 2945 | int key_type_arg, |
| 2946 | data_t *key_data, |
| 2947 | data_t *iv, |
| 2948 | data_t *input_arg, |
| 2949 | data_t *expected_output ) |
| 2950 | { |
| 2951 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2952 | psa_key_type_t key_type = key_type_arg; |
| 2953 | psa_algorithm_t alg = alg_arg; |
| 2954 | unsigned char *input = NULL; |
| 2955 | size_t input_buffer_size = 0; |
| 2956 | unsigned char *output = NULL; |
| 2957 | size_t output_buffer_size = 0; |
| 2958 | size_t output_length = 0; |
| 2959 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2960 | |
| 2961 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2962 | |
| 2963 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 2964 | psa_set_key_algorithm( &attributes, alg ); |
| 2965 | psa_set_key_type( &attributes, key_type ); |
| 2966 | |
| 2967 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 2968 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 2969 | if ( input_buffer_size > 0 ) |
| 2970 | { |
| 2971 | ASSERT_ALLOC( input, input_buffer_size ); |
| 2972 | memcpy( input, iv->x, iv->len ); |
| 2973 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 2974 | } |
| 2975 | |
| 2976 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 2977 | ASSERT_ALLOC( output, output_buffer_size ); |
| 2978 | |
| 2979 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2980 | &key ) ); |
| 2981 | |
| 2982 | PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 2983 | output_buffer_size, &output_length ) ); |
| 2984 | TEST_ASSERT( output_length <= |
| 2985 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) ); |
| 2986 | TEST_ASSERT( output_length <= |
| 2987 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) ); |
| 2988 | |
| 2989 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2990 | output, output_length ); |
| 2991 | exit: |
| 2992 | mbedtls_free( input ); |
| 2993 | mbedtls_free( output ); |
| 2994 | psa_destroy_key( key ); |
| 2995 | PSA_DONE( ); |
| 2996 | } |
| 2997 | /* END_CASE */ |
| 2998 | |
| 2999 | /* BEGIN_CASE */ |
| 3000 | void cipher_verify_output( int alg_arg, |
| 3001 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3002 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3003 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3004 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3005 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3006 | psa_key_type_t key_type = key_type_arg; |
| 3007 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3008 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3009 | size_t output1_size = 0; |
| 3010 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3011 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3012 | size_t output2_size = 0; |
| 3013 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3014 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3015 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3016 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3017 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3018 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3019 | psa_set_key_algorithm( &attributes, alg ); |
| 3020 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3021 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3022 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3023 | &key ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3024 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3025 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3026 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3027 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, |
| 3028 | output1, output1_size, |
| 3029 | &output1_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3030 | TEST_ASSERT( output1_length <= |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3031 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3032 | TEST_ASSERT( output1_length <= |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3033 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3034 | |
| 3035 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3036 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3037 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3038 | PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length, |
| 3039 | output2, output2_size, |
| 3040 | &output2_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3041 | TEST_ASSERT( output2_length <= |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3042 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3043 | TEST_ASSERT( output2_length <= |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3044 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3045 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3046 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3047 | |
| 3048 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3049 | mbedtls_free( output1 ); |
| 3050 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3051 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3052 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3053 | } |
| 3054 | /* END_CASE */ |
| 3055 | |
| 3056 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3057 | void cipher_verify_output_multipart( int alg_arg, |
| 3058 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3059 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3060 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3061 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3062 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3063 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3064 | psa_key_type_t key_type = key_type_arg; |
| 3065 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3066 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3067 | unsigned char iv[16] = {0}; |
| 3068 | size_t iv_size = 16; |
| 3069 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3070 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3071 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3072 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3073 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3074 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3075 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3076 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3077 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3078 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3079 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3080 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3081 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3082 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3083 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3084 | psa_set_key_algorithm( &attributes, alg ); |
| 3085 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3086 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3087 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3088 | &key ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3089 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3090 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 3091 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3092 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3093 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3094 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3095 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3096 | iv, iv_size, |
| 3097 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3098 | } |
| 3099 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3100 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3101 | TEST_ASSERT( output1_buffer_size <= |
| 3102 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3103 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3104 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3105 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3106 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3107 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 3108 | output1, output1_buffer_size, |
| 3109 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3110 | TEST_ASSERT( function_output_length <= |
| 3111 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3112 | TEST_ASSERT( function_output_length <= |
| 3113 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3114 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3115 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3116 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 3117 | input->x + first_part_size, |
| 3118 | input->len - first_part_size, |
| 3119 | output1, output1_buffer_size, |
| 3120 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3121 | TEST_ASSERT( function_output_length <= |
| 3122 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3123 | alg, |
| 3124 | input->len - first_part_size ) ); |
| 3125 | TEST_ASSERT( function_output_length <= |
| 3126 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3127 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3128 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3129 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 3130 | output1 + output1_length, |
| 3131 | output1_buffer_size - output1_length, |
| 3132 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3133 | TEST_ASSERT( function_output_length <= |
| 3134 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3135 | TEST_ASSERT( function_output_length <= |
| 3136 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3137 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3138 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3139 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3140 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3141 | output2_buffer_size = output1_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3142 | TEST_ASSERT( output2_buffer_size <= |
| 3143 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 3144 | TEST_ASSERT( output2_buffer_size <= |
| 3145 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3146 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3147 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3148 | if( iv_length > 0 ) |
| 3149 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3150 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3151 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3152 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3153 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3154 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 3155 | output2, output2_buffer_size, |
| 3156 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3157 | TEST_ASSERT( function_output_length <= |
| 3158 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3159 | TEST_ASSERT( function_output_length <= |
| 3160 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3161 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3162 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3163 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 3164 | output1 + first_part_size, |
| 3165 | output1_length - first_part_size, |
| 3166 | output2, output2_buffer_size, |
| 3167 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3168 | TEST_ASSERT( function_output_length <= |
| 3169 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3170 | alg, |
| 3171 | output1_length - first_part_size ) ); |
| 3172 | TEST_ASSERT( function_output_length <= |
| 3173 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3174 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3175 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3176 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3177 | output2 + output2_length, |
| 3178 | output2_buffer_size - output2_length, |
| 3179 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3180 | TEST_ASSERT( function_output_length <= |
| 3181 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3182 | TEST_ASSERT( function_output_length <= |
| 3183 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3184 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3185 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3186 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3187 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3188 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3189 | |
| 3190 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3191 | psa_cipher_abort( &operation1 ); |
| 3192 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3193 | mbedtls_free( output1 ); |
| 3194 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3195 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3196 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3197 | } |
| 3198 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 3199 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3200 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3201 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3202 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3203 | data_t *nonce, |
| 3204 | data_t *additional_data, |
| 3205 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3206 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3207 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3208 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3209 | psa_key_type_t key_type = key_type_arg; |
| 3210 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3211 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3212 | unsigned char *output_data = NULL; |
| 3213 | size_t output_size = 0; |
| 3214 | size_t output_length = 0; |
| 3215 | unsigned char *output_data2 = NULL; |
| 3216 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 3217 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3218 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3219 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3220 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3221 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3222 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3223 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3224 | psa_set_key_algorithm( &attributes, alg ); |
| 3225 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3226 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3227 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3228 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3229 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3230 | key_bits = psa_get_key_bits( &attributes ); |
| 3231 | |
| 3232 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3233 | alg ); |
| 3234 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3235 | * should be exact. */ |
| 3236 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 3237 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 3238 | { |
| 3239 | TEST_EQUAL( output_size, |
| 3240 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3241 | TEST_ASSERT( output_size <= |
| 3242 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3243 | } |
| 3244 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3245 | |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 3246 | status = psa_aead_encrypt( key, alg, |
| 3247 | nonce->x, nonce->len, |
| 3248 | additional_data->x, |
| 3249 | additional_data->len, |
| 3250 | input_data->x, input_data->len, |
| 3251 | output_data, output_size, |
| 3252 | &output_length ); |
| 3253 | |
| 3254 | /* If the operation is not supported, just skip and not fail in case the |
| 3255 | * encryption involves a common limitation of cryptography hardwares and |
| 3256 | * an alternative implementation. */ |
| 3257 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 3258 | { |
| 3259 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3260 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 3261 | } |
| 3262 | |
| 3263 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3264 | |
| 3265 | if( PSA_SUCCESS == expected_result ) |
| 3266 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3267 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3268 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3269 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3270 | * should be exact. */ |
| 3271 | TEST_EQUAL( input_data->len, |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3272 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) ); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3273 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3274 | TEST_ASSERT( input_data->len <= |
| 3275 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) ); |
| 3276 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3277 | TEST_EQUAL( psa_aead_decrypt( key, alg, |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3278 | nonce->x, nonce->len, |
| 3279 | additional_data->x, |
| 3280 | additional_data->len, |
| 3281 | output_data, output_length, |
| 3282 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3283 | &output_length2 ), |
| 3284 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3285 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3286 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 3287 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3288 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3289 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3290 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3291 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3292 | mbedtls_free( output_data ); |
| 3293 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3294 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3295 | } |
| 3296 | /* END_CASE */ |
| 3297 | |
| 3298 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3299 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 3300 | int alg_arg, |
| 3301 | data_t *nonce, |
| 3302 | data_t *additional_data, |
| 3303 | data_t *input_data, |
| 3304 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3305 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3306 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3307 | psa_key_type_t key_type = key_type_arg; |
| 3308 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3309 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3310 | unsigned char *output_data = NULL; |
| 3311 | size_t output_size = 0; |
| 3312 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3313 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3314 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3315 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3316 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3317 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3318 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3319 | psa_set_key_algorithm( &attributes, alg ); |
| 3320 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3321 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3322 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3323 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3324 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3325 | key_bits = psa_get_key_bits( &attributes ); |
| 3326 | |
| 3327 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3328 | alg ); |
| 3329 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3330 | * should be exact. */ |
| 3331 | TEST_EQUAL( output_size, |
| 3332 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3333 | TEST_ASSERT( output_size <= |
| 3334 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3335 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3336 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3337 | status = psa_aead_encrypt( key, alg, |
| 3338 | nonce->x, nonce->len, |
| 3339 | additional_data->x, additional_data->len, |
| 3340 | input_data->x, input_data->len, |
| 3341 | output_data, output_size, |
| 3342 | &output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3343 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3344 | /* If the operation is not supported, just skip and not fail in case the |
| 3345 | * encryption involves a common limitation of cryptography hardwares and |
| 3346 | * an alternative implementation. */ |
| 3347 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3348 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3349 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3350 | 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] | 3351 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3352 | |
| 3353 | PSA_ASSERT( status ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3354 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 3355 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3356 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3357 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3358 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3359 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3360 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3361 | } |
| 3362 | /* END_CASE */ |
| 3363 | |
| 3364 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3365 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 3366 | int alg_arg, |
| 3367 | data_t *nonce, |
| 3368 | data_t *additional_data, |
| 3369 | data_t *input_data, |
| 3370 | data_t *expected_data, |
| 3371 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3372 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3373 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3374 | psa_key_type_t key_type = key_type_arg; |
| 3375 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3376 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3377 | unsigned char *output_data = NULL; |
| 3378 | size_t output_size = 0; |
| 3379 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3380 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3381 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3382 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3383 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3384 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3385 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3386 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3387 | psa_set_key_algorithm( &attributes, alg ); |
| 3388 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3389 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3390 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3391 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3392 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3393 | key_bits = psa_get_key_bits( &attributes ); |
| 3394 | |
| 3395 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3396 | alg ); |
| 3397 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 3398 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 3399 | { |
| 3400 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3401 | * should be exact. */ |
| 3402 | TEST_EQUAL( output_size, |
| 3403 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3404 | TEST_ASSERT( output_size <= |
| 3405 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3406 | } |
| 3407 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3408 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3409 | status = psa_aead_decrypt( key, alg, |
| 3410 | nonce->x, nonce->len, |
| 3411 | additional_data->x, |
| 3412 | additional_data->len, |
| 3413 | input_data->x, input_data->len, |
| 3414 | output_data, output_size, |
| 3415 | &output_length ); |
| 3416 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3417 | /* If the operation is not supported, just skip and not fail in case the |
| 3418 | * decryption involves a common limitation of cryptography hardwares and |
| 3419 | * an alternative implementation. */ |
| 3420 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3421 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3422 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3423 | 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] | 3424 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3425 | |
| 3426 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3427 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3428 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3429 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 3430 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3431 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3432 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3433 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3434 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3435 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3436 | } |
| 3437 | /* END_CASE */ |
| 3438 | |
| 3439 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3440 | void signature_size( int type_arg, |
| 3441 | int bits, |
| 3442 | int alg_arg, |
| 3443 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3444 | { |
| 3445 | psa_key_type_t type = type_arg; |
| 3446 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3447 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 3448 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3449 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 3450 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3451 | exit: |
| 3452 | ; |
| 3453 | } |
| 3454 | /* END_CASE */ |
| 3455 | |
| 3456 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 3457 | void sign_hash_deterministic( int key_type_arg, data_t *key_data, |
| 3458 | int alg_arg, data_t *input_data, |
| 3459 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3460 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3461 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3462 | psa_key_type_t key_type = key_type_arg; |
| 3463 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3464 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3465 | unsigned char *signature = NULL; |
| 3466 | size_t signature_size; |
| 3467 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3468 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3469 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3470 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3471 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3472 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3473 | psa_set_key_algorithm( &attributes, alg ); |
| 3474 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 3475 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3476 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3477 | &key ) ); |
| 3478 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3479 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3480 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3481 | /* Allocate a buffer which has the size advertized by the |
| 3482 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3483 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3484 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3485 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3486 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3487 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3488 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3489 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3490 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3491 | input_data->x, input_data->len, |
| 3492 | signature, signature_size, |
| 3493 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3494 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3495 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 3496 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3497 | |
| 3498 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3499 | /* |
| 3500 | * Key attributes may have been returned by psa_get_key_attributes() |
| 3501 | * thus reset them as required. |
| 3502 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3503 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3504 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3505 | psa_destroy_key( key ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 3506 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3507 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3508 | } |
| 3509 | /* END_CASE */ |
| 3510 | |
| 3511 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 3512 | void sign_hash_fail( int key_type_arg, data_t *key_data, |
| 3513 | int alg_arg, data_t *input_data, |
| 3514 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3515 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3516 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3517 | psa_key_type_t key_type = key_type_arg; |
| 3518 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3519 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3520 | psa_status_t actual_status; |
| 3521 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 3522 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 3523 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3524 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3525 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3526 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3527 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3528 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3529 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3530 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3531 | psa_set_key_algorithm( &attributes, alg ); |
| 3532 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 3533 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3534 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3535 | &key ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3536 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3537 | actual_status = psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3538 | input_data->x, input_data->len, |
| 3539 | signature, signature_size, |
| 3540 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3541 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 3542 | /* The value of *signature_length is unspecified on error, but |
| 3543 | * whatever it is, it should be less than signature_size, so that |
| 3544 | * if the caller tries to read *signature_length bytes without |
| 3545 | * checking the error code then they don't overflow a buffer. */ |
| 3546 | TEST_ASSERT( signature_length <= signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3547 | |
| 3548 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3549 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3550 | psa_destroy_key( key ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3551 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3552 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3553 | } |
| 3554 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 3555 | |
| 3556 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 3557 | void sign_verify_hash( int key_type_arg, data_t *key_data, |
| 3558 | int alg_arg, data_t *input_data ) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3559 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3560 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3561 | psa_key_type_t key_type = key_type_arg; |
| 3562 | psa_algorithm_t alg = alg_arg; |
| 3563 | size_t key_bits; |
| 3564 | unsigned char *signature = NULL; |
| 3565 | size_t signature_size; |
| 3566 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3567 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3568 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3569 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3570 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3571 | 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] | 3572 | psa_set_key_algorithm( &attributes, alg ); |
| 3573 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3574 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3575 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3576 | &key ) ); |
| 3577 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3578 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3579 | |
| 3580 | /* Allocate a buffer which has the size advertized by the |
| 3581 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3582 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3583 | key_bits, alg ); |
| 3584 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3585 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3586 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3587 | |
| 3588 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3589 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3590 | input_data->x, input_data->len, |
| 3591 | signature, signature_size, |
| 3592 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3593 | /* Check that the signature length looks sensible. */ |
| 3594 | TEST_ASSERT( signature_length <= signature_size ); |
| 3595 | TEST_ASSERT( signature_length > 0 ); |
| 3596 | |
| 3597 | /* Use the library to verify that the signature is correct. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3598 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3599 | input_data->x, input_data->len, |
| 3600 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3601 | |
| 3602 | if( input_data->len != 0 ) |
| 3603 | { |
| 3604 | /* Flip a bit in the input and verify that the signature is now |
| 3605 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 3606 | * because ECDSA may ignore the last few bits of the input. */ |
| 3607 | input_data->x[0] ^= 1; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3608 | TEST_EQUAL( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3609 | input_data->x, input_data->len, |
| 3610 | signature, signature_length ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3611 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3612 | } |
| 3613 | |
| 3614 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3615 | /* |
| 3616 | * Key attributes may have been returned by psa_get_key_attributes() |
| 3617 | * thus reset them as required. |
| 3618 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3619 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 3620 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3621 | psa_destroy_key( key ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3622 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3623 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3624 | } |
| 3625 | /* END_CASE */ |
| 3626 | |
| 3627 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 3628 | void verify_hash( int key_type_arg, data_t *key_data, |
| 3629 | int alg_arg, data_t *hash_data, |
| 3630 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3631 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3632 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3633 | psa_key_type_t key_type = key_type_arg; |
| 3634 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3635 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3636 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3637 | TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3638 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3639 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3640 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3641 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3642 | psa_set_key_algorithm( &attributes, alg ); |
| 3643 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3644 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3645 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3646 | &key ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3647 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3648 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3649 | hash_data->x, hash_data->len, |
| 3650 | signature_data->x, signature_data->len ) ); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 3651 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3652 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3653 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3654 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3655 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3656 | } |
| 3657 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3658 | |
| 3659 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 3660 | void verify_hash_fail( int key_type_arg, data_t *key_data, |
| 3661 | int alg_arg, data_t *hash_data, |
| 3662 | data_t *signature_data, |
| 3663 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3664 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3665 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3666 | psa_key_type_t key_type = key_type_arg; |
| 3667 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3668 | psa_status_t actual_status; |
| 3669 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3670 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3671 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3672 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3673 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3674 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3675 | psa_set_key_algorithm( &attributes, alg ); |
| 3676 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3677 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3678 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3679 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3680 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3681 | actual_status = psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3682 | hash_data->x, hash_data->len, |
| 3683 | signature_data->x, signature_data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3684 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3685 | |
| 3686 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 3687 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3688 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3689 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3690 | } |
| 3691 | /* END_CASE */ |
| 3692 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3693 | /* BEGIN_CASE */ |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 3694 | void sign_message_deterministic( int key_type_arg, |
| 3695 | data_t *key_data, |
| 3696 | int alg_arg, |
| 3697 | data_t *input_data, |
| 3698 | data_t *output_data ) |
| 3699 | { |
| 3700 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3701 | psa_key_type_t key_type = key_type_arg; |
| 3702 | psa_algorithm_t alg = alg_arg; |
| 3703 | size_t key_bits; |
| 3704 | unsigned char *signature = NULL; |
| 3705 | size_t signature_size; |
| 3706 | size_t signature_length = 0xdeadbeef; |
| 3707 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3708 | |
| 3709 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3710 | |
| 3711 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 3712 | psa_set_key_algorithm( &attributes, alg ); |
| 3713 | psa_set_key_type( &attributes, key_type ); |
| 3714 | |
| 3715 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3716 | &key ) ); |
| 3717 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3718 | key_bits = psa_get_key_bits( &attributes ); |
| 3719 | |
| 3720 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 3721 | TEST_ASSERT( signature_size != 0 ); |
| 3722 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
| 3723 | ASSERT_ALLOC( signature, signature_size ); |
| 3724 | |
| 3725 | PSA_ASSERT( psa_sign_message( key, alg, |
| 3726 | input_data->x, input_data->len, |
| 3727 | signature, signature_size, |
| 3728 | &signature_length ) ); |
| 3729 | |
| 3730 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 3731 | signature, signature_length ); |
| 3732 | |
| 3733 | exit: |
| 3734 | psa_reset_key_attributes( &attributes ); |
| 3735 | |
| 3736 | psa_destroy_key( key ); |
| 3737 | mbedtls_free( signature ); |
| 3738 | PSA_DONE( ); |
| 3739 | |
| 3740 | } |
| 3741 | /* END_CASE */ |
| 3742 | |
| 3743 | /* BEGIN_CASE */ |
| 3744 | void sign_message_fail( int key_type_arg, |
| 3745 | data_t *key_data, |
| 3746 | int alg_arg, |
| 3747 | data_t *input_data, |
| 3748 | int signature_size_arg, |
| 3749 | int expected_status_arg ) |
| 3750 | { |
| 3751 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3752 | psa_key_type_t key_type = key_type_arg; |
| 3753 | psa_algorithm_t alg = alg_arg; |
| 3754 | size_t signature_size = signature_size_arg; |
| 3755 | psa_status_t actual_status; |
| 3756 | psa_status_t expected_status = expected_status_arg; |
| 3757 | unsigned char *signature = NULL; |
| 3758 | size_t signature_length = 0xdeadbeef; |
| 3759 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3760 | |
| 3761 | ASSERT_ALLOC( signature, signature_size ); |
| 3762 | |
| 3763 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3764 | |
| 3765 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 3766 | psa_set_key_algorithm( &attributes, alg ); |
| 3767 | psa_set_key_type( &attributes, key_type ); |
| 3768 | |
| 3769 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3770 | &key ) ); |
| 3771 | |
| 3772 | actual_status = psa_sign_message( key, alg, |
| 3773 | input_data->x, input_data->len, |
| 3774 | signature, signature_size, |
| 3775 | &signature_length ); |
| 3776 | TEST_EQUAL( actual_status, expected_status ); |
| 3777 | /* The value of *signature_length is unspecified on error, but |
| 3778 | * whatever it is, it should be less than signature_size, so that |
| 3779 | * if the caller tries to read *signature_length bytes without |
| 3780 | * checking the error code then they don't overflow a buffer. */ |
| 3781 | TEST_ASSERT( signature_length <= signature_size ); |
| 3782 | |
| 3783 | exit: |
| 3784 | psa_reset_key_attributes( &attributes ); |
| 3785 | psa_destroy_key( key ); |
| 3786 | mbedtls_free( signature ); |
| 3787 | PSA_DONE( ); |
| 3788 | } |
| 3789 | /* END_CASE */ |
| 3790 | |
| 3791 | /* BEGIN_CASE */ |
| 3792 | void sign_verify_message( int key_type_arg, |
| 3793 | data_t *key_data, |
| 3794 | int alg_arg, |
| 3795 | data_t *input_data ) |
| 3796 | { |
| 3797 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3798 | psa_key_type_t key_type = key_type_arg; |
| 3799 | psa_algorithm_t alg = alg_arg; |
| 3800 | size_t key_bits; |
| 3801 | unsigned char *signature = NULL; |
| 3802 | size_t signature_size; |
| 3803 | size_t signature_length = 0xdeadbeef; |
| 3804 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3805 | |
| 3806 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3807 | |
| 3808 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 3809 | PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 3810 | psa_set_key_algorithm( &attributes, alg ); |
| 3811 | psa_set_key_type( &attributes, key_type ); |
| 3812 | |
| 3813 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3814 | &key ) ); |
| 3815 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3816 | key_bits = psa_get_key_bits( &attributes ); |
| 3817 | |
| 3818 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 3819 | TEST_ASSERT( signature_size != 0 ); |
| 3820 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
| 3821 | ASSERT_ALLOC( signature, signature_size ); |
| 3822 | |
| 3823 | PSA_ASSERT( psa_sign_message( key, alg, |
| 3824 | input_data->x, input_data->len, |
| 3825 | signature, signature_size, |
| 3826 | &signature_length ) ); |
| 3827 | TEST_ASSERT( signature_length <= signature_size ); |
| 3828 | TEST_ASSERT( signature_length > 0 ); |
| 3829 | |
| 3830 | PSA_ASSERT( psa_verify_message( key, alg, |
| 3831 | input_data->x, input_data->len, |
| 3832 | signature, signature_length ) ); |
| 3833 | |
| 3834 | if( input_data->len != 0 ) |
| 3835 | { |
| 3836 | /* Flip a bit in the input and verify that the signature is now |
| 3837 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 3838 | * because ECDSA may ignore the last few bits of the input. */ |
| 3839 | input_data->x[0] ^= 1; |
| 3840 | TEST_EQUAL( psa_verify_message( key, alg, |
| 3841 | input_data->x, input_data->len, |
| 3842 | signature, signature_length ), |
| 3843 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3844 | } |
| 3845 | |
| 3846 | exit: |
| 3847 | psa_reset_key_attributes( &attributes ); |
| 3848 | |
| 3849 | psa_destroy_key( key ); |
| 3850 | mbedtls_free( signature ); |
| 3851 | PSA_DONE( ); |
| 3852 | } |
| 3853 | /* END_CASE */ |
| 3854 | |
| 3855 | /* BEGIN_CASE */ |
| 3856 | void verify_message( int key_type_arg, |
| 3857 | data_t *key_data, |
| 3858 | int alg_arg, |
| 3859 | data_t *input_data, |
| 3860 | data_t *signature_data ) |
| 3861 | { |
| 3862 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3863 | psa_key_type_t key_type = key_type_arg; |
| 3864 | psa_algorithm_t alg = alg_arg; |
| 3865 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3866 | |
| 3867 | TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE ); |
| 3868 | |
| 3869 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3870 | |
| 3871 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 3872 | psa_set_key_algorithm( &attributes, alg ); |
| 3873 | psa_set_key_type( &attributes, key_type ); |
| 3874 | |
| 3875 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3876 | &key ) ); |
| 3877 | |
| 3878 | PSA_ASSERT( psa_verify_message( key, alg, |
| 3879 | input_data->x, input_data->len, |
| 3880 | signature_data->x, signature_data->len ) ); |
| 3881 | |
| 3882 | exit: |
| 3883 | psa_reset_key_attributes( &attributes ); |
| 3884 | psa_destroy_key( key ); |
| 3885 | PSA_DONE( ); |
| 3886 | } |
| 3887 | /* END_CASE */ |
| 3888 | |
| 3889 | /* BEGIN_CASE */ |
| 3890 | void verify_message_fail( int key_type_arg, |
| 3891 | data_t *key_data, |
| 3892 | int alg_arg, |
| 3893 | data_t *hash_data, |
| 3894 | data_t *signature_data, |
| 3895 | int expected_status_arg ) |
| 3896 | { |
| 3897 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3898 | psa_key_type_t key_type = key_type_arg; |
| 3899 | psa_algorithm_t alg = alg_arg; |
| 3900 | psa_status_t actual_status; |
| 3901 | psa_status_t expected_status = expected_status_arg; |
| 3902 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3903 | |
| 3904 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3905 | |
| 3906 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 3907 | psa_set_key_algorithm( &attributes, alg ); |
| 3908 | psa_set_key_type( &attributes, key_type ); |
| 3909 | |
| 3910 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3911 | &key ) ); |
| 3912 | |
| 3913 | actual_status = psa_verify_message( key, alg, |
| 3914 | hash_data->x, hash_data->len, |
| 3915 | signature_data->x, |
| 3916 | signature_data->len ); |
| 3917 | TEST_EQUAL( actual_status, expected_status ); |
| 3918 | |
| 3919 | exit: |
| 3920 | psa_reset_key_attributes( &attributes ); |
| 3921 | psa_destroy_key( key ); |
| 3922 | PSA_DONE( ); |
| 3923 | } |
| 3924 | /* END_CASE */ |
| 3925 | |
| 3926 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3927 | void asymmetric_encrypt( int key_type_arg, |
| 3928 | data_t *key_data, |
| 3929 | int alg_arg, |
| 3930 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3931 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3932 | int expected_output_length_arg, |
| 3933 | int expected_status_arg ) |
| 3934 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3935 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3936 | psa_key_type_t key_type = key_type_arg; |
| 3937 | psa_algorithm_t alg = alg_arg; |
| 3938 | size_t expected_output_length = expected_output_length_arg; |
| 3939 | size_t key_bits; |
| 3940 | unsigned char *output = NULL; |
| 3941 | size_t output_size; |
| 3942 | size_t output_length = ~0; |
| 3943 | psa_status_t actual_status; |
| 3944 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3945 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3946 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3947 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3948 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3949 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3950 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3951 | psa_set_key_algorithm( &attributes, alg ); |
| 3952 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3953 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3954 | &key ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3955 | |
| 3956 | /* Determine the maximum output length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3957 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 3958 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3959 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3960 | 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] | 3961 | TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3962 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3963 | |
| 3964 | /* Encrypt the input */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3965 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3966 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3967 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3968 | output, output_size, |
| 3969 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3970 | TEST_EQUAL( actual_status, expected_status ); |
| 3971 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3972 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3973 | /* If the label is empty, the test framework puts a non-null pointer |
| 3974 | * in label->x. Test that a null pointer works as well. */ |
| 3975 | if( label->len == 0 ) |
| 3976 | { |
| 3977 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 3978 | if( output_size != 0 ) |
| 3979 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3980 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3981 | input_data->x, input_data->len, |
| 3982 | NULL, label->len, |
| 3983 | output, output_size, |
| 3984 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3985 | TEST_EQUAL( actual_status, expected_status ); |
| 3986 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3987 | } |
| 3988 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 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 ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3997 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3998 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3999 | } |
| 4000 | /* END_CASE */ |
| 4001 | |
| 4002 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4003 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 4004 | data_t *key_data, |
| 4005 | int alg_arg, |
| 4006 | data_t *input_data, |
| 4007 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4008 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4009 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4010 | psa_key_type_t key_type = key_type_arg; |
| 4011 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4012 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4013 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4014 | size_t output_size; |
| 4015 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4016 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4017 | size_t output2_size; |
| 4018 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4019 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4020 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4021 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4022 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4023 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4024 | psa_set_key_algorithm( &attributes, alg ); |
| 4025 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4026 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4027 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4028 | &key ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4029 | |
| 4030 | /* Determine the maximum ciphertext length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4031 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4032 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4033 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4034 | 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] | 4035 | TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4036 | ASSERT_ALLOC( output, output_size ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4037 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4038 | output2_size = input_data->len; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4039 | TEST_ASSERT( output2_size <= |
| 4040 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) ); |
| 4041 | TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4042 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4043 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 4044 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 4045 | * the original plaintext because of the non-optional random |
| 4046 | * part of encryption process which prevents using fixed vectors. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4047 | PSA_ASSERT( psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4048 | input_data->x, input_data->len, |
| 4049 | label->x, label->len, |
| 4050 | output, output_size, |
| 4051 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4052 | /* We don't know what ciphertext length to expect, but check that |
| 4053 | * it looks sensible. */ |
| 4054 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4055 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4056 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4057 | output, output_length, |
| 4058 | label->x, label->len, |
| 4059 | output2, output2_size, |
| 4060 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4061 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 4062 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4063 | |
| 4064 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4065 | /* |
| 4066 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4067 | * thus reset them as required. |
| 4068 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4069 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4070 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4071 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4072 | mbedtls_free( output ); |
| 4073 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4074 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4075 | } |
| 4076 | /* END_CASE */ |
| 4077 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4078 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4079 | void asymmetric_decrypt( int key_type_arg, |
| 4080 | data_t *key_data, |
| 4081 | int alg_arg, |
| 4082 | data_t *input_data, |
| 4083 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 4084 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4085 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4086 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4087 | psa_key_type_t key_type = key_type_arg; |
| 4088 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4089 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4090 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 4091 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4092 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4093 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4094 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4095 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4096 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4097 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4098 | psa_set_key_algorithm( &attributes, alg ); |
| 4099 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4100 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4101 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4102 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4103 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4104 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4105 | key_bits = psa_get_key_bits( &attributes ); |
| 4106 | |
| 4107 | /* Determine the maximum ciphertext length */ |
| 4108 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 4109 | TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
| 4110 | ASSERT_ALLOC( output, output_size ); |
| 4111 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4112 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4113 | input_data->x, input_data->len, |
| 4114 | label->x, label->len, |
| 4115 | output, |
| 4116 | output_size, |
| 4117 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4118 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4119 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4120 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4121 | /* If the label is empty, the test framework puts a non-null pointer |
| 4122 | * in label->x. Test that a null pointer works as well. */ |
| 4123 | if( label->len == 0 ) |
| 4124 | { |
| 4125 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4126 | if( output_size != 0 ) |
| 4127 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4128 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4129 | input_data->x, input_data->len, |
| 4130 | NULL, label->len, |
| 4131 | output, |
| 4132 | output_size, |
| 4133 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4134 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4135 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4136 | } |
| 4137 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4138 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4139 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4140 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4141 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4142 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4143 | } |
| 4144 | /* END_CASE */ |
| 4145 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4146 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4147 | void asymmetric_decrypt_fail( int key_type_arg, |
| 4148 | data_t *key_data, |
| 4149 | int alg_arg, |
| 4150 | data_t *input_data, |
| 4151 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4152 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4153 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4154 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4155 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4156 | psa_key_type_t key_type = key_type_arg; |
| 4157 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4158 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4159 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4160 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4161 | psa_status_t actual_status; |
| 4162 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4163 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4164 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4165 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4166 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4167 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4168 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4169 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4170 | psa_set_key_algorithm( &attributes, alg ); |
| 4171 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4172 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4173 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4174 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4175 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4176 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4177 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4178 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4179 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4180 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4181 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4182 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4183 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4184 | /* If the label is empty, the test framework puts a non-null pointer |
| 4185 | * in label->x. Test that a null pointer works as well. */ |
| 4186 | if( label->len == 0 ) |
| 4187 | { |
| 4188 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4189 | if( output_size != 0 ) |
| 4190 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4191 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4192 | input_data->x, input_data->len, |
| 4193 | NULL, label->len, |
| 4194 | output, output_size, |
| 4195 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4196 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4197 | TEST_ASSERT( output_length <= output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4198 | } |
| 4199 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4200 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4201 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4202 | psa_destroy_key( key ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4203 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4204 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4205 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4206 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4207 | |
| 4208 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4209 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4210 | { |
| 4211 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 4212 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 4213 | * though it's OK by the C standard. We could test for this, but we'd need |
| 4214 | * to supress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4215 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4216 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 4217 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4218 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4219 | |
| 4220 | memset( &zero, 0, sizeof( zero ) ); |
| 4221 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4222 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4223 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4224 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4225 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4226 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4227 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4228 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4229 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4230 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4231 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 4232 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 4233 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4234 | } |
| 4235 | /* END_CASE */ |
| 4236 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4237 | /* BEGIN_CASE */ |
| 4238 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4239 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4240 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4241 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4242 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4243 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4244 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4245 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4246 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4247 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4248 | |
| 4249 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4250 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4251 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4252 | } |
| 4253 | /* END_CASE */ |
| 4254 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4255 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4256 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 4257 | int expected_status_arg ) |
| 4258 | { |
| 4259 | psa_algorithm_t alg = alg_arg; |
| 4260 | size_t capacity = capacity_arg; |
| 4261 | psa_status_t expected_status = expected_status_arg; |
| 4262 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4263 | |
| 4264 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4265 | |
| 4266 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4267 | |
| 4268 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 4269 | expected_status ); |
| 4270 | |
| 4271 | exit: |
| 4272 | psa_key_derivation_abort( &operation ); |
| 4273 | PSA_DONE( ); |
| 4274 | } |
| 4275 | /* END_CASE */ |
| 4276 | |
| 4277 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4278 | void derive_input( int alg_arg, |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4279 | int step_arg1, int key_type_arg1, data_t *input1, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4280 | int expected_status_arg1, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 4281 | int step_arg2, int key_type_arg2, data_t *input2, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4282 | int expected_status_arg2, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 4283 | int step_arg3, int key_type_arg3, data_t *input3, |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4284 | int expected_status_arg3, |
| 4285 | int output_key_type_arg, int expected_output_status_arg ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4286 | { |
| 4287 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4288 | psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3}; |
| 4289 | 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] | 4290 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 4291 | expected_status_arg2, |
| 4292 | expected_status_arg3}; |
| 4293 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4294 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 4295 | MBEDTLS_SVC_KEY_ID_INIT, |
| 4296 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4297 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4298 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4299 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4300 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4301 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4302 | psa_status_t expected_output_status = expected_output_status_arg; |
| 4303 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4304 | |
| 4305 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4306 | |
| 4307 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4308 | psa_set_key_algorithm( &attributes, alg ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4309 | |
| 4310 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4311 | |
| 4312 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 4313 | { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 4314 | mbedtls_test_set_step( i ); |
| 4315 | if( steps[i] == 0 ) |
| 4316 | { |
| 4317 | /* Skip this step */ |
| 4318 | } |
| 4319 | else if( key_types[i] != PSA_KEY_TYPE_NONE ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4320 | { |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4321 | psa_set_key_type( &attributes, key_types[i] ); |
| 4322 | PSA_ASSERT( psa_import_key( &attributes, |
| 4323 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4324 | &keys[i] ) ); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 4325 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) && |
| 4326 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 4327 | { |
| 4328 | // When taking a private key as secret input, use key agreement |
| 4329 | // to add the shared secret to the derivation |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 4330 | TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self( |
| 4331 | &operation, keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 4332 | expected_statuses[i] ); |
| 4333 | } |
| 4334 | else |
| 4335 | { |
| 4336 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4337 | keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 4338 | expected_statuses[i] ); |
| 4339 | } |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4340 | } |
| 4341 | else |
| 4342 | { |
| 4343 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 4344 | &operation, steps[i], |
| 4345 | inputs[i]->x, inputs[i]->len ), |
| 4346 | expected_statuses[i] ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4347 | } |
| 4348 | } |
| 4349 | |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4350 | if( output_key_type != PSA_KEY_TYPE_NONE ) |
| 4351 | { |
| 4352 | psa_reset_key_attributes( &attributes ); |
| 4353 | psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA ); |
| 4354 | psa_set_key_bits( &attributes, 8 ); |
| 4355 | actual_output_status = |
| 4356 | psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4357 | &output_key ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4358 | } |
| 4359 | else |
| 4360 | { |
| 4361 | uint8_t buffer[1]; |
| 4362 | actual_output_status = |
| 4363 | psa_key_derivation_output_bytes( &operation, |
| 4364 | buffer, sizeof( buffer ) ); |
| 4365 | } |
| 4366 | TEST_EQUAL( actual_output_status, expected_output_status ); |
| 4367 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4368 | exit: |
| 4369 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4370 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 4371 | psa_destroy_key( keys[i] ); |
| 4372 | psa_destroy_key( output_key ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4373 | PSA_DONE( ); |
| 4374 | } |
| 4375 | /* END_CASE */ |
| 4376 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4377 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 4378 | void derive_over_capacity( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4379 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4380 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4381 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 4382 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4383 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4384 | unsigned char input1[] = "Input 1"; |
| 4385 | size_t input1_length = sizeof( input1 ); |
| 4386 | unsigned char input2[] = "Input 2"; |
| 4387 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4388 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 4389 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 4390 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4391 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4392 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4393 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4394 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4395 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4396 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4397 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4398 | psa_set_key_algorithm( &attributes, alg ); |
| 4399 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4400 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 4401 | PSA_ASSERT( psa_import_key( &attributes, |
| 4402 | key_data, sizeof( key_data ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4403 | &key ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4404 | |
| 4405 | /* valid key derivation */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 4406 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 4407 | input1, input1_length, |
| 4408 | input2, input2_length, |
| 4409 | capacity ) ) |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4410 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4411 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4412 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4413 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4414 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4415 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4416 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4417 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4418 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4419 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4420 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4421 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4422 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4423 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4424 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4425 | } |
| 4426 | /* END_CASE */ |
| 4427 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4428 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 4429 | void derive_actions_without_setup( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4430 | { |
| 4431 | uint8_t output_buffer[16]; |
| 4432 | size_t buffer_size = 16; |
| 4433 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4434 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4435 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4436 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 4437 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4438 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4439 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4440 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4441 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4442 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4443 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4444 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4445 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 4446 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4447 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4448 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4449 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4450 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4451 | |
| 4452 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4453 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4454 | } |
| 4455 | /* END_CASE */ |
| 4456 | |
| 4457 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4458 | void derive_output( int alg_arg, |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4459 | int step1_arg, data_t *input1, |
| 4460 | int step2_arg, data_t *input2, |
| 4461 | int step3_arg, data_t *input3, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4462 | int requested_capacity_arg, |
| 4463 | data_t *expected_output1, |
| 4464 | data_t *expected_output2 ) |
| 4465 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4466 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4467 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg}; |
| 4468 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4469 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 4470 | MBEDTLS_SVC_KEY_ID_INIT, |
| 4471 | MBEDTLS_SVC_KEY_ID_INIT }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4472 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4473 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4474 | uint8_t *expected_outputs[2] = |
| 4475 | {expected_output1->x, expected_output2->x}; |
| 4476 | size_t output_sizes[2] = |
| 4477 | {expected_output1->len, expected_output2->len}; |
| 4478 | size_t output_buffer_size = 0; |
| 4479 | uint8_t *output_buffer = NULL; |
| 4480 | size_t expected_capacity; |
| 4481 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4482 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4483 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4484 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4485 | |
| 4486 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 4487 | { |
| 4488 | if( output_sizes[i] > output_buffer_size ) |
| 4489 | output_buffer_size = output_sizes[i]; |
| 4490 | if( output_sizes[i] == 0 ) |
| 4491 | expected_outputs[i] = NULL; |
| 4492 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4493 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4494 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4495 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4496 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4497 | psa_set_key_algorithm( &attributes, alg ); |
| 4498 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4499 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4500 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4501 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4502 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 4503 | requested_capacity ) ); |
| 4504 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4505 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4506 | switch( steps[i] ) |
| 4507 | { |
| 4508 | case 0: |
| 4509 | break; |
| 4510 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 4511 | PSA_ASSERT( psa_import_key( &attributes, |
| 4512 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4513 | &keys[i] ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4514 | |
| 4515 | if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 4516 | { |
| 4517 | PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) ); |
| 4518 | TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <= |
| 4519 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ); |
| 4520 | } |
| 4521 | |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4522 | PSA_ASSERT( psa_key_derivation_input_key( |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4523 | &operation, steps[i], keys[i] ) ); |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4524 | break; |
| 4525 | default: |
| 4526 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 4527 | &operation, steps[i], |
| 4528 | inputs[i]->x, inputs[i]->len ) ); |
| 4529 | break; |
| 4530 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 4531 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4532 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4533 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4534 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4535 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4536 | expected_capacity = requested_capacity; |
| 4537 | |
| 4538 | /* Expansion phase. */ |
| 4539 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 4540 | { |
| 4541 | /* Read some bytes. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4542 | status = psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4543 | output_buffer, output_sizes[i] ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4544 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 4545 | { |
| 4546 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 4547 | TEST_ASSERT( status == PSA_SUCCESS || |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4548 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4549 | continue; |
| 4550 | } |
| 4551 | else if( expected_capacity == 0 || |
| 4552 | output_sizes[i] > expected_capacity ) |
| 4553 | { |
| 4554 | /* Capacity exceeded. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4555 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4556 | expected_capacity = 0; |
| 4557 | continue; |
| 4558 | } |
| 4559 | /* Success. Check the read data. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4560 | PSA_ASSERT( status ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4561 | if( output_sizes[i] != 0 ) |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4562 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 4563 | expected_outputs[i], output_sizes[i] ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4564 | /* Check the operation status. */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4565 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4566 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4567 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4568 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4569 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4570 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4571 | |
| 4572 | exit: |
| 4573 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4574 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4575 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 4576 | psa_destroy_key( keys[i] ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4577 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4578 | } |
| 4579 | /* END_CASE */ |
| 4580 | |
| 4581 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4582 | void derive_full( int alg_arg, |
| 4583 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 4584 | data_t *input1, |
| 4585 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4586 | int requested_capacity_arg ) |
| 4587 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4588 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4589 | psa_algorithm_t alg = alg_arg; |
| 4590 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4591 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4592 | unsigned char output_buffer[16]; |
| 4593 | size_t expected_capacity = requested_capacity; |
| 4594 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4595 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4596 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4597 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4598 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4599 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4600 | psa_set_key_algorithm( &attributes, alg ); |
| 4601 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4602 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4603 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4604 | &key ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4605 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 4606 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 4607 | input1->x, input1->len, |
| 4608 | input2->x, input2->len, |
| 4609 | requested_capacity ) ) |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 4610 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 4611 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4612 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4613 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4614 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4615 | |
| 4616 | /* Expansion phase. */ |
| 4617 | while( current_capacity > 0 ) |
| 4618 | { |
| 4619 | size_t read_size = sizeof( output_buffer ); |
| 4620 | if( read_size > current_capacity ) |
| 4621 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4622 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4623 | output_buffer, |
| 4624 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4625 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4626 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4627 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4628 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4629 | } |
| 4630 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4631 | /* Check that the operation refuses to go over capacity. */ |
| 4632 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4633 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4634 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4635 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4636 | |
| 4637 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4638 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4639 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4640 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 4641 | } |
| 4642 | /* END_CASE */ |
| 4643 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4644 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4645 | void derive_key_exercise( int alg_arg, |
| 4646 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4647 | data_t *input1, |
| 4648 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4649 | int derived_type_arg, |
| 4650 | int derived_bits_arg, |
| 4651 | int derived_usage_arg, |
| 4652 | int derived_alg_arg ) |
| 4653 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4654 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4655 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4656 | psa_algorithm_t alg = alg_arg; |
| 4657 | psa_key_type_t derived_type = derived_type_arg; |
| 4658 | size_t derived_bits = derived_bits_arg; |
| 4659 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 4660 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 4661 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4662 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4663 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4664 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4665 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4666 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4667 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4668 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4669 | psa_set_key_algorithm( &attributes, alg ); |
| 4670 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4671 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4672 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4673 | |
| 4674 | /* Derive a key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 4675 | if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 4676 | input1->x, input1->len, |
| 4677 | input2->x, input2->len, |
| 4678 | capacity ) ) |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 4679 | goto exit; |
| 4680 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4681 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 4682 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 4683 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4684 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4685 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4686 | &derived_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4687 | |
| 4688 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4689 | PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4690 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 4691 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4692 | |
| 4693 | /* Exercise the derived key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 4694 | if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4695 | goto exit; |
| 4696 | |
| 4697 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4698 | /* |
| 4699 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4700 | * thus reset them as required. |
| 4701 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4702 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4703 | |
| 4704 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4705 | psa_destroy_key( base_key ); |
| 4706 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4707 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4708 | } |
| 4709 | /* END_CASE */ |
| 4710 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4711 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4712 | void derive_key_export( int alg_arg, |
| 4713 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4714 | data_t *input1, |
| 4715 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4716 | int bytes1_arg, |
| 4717 | int bytes2_arg ) |
| 4718 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4719 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4720 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4721 | psa_algorithm_t alg = alg_arg; |
| 4722 | size_t bytes1 = bytes1_arg; |
| 4723 | size_t bytes2 = bytes2_arg; |
| 4724 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4725 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4726 | uint8_t *output_buffer = NULL; |
| 4727 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4728 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4729 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4730 | size_t length; |
| 4731 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4732 | ASSERT_ALLOC( output_buffer, capacity ); |
| 4733 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4734 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4735 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4736 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 4737 | psa_set_key_algorithm( &base_attributes, alg ); |
| 4738 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4739 | 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] | 4740 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4741 | |
| 4742 | /* Derive some material and output it. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 4743 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 4744 | input1->x, input1->len, |
| 4745 | input2->x, input2->len, |
| 4746 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4747 | goto exit; |
| 4748 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4749 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4750 | output_buffer, |
| 4751 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4752 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4753 | |
| 4754 | /* 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] | 4755 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 4756 | input1->x, input1->len, |
| 4757 | input2->x, input2->len, |
| 4758 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 4759 | goto exit; |
| 4760 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4761 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 4762 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 4763 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4764 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4765 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4766 | &derived_key ) ); |
| 4767 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4768 | export_buffer, bytes1, |
| 4769 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4770 | TEST_EQUAL( length, bytes1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4771 | PSA_ASSERT( psa_destroy_key( derived_key ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 4772 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4773 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4774 | &derived_key ) ); |
| 4775 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4776 | export_buffer + bytes1, bytes2, |
| 4777 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4778 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4779 | |
| 4780 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 4781 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 4782 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4783 | |
| 4784 | exit: |
| 4785 | mbedtls_free( output_buffer ); |
| 4786 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4787 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4788 | psa_destroy_key( base_key ); |
| 4789 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4790 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 4791 | } |
| 4792 | /* END_CASE */ |
| 4793 | |
| 4794 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 4795 | void derive_key( int alg_arg, |
| 4796 | data_t *key_data, data_t *input1, data_t *input2, |
| 4797 | int type_arg, int bits_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 4798 | int expected_status_arg, |
| 4799 | int is_large_output ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4800 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4801 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4802 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4803 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 4804 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4805 | size_t bits = bits_arg; |
| 4806 | psa_status_t expected_status = expected_status_arg; |
| 4807 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4808 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4809 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4810 | |
| 4811 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4812 | |
| 4813 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 4814 | psa_set_key_algorithm( &base_attributes, alg ); |
| 4815 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 4816 | 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] | 4817 | &base_key ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4818 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 4819 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 4820 | input1->x, input1->len, |
| 4821 | input2->x, input2->len, |
| 4822 | SIZE_MAX ) ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4823 | goto exit; |
| 4824 | |
| 4825 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 4826 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 4827 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4828 | psa_set_key_bits( &derived_attributes, bits ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 4829 | |
| 4830 | psa_status_t status = |
| 4831 | psa_key_derivation_output_key( &derived_attributes, |
| 4832 | &operation, |
| 4833 | &derived_key ); |
| 4834 | if( is_large_output > 0 ) |
| 4835 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 4836 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4837 | |
| 4838 | exit: |
| 4839 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4840 | psa_destroy_key( base_key ); |
| 4841 | psa_destroy_key( derived_key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 4842 | PSA_DONE( ); |
| 4843 | } |
| 4844 | /* END_CASE */ |
| 4845 | |
| 4846 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4847 | void key_agreement_setup( int alg_arg, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 4848 | int our_key_type_arg, int our_key_alg_arg, |
| 4849 | data_t *our_key_data, data_t *peer_key_data, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4850 | int expected_status_arg ) |
| 4851 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4852 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4853 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 4854 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4855 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4856 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4857 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4858 | psa_status_t expected_status = expected_status_arg; |
| 4859 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4860 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4861 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4862 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4863 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 4864 | psa_set_key_algorithm( &attributes, our_key_alg ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4865 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4866 | PSA_ASSERT( psa_import_key( &attributes, |
| 4867 | our_key_data->x, our_key_data->len, |
| 4868 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4869 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4870 | /* The tests currently include inputs that should fail at either step. |
| 4871 | * Test cases that fail at the setup step should be changed to call |
| 4872 | * key_derivation_setup instead, and this function should be renamed |
| 4873 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4874 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4875 | if( status == PSA_SUCCESS ) |
| 4876 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4877 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 4878 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 4879 | our_key, |
| 4880 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 4881 | expected_status ); |
| 4882 | } |
| 4883 | else |
| 4884 | { |
| 4885 | TEST_ASSERT( status == expected_status ); |
| 4886 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4887 | |
| 4888 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4889 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4890 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4891 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 4892 | } |
| 4893 | /* END_CASE */ |
| 4894 | |
| 4895 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4896 | void raw_key_agreement( int alg_arg, |
| 4897 | int our_key_type_arg, data_t *our_key_data, |
| 4898 | data_t *peer_key_data, |
| 4899 | data_t *expected_output ) |
| 4900 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4901 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4902 | psa_algorithm_t alg = alg_arg; |
| 4903 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4904 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4905 | unsigned char *output = NULL; |
| 4906 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4907 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4908 | |
| 4909 | ASSERT_ALLOC( output, expected_output->len ); |
| 4910 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4911 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4912 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4913 | psa_set_key_algorithm( &attributes, alg ); |
| 4914 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4915 | PSA_ASSERT( psa_import_key( &attributes, |
| 4916 | our_key_data->x, our_key_data->len, |
| 4917 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4918 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4919 | PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) ); |
| 4920 | key_bits = psa_get_key_bits( &attributes ); |
| 4921 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 4922 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 4923 | peer_key_data->x, peer_key_data->len, |
| 4924 | output, expected_output->len, |
| 4925 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4926 | ASSERT_COMPARE( output, output_length, |
| 4927 | expected_output->x, expected_output->len ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4928 | TEST_ASSERT( output_length <= |
| 4929 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) ); |
| 4930 | TEST_ASSERT( output_length <= |
| 4931 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4932 | |
| 4933 | exit: |
| 4934 | mbedtls_free( output ); |
| 4935 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4936 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 4937 | } |
| 4938 | /* END_CASE */ |
| 4939 | |
| 4940 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4941 | void key_agreement_capacity( int alg_arg, |
| 4942 | int our_key_type_arg, data_t *our_key_data, |
| 4943 | data_t *peer_key_data, |
| 4944 | int expected_capacity_arg ) |
| 4945 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4946 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4947 | psa_algorithm_t alg = alg_arg; |
| 4948 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4949 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4950 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4951 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4952 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4953 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4954 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4955 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4956 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4957 | psa_set_key_algorithm( &attributes, alg ); |
| 4958 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4959 | PSA_ASSERT( psa_import_key( &attributes, |
| 4960 | our_key_data->x, our_key_data->len, |
| 4961 | &our_key ) ); |
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_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4964 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 4965 | &operation, |
| 4966 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 4967 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 4968 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 4969 | { |
| 4970 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4971 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 4972 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 4973 | NULL, 0 ) ); |
| 4974 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4975 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4976 | /* Test the advertized capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4977 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4978 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4979 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4980 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4981 | /* Test the actual capacity by reading the output. */ |
| 4982 | while( actual_capacity > sizeof( output ) ) |
| 4983 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4984 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4985 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4986 | actual_capacity -= sizeof( output ); |
| 4987 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4988 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4989 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4990 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4991 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 4992 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4993 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4994 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4995 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4996 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 4997 | } |
| 4998 | /* END_CASE */ |
| 4999 | |
| 5000 | /* BEGIN_CASE */ |
| 5001 | void key_agreement_output( int alg_arg, |
| 5002 | int our_key_type_arg, data_t *our_key_data, |
| 5003 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5004 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5005 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5006 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5007 | psa_algorithm_t alg = alg_arg; |
| 5008 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5009 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5010 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5011 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5012 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5013 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 5014 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5015 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5016 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5017 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5018 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5019 | psa_set_key_algorithm( &attributes, alg ); |
| 5020 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5021 | PSA_ASSERT( psa_import_key( &attributes, |
| 5022 | our_key_data->x, our_key_data->len, |
| 5023 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5024 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5025 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5026 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 5027 | &operation, |
| 5028 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 5029 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5030 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 5031 | { |
| 5032 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5033 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5034 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5035 | NULL, 0 ) ); |
| 5036 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5037 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5038 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5039 | actual_output, |
| 5040 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5041 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 5042 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5043 | if( expected_output2->len != 0 ) |
| 5044 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5045 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5046 | actual_output, |
| 5047 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5048 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 5049 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5050 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5051 | |
| 5052 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5053 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5054 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5055 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5056 | mbedtls_free( actual_output ); |
| 5057 | } |
| 5058 | /* END_CASE */ |
| 5059 | |
| 5060 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5061 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5062 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5063 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5064 | unsigned char *output = NULL; |
| 5065 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5066 | size_t i; |
| 5067 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5068 | |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 5069 | TEST_ASSERT( bytes_arg >= 0 ); |
| 5070 | |
Gilles Peskine | 9189202 | 2021-02-08 19:50:26 +0100 | [diff] [blame] | 5071 | ASSERT_ALLOC( output, bytes ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5072 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5073 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5074 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5075 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5076 | /* Run several times, to ensure that every output byte will be |
| 5077 | * nonzero at least once with overwhelming probability |
| 5078 | * (2^(-8*number_of_runs)). */ |
| 5079 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5080 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 5081 | if( bytes != 0 ) |
| 5082 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5083 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5084 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5085 | for( i = 0; i < bytes; i++ ) |
| 5086 | { |
| 5087 | if( output[i] != 0 ) |
| 5088 | ++changed[i]; |
| 5089 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5090 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5091 | |
| 5092 | /* Check that every byte was changed to nonzero at least once. This |
| 5093 | * validates that psa_generate_random is overwriting every byte of |
| 5094 | * the output buffer. */ |
| 5095 | for( i = 0; i < bytes; i++ ) |
| 5096 | { |
| 5097 | TEST_ASSERT( changed[i] != 0 ); |
| 5098 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5099 | |
| 5100 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5101 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5102 | mbedtls_free( output ); |
| 5103 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5104 | } |
| 5105 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5106 | |
| 5107 | /* BEGIN_CASE */ |
| 5108 | void generate_key( int type_arg, |
| 5109 | int bits_arg, |
| 5110 | int usage_arg, |
| 5111 | int alg_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 5112 | int expected_status_arg, |
| 5113 | int is_large_key ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5114 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5115 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5116 | psa_key_type_t type = type_arg; |
| 5117 | psa_key_usage_t usage = usage_arg; |
| 5118 | size_t bits = bits_arg; |
| 5119 | psa_algorithm_t alg = alg_arg; |
| 5120 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5121 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5122 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5123 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5124 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5125 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5126 | psa_set_key_usage_flags( &attributes, usage ); |
| 5127 | psa_set_key_algorithm( &attributes, alg ); |
| 5128 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5129 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5130 | |
| 5131 | /* Generate a key */ |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 5132 | psa_status_t status = psa_generate_key( &attributes, &key ); |
| 5133 | |
| 5134 | if( is_large_key > 0 ) |
| 5135 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5136 | TEST_EQUAL( status , expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5137 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5138 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5139 | |
| 5140 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5141 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5142 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 5143 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5144 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 5145 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5146 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 5147 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5148 | |
| 5149 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5150 | /* |
| 5151 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5152 | * thus reset them as required. |
| 5153 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5154 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5155 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5156 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5157 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5158 | } |
| 5159 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5160 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 5161 | /* 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] | 5162 | void generate_key_rsa( int bits_arg, |
| 5163 | data_t *e_arg, |
| 5164 | int expected_status_arg ) |
| 5165 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5166 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5167 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5168 | size_t bits = bits_arg; |
| 5169 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 5170 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 5171 | psa_status_t expected_status = expected_status_arg; |
| 5172 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5173 | uint8_t *exported = NULL; |
| 5174 | size_t exported_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5175 | PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5176 | size_t exported_length = SIZE_MAX; |
| 5177 | uint8_t *e_read_buffer = NULL; |
| 5178 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 5179 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5180 | size_t e_read_length = SIZE_MAX; |
| 5181 | |
| 5182 | if( e_arg->len == 0 || |
| 5183 | ( e_arg->len == 3 && |
| 5184 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 5185 | { |
| 5186 | is_default_public_exponent = 1; |
| 5187 | e_read_size = 0; |
| 5188 | } |
| 5189 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 5190 | ASSERT_ALLOC( exported, exported_size ); |
| 5191 | |
| 5192 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5193 | |
| 5194 | psa_set_key_usage_flags( &attributes, usage ); |
| 5195 | psa_set_key_algorithm( &attributes, alg ); |
| 5196 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 5197 | e_arg->x, e_arg->len ) ); |
| 5198 | psa_set_key_bits( &attributes, bits ); |
| 5199 | |
| 5200 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5201 | TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5202 | if( expected_status != PSA_SUCCESS ) |
| 5203 | goto exit; |
| 5204 | |
| 5205 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5206 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5207 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 5208 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 5209 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 5210 | e_read_buffer, e_read_size, |
| 5211 | &e_read_length ) ); |
| 5212 | if( is_default_public_exponent ) |
| 5213 | TEST_EQUAL( e_read_length, 0 ); |
| 5214 | else |
| 5215 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 5216 | |
| 5217 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5218 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5219 | goto exit; |
| 5220 | |
| 5221 | /* Export the key and check the public exponent. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5222 | PSA_ASSERT( psa_export_public_key( key, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5223 | exported, exported_size, |
| 5224 | &exported_length ) ); |
| 5225 | { |
| 5226 | uint8_t *p = exported; |
| 5227 | uint8_t *end = exported + exported_length; |
| 5228 | size_t len; |
| 5229 | /* RSAPublicKey ::= SEQUENCE { |
| 5230 | * modulus INTEGER, -- n |
| 5231 | * publicExponent INTEGER } -- e |
| 5232 | */ |
| 5233 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5234 | MBEDTLS_ASN1_SEQUENCE | |
| 5235 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 5236 | TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5237 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 5238 | MBEDTLS_ASN1_INTEGER ) ); |
| 5239 | if( len >= 1 && p[0] == 0 ) |
| 5240 | { |
| 5241 | ++p; |
| 5242 | --len; |
| 5243 | } |
| 5244 | if( e_arg->len == 0 ) |
| 5245 | { |
| 5246 | TEST_EQUAL( len, 3 ); |
| 5247 | TEST_EQUAL( p[0], 1 ); |
| 5248 | TEST_EQUAL( p[1], 0 ); |
| 5249 | TEST_EQUAL( p[2], 1 ); |
| 5250 | } |
| 5251 | else |
| 5252 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 5253 | } |
| 5254 | |
| 5255 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5256 | /* |
| 5257 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 5258 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 5259 | */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5260 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5261 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5262 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5263 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5264 | mbedtls_free( e_read_buffer ); |
| 5265 | mbedtls_free( exported ); |
| 5266 | } |
| 5267 | /* END_CASE */ |
| 5268 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5269 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5270 | void persistent_key_load_key_from_storage( data_t *data, |
| 5271 | int type_arg, int bits_arg, |
| 5272 | int usage_flags_arg, int alg_arg, |
| 5273 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5274 | { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 5275 | 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] | 5276 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5277 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5278 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5279 | psa_key_type_t type = type_arg; |
| 5280 | size_t bits = bits_arg; |
| 5281 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 5282 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5283 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5284 | unsigned char *first_export = NULL; |
| 5285 | unsigned char *second_export = NULL; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5286 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5287 | size_t first_exported_length; |
| 5288 | size_t second_exported_length; |
| 5289 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5290 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 5291 | { |
| 5292 | ASSERT_ALLOC( first_export, export_size ); |
| 5293 | ASSERT_ALLOC( second_export, export_size ); |
| 5294 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5295 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5296 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5297 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 5298 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5299 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 5300 | psa_set_key_algorithm( &attributes, alg ); |
| 5301 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5302 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5303 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5304 | switch( generation_method ) |
| 5305 | { |
| 5306 | case IMPORT_KEY: |
| 5307 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5308 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5309 | &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5310 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5311 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5312 | case GENERATE_KEY: |
| 5313 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5314 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5315 | break; |
| 5316 | |
| 5317 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 5318 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5319 | { |
| 5320 | /* Create base key */ |
| 5321 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 5322 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5323 | psa_set_key_usage_flags( &base_attributes, |
| 5324 | PSA_KEY_USAGE_DERIVE ); |
| 5325 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 5326 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5327 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 5328 | data->x, data->len, |
| 5329 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5330 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5331 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5332 | PSA_ASSERT( psa_key_derivation_input_key( |
| 5333 | &operation, |
| 5334 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5335 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5336 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5337 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5338 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 5339 | &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5340 | &key ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5341 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5342 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5343 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5344 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 5345 | #else |
| 5346 | TEST_ASSUME( ! "KDF not supported in this configuration" ); |
| 5347 | #endif |
| 5348 | break; |
| 5349 | |
| 5350 | default: |
| 5351 | TEST_ASSERT( ! "generation_method not implemented in test" ); |
| 5352 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5353 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5354 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5355 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5356 | /* Export the key if permitted by the key policy. */ |
| 5357 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 5358 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5359 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5360 | first_export, export_size, |
| 5361 | &first_exported_length ) ); |
| 5362 | if( generation_method == IMPORT_KEY ) |
| 5363 | ASSERT_COMPARE( data->x, data->len, |
| 5364 | first_export, first_exported_length ); |
| 5365 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5366 | |
| 5367 | /* Shutdown and restart */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5368 | PSA_ASSERT( psa_purge_key( key ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5369 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5370 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5371 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5372 | /* Check key slot still contains key data */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5373 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 5374 | TEST_ASSERT( mbedtls_svc_key_id_equal( |
| 5375 | psa_get_key_id( &attributes ), key_id ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5376 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 5377 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 5378 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 5379 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 5380 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 5381 | mbedtls_test_update_key_usage_flags( usage_flags ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5382 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5383 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5384 | /* Export the key again if permitted by the key policy. */ |
| 5385 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5386 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5387 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5388 | second_export, export_size, |
| 5389 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5390 | ASSERT_COMPARE( first_export, first_exported_length, |
| 5391 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5392 | } |
| 5393 | |
| 5394 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5395 | if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5396 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5397 | |
| 5398 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5399 | /* |
| 5400 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5401 | * thus reset them as required. |
| 5402 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5403 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5404 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5405 | mbedtls_free( first_export ); |
| 5406 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5407 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5408 | psa_destroy_key( base_key ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5409 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5410 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5411 | } |
| 5412 | /* END_CASE */ |