Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2 | #include <stdint.h> |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 3 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 4 | #include "mbedtls/asn1.h" |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 5 | #include "mbedtls/asn1write.h" |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 6 | #include "mbedtls/oid.h" |
| 7 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 8 | /* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random() |
| 9 | * uses mbedtls_ctr_drbg internally. */ |
| 10 | #include "mbedtls/ctr_drbg.h" |
| 11 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 12 | #include "psa/crypto.h" |
Ronald Cron | 4184107 | 2020-09-17 15:28:26 +0200 | [diff] [blame] | 13 | #include "psa_crypto_slot_management.h" |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 14 | |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 15 | #include "test/asn1_helpers.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 16 | #include "test/psa_crypto_helpers.h" |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 17 | #include "test/psa_exercise_key.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 18 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 19 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 20 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 21 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 22 | /** Test if a buffer contains a constant byte value. |
| 23 | * |
| 24 | * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 25 | * |
| 26 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 27 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 28 | * \param size Size of the buffer in bytes. |
| 29 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 30 | * \return 1 if the buffer is all-bits-zero. |
| 31 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 32 | */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 33 | static int mem_is_char( void *buffer, unsigned char c, size_t size ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 34 | { |
| 35 | size_t i; |
| 36 | for( i = 0; i < size; i++ ) |
| 37 | { |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 38 | if( ( (unsigned char *) buffer )[i] != c ) |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 39 | return( 0 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 40 | } |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 41 | return( 1 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 42 | } |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 43 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 44 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
| 45 | static int asn1_write_10x( unsigned char **p, |
| 46 | unsigned char *start, |
| 47 | size_t bits, |
| 48 | unsigned char x ) |
| 49 | { |
| 50 | int ret; |
| 51 | int len = bits / 8 + 1; |
Gilles Peskine | 480416a | 2018-06-28 19:04:07 +0200 | [diff] [blame] | 52 | if( bits == 0 ) |
| 53 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
| 54 | if( bits <= 8 && x >= 1 << ( bits - 1 ) ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 55 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 56 | if( *p < start || *p - start < (ptrdiff_t) len ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 57 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 58 | *p -= len; |
| 59 | ( *p )[len-1] = x; |
| 60 | if( bits % 8 == 0 ) |
| 61 | ( *p )[1] |= 1; |
| 62 | else |
| 63 | ( *p )[0] |= 1 << ( bits % 8 ); |
| 64 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 65 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
| 66 | MBEDTLS_ASN1_INTEGER ) ); |
| 67 | return( len ); |
| 68 | } |
| 69 | |
| 70 | static int construct_fake_rsa_key( unsigned char *buffer, |
| 71 | size_t buffer_size, |
| 72 | unsigned char **p, |
| 73 | size_t bits, |
| 74 | int keypair ) |
| 75 | { |
| 76 | size_t half_bits = ( bits + 1 ) / 2; |
| 77 | int ret; |
| 78 | int len = 0; |
| 79 | /* Construct something that looks like a DER encoding of |
| 80 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 81 | * RSAPrivateKey ::= SEQUENCE { |
| 82 | * version Version, |
| 83 | * modulus INTEGER, -- n |
| 84 | * publicExponent INTEGER, -- e |
| 85 | * privateExponent INTEGER, -- d |
| 86 | * prime1 INTEGER, -- p |
| 87 | * prime2 INTEGER, -- q |
| 88 | * exponent1 INTEGER, -- d mod (p-1) |
| 89 | * exponent2 INTEGER, -- d mod (q-1) |
| 90 | * coefficient INTEGER, -- (inverse of q) mod p |
| 91 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 92 | * } |
| 93 | * Or, for a public key, the same structure with only |
| 94 | * version, modulus and publicExponent. |
| 95 | */ |
| 96 | *p = buffer + buffer_size; |
| 97 | if( keypair ) |
| 98 | { |
| 99 | MBEDTLS_ASN1_CHK_ADD( len, /* pq */ |
| 100 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 101 | MBEDTLS_ASN1_CHK_ADD( len, /* dq */ |
| 102 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 103 | MBEDTLS_ASN1_CHK_ADD( len, /* dp */ |
| 104 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 105 | MBEDTLS_ASN1_CHK_ADD( len, /* q */ |
| 106 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 107 | MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */ |
| 108 | asn1_write_10x( p, buffer, half_bits, 3 ) ); |
| 109 | MBEDTLS_ASN1_CHK_ADD( len, /* d */ |
| 110 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 111 | } |
| 112 | MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */ |
| 113 | asn1_write_10x( p, buffer, 17, 1 ) ); |
| 114 | MBEDTLS_ASN1_CHK_ADD( len, /* n */ |
| 115 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 116 | if( keypair ) |
| 117 | MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */ |
| 118 | mbedtls_asn1_write_int( p, buffer, 0 ) ); |
| 119 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) ); |
| 120 | { |
| 121 | const unsigned char tag = |
| 122 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
| 123 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) ); |
| 124 | } |
| 125 | return( len ); |
| 126 | } |
| 127 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 128 | int exercise_mac_setup( psa_key_type_t key_type, |
| 129 | const unsigned char *key_bytes, |
| 130 | size_t key_length, |
| 131 | psa_algorithm_t alg, |
| 132 | psa_mac_operation_t *operation, |
| 133 | psa_status_t *status ) |
| 134 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 135 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 136 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 137 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 138 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 139 | psa_set_key_algorithm( &attributes, alg ); |
| 140 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 141 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 142 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 143 | *status = psa_mac_sign_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 144 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 145 | PSA_ASSERT( psa_mac_abort( operation ) ); |
| 146 | /* If setup failed, reproduce the failure, so that the caller can |
| 147 | * test the resulting state of the operation object. */ |
| 148 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 149 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 150 | TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 153 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 154 | return( 1 ); |
| 155 | |
| 156 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 157 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 158 | return( 0 ); |
| 159 | } |
| 160 | |
| 161 | int exercise_cipher_setup( psa_key_type_t key_type, |
| 162 | const unsigned char *key_bytes, |
| 163 | size_t key_length, |
| 164 | psa_algorithm_t alg, |
| 165 | psa_cipher_operation_t *operation, |
| 166 | psa_status_t *status ) |
| 167 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 168 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 169 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 170 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 171 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 172 | psa_set_key_algorithm( &attributes, alg ); |
| 173 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 174 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 175 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 176 | *status = psa_cipher_encrypt_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 177 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 178 | PSA_ASSERT( psa_cipher_abort( operation ) ); |
| 179 | /* If setup failed, reproduce the failure, so that the caller can |
| 180 | * test the resulting state of the operation object. */ |
| 181 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 182 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 183 | TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ), |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 184 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 185 | } |
| 186 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 187 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 188 | return( 1 ); |
| 189 | |
| 190 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 191 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 192 | return( 0 ); |
| 193 | } |
| 194 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 195 | static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key ) |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 196 | { |
| 197 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 198 | mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 199 | uint8_t buffer[1]; |
| 200 | size_t length; |
| 201 | int ok = 0; |
| 202 | |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 203 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 204 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 205 | psa_set_key_algorithm( &attributes, PSA_ALG_CTR ); |
| 206 | psa_set_key_type( &attributes, PSA_KEY_TYPE_AES ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 207 | TEST_EQUAL( psa_get_key_attributes( key, &attributes ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 208 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 209 | TEST_EQUAL( |
| 210 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 ); |
| 211 | TEST_EQUAL( |
| 212 | MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 213 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 214 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 215 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 216 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
| 217 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
| 218 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 219 | TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 220 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 221 | TEST_EQUAL( psa_export_public_key( key, |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 222 | buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 223 | PSA_ERROR_INVALID_HANDLE ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 224 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 225 | ok = 1; |
| 226 | |
| 227 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 228 | /* |
| 229 | * Key attributes may have been returned by psa_get_key_attributes() |
| 230 | * thus reset them as required. |
| 231 | */ |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 232 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 233 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 234 | return( ok ); |
| 235 | } |
| 236 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 237 | /* Assert that a key isn't reported as having a slot number. */ |
| 238 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 239 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 240 | do \ |
| 241 | { \ |
| 242 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
| 243 | TEST_EQUAL( psa_get_key_slot_number( \ |
| 244 | attributes, \ |
| 245 | &ASSERT_NO_SLOT_NUMBER_slot_number ), \ |
| 246 | PSA_ERROR_INVALID_ARGUMENT ); \ |
| 247 | } \ |
| 248 | while( 0 ) |
| 249 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 250 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 251 | ( (void) 0 ) |
| 252 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 253 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 254 | /* An overapproximation of the amount of storage needed for a key of the |
| 255 | * given type and with the given content. The API doesn't make it easy |
| 256 | * to find a good value for the size. The current implementation doesn't |
| 257 | * care about the value anyway. */ |
| 258 | #define KEY_BITS_FROM_DATA( type, data ) \ |
| 259 | ( data )->len |
| 260 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 261 | typedef enum { |
| 262 | IMPORT_KEY = 0, |
| 263 | GENERATE_KEY = 1, |
| 264 | DERIVE_KEY = 2 |
| 265 | } generate_method; |
| 266 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 267 | static psa_status_t aead_multipart_encrypt_internal( int key_type_arg, |
| 268 | data_t *key_data, |
| 269 | int alg_arg, |
| 270 | data_t *nonce, |
| 271 | data_t *additional_data, |
| 272 | int ad_part_len, |
| 273 | data_t *input_data, |
| 274 | int data_part_len, |
| 275 | data_t *expected_result ) |
| 276 | { |
| 277 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 278 | psa_key_type_t key_type = key_type_arg; |
| 279 | psa_algorithm_t alg = alg_arg; |
| 280 | psa_aead_operation_t operation; |
| 281 | unsigned char *output_data = NULL; |
| 282 | unsigned char *part_data = NULL; |
| 283 | unsigned char *final_data = NULL; |
| 284 | size_t output_size = 0; |
| 285 | size_t finish_output_size; |
| 286 | size_t part_data_size = 0; |
| 287 | size_t output_length = 0; |
| 288 | size_t key_bits = 0; |
| 289 | size_t tag_length = 0; |
| 290 | size_t tag_size = 0; |
| 291 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 292 | uint32_t part_offset = 0; |
| 293 | size_t part_length = 0; |
| 294 | size_t output_part_length = 0; |
| 295 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 296 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 297 | |
| 298 | PSA_ASSERT( psa_crypto_init( ) ); |
| 299 | |
| 300 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 301 | psa_set_key_algorithm( &attributes, alg ); |
| 302 | psa_set_key_type( &attributes, key_type ); |
| 303 | |
| 304 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 305 | &key ) ); |
| 306 | |
| 307 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 308 | key_bits = psa_get_key_bits( &attributes ); |
| 309 | |
| 310 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 311 | |
| 312 | TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE ); |
| 313 | |
| 314 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 315 | ( input_data->len + |
| 316 | tag_length ) ); |
| 317 | |
| 318 | ASSERT_ALLOC( output_data, output_size ); |
| 319 | |
| 320 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 321 | |
| 322 | TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
| 323 | |
| 324 | ASSERT_ALLOC( final_data, finish_output_size ); |
| 325 | |
| 326 | operation = psa_aead_operation_init( ); |
| 327 | |
| 328 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 329 | |
| 330 | /* If the operation is not supported, just skip and not fail in case the |
| 331 | * encryption involves a common limitation of cryptography hardwares and |
| 332 | * an alternative implementation. */ |
| 333 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 334 | { |
| 335 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 336 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 337 | } |
| 338 | |
| 339 | PSA_ASSERT( status ); |
| 340 | |
| 341 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 342 | |
| 343 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 344 | if( operation.alg == PSA_ALG_GCM ) |
| 345 | { |
| 346 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 347 | input_data->len ) ); |
| 348 | } |
| 349 | #endif |
| 350 | |
| 351 | if( ad_part_len != -1 ) |
| 352 | { |
| 353 | /* Pass additional data in parts */ |
| 354 | part_offset = 0; |
| 355 | |
| 356 | while( part_offset < additional_data->len ) |
| 357 | { |
| 358 | if( additional_data->len - part_offset < ( uint32_t ) ad_part_len ) |
| 359 | { |
| 360 | part_length = additional_data->len - part_offset; |
| 361 | } |
| 362 | else |
| 363 | { |
| 364 | part_length = ad_part_len; |
| 365 | } |
| 366 | |
| 367 | PSA_ASSERT( psa_aead_update_ad( &operation, |
| 368 | additional_data->x + part_offset, |
| 369 | part_length ) ); |
| 370 | |
| 371 | part_offset += part_length; |
| 372 | } |
| 373 | } |
| 374 | else |
| 375 | { |
| 376 | /* Pass additional data in one go. */ |
| 377 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 378 | additional_data->len ) ); |
| 379 | } |
| 380 | |
| 381 | if( data_part_len != -1 ) |
| 382 | { |
| 383 | /* Pass data in parts */ |
| 384 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 385 | ( size_t ) data_part_len ); |
| 386 | |
| 387 | ASSERT_ALLOC( part_data, part_data_size ); |
| 388 | |
| 389 | part_offset = 0; |
| 390 | |
| 391 | while( part_offset < input_data->len ) |
| 392 | { |
| 393 | if( input_data->len - part_offset < ( uint32_t ) data_part_len ) |
| 394 | { |
| 395 | part_length = input_data->len - part_offset; |
| 396 | } |
| 397 | else |
| 398 | { |
| 399 | part_length = data_part_len; |
| 400 | } |
| 401 | |
| 402 | PSA_ASSERT( psa_aead_update( &operation, |
| 403 | ( input_data->x + part_offset ), |
| 404 | part_length, part_data, |
| 405 | part_data_size, |
| 406 | &output_part_length ) ); |
| 407 | |
| 408 | if( output_data && output_part_length ) |
| 409 | { |
| 410 | memcpy( ( output_data + part_offset ), part_data, |
| 411 | output_part_length ); |
| 412 | } |
| 413 | |
| 414 | part_offset += part_length; |
| 415 | output_length += output_part_length; |
| 416 | } |
| 417 | } |
| 418 | else |
| 419 | { |
| 420 | /* Pass whole data in one go */ |
| 421 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 422 | input_data->len, output_data, |
| 423 | output_size, &output_length ) ); |
| 424 | } |
| 425 | |
| 426 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 427 | finish_output_size, |
| 428 | &output_part_length, |
| 429 | tag_buffer, tag_length, |
| 430 | &tag_size ) ); |
| 431 | |
| 432 | if( output_data && output_part_length ) |
| 433 | { |
| 434 | memcpy( ( output_data + output_length ), final_data, |
| 435 | output_part_length ); |
| 436 | } |
| 437 | |
| 438 | TEST_EQUAL( tag_length, tag_size ); |
| 439 | |
| 440 | output_length += output_part_length; |
| 441 | |
| 442 | if( output_data && tag_length ) |
| 443 | { |
| 444 | memcpy( ( output_data + output_length ), tag_buffer, tag_length ); |
| 445 | } |
| 446 | |
| 447 | output_length += tag_length; |
| 448 | |
| 449 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 450 | * should be exact. */ |
| 451 | TEST_EQUAL( output_length, |
| 452 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 453 | input_data->len ) ); |
| 454 | TEST_ASSERT( output_length <= |
| 455 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 456 | |
| 457 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 458 | output_data, output_length ); |
| 459 | |
| 460 | exit: |
| 461 | psa_destroy_key( key ); |
| 462 | psa_aead_abort( &operation ); |
| 463 | mbedtls_free( output_data ); |
| 464 | mbedtls_free( part_data ); |
| 465 | mbedtls_free( final_data ); |
| 466 | PSA_DONE( ); |
| 467 | |
| 468 | return( status ); |
| 469 | } |
| 470 | |
| 471 | void aead_multipart_decrypt_internal( int key_type_arg, data_t *key_data, |
| 472 | int alg_arg, |
| 473 | data_t *nonce, |
| 474 | data_t *additional_data, |
| 475 | int ad_part_len, |
| 476 | data_t *input_data, |
| 477 | int data_part_len, |
| 478 | data_t *expected_data, |
| 479 | int expected_result_arg ) |
| 480 | { |
| 481 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 482 | psa_key_type_t key_type = key_type_arg; |
| 483 | psa_algorithm_t alg = alg_arg; |
| 484 | psa_aead_operation_t operation; |
| 485 | unsigned char *output_data = NULL; |
| 486 | unsigned char *part_data = NULL; |
| 487 | unsigned char *final_data = NULL; |
| 488 | size_t part_data_size; |
| 489 | size_t output_size = 0; |
| 490 | size_t verify_output_size = 0; |
| 491 | size_t output_length = 0; |
| 492 | size_t key_bits = 0; |
| 493 | size_t tag_length = 0; |
| 494 | uint32_t part_offset = 0; |
| 495 | size_t part_length = 0; |
| 496 | size_t output_part_length = 0; |
| 497 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 498 | psa_status_t expected_result = expected_result_arg; |
| 499 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 500 | |
| 501 | PSA_ASSERT( psa_crypto_init( ) ); |
| 502 | |
| 503 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 504 | psa_set_key_algorithm( &attributes, alg ); |
| 505 | psa_set_key_type( &attributes, key_type ); |
| 506 | |
| 507 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 508 | &key ) ); |
| 509 | |
| 510 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 511 | key_bits = psa_get_key_bits( &attributes ); |
| 512 | |
| 513 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 514 | |
| 515 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 516 | ( input_data->len - |
| 517 | tag_length ) ); |
| 518 | |
| 519 | ASSERT_ALLOC( output_data, output_size ); |
| 520 | |
| 521 | verify_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg ); |
| 522 | TEST_ASSERT( verify_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE ); |
| 523 | ASSERT_ALLOC( final_data, verify_output_size ); |
| 524 | |
| 525 | operation = psa_aead_operation_init( ); |
| 526 | |
| 527 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 528 | |
| 529 | /* If the operation is not supported, just skip and not fail in case the |
| 530 | * encryption involves a common limitation of cryptography hardwares and |
| 531 | * an alternative implementation. */ |
| 532 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 533 | { |
| 534 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 535 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 536 | } |
| 537 | |
| 538 | if( status != PSA_SUCCESS ) |
| 539 | { |
| 540 | TEST_EQUAL( status, expected_result_arg ); |
| 541 | goto exit; |
| 542 | } |
| 543 | |
| 544 | status = psa_aead_set_nonce( &operation, nonce->x, nonce->len ); |
| 545 | |
| 546 | if( status != PSA_SUCCESS ) |
| 547 | { |
| 548 | TEST_EQUAL( status, expected_result_arg ); |
| 549 | goto exit; |
| 550 | } |
| 551 | |
| 552 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 553 | if( operation.alg == PSA_ALG_GCM ) |
| 554 | { |
| 555 | status = psa_aead_set_lengths( &operation, additional_data->len, |
| 556 | ( input_data->len - tag_length ) ); |
| 557 | |
| 558 | if( status != PSA_SUCCESS ) |
| 559 | { |
| 560 | TEST_EQUAL( status, expected_result_arg ); |
| 561 | goto exit; |
| 562 | } |
| 563 | } |
| 564 | #endif |
| 565 | |
| 566 | if( ad_part_len != -1 ) |
| 567 | { |
| 568 | part_offset = 0; |
| 569 | |
| 570 | while( part_offset < additional_data->len ) |
| 571 | { |
| 572 | if( additional_data->len - part_offset < ( uint32_t ) ad_part_len ) |
| 573 | { |
| 574 | part_length = additional_data->len - part_offset; |
| 575 | } |
| 576 | else |
| 577 | { |
| 578 | part_length = ad_part_len; |
| 579 | } |
| 580 | |
| 581 | status = psa_aead_update_ad( &operation, |
| 582 | additional_data->x + part_offset, |
| 583 | part_length ); |
| 584 | |
| 585 | if( status != PSA_SUCCESS ) |
| 586 | { |
| 587 | TEST_EQUAL( status, expected_result_arg ); |
| 588 | goto exit; |
| 589 | } |
| 590 | |
| 591 | part_offset += part_length; |
| 592 | } |
| 593 | } |
| 594 | else |
| 595 | { |
| 596 | status = psa_aead_update_ad( &operation, additional_data->x, |
| 597 | additional_data->len ); |
| 598 | |
| 599 | if( status != PSA_SUCCESS ) |
| 600 | { |
| 601 | TEST_EQUAL( status, expected_result_arg ); |
| 602 | goto exit; |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | if( data_part_len != -1 ) |
| 607 | { |
| 608 | /* Pass data in parts */ |
| 609 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 610 | ( size_t ) data_part_len ); |
| 611 | |
| 612 | ASSERT_ALLOC( part_data, part_data_size ); |
| 613 | |
| 614 | part_offset = 0; |
| 615 | |
| 616 | while( part_offset < ( input_data->len - tag_length) ) |
| 617 | { |
| 618 | if( (input_data->len - tag_length - part_offset ) < |
| 619 | ( uint32_t ) data_part_len ) |
| 620 | { |
| 621 | part_length = ( input_data->len - tag_length - part_offset ); |
| 622 | } |
| 623 | else |
| 624 | { |
| 625 | part_length = data_part_len; |
| 626 | } |
| 627 | |
| 628 | status = psa_aead_update( &operation, |
| 629 | ( input_data->x + part_offset ), |
| 630 | part_length, part_data, |
| 631 | part_data_size, &output_part_length ); |
| 632 | |
| 633 | if( status != PSA_SUCCESS ) |
| 634 | { |
| 635 | TEST_EQUAL( status, expected_result_arg ); |
| 636 | goto exit; |
| 637 | } |
| 638 | |
| 639 | if( output_data && output_part_length ) |
| 640 | { |
| 641 | memcpy( ( output_data + part_offset ), part_data, |
| 642 | output_part_length ); |
| 643 | } |
| 644 | |
| 645 | part_offset += part_length; |
| 646 | output_length += output_part_length; |
| 647 | } |
| 648 | } |
| 649 | else |
| 650 | { |
| 651 | status = psa_aead_update( &operation, input_data->x, |
| 652 | ( input_data->len - tag_length ), output_data, |
| 653 | output_size, &output_length ); |
| 654 | |
| 655 | if( status != PSA_SUCCESS ) |
| 656 | { |
| 657 | TEST_EQUAL( status, expected_result_arg ); |
| 658 | goto exit; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | status = psa_aead_verify( &operation, final_data, |
| 663 | verify_output_size, |
| 664 | &output_part_length, |
| 665 | ( input_data->x + input_data->len - tag_length ), |
| 666 | tag_length ); |
| 667 | |
| 668 | if( status != PSA_SUCCESS ) |
| 669 | { |
| 670 | TEST_EQUAL( status, expected_result_arg ); |
| 671 | goto exit; |
| 672 | } |
| 673 | |
| 674 | if( output_data && output_part_length ) |
| 675 | { |
| 676 | memcpy( ( output_data + output_length ), final_data, |
| 677 | output_part_length ); |
| 678 | } |
| 679 | |
| 680 | output_length += output_part_length; |
| 681 | |
| 682 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT ) |
| 683 | { |
| 684 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 685 | * should be exact. */ |
| 686 | TEST_EQUAL( output_length, |
| 687 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, |
| 688 | input_data->len ) ); |
| 689 | TEST_ASSERT( output_length <= |
| 690 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 691 | } |
| 692 | |
| 693 | if( expected_result == PSA_SUCCESS ) |
| 694 | { |
| 695 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 696 | output_data, output_length ); |
| 697 | } |
| 698 | |
| 699 | exit: |
| 700 | psa_destroy_key( key ); |
| 701 | psa_aead_abort( &operation ); |
| 702 | mbedtls_free( output_data ); |
| 703 | mbedtls_free( part_data ); |
| 704 | mbedtls_free( final_data ); |
| 705 | PSA_DONE( ); |
| 706 | } |
| 707 | |
| 708 | void aead_multipart_encrypt_decrypt_internal( int key_type_arg, |
| 709 | data_t *key_data, |
| 710 | int alg_arg, |
| 711 | data_t *nonce, |
| 712 | data_t *additional_data, |
| 713 | int ad_part_len, |
| 714 | data_t *input_data, |
| 715 | int data_part_len, |
| 716 | int expected_status_arg ) |
| 717 | { |
| 718 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 719 | psa_key_type_t key_type = key_type_arg; |
| 720 | psa_algorithm_t alg = alg_arg; |
| 721 | psa_aead_operation_t operation; |
| 722 | unsigned char *output_data = NULL; |
| 723 | unsigned char *part_data = NULL; |
| 724 | unsigned char *final_data = NULL; |
| 725 | size_t part_data_size; |
| 726 | size_t output_size = 0; |
| 727 | size_t finish_output_size = 0; |
| 728 | size_t output_length = 0; |
| 729 | unsigned char *output_data2 = NULL; |
| 730 | size_t output_size2 = 0; |
| 731 | size_t output_length2 = 0; |
| 732 | size_t key_bits = 0; |
| 733 | size_t tag_length = 0; |
| 734 | size_t tag_size = 0; |
| 735 | size_t nonce_length = 0; |
| 736 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 737 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 738 | uint32_t part_offset = 0; |
| 739 | size_t part_length = 0; |
| 740 | size_t output_part_length = 0; |
| 741 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 742 | psa_status_t expected_status = expected_status_arg; |
| 743 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 744 | |
| 745 | PSA_ASSERT( psa_crypto_init( ) ); |
| 746 | |
| 747 | psa_set_key_usage_flags( &attributes, |
| 748 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 749 | psa_set_key_algorithm( &attributes, alg ); |
| 750 | psa_set_key_type( &attributes, key_type ); |
| 751 | |
| 752 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 753 | &key ) ); |
| 754 | |
| 755 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 756 | key_bits = psa_get_key_bits( &attributes ); |
| 757 | |
| 758 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 759 | |
| 760 | TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE ); |
| 761 | |
| 762 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 763 | |
| 764 | ASSERT_ALLOC( output_data, output_size ); |
| 765 | |
| 766 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 767 | |
| 768 | TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
| 769 | |
| 770 | ASSERT_ALLOC( final_data, finish_output_size ); |
| 771 | |
| 772 | operation = psa_aead_operation_init( ); |
| 773 | |
| 774 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 775 | |
| 776 | /* If the operation is not supported, just skip and not fail in case the |
| 777 | * encryption involves a common limitation of cryptography hardwares and |
| 778 | * an alternative implementation. */ |
| 779 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 780 | { |
| 781 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 782 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 783 | } |
| 784 | |
| 785 | if( status != PSA_SUCCESS ) |
| 786 | { |
| 787 | TEST_EQUAL( status, expected_status ); |
| 788 | goto exit; |
| 789 | } |
| 790 | |
| 791 | nonce_length = nonce->len; |
| 792 | status = psa_aead_set_nonce( &operation, nonce->x, nonce->len ); |
| 793 | |
| 794 | if( status != PSA_SUCCESS ) |
| 795 | { |
| 796 | TEST_EQUAL( status, expected_status ); |
| 797 | goto exit; |
| 798 | } |
| 799 | |
| 800 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 801 | if( operation.alg == PSA_ALG_GCM ) |
| 802 | { |
| 803 | status = psa_aead_set_lengths( &operation, additional_data->len, |
| 804 | input_data->len ); |
| 805 | |
| 806 | if( status != PSA_SUCCESS ) |
| 807 | { |
| 808 | TEST_EQUAL( status, expected_status ); |
| 809 | goto exit; |
| 810 | } |
| 811 | } |
| 812 | #endif |
| 813 | |
| 814 | if( ad_part_len != -1 ) |
| 815 | { |
| 816 | part_offset = 0; |
| 817 | |
| 818 | while( part_offset < additional_data->len ) |
| 819 | { |
| 820 | if( additional_data->len - part_offset < ( uint32_t ) ad_part_len ) |
| 821 | { |
| 822 | part_length = additional_data->len - part_offset; |
| 823 | } |
| 824 | else |
| 825 | { |
| 826 | part_length = ad_part_len; |
| 827 | } |
| 828 | |
| 829 | status = psa_aead_update_ad( &operation, |
| 830 | additional_data->x + part_offset, |
| 831 | part_length ); |
| 832 | |
| 833 | if( status != PSA_SUCCESS ) |
| 834 | { |
| 835 | TEST_EQUAL( status, expected_status ); |
| 836 | goto exit; |
| 837 | } |
| 838 | |
| 839 | part_offset += part_length; |
| 840 | } |
| 841 | } |
| 842 | else |
| 843 | { |
| 844 | status = psa_aead_update_ad( &operation, additional_data->x, |
| 845 | additional_data->len ); |
| 846 | |
| 847 | if( status != PSA_SUCCESS ) |
| 848 | { |
| 849 | TEST_EQUAL( status, expected_status ); |
| 850 | goto exit; |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | if( data_part_len != -1 ) |
| 855 | { |
| 856 | /* Pass data in parts */ |
| 857 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 858 | ( size_t ) data_part_len ); |
| 859 | |
| 860 | ASSERT_ALLOC( part_data, part_data_size ); |
| 861 | |
| 862 | part_offset = 0; |
| 863 | |
| 864 | while( part_offset < input_data->len ) |
| 865 | { |
| 866 | if( input_data->len - part_offset < ( uint32_t ) data_part_len ) |
| 867 | { |
| 868 | part_length = input_data->len - part_offset; |
| 869 | } |
| 870 | else |
| 871 | { |
| 872 | part_length = data_part_len; |
| 873 | } |
| 874 | |
| 875 | status = psa_aead_update( &operation, |
| 876 | ( input_data->x + part_offset ), |
| 877 | part_length, part_data, |
| 878 | part_data_size, &output_part_length ); |
| 879 | |
| 880 | if( status != PSA_SUCCESS ) |
| 881 | { |
| 882 | TEST_EQUAL( status, expected_status ); |
| 883 | goto exit; |
| 884 | } |
| 885 | |
| 886 | if( output_data && output_part_length ) |
| 887 | { |
| 888 | memcpy( ( output_data + part_offset ), part_data, |
| 889 | output_part_length ); |
| 890 | } |
| 891 | |
| 892 | part_offset += part_length; |
| 893 | output_length += output_part_length; |
| 894 | } |
| 895 | } |
| 896 | else |
| 897 | { |
| 898 | status = psa_aead_update( &operation, input_data->x, |
| 899 | input_data->len, output_data, |
| 900 | output_size, &output_length ); |
| 901 | |
| 902 | if( status != PSA_SUCCESS ) |
| 903 | { |
| 904 | TEST_EQUAL( status, expected_status ); |
| 905 | goto exit; |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | status = psa_aead_finish( &operation, final_data, |
| 910 | finish_output_size, |
| 911 | &output_part_length, |
| 912 | tag_buffer, tag_length, |
| 913 | &tag_size ); |
| 914 | |
| 915 | if( status != PSA_SUCCESS ) |
| 916 | { |
| 917 | TEST_EQUAL( status, expected_status ); |
| 918 | goto exit; |
| 919 | } |
| 920 | |
| 921 | if( output_data && output_part_length ) |
| 922 | { |
| 923 | memcpy( ( output_data + output_length ), final_data, |
| 924 | output_part_length ); |
| 925 | } |
| 926 | |
| 927 | output_length += output_part_length; |
| 928 | |
| 929 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 930 | * should be exact. */ |
| 931 | if( expected_status != PSA_ERROR_INVALID_ARGUMENT ) |
| 932 | { |
| 933 | TEST_EQUAL( ( output_length + tag_length ), |
| 934 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 935 | input_data->len ) ); |
| 936 | } |
| 937 | |
| 938 | TEST_EQUAL( tag_length, tag_size ); |
| 939 | |
| 940 | if( PSA_SUCCESS == expected_status ) |
| 941 | { |
| 942 | output_size2 = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 943 | output_length ); |
| 944 | ASSERT_ALLOC( output_data2, output_size2 ); |
| 945 | |
| 946 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 947 | * should be exact. */ |
| 948 | TEST_EQUAL( input_data->len, |
| 949 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, |
| 950 | ( output_length + |
| 951 | tag_length ) ) ); |
| 952 | |
| 953 | TEST_ASSERT( input_data->len <= |
| 954 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length + |
| 955 | tag_length ) ); |
| 956 | |
| 957 | operation = psa_aead_operation_init( ); |
| 958 | |
| 959 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 960 | |
| 961 | /* If the operation is not supported, just skip and not fail in case the |
| 962 | * encryption involves a common limitation of cryptography hardwares and |
| 963 | * an alternative implementation. */ |
| 964 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 965 | { |
| 966 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 967 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, |
| 968 | nonce->len ); |
| 969 | } |
| 970 | |
| 971 | TEST_EQUAL( status, expected_status ); |
| 972 | |
| 973 | if( nonce->len == 0 ) |
| 974 | { |
| 975 | /* Use previously generated nonce. */ |
| 976 | status = psa_aead_set_nonce( &operation, nonce_buffer, |
| 977 | nonce_length ); |
| 978 | } |
| 979 | else |
| 980 | { |
| 981 | nonce_length = nonce->len; |
| 982 | status = psa_aead_set_nonce( &operation, nonce->x, nonce->len ); |
| 983 | } |
| 984 | |
| 985 | if( status != PSA_SUCCESS ) |
| 986 | { |
| 987 | TEST_EQUAL( status, expected_status); |
| 988 | } |
| 989 | |
| 990 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 991 | if( operation.alg == PSA_ALG_GCM ) |
| 992 | { |
| 993 | status = psa_aead_set_lengths( &operation, additional_data->len, |
| 994 | output_length ); |
| 995 | |
| 996 | if( status != PSA_SUCCESS ) |
| 997 | { |
| 998 | TEST_EQUAL( status, expected_status ); |
| 999 | } |
| 1000 | } |
| 1001 | #endif |
| 1002 | |
| 1003 | if( ad_part_len != -1 ) |
| 1004 | { |
| 1005 | part_offset = 0; |
| 1006 | |
| 1007 | while( part_offset < additional_data->len ) |
| 1008 | { |
| 1009 | if( additional_data->len - part_offset < |
| 1010 | ( uint32_t ) ad_part_len ) |
| 1011 | { |
| 1012 | part_length = additional_data->len - part_offset; |
| 1013 | } |
| 1014 | else |
| 1015 | { |
| 1016 | part_length = ad_part_len; |
| 1017 | } |
| 1018 | |
| 1019 | PSA_ASSERT( psa_aead_update_ad( &operation, |
| 1020 | additional_data->x + |
| 1021 | part_offset, |
| 1022 | part_length ) ); |
| 1023 | |
| 1024 | part_offset += part_length; |
| 1025 | } |
| 1026 | } |
| 1027 | else |
| 1028 | { |
| 1029 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 1030 | additional_data->len ) ); |
| 1031 | } |
| 1032 | |
| 1033 | if( data_part_len != -1 ) |
| 1034 | { |
| 1035 | /* Pass data in parts */ |
| 1036 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 1037 | ( size_t ) data_part_len ); |
| 1038 | |
| 1039 | part_data = NULL; |
| 1040 | ASSERT_ALLOC( part_data, part_data_size ); |
| 1041 | |
| 1042 | part_offset = 0; |
| 1043 | |
| 1044 | while( part_offset < output_length ) |
| 1045 | { |
| 1046 | if( ( output_length - part_offset ) < |
| 1047 | ( uint32_t ) data_part_len ) |
| 1048 | { |
| 1049 | part_length = ( output_length - part_offset ); |
| 1050 | } |
| 1051 | else |
| 1052 | { |
| 1053 | part_length = data_part_len; |
| 1054 | } |
| 1055 | |
| 1056 | PSA_ASSERT( psa_aead_update( &operation, |
| 1057 | ( output_data + part_offset ), |
| 1058 | part_length, part_data, |
| 1059 | part_data_size, |
| 1060 | &output_part_length ) ); |
| 1061 | |
| 1062 | if( output_data2 && output_part_length ) |
| 1063 | { |
| 1064 | memcpy( ( output_data2 + part_offset ), |
| 1065 | part_data, output_part_length ); |
| 1066 | } |
| 1067 | |
| 1068 | part_offset += part_length; |
| 1069 | output_length2 += output_part_length; |
| 1070 | } |
| 1071 | } |
| 1072 | else |
| 1073 | { |
| 1074 | PSA_ASSERT( psa_aead_update( &operation, output_data, |
| 1075 | output_length, output_data2, |
| 1076 | output_size2, &output_length2 ) ); |
| 1077 | } |
| 1078 | |
| 1079 | PSA_ASSERT( psa_aead_verify( &operation, final_data, |
| 1080 | finish_output_size, |
| 1081 | &output_part_length, |
| 1082 | tag_buffer, tag_length ) ); |
| 1083 | |
| 1084 | if( output_data2 && output_part_length ) |
| 1085 | { |
| 1086 | memcpy( ( output_data2 + output_length2 ), final_data, |
| 1087 | output_part_length ); |
| 1088 | } |
| 1089 | |
| 1090 | output_length2 += output_part_length; |
| 1091 | |
| 1092 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 1093 | output_data2, output_length2 ); |
| 1094 | } |
| 1095 | |
| 1096 | exit: |
| 1097 | psa_destroy_key( key ); |
| 1098 | psa_aead_abort( &operation ); |
| 1099 | mbedtls_free( output_data ); |
| 1100 | mbedtls_free( output_data2 ); |
| 1101 | mbedtls_free( part_data ); |
| 1102 | mbedtls_free( final_data ); |
| 1103 | PSA_DONE( ); |
| 1104 | } |
| 1105 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1106 | /* END_HEADER */ |
| 1107 | |
| 1108 | /* BEGIN_DEPENDENCIES |
| 1109 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1110 | * END_DEPENDENCIES |
| 1111 | */ |
| 1112 | |
| 1113 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1114 | void static_checks( ) |
| 1115 | { |
| 1116 | size_t max_truncated_mac_size = |
| 1117 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1118 | |
| 1119 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1120 | * encoding. The shifted mask is the maximum truncated value. The |
| 1121 | * untruncated algorithm may be one byte larger. */ |
| 1122 | TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size ); |
| 1123 | } |
| 1124 | /* END_CASE */ |
| 1125 | |
| 1126 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1127 | void import_with_policy( int type_arg, |
| 1128 | int usage_arg, int alg_arg, |
| 1129 | int expected_status_arg ) |
| 1130 | { |
| 1131 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1132 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1133 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1134 | psa_key_type_t type = type_arg; |
| 1135 | psa_key_usage_t usage = usage_arg; |
| 1136 | psa_algorithm_t alg = alg_arg; |
| 1137 | psa_status_t expected_status = expected_status_arg; |
| 1138 | const uint8_t key_material[16] = {0}; |
| 1139 | psa_status_t status; |
| 1140 | |
| 1141 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1142 | |
| 1143 | psa_set_key_type( &attributes, type ); |
| 1144 | psa_set_key_usage_flags( &attributes, usage ); |
| 1145 | psa_set_key_algorithm( &attributes, alg ); |
| 1146 | |
| 1147 | status = psa_import_key( &attributes, |
| 1148 | key_material, sizeof( key_material ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1149 | &key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1150 | TEST_EQUAL( status, expected_status ); |
| 1151 | if( status != PSA_SUCCESS ) |
| 1152 | goto exit; |
| 1153 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1154 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1155 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1156 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 1157 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1158 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1159 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1160 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1161 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1162 | |
| 1163 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1164 | /* |
| 1165 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1166 | * thus reset them as required. |
| 1167 | */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1168 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1169 | |
| 1170 | psa_destroy_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1171 | PSA_DONE( ); |
| 1172 | } |
| 1173 | /* END_CASE */ |
| 1174 | |
| 1175 | /* BEGIN_CASE */ |
| 1176 | void import_with_data( data_t *data, int type_arg, |
| 1177 | int attr_bits_arg, |
| 1178 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1179 | { |
| 1180 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1181 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1182 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1183 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1184 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1185 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1186 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1187 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1188 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1189 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1190 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1191 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1192 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1193 | status = psa_import_key( &attributes, data->x, data->len, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1194 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1195 | if( status != PSA_SUCCESS ) |
| 1196 | goto exit; |
| 1197 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1198 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1199 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1200 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1201 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1202 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1203 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1204 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1205 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1206 | |
| 1207 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1208 | /* |
| 1209 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1210 | * thus reset them as required. |
| 1211 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1212 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1213 | |
| 1214 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1215 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1216 | } |
| 1217 | /* END_CASE */ |
| 1218 | |
| 1219 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1220 | void import_large_key( int type_arg, int byte_size_arg, |
| 1221 | int expected_status_arg ) |
| 1222 | { |
| 1223 | psa_key_type_t type = type_arg; |
| 1224 | size_t byte_size = byte_size_arg; |
| 1225 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1226 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1227 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1228 | psa_status_t status; |
| 1229 | uint8_t *buffer = NULL; |
| 1230 | size_t buffer_size = byte_size + 1; |
| 1231 | size_t n; |
| 1232 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1233 | /* Skip the test case if the target running the test cannot |
| 1234 | * accomodate large keys due to heap size constraints */ |
| 1235 | ASSERT_ALLOC_WEAK( buffer, buffer_size ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1236 | memset( buffer, 'K', byte_size ); |
| 1237 | |
| 1238 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1239 | |
| 1240 | /* Try importing the key */ |
| 1241 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1242 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1243 | status = psa_import_key( &attributes, buffer, byte_size, &key ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 1244 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1245 | TEST_EQUAL( status, expected_status ); |
| 1246 | |
| 1247 | if( status == PSA_SUCCESS ) |
| 1248 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1249 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1250 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 1251 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 1252 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1253 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1254 | memset( buffer, 0, byte_size + 1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1255 | PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1256 | for( n = 0; n < byte_size; n++ ) |
| 1257 | TEST_EQUAL( buffer[n], 'K' ); |
| 1258 | for( n = byte_size; n < buffer_size; n++ ) |
| 1259 | TEST_EQUAL( buffer[n], 0 ); |
| 1260 | } |
| 1261 | |
| 1262 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1263 | /* |
| 1264 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1265 | * thus reset them as required. |
| 1266 | */ |
| 1267 | psa_reset_key_attributes( &attributes ); |
| 1268 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1269 | psa_destroy_key( key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1270 | PSA_DONE( ); |
| 1271 | mbedtls_free( buffer ); |
| 1272 | } |
| 1273 | /* END_CASE */ |
| 1274 | |
| 1275 | /* BEGIN_CASE */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1276 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 1277 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1278 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1279 | size_t bits = bits_arg; |
| 1280 | psa_status_t expected_status = expected_status_arg; |
| 1281 | psa_status_t status; |
| 1282 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1283 | 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] | 1284 | size_t buffer_size = /* Slight overapproximations */ |
| 1285 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1286 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1287 | unsigned char *p; |
| 1288 | int ret; |
| 1289 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1290 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1291 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1292 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1293 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1294 | |
| 1295 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 1296 | bits, keypair ) ) >= 0 ); |
| 1297 | length = ret; |
| 1298 | |
| 1299 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1300 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1301 | status = psa_import_key( &attributes, p, length, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1302 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1303 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1304 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1305 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1306 | |
| 1307 | exit: |
| 1308 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1309 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1310 | } |
| 1311 | /* END_CASE */ |
| 1312 | |
| 1313 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1314 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 1315 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 1316 | int usage_arg, int alg_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1317 | int expected_bits, |
| 1318 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1319 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1320 | int canonical_input ) |
| 1321 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1322 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1323 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1324 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1325 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1326 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1327 | unsigned char *exported = NULL; |
| 1328 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1329 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1330 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1331 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1332 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1333 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1334 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1335 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1336 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1337 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1338 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1339 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1340 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1341 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 1342 | psa_set_key_algorithm( &attributes, alg ); |
| 1343 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1344 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1345 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1346 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1347 | |
| 1348 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1349 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1350 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1351 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1352 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1353 | |
| 1354 | /* Export the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1355 | status = psa_export_key( key, exported, export_size, &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1356 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1357 | |
| 1358 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1359 | * and export_size. On errors, the exported length must be 0. */ |
| 1360 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 1361 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
| 1362 | TEST_ASSERT( exported_length <= export_size ); |
| 1363 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1364 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 1365 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1366 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1367 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1368 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1369 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1370 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1371 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1372 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1373 | * this validates the canonical representations. For canonical inputs, |
| 1374 | * this doesn't directly validate the implementation, but it still helps |
| 1375 | * by cross-validating the test data with the sanity check code. */ |
| 1376 | if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) ) |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1377 | goto exit; |
| 1378 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1379 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1380 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1381 | else |
| 1382 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1383 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1384 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1385 | &key2 ) ); |
| 1386 | PSA_ASSERT( psa_export_key( key2, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1387 | reexported, |
| 1388 | export_size, |
| 1389 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1390 | ASSERT_COMPARE( exported, exported_length, |
| 1391 | reexported, reexported_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1392 | PSA_ASSERT( psa_destroy_key( key2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1393 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 1394 | TEST_ASSERT( exported_length <= |
| 1395 | PSA_EXPORT_KEY_OUTPUT_SIZE( type, |
| 1396 | psa_get_key_bits( &got_attributes ) ) ); |
| 1397 | TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1398 | |
| 1399 | destroy: |
| 1400 | /* Destroy the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1401 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1402 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1403 | |
| 1404 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1405 | /* |
| 1406 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1407 | * thus reset them as required. |
| 1408 | */ |
| 1409 | psa_reset_key_attributes( &got_attributes ); |
| 1410 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1411 | mbedtls_free( exported ); |
| 1412 | mbedtls_free( reexported ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1413 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1414 | } |
| 1415 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1416 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1417 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1418 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1419 | int type_arg, |
| 1420 | int alg_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1421 | int export_size_delta, |
| 1422 | int expected_export_status_arg, |
| 1423 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1424 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1425 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1426 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1427 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1428 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1429 | psa_status_t status; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1430 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1431 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1432 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1433 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1434 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1435 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1436 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1437 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1438 | psa_set_key_algorithm( &attributes, alg ); |
| 1439 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1440 | |
| 1441 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1442 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1443 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1444 | /* Export the public key */ |
| 1445 | ASSERT_ALLOC( exported, export_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1446 | status = psa_export_public_key( key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1447 | exported, export_size, |
| 1448 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1449 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1450 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1451 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1452 | 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] | 1453 | size_t bits; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1454 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1455 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1456 | TEST_ASSERT( expected_public_key->len <= |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1457 | PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 1458 | TEST_ASSERT( expected_public_key->len <= |
| 1459 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 1460 | TEST_ASSERT( expected_public_key->len <= |
| 1461 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1462 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 1463 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1464 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1465 | |
| 1466 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1467 | /* |
| 1468 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1469 | * thus reset them as required. |
| 1470 | */ |
| 1471 | psa_reset_key_attributes( &attributes ); |
| 1472 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1473 | mbedtls_free( exported ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1474 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1475 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1476 | } |
| 1477 | /* END_CASE */ |
| 1478 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1479 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1480 | void import_and_exercise_key( data_t *data, |
| 1481 | int type_arg, |
| 1482 | int bits_arg, |
| 1483 | int alg_arg ) |
| 1484 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1485 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1486 | psa_key_type_t type = type_arg; |
| 1487 | size_t bits = bits_arg; |
| 1488 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1489 | 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] | 1490 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1491 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1492 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1493 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1494 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1495 | psa_set_key_usage_flags( &attributes, usage ); |
| 1496 | psa_set_key_algorithm( &attributes, alg ); |
| 1497 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1498 | |
| 1499 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1500 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1501 | |
| 1502 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1503 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1504 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1505 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1506 | |
| 1507 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1508 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1509 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1510 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1511 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1512 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1513 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1514 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1515 | /* |
| 1516 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1517 | * thus reset them as required. |
| 1518 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1519 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1520 | |
| 1521 | psa_reset_key_attributes( &attributes ); |
| 1522 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1523 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1524 | } |
| 1525 | /* END_CASE */ |
| 1526 | |
| 1527 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1528 | void effective_key_attributes( int type_arg, int expected_type_arg, |
| 1529 | int bits_arg, int expected_bits_arg, |
| 1530 | int usage_arg, int expected_usage_arg, |
| 1531 | int alg_arg, int expected_alg_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1532 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1533 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1534 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1535 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1536 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1537 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1538 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1539 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1540 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1541 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1542 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1543 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1544 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1545 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1546 | psa_set_key_usage_flags( &attributes, usage ); |
| 1547 | psa_set_key_algorithm( &attributes, alg ); |
| 1548 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1549 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1550 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1551 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1552 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1553 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1554 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1555 | TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type ); |
| 1556 | TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits ); |
| 1557 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1558 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1559 | |
| 1560 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1561 | /* |
| 1562 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1563 | * thus reset them as required. |
| 1564 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1565 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1566 | |
| 1567 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1568 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1569 | } |
| 1570 | /* END_CASE */ |
| 1571 | |
| 1572 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1573 | void check_key_policy( int type_arg, int bits_arg, |
| 1574 | int usage_arg, int alg_arg ) |
| 1575 | { |
| 1576 | test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg, |
| 1577 | usage_arg, usage_arg, alg_arg, alg_arg ); |
| 1578 | goto exit; |
| 1579 | } |
| 1580 | /* END_CASE */ |
| 1581 | |
| 1582 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1583 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1584 | { |
| 1585 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1586 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1587 | * though it's OK by the C standard. We could test for this, but we'd need |
| 1588 | * to supress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1589 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 1590 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1591 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1592 | |
| 1593 | memset( &zero, 0, sizeof( zero ) ); |
| 1594 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1595 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1596 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1597 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1598 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1599 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 1600 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 1601 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 1602 | |
| 1603 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 1604 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 1605 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 1606 | |
| 1607 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 1608 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 1609 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 1610 | |
| 1611 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 1612 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 1613 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1614 | } |
| 1615 | /* END_CASE */ |
| 1616 | |
| 1617 | /* BEGIN_CASE */ |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1618 | void mac_key_policy( int policy_usage, |
| 1619 | int policy_alg, |
| 1620 | int key_type, |
| 1621 | data_t *key_data, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1622 | int exercise_alg, |
| 1623 | int expected_status_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1624 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1625 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1626 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1627 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1628 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1629 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1630 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1631 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1632 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1633 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1634 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1635 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1636 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1637 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1638 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1639 | &key ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1640 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1641 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1642 | if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1643 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1644 | else |
| 1645 | TEST_EQUAL( status, expected_status ); |
| 1646 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1647 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1648 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1649 | memset( mac, 0, sizeof( mac ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1650 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1651 | if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1652 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1653 | else |
| 1654 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1655 | |
| 1656 | exit: |
| 1657 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1658 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1659 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1660 | } |
| 1661 | /* END_CASE */ |
| 1662 | |
| 1663 | /* BEGIN_CASE */ |
| 1664 | void cipher_key_policy( int policy_usage, |
| 1665 | int policy_alg, |
| 1666 | int key_type, |
| 1667 | data_t *key_data, |
| 1668 | int exercise_alg ) |
| 1669 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1670 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1671 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1672 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1673 | psa_status_t status; |
| 1674 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1675 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1676 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1677 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1678 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1679 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1680 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1681 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1682 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1683 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1684 | status = psa_cipher_encrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1685 | if( policy_alg == exercise_alg && |
| 1686 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1687 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1688 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1689 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1690 | psa_cipher_abort( &operation ); |
| 1691 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1692 | status = psa_cipher_decrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1693 | if( policy_alg == exercise_alg && |
| 1694 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1695 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1696 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1697 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1698 | |
| 1699 | exit: |
| 1700 | psa_cipher_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1701 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1702 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1703 | } |
| 1704 | /* END_CASE */ |
| 1705 | |
| 1706 | /* BEGIN_CASE */ |
| 1707 | void aead_key_policy( int policy_usage, |
| 1708 | int policy_alg, |
| 1709 | int key_type, |
| 1710 | data_t *key_data, |
| 1711 | int nonce_length_arg, |
| 1712 | int tag_length_arg, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1713 | int exercise_alg, |
| 1714 | int expected_status_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1715 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1716 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1717 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1718 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1719 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1720 | unsigned char nonce[16] = {0}; |
| 1721 | size_t nonce_length = nonce_length_arg; |
| 1722 | unsigned char tag[16]; |
| 1723 | size_t tag_length = tag_length_arg; |
| 1724 | size_t output_length; |
| 1725 | |
| 1726 | TEST_ASSERT( nonce_length <= sizeof( nonce ) ); |
| 1727 | TEST_ASSERT( tag_length <= sizeof( tag ) ); |
| 1728 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1729 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1730 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1731 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1732 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1733 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1734 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1735 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1736 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1737 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1738 | status = psa_aead_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1739 | nonce, nonce_length, |
| 1740 | NULL, 0, |
| 1741 | NULL, 0, |
| 1742 | tag, tag_length, |
| 1743 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1744 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1745 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1746 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1747 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1748 | |
| 1749 | memset( tag, 0, sizeof( tag ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1750 | status = psa_aead_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1751 | nonce, nonce_length, |
| 1752 | NULL, 0, |
| 1753 | tag, tag_length, |
| 1754 | NULL, 0, |
| 1755 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1756 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 1757 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1758 | else if( expected_status == PSA_SUCCESS ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1759 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1760 | else |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1761 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1762 | |
| 1763 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1764 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1765 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1766 | } |
| 1767 | /* END_CASE */ |
| 1768 | |
| 1769 | /* BEGIN_CASE */ |
| 1770 | void asymmetric_encryption_key_policy( int policy_usage, |
| 1771 | int policy_alg, |
| 1772 | int key_type, |
| 1773 | data_t *key_data, |
| 1774 | int exercise_alg ) |
| 1775 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1776 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1777 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1778 | psa_status_t status; |
| 1779 | size_t key_bits; |
| 1780 | size_t buffer_length; |
| 1781 | unsigned char *buffer = NULL; |
| 1782 | size_t output_length; |
| 1783 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1784 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1785 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1786 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1787 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1788 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1789 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1790 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1791 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1792 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1793 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1794 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1795 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 1796 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1797 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1798 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1799 | status = psa_asymmetric_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1800 | NULL, 0, |
| 1801 | NULL, 0, |
| 1802 | buffer, buffer_length, |
| 1803 | &output_length ); |
| 1804 | if( policy_alg == exercise_alg && |
| 1805 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1806 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1807 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1808 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1809 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 1810 | if( buffer_length != 0 ) |
| 1811 | memset( buffer, 0, buffer_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1812 | status = psa_asymmetric_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1813 | buffer, buffer_length, |
| 1814 | NULL, 0, |
| 1815 | buffer, buffer_length, |
| 1816 | &output_length ); |
| 1817 | if( policy_alg == exercise_alg && |
| 1818 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1819 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1820 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1821 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1822 | |
| 1823 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1824 | /* |
| 1825 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1826 | * thus reset them as required. |
| 1827 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1828 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1829 | |
| 1830 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1831 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1832 | mbedtls_free( buffer ); |
| 1833 | } |
| 1834 | /* END_CASE */ |
| 1835 | |
| 1836 | /* BEGIN_CASE */ |
| 1837 | void asymmetric_signature_key_policy( int policy_usage, |
| 1838 | int policy_alg, |
| 1839 | int key_type, |
| 1840 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1841 | int exercise_alg, |
| 1842 | int payload_length_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1843 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1844 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1845 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1846 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1847 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 1848 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 1849 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 1850 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 1851 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 1852 | int compatible_alg = payload_length_arg > 0; |
| 1853 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1854 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1855 | size_t signature_length; |
| 1856 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1857 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1858 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1859 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1860 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1861 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1862 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1863 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1864 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1865 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1866 | status = psa_sign_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1867 | payload, payload_length, |
| 1868 | signature, sizeof( signature ), |
| 1869 | &signature_length ); |
| 1870 | if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1871 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1872 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1873 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1874 | |
| 1875 | memset( signature, 0, sizeof( signature ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1876 | status = psa_verify_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1877 | payload, payload_length, |
| 1878 | signature, sizeof( signature ) ); |
| 1879 | if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1880 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1881 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1882 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1883 | |
| 1884 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1885 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1886 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1887 | } |
| 1888 | /* END_CASE */ |
| 1889 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1890 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1891 | void derive_key_policy( int policy_usage, |
| 1892 | int policy_alg, |
| 1893 | int key_type, |
| 1894 | data_t *key_data, |
| 1895 | int exercise_alg ) |
| 1896 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1897 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1898 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1899 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1900 | psa_status_t status; |
| 1901 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1902 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1903 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1904 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1905 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1906 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1907 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1908 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1909 | &key ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1910 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1911 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 1912 | |
| 1913 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 1914 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1915 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1916 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 1917 | &operation, |
| 1918 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 1919 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1920 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1921 | |
| 1922 | status = psa_key_derivation_input_key( &operation, |
| 1923 | PSA_KEY_DERIVATION_INPUT_SECRET, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1924 | key ); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1925 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1926 | if( policy_alg == exercise_alg && |
| 1927 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1928 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1929 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1930 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1931 | |
| 1932 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1933 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1934 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1935 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1936 | } |
| 1937 | /* END_CASE */ |
| 1938 | |
| 1939 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1940 | void agreement_key_policy( int policy_usage, |
| 1941 | int policy_alg, |
| 1942 | int key_type_arg, |
| 1943 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1944 | int exercise_alg, |
| 1945 | int expected_status_arg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1946 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1947 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1948 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1949 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1950 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1951 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1952 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1953 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1954 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1955 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1956 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1957 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1958 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1959 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1960 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1961 | &key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1962 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1963 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1964 | status = mbedtls_test_psa_key_agreement_with_self( &operation, key ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1965 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1966 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1967 | |
| 1968 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1969 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1970 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1971 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1972 | } |
| 1973 | /* END_CASE */ |
| 1974 | |
| 1975 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1976 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 1977 | int usage_arg, int alg_arg, int alg2_arg ) |
| 1978 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1979 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1980 | psa_key_type_t key_type = key_type_arg; |
| 1981 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1982 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1983 | psa_key_usage_t usage = usage_arg; |
| 1984 | psa_algorithm_t alg = alg_arg; |
| 1985 | psa_algorithm_t alg2 = alg2_arg; |
| 1986 | |
| 1987 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1988 | |
| 1989 | psa_set_key_usage_flags( &attributes, usage ); |
| 1990 | psa_set_key_algorithm( &attributes, alg ); |
| 1991 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 1992 | psa_set_key_type( &attributes, key_type ); |
| 1993 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1994 | &key ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1995 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1996 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1997 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 1998 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 1999 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 2000 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2001 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2002 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2003 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2004 | goto exit; |
| 2005 | |
| 2006 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2007 | /* |
| 2008 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2009 | * thus reset them as required. |
| 2010 | */ |
| 2011 | psa_reset_key_attributes( &got_attributes ); |
| 2012 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2013 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2014 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2015 | } |
| 2016 | /* END_CASE */ |
| 2017 | |
| 2018 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2019 | void raw_agreement_key_policy( int policy_usage, |
| 2020 | int policy_alg, |
| 2021 | int key_type_arg, |
| 2022 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2023 | int exercise_alg, |
| 2024 | int expected_status_arg ) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2025 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2026 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2027 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2028 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2029 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2030 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2031 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2032 | |
| 2033 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2034 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2035 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2036 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2037 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2038 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2039 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2040 | &key ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2041 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2042 | status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2043 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2044 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2045 | |
| 2046 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2047 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2048 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2049 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2050 | } |
| 2051 | /* END_CASE */ |
| 2052 | |
| 2053 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2054 | void copy_success( int source_usage_arg, |
| 2055 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2056 | int type_arg, data_t *material, |
| 2057 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2058 | int target_usage_arg, |
| 2059 | int target_alg_arg, int target_alg2_arg, |
| 2060 | int expected_usage_arg, |
| 2061 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2062 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2063 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2064 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2065 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2066 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2067 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2068 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2069 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2070 | uint8_t *export_buffer = NULL; |
| 2071 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2072 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2073 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2074 | /* Prepare the source key. */ |
| 2075 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2076 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2077 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2078 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2079 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2080 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2081 | &source_key ) ); |
| 2082 | PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2083 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2084 | /* Prepare the target attributes. */ |
| 2085 | if( copy_attributes ) |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2086 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2087 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2088 | /* Set volatile lifetime to reset the key identifier to 0. */ |
| 2089 | psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE ); |
| 2090 | } |
| 2091 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2092 | if( target_usage_arg != -1 ) |
| 2093 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2094 | if( target_alg_arg != -1 ) |
| 2095 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2096 | if( target_alg2_arg != -1 ) |
| 2097 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2098 | |
| 2099 | /* Copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2100 | PSA_ASSERT( psa_copy_key( source_key, |
| 2101 | &target_attributes, &target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2102 | |
| 2103 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2104 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2105 | |
| 2106 | /* Test that the target slot has the expected content and policy. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2107 | PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2108 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 2109 | psa_get_key_type( &target_attributes ) ); |
| 2110 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 2111 | psa_get_key_bits( &target_attributes ) ); |
| 2112 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 2113 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2114 | TEST_EQUAL( expected_alg2, |
| 2115 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2116 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 2117 | { |
| 2118 | size_t length; |
| 2119 | ASSERT_ALLOC( export_buffer, material->len ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2120 | PSA_ASSERT( psa_export_key( target_key, export_buffer, |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2121 | material->len, &length ) ); |
| 2122 | ASSERT_COMPARE( material->x, material->len, |
| 2123 | export_buffer, length ); |
| 2124 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2125 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2126 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2127 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2128 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) ) |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2129 | goto exit; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2130 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2131 | PSA_ASSERT( psa_destroy_key( target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2132 | |
| 2133 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2134 | /* |
| 2135 | * Source and target key attributes may have been returned by |
| 2136 | * psa_get_key_attributes() thus reset them as required. |
| 2137 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2138 | psa_reset_key_attributes( &source_attributes ); |
| 2139 | psa_reset_key_attributes( &target_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2140 | |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2141 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2142 | mbedtls_free( export_buffer ); |
| 2143 | } |
| 2144 | /* END_CASE */ |
| 2145 | |
| 2146 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2147 | void copy_fail( int source_usage_arg, |
| 2148 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2149 | int type_arg, data_t *material, |
| 2150 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2151 | int target_usage_arg, |
| 2152 | int target_alg_arg, int target_alg2_arg, |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2153 | int target_id_arg, int target_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2154 | int expected_status_arg ) |
| 2155 | { |
| 2156 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2157 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2158 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2159 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2160 | 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] | 2161 | |
| 2162 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2163 | |
| 2164 | /* Prepare the source key. */ |
| 2165 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2166 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2167 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2168 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2169 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2170 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2171 | &source_key ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2172 | |
| 2173 | /* Prepare the target attributes. */ |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2174 | psa_set_key_id( &target_attributes, key_id ); |
| 2175 | psa_set_key_lifetime( &target_attributes, target_lifetime_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2176 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 2177 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 2178 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2179 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2180 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2181 | |
| 2182 | /* Try to copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2183 | TEST_EQUAL( psa_copy_key( source_key, |
| 2184 | &target_attributes, &target_key ), |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2185 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2186 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2187 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2188 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2189 | exit: |
| 2190 | psa_reset_key_attributes( &source_attributes ); |
| 2191 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2192 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2193 | } |
| 2194 | /* END_CASE */ |
| 2195 | |
| 2196 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2197 | void hash_operation_init( ) |
| 2198 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2199 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2200 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2201 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2202 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2203 | * to supress the Clang warning for the test. */ |
| 2204 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 2205 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2206 | psa_hash_operation_t zero; |
| 2207 | |
| 2208 | memset( &zero, 0, sizeof( zero ) ); |
| 2209 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2210 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2211 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 2212 | PSA_ERROR_BAD_STATE ); |
| 2213 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 2214 | PSA_ERROR_BAD_STATE ); |
| 2215 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 2216 | PSA_ERROR_BAD_STATE ); |
| 2217 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2218 | /* A default hash operation should be abortable without error. */ |
| 2219 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 2220 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 2221 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2222 | } |
| 2223 | /* END_CASE */ |
| 2224 | |
| 2225 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2226 | void hash_setup( int alg_arg, |
| 2227 | int expected_status_arg ) |
| 2228 | { |
| 2229 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2230 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2231 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2232 | psa_status_t status; |
| 2233 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2234 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2235 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2236 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2237 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2238 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2239 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 2240 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2241 | |
| 2242 | /* If setup failed, reproduce the failure, so as to |
| 2243 | * test the resulting state of the operation object. */ |
| 2244 | if( status != PSA_SUCCESS ) |
| 2245 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 2246 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2247 | /* Now the operation object should be reusable. */ |
| 2248 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 2249 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 2250 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2251 | #endif |
| 2252 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2253 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2254 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2255 | } |
| 2256 | /* END_CASE */ |
| 2257 | |
| 2258 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2259 | void hash_compute_fail( int alg_arg, data_t *input, |
| 2260 | int output_size_arg, int expected_status_arg ) |
| 2261 | { |
| 2262 | psa_algorithm_t alg = alg_arg; |
| 2263 | uint8_t *output = NULL; |
| 2264 | size_t output_size = output_size_arg; |
| 2265 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 2266 | psa_status_t expected_status = expected_status_arg; |
| 2267 | psa_status_t status; |
| 2268 | |
| 2269 | ASSERT_ALLOC( output, output_size ); |
| 2270 | |
| 2271 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2272 | |
| 2273 | status = psa_hash_compute( alg, input->x, input->len, |
| 2274 | output, output_size, &output_length ); |
| 2275 | TEST_EQUAL( status, expected_status ); |
| 2276 | TEST_ASSERT( output_length <= output_size ); |
| 2277 | |
| 2278 | exit: |
| 2279 | mbedtls_free( output ); |
| 2280 | PSA_DONE( ); |
| 2281 | } |
| 2282 | /* END_CASE */ |
| 2283 | |
| 2284 | /* BEGIN_CASE */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2285 | void hash_compare_fail( int alg_arg, data_t *input, |
| 2286 | data_t *reference_hash, |
| 2287 | int expected_status_arg ) |
| 2288 | { |
| 2289 | psa_algorithm_t alg = alg_arg; |
| 2290 | psa_status_t expected_status = expected_status_arg; |
| 2291 | psa_status_t status; |
| 2292 | |
| 2293 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2294 | |
| 2295 | status = psa_hash_compare( alg, input->x, input->len, |
| 2296 | reference_hash->x, reference_hash->len ); |
| 2297 | TEST_EQUAL( status, expected_status ); |
| 2298 | |
| 2299 | exit: |
| 2300 | PSA_DONE( ); |
| 2301 | } |
| 2302 | /* END_CASE */ |
| 2303 | |
| 2304 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2305 | void hash_compute_compare( int alg_arg, data_t *input, |
| 2306 | data_t *expected_output ) |
| 2307 | { |
| 2308 | psa_algorithm_t alg = alg_arg; |
| 2309 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2310 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 2311 | size_t i; |
| 2312 | |
| 2313 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2314 | |
| 2315 | /* Compute with tight buffer */ |
| 2316 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2317 | output, PSA_HASH_LENGTH( alg ), |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2318 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2319 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2320 | ASSERT_COMPARE( output, output_length, |
| 2321 | expected_output->x, expected_output->len ); |
| 2322 | |
| 2323 | /* Compute with larger buffer */ |
| 2324 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
| 2325 | output, sizeof( output ), |
| 2326 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2327 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2328 | ASSERT_COMPARE( output, output_length, |
| 2329 | expected_output->x, expected_output->len ); |
| 2330 | |
| 2331 | /* Compare with correct hash */ |
| 2332 | PSA_ASSERT( psa_hash_compare( alg, input->x, input->len, |
| 2333 | output, output_length ) ); |
| 2334 | |
| 2335 | /* Compare with trailing garbage */ |
| 2336 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2337 | output, output_length + 1 ), |
| 2338 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2339 | |
| 2340 | /* Compare with truncated hash */ |
| 2341 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2342 | output, output_length - 1 ), |
| 2343 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2344 | |
| 2345 | /* Compare with corrupted value */ |
| 2346 | for( i = 0; i < output_length; i++ ) |
| 2347 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2348 | mbedtls_test_set_step( i ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2349 | output[i] ^= 1; |
| 2350 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2351 | output, output_length ), |
| 2352 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2353 | output[i] ^= 1; |
| 2354 | } |
| 2355 | |
| 2356 | exit: |
| 2357 | PSA_DONE( ); |
| 2358 | } |
| 2359 | /* END_CASE */ |
| 2360 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2361 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2362 | void hash_bad_order( ) |
| 2363 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2364 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2365 | unsigned char input[] = ""; |
| 2366 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2367 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2368 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2369 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2370 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2371 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2372 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2373 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2374 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2375 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2376 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2377 | /* Call setup twice in a row. */ |
| 2378 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2379 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 2380 | PSA_ERROR_BAD_STATE ); |
| 2381 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2382 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2383 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2384 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2385 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2386 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2387 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2388 | /* Call update after finish. */ |
| 2389 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2390 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2391 | hash, sizeof( hash ), &hash_len ) ); |
| 2392 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2393 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2394 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2395 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2396 | /* Call verify without calling setup beforehand. */ |
| 2397 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2398 | valid_hash, sizeof( valid_hash ) ), |
| 2399 | PSA_ERROR_BAD_STATE ); |
| 2400 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2401 | |
| 2402 | /* Call verify after finish. */ |
| 2403 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2404 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2405 | hash, sizeof( hash ), &hash_len ) ); |
| 2406 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2407 | valid_hash, sizeof( valid_hash ) ), |
| 2408 | PSA_ERROR_BAD_STATE ); |
| 2409 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2410 | |
| 2411 | /* Call verify twice in a row. */ |
| 2412 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2413 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2414 | valid_hash, sizeof( valid_hash ) ) ); |
| 2415 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2416 | valid_hash, sizeof( valid_hash ) ), |
| 2417 | PSA_ERROR_BAD_STATE ); |
| 2418 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2419 | |
| 2420 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2421 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2422 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2423 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2424 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2425 | |
| 2426 | /* Call finish twice in a row. */ |
| 2427 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2428 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2429 | hash, sizeof( hash ), &hash_len ) ); |
| 2430 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2431 | hash, sizeof( hash ), &hash_len ), |
| 2432 | PSA_ERROR_BAD_STATE ); |
| 2433 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2434 | |
| 2435 | /* Call finish after calling verify. */ |
| 2436 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2437 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2438 | valid_hash, sizeof( valid_hash ) ) ); |
| 2439 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2440 | hash, sizeof( hash ), &hash_len ), |
| 2441 | PSA_ERROR_BAD_STATE ); |
| 2442 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2443 | |
| 2444 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2445 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2446 | } |
| 2447 | /* END_CASE */ |
| 2448 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2449 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2450 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2451 | { |
| 2452 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2453 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2454 | * appended to it */ |
| 2455 | unsigned char hash[] = { |
| 2456 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2457 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2458 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2459 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2460 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2461 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2462 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2463 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2464 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2465 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2466 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2467 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2468 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2469 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2470 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2471 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2472 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2473 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2474 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2475 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2476 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2477 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2478 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2479 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2480 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2481 | } |
| 2482 | /* END_CASE */ |
| 2483 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2484 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2485 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2486 | { |
| 2487 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2488 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2489 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2490 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2491 | size_t hash_len; |
| 2492 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2493 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2494 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2495 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2496 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2497 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2498 | hash, expected_size - 1, &hash_len ), |
| 2499 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2500 | |
| 2501 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2502 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2503 | } |
| 2504 | /* END_CASE */ |
| 2505 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2506 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2507 | void hash_clone_source_state( ) |
| 2508 | { |
| 2509 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2510 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2511 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 2512 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2513 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2514 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2515 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2516 | size_t hash_len; |
| 2517 | |
| 2518 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2519 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 2520 | |
| 2521 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2522 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2523 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2524 | hash, sizeof( hash ), &hash_len ) ); |
| 2525 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2526 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2527 | |
| 2528 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 2529 | PSA_ERROR_BAD_STATE ); |
| 2530 | |
| 2531 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 2532 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 2533 | hash, sizeof( hash ), &hash_len ) ); |
| 2534 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 2535 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2536 | hash, sizeof( hash ), &hash_len ) ); |
| 2537 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 2538 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 2539 | hash, sizeof( hash ), &hash_len ) ); |
| 2540 | |
| 2541 | exit: |
| 2542 | psa_hash_abort( &op_source ); |
| 2543 | psa_hash_abort( &op_init ); |
| 2544 | psa_hash_abort( &op_setup ); |
| 2545 | psa_hash_abort( &op_finished ); |
| 2546 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2547 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2548 | } |
| 2549 | /* END_CASE */ |
| 2550 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2551 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2552 | void hash_clone_target_state( ) |
| 2553 | { |
| 2554 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2555 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2556 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2557 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2558 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2559 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2560 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 2561 | size_t hash_len; |
| 2562 | |
| 2563 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2564 | |
| 2565 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2566 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2567 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2568 | hash, sizeof( hash ), &hash_len ) ); |
| 2569 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2570 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2571 | |
| 2572 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 2573 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 2574 | hash, sizeof( hash ), &hash_len ) ); |
| 2575 | |
| 2576 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 2577 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 2578 | PSA_ERROR_BAD_STATE ); |
| 2579 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 2580 | PSA_ERROR_BAD_STATE ); |
| 2581 | |
| 2582 | exit: |
| 2583 | psa_hash_abort( &op_target ); |
| 2584 | psa_hash_abort( &op_init ); |
| 2585 | psa_hash_abort( &op_setup ); |
| 2586 | psa_hash_abort( &op_finished ); |
| 2587 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2588 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2589 | } |
| 2590 | /* END_CASE */ |
| 2591 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2592 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2593 | void mac_operation_init( ) |
| 2594 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2595 | const uint8_t input[1] = { 0 }; |
| 2596 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2597 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2598 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2599 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2600 | * to supress the Clang warning for the test. */ |
| 2601 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 2602 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 2603 | psa_mac_operation_t zero; |
| 2604 | |
| 2605 | memset( &zero, 0, sizeof( zero ) ); |
| 2606 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2607 | /* A freshly-initialized MAC operation should not be usable. */ |
| 2608 | TEST_EQUAL( psa_mac_update( &func, |
| 2609 | input, sizeof( input ) ), |
| 2610 | PSA_ERROR_BAD_STATE ); |
| 2611 | TEST_EQUAL( psa_mac_update( &init, |
| 2612 | input, sizeof( input ) ), |
| 2613 | PSA_ERROR_BAD_STATE ); |
| 2614 | TEST_EQUAL( psa_mac_update( &zero, |
| 2615 | input, sizeof( input ) ), |
| 2616 | PSA_ERROR_BAD_STATE ); |
| 2617 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2618 | /* A default MAC operation should be abortable without error. */ |
| 2619 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 2620 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 2621 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2622 | } |
| 2623 | /* END_CASE */ |
| 2624 | |
| 2625 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2626 | void mac_setup( int key_type_arg, |
| 2627 | data_t *key, |
| 2628 | int alg_arg, |
| 2629 | int expected_status_arg ) |
| 2630 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2631 | psa_key_type_t key_type = key_type_arg; |
| 2632 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2633 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2634 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2635 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 2636 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2637 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2638 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2639 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2640 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2641 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2642 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 2643 | &operation, &status ) ) |
| 2644 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2645 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2646 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2647 | /* The operation object should be reusable. */ |
| 2648 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2649 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 2650 | smoke_test_key_data, |
| 2651 | sizeof( smoke_test_key_data ), |
| 2652 | KNOWN_SUPPORTED_MAC_ALG, |
| 2653 | &operation, &status ) ) |
| 2654 | goto exit; |
| 2655 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2656 | #endif |
| 2657 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2658 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2659 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2660 | } |
| 2661 | /* END_CASE */ |
| 2662 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2663 | /* 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] | 2664 | void mac_bad_order( ) |
| 2665 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2666 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2667 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 2668 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2669 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2670 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2671 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2672 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2673 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2674 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 2675 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 2676 | size_t sign_mac_length = 0; |
| 2677 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 2678 | const uint8_t verify_mac[] = { |
| 2679 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 2680 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 2681 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 2682 | |
| 2683 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2684 | 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] | 2685 | psa_set_key_algorithm( &attributes, alg ); |
| 2686 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2687 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2688 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 2689 | &key ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2690 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2691 | /* Call update without calling setup beforehand. */ |
| 2692 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2693 | PSA_ERROR_BAD_STATE ); |
| 2694 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2695 | |
| 2696 | /* Call sign finish without calling setup beforehand. */ |
| 2697 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 2698 | &sign_mac_length), |
| 2699 | PSA_ERROR_BAD_STATE ); |
| 2700 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2701 | |
| 2702 | /* Call verify finish without calling setup beforehand. */ |
| 2703 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2704 | verify_mac, sizeof( verify_mac ) ), |
| 2705 | PSA_ERROR_BAD_STATE ); |
| 2706 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2707 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2708 | /* Call setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2709 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
| 2710 | TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2711 | PSA_ERROR_BAD_STATE ); |
| 2712 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2713 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2714 | /* Call update after sign finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2715 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2716 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2717 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2718 | sign_mac, sizeof( sign_mac ), |
| 2719 | &sign_mac_length ) ); |
| 2720 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2721 | PSA_ERROR_BAD_STATE ); |
| 2722 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2723 | |
| 2724 | /* Call update after verify finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2725 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2726 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2727 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2728 | verify_mac, sizeof( verify_mac ) ) ); |
| 2729 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2730 | PSA_ERROR_BAD_STATE ); |
| 2731 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2732 | |
| 2733 | /* Call sign finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2734 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2735 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2736 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2737 | sign_mac, sizeof( sign_mac ), |
| 2738 | &sign_mac_length ) ); |
| 2739 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2740 | sign_mac, sizeof( sign_mac ), |
| 2741 | &sign_mac_length ), |
| 2742 | PSA_ERROR_BAD_STATE ); |
| 2743 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2744 | |
| 2745 | /* Call verify finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2746 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2747 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2748 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2749 | verify_mac, sizeof( verify_mac ) ) ); |
| 2750 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2751 | verify_mac, sizeof( verify_mac ) ), |
| 2752 | PSA_ERROR_BAD_STATE ); |
| 2753 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2754 | |
| 2755 | /* Setup sign but try verify. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2756 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2757 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2758 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2759 | verify_mac, sizeof( verify_mac ) ), |
| 2760 | PSA_ERROR_BAD_STATE ); |
| 2761 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2762 | |
| 2763 | /* Setup verify but try sign. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2764 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2765 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2766 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2767 | sign_mac, sizeof( sign_mac ), |
| 2768 | &sign_mac_length ), |
| 2769 | PSA_ERROR_BAD_STATE ); |
| 2770 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2771 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2772 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2773 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2774 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2775 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2776 | } |
| 2777 | /* END_CASE */ |
| 2778 | |
| 2779 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2780 | void mac_sign( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2781 | data_t *key_data, |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2782 | int alg_arg, |
| 2783 | data_t *input, |
| 2784 | data_t *expected_mac ) |
| 2785 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2786 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2787 | psa_key_type_t key_type = key_type_arg; |
| 2788 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2789 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2790 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2791 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2792 | size_t mac_buffer_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2793 | 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] | 2794 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2795 | const size_t output_sizes_to_test[] = { |
| 2796 | 0, |
| 2797 | 1, |
| 2798 | expected_mac->len - 1, |
| 2799 | expected_mac->len, |
| 2800 | expected_mac->len + 1, |
| 2801 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2802 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2803 | TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2804 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 3d404d6 | 2020-08-25 23:47:36 +0200 | [diff] [blame] | 2805 | TEST_ASSERT( expected_mac->len == mac_buffer_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2806 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2807 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2808 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2809 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2810 | psa_set_key_algorithm( &attributes, alg ); |
| 2811 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2812 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2813 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2814 | &key ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2815 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2816 | for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ ) |
| 2817 | { |
| 2818 | const size_t output_size = output_sizes_to_test[i]; |
| 2819 | psa_status_t expected_status = |
| 2820 | ( output_size >= expected_mac->len ? PSA_SUCCESS : |
| 2821 | PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2822 | |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2823 | mbedtls_test_set_step( output_size ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2824 | ASSERT_ALLOC( actual_mac, output_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2825 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2826 | /* Calculate the MAC. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2827 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2828 | PSA_ASSERT( psa_mac_update( &operation, |
| 2829 | input->x, input->len ) ); |
| 2830 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2831 | actual_mac, output_size, |
| 2832 | &mac_length ), |
| 2833 | expected_status ); |
| 2834 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2835 | |
| 2836 | if( expected_status == PSA_SUCCESS ) |
| 2837 | { |
| 2838 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 2839 | actual_mac, mac_length ); |
| 2840 | } |
| 2841 | mbedtls_free( actual_mac ); |
| 2842 | actual_mac = NULL; |
| 2843 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2844 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2845 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2846 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2847 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2848 | PSA_DONE( ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2849 | mbedtls_free( actual_mac ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2850 | } |
| 2851 | /* END_CASE */ |
| 2852 | |
| 2853 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2854 | void mac_verify( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2855 | data_t *key_data, |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2856 | int alg_arg, |
| 2857 | data_t *input, |
| 2858 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2859 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2860 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2861 | psa_key_type_t key_type = key_type_arg; |
| 2862 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2863 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2864 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2865 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2866 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 2867 | TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE ); |
| 2868 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2869 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2870 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2871 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2872 | psa_set_key_algorithm( &attributes, alg ); |
| 2873 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2874 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2875 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2876 | &key ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2877 | |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2878 | /* Test the correct MAC. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2879 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2880 | PSA_ASSERT( psa_mac_update( &operation, |
| 2881 | input->x, input->len ) ); |
| 2882 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2883 | expected_mac->x, |
| 2884 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2885 | |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2886 | /* Test a MAC that's too short. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2887 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2888 | PSA_ASSERT( psa_mac_update( &operation, |
| 2889 | input->x, input->len ) ); |
| 2890 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2891 | expected_mac->x, |
| 2892 | expected_mac->len - 1 ), |
| 2893 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2894 | |
| 2895 | /* Test a MAC that's too long. */ |
| 2896 | ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 ); |
| 2897 | memcpy( perturbed_mac, expected_mac->x, expected_mac->len ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2898 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2899 | PSA_ASSERT( psa_mac_update( &operation, |
| 2900 | input->x, input->len ) ); |
| 2901 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2902 | perturbed_mac, |
| 2903 | expected_mac->len + 1 ), |
| 2904 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2905 | |
| 2906 | /* Test changing one byte. */ |
| 2907 | for( size_t i = 0; i < expected_mac->len; i++ ) |
| 2908 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2909 | mbedtls_test_set_step( i ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2910 | perturbed_mac[i] ^= 1; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2911 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2912 | PSA_ASSERT( psa_mac_update( &operation, |
| 2913 | input->x, input->len ) ); |
| 2914 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2915 | perturbed_mac, |
| 2916 | expected_mac->len ), |
| 2917 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2918 | perturbed_mac[i] ^= 1; |
| 2919 | } |
| 2920 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2921 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2922 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2923 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2924 | PSA_DONE( ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2925 | mbedtls_free( perturbed_mac ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2926 | } |
| 2927 | /* END_CASE */ |
| 2928 | |
| 2929 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2930 | void cipher_operation_init( ) |
| 2931 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2932 | const uint8_t input[1] = { 0 }; |
| 2933 | unsigned char output[1] = { 0 }; |
| 2934 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2935 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2936 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2937 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2938 | * to supress the Clang warning for the test. */ |
| 2939 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 2940 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 2941 | psa_cipher_operation_t zero; |
| 2942 | |
| 2943 | memset( &zero, 0, sizeof( zero ) ); |
| 2944 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2945 | /* A freshly-initialized cipher operation should not be usable. */ |
| 2946 | TEST_EQUAL( psa_cipher_update( &func, |
| 2947 | input, sizeof( input ), |
| 2948 | output, sizeof( output ), |
| 2949 | &output_length ), |
| 2950 | PSA_ERROR_BAD_STATE ); |
| 2951 | TEST_EQUAL( psa_cipher_update( &init, |
| 2952 | input, sizeof( input ), |
| 2953 | output, sizeof( output ), |
| 2954 | &output_length ), |
| 2955 | PSA_ERROR_BAD_STATE ); |
| 2956 | TEST_EQUAL( psa_cipher_update( &zero, |
| 2957 | input, sizeof( input ), |
| 2958 | output, sizeof( output ), |
| 2959 | &output_length ), |
| 2960 | PSA_ERROR_BAD_STATE ); |
| 2961 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2962 | /* A default cipher operation should be abortable without error. */ |
| 2963 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 2964 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 2965 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2966 | } |
| 2967 | /* END_CASE */ |
| 2968 | |
| 2969 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2970 | void cipher_setup( int key_type_arg, |
| 2971 | data_t *key, |
| 2972 | int alg_arg, |
| 2973 | int expected_status_arg ) |
| 2974 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2975 | psa_key_type_t key_type = key_type_arg; |
| 2976 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2977 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2978 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2979 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 2980 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2981 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2982 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2983 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2984 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2985 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2986 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 2987 | &operation, &status ) ) |
| 2988 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2989 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2990 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2991 | /* The operation object should be reusable. */ |
| 2992 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 2993 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 2994 | smoke_test_key_data, |
| 2995 | sizeof( smoke_test_key_data ), |
| 2996 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 2997 | &operation, &status ) ) |
| 2998 | goto exit; |
| 2999 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 3000 | #endif |
| 3001 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3002 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3003 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3004 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3005 | } |
| 3006 | /* END_CASE */ |
| 3007 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3008 | /* 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] | 3009 | void cipher_bad_order( ) |
| 3010 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3011 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3012 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3013 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3014 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3015 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3016 | 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] | 3017 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3018 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3019 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 3020 | const uint8_t text[] = { |
| 3021 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 3022 | 0xbb, 0xbb, 0xbb, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3023 | 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] | 3024 | size_t length = 0; |
| 3025 | |
| 3026 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3027 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3028 | psa_set_key_algorithm( &attributes, alg ); |
| 3029 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3030 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 3031 | &key ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3032 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3033 | /* Call encrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3034 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3035 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3036 | PSA_ERROR_BAD_STATE ); |
| 3037 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3038 | |
| 3039 | /* Call decrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3040 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3041 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3042 | PSA_ERROR_BAD_STATE ); |
| 3043 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3044 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3045 | /* Generate an IV without calling setup beforehand. */ |
| 3046 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3047 | buffer, sizeof( buffer ), |
| 3048 | &length ), |
| 3049 | PSA_ERROR_BAD_STATE ); |
| 3050 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3051 | |
| 3052 | /* Generate an IV twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3053 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3054 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3055 | buffer, sizeof( buffer ), |
| 3056 | &length ) ); |
| 3057 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3058 | buffer, sizeof( buffer ), |
| 3059 | &length ), |
| 3060 | PSA_ERROR_BAD_STATE ); |
| 3061 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3062 | |
| 3063 | /* Generate an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3064 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3065 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3066 | iv, sizeof( iv ) ) ); |
| 3067 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3068 | buffer, sizeof( buffer ), |
| 3069 | &length ), |
| 3070 | PSA_ERROR_BAD_STATE ); |
| 3071 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3072 | |
| 3073 | /* Set an IV without calling setup beforehand. */ |
| 3074 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3075 | iv, sizeof( iv ) ), |
| 3076 | PSA_ERROR_BAD_STATE ); |
| 3077 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3078 | |
| 3079 | /* Set an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3080 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3081 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3082 | iv, sizeof( iv ) ) ); |
| 3083 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3084 | iv, sizeof( iv ) ), |
| 3085 | PSA_ERROR_BAD_STATE ); |
| 3086 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3087 | |
| 3088 | /* Set an IV after it's already generated. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3089 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3090 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3091 | buffer, sizeof( buffer ), |
| 3092 | &length ) ); |
| 3093 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3094 | iv, sizeof( iv ) ), |
| 3095 | PSA_ERROR_BAD_STATE ); |
| 3096 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3097 | |
| 3098 | /* Call update without calling setup beforehand. */ |
| 3099 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3100 | text, sizeof( text ), |
| 3101 | buffer, sizeof( buffer ), |
| 3102 | &length ), |
| 3103 | PSA_ERROR_BAD_STATE ); |
| 3104 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3105 | |
| 3106 | /* Call update without an IV where an IV is required. */ |
| 3107 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3108 | text, sizeof( text ), |
| 3109 | buffer, sizeof( buffer ), |
| 3110 | &length ), |
| 3111 | PSA_ERROR_BAD_STATE ); |
| 3112 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3113 | |
| 3114 | /* Call update after finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3115 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3116 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3117 | iv, sizeof( iv ) ) ); |
| 3118 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3119 | buffer, sizeof( buffer ), &length ) ); |
| 3120 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3121 | text, sizeof( text ), |
| 3122 | buffer, sizeof( buffer ), |
| 3123 | &length ), |
| 3124 | PSA_ERROR_BAD_STATE ); |
| 3125 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3126 | |
| 3127 | /* Call finish without calling setup beforehand. */ |
| 3128 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3129 | buffer, sizeof( buffer ), &length ), |
| 3130 | PSA_ERROR_BAD_STATE ); |
| 3131 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3132 | |
| 3133 | /* Call finish without an IV where an IV is required. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3134 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3135 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3136 | * for cipher modes with padding. */ |
| 3137 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3138 | buffer, sizeof( buffer ), &length ), |
| 3139 | PSA_ERROR_BAD_STATE ); |
| 3140 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3141 | |
| 3142 | /* Call finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3143 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3144 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3145 | iv, sizeof( iv ) ) ); |
| 3146 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3147 | buffer, sizeof( buffer ), &length ) ); |
| 3148 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3149 | buffer, sizeof( buffer ), &length ), |
| 3150 | PSA_ERROR_BAD_STATE ); |
| 3151 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3152 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3153 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3154 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3155 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3156 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3157 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3158 | } |
| 3159 | /* END_CASE */ |
| 3160 | |
| 3161 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3162 | void cipher_encrypt( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3163 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3164 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3165 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3166 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3167 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3168 | psa_status_t status; |
| 3169 | psa_key_type_t key_type = key_type_arg; |
| 3170 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3171 | psa_status_t expected_status = expected_status_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3172 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3173 | size_t output_buffer_size = 0; |
| 3174 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3175 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3176 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3177 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3178 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3179 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3180 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3181 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3182 | psa_set_key_algorithm( &attributes, alg ); |
| 3183 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3184 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3185 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3186 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3187 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3188 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3189 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3190 | if( iv->len > 0 ) |
| 3191 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3192 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3193 | } |
| 3194 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3195 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3196 | TEST_ASSERT( output_buffer_size <= |
| 3197 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3198 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3199 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3200 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3201 | input->x, input->len, |
| 3202 | output, output_buffer_size, |
| 3203 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3204 | TEST_ASSERT( function_output_length <= |
| 3205 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 3206 | TEST_ASSERT( function_output_length <= |
| 3207 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3208 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3209 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3210 | status = psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 3211 | ( output_buffer_size == 0 ? NULL : |
| 3212 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3213 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3214 | &function_output_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3215 | TEST_ASSERT( function_output_length <= |
| 3216 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3217 | TEST_ASSERT( function_output_length <= |
| 3218 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3219 | total_output_length += function_output_length; |
| 3220 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3221 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3222 | if( expected_status == PSA_SUCCESS ) |
| 3223 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3224 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3225 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3226 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3227 | } |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3228 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3229 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3230 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3231 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3232 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3233 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3234 | } |
| 3235 | /* END_CASE */ |
| 3236 | |
| 3237 | /* BEGIN_CASE */ |
| 3238 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3239 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3240 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3241 | int first_part_size_arg, |
| 3242 | int output1_length_arg, int output2_length_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3243 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3244 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3245 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3246 | psa_key_type_t key_type = key_type_arg; |
| 3247 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3248 | size_t first_part_size = first_part_size_arg; |
| 3249 | size_t output1_length = output1_length_arg; |
| 3250 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3251 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3252 | size_t output_buffer_size = 0; |
| 3253 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3254 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3255 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3256 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3257 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3258 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3259 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3260 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3261 | psa_set_key_algorithm( &attributes, alg ); |
| 3262 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3263 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3264 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3265 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3266 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3267 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3268 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3269 | if( iv->len > 0 ) |
| 3270 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3271 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3272 | } |
| 3273 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3274 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3275 | TEST_ASSERT( output_buffer_size <= |
| 3276 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3277 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3278 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3279 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3280 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 3281 | output, output_buffer_size, |
| 3282 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3283 | TEST_ASSERT( function_output_length == output1_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3284 | TEST_ASSERT( function_output_length <= |
| 3285 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3286 | TEST_ASSERT( function_output_length <= |
| 3287 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3288 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3289 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3290 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3291 | input->x + first_part_size, |
| 3292 | input->len - first_part_size, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 3293 | ( output_buffer_size == 0 ? NULL : |
| 3294 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3295 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3296 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3297 | TEST_ASSERT( function_output_length == output2_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3298 | TEST_ASSERT( function_output_length <= |
| 3299 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3300 | alg, |
| 3301 | input->len - first_part_size ) ); |
| 3302 | TEST_ASSERT( function_output_length <= |
| 3303 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3304 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3305 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3306 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 3307 | ( output_buffer_size == 0 ? NULL : |
| 3308 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3309 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3310 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3311 | TEST_ASSERT( function_output_length <= |
| 3312 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3313 | TEST_ASSERT( function_output_length <= |
| 3314 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3315 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3316 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3317 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3318 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3319 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3320 | |
| 3321 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3322 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3323 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3324 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3325 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3326 | } |
| 3327 | /* END_CASE */ |
| 3328 | |
| 3329 | /* BEGIN_CASE */ |
| 3330 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3331 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3332 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3333 | int first_part_size_arg, |
| 3334 | int output1_length_arg, int output2_length_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3335 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3336 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3337 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3338 | psa_key_type_t key_type = key_type_arg; |
| 3339 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3340 | size_t first_part_size = first_part_size_arg; |
| 3341 | size_t output1_length = output1_length_arg; |
| 3342 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3343 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3344 | size_t output_buffer_size = 0; |
| 3345 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3346 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3347 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3348 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3349 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3350 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3351 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3352 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3353 | psa_set_key_algorithm( &attributes, alg ); |
| 3354 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3355 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3356 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3357 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3358 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3359 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3360 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3361 | if( iv->len > 0 ) |
| 3362 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3363 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3364 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3365 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3366 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3367 | TEST_ASSERT( output_buffer_size <= |
| 3368 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3369 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3370 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3371 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3372 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3373 | input->x, first_part_size, |
| 3374 | output, output_buffer_size, |
| 3375 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3376 | TEST_ASSERT( function_output_length == output1_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3377 | TEST_ASSERT( function_output_length <= |
| 3378 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3379 | TEST_ASSERT( function_output_length <= |
| 3380 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3381 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3382 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3383 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3384 | input->x + first_part_size, |
| 3385 | input->len - first_part_size, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 3386 | ( output_buffer_size == 0 ? NULL : |
| 3387 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3388 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3389 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3390 | TEST_ASSERT( function_output_length == output2_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3391 | TEST_ASSERT( function_output_length <= |
| 3392 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3393 | alg, |
| 3394 | input->len - first_part_size ) ); |
| 3395 | TEST_ASSERT( function_output_length <= |
| 3396 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3397 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3398 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3399 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 3400 | ( output_buffer_size == 0 ? NULL : |
| 3401 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3402 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3403 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3404 | TEST_ASSERT( function_output_length <= |
| 3405 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3406 | TEST_ASSERT( function_output_length <= |
| 3407 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3408 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3409 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3410 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3411 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3412 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3413 | |
| 3414 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3415 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3416 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3417 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3418 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3419 | } |
| 3420 | /* END_CASE */ |
| 3421 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3422 | /* BEGIN_CASE */ |
| 3423 | void cipher_decrypt( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3424 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3425 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3426 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3427 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3428 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3429 | psa_status_t status; |
| 3430 | psa_key_type_t key_type = key_type_arg; |
| 3431 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3432 | psa_status_t expected_status = expected_status_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3433 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3434 | size_t output_buffer_size = 0; |
| 3435 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3436 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3437 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3438 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3439 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3440 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3441 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3442 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3443 | psa_set_key_algorithm( &attributes, alg ); |
| 3444 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3445 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3446 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3447 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3448 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3449 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3450 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3451 | if( iv->len > 0 ) |
| 3452 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3453 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3454 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3455 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3456 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3457 | TEST_ASSERT( output_buffer_size <= |
| 3458 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3459 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3460 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3461 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3462 | input->x, input->len, |
| 3463 | output, output_buffer_size, |
| 3464 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3465 | TEST_ASSERT( function_output_length <= |
| 3466 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 3467 | TEST_ASSERT( function_output_length <= |
| 3468 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3469 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3470 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3471 | status = psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 3472 | ( output_buffer_size == 0 ? NULL : |
| 3473 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3474 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3475 | &function_output_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3476 | TEST_ASSERT( function_output_length <= |
| 3477 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3478 | TEST_ASSERT( function_output_length <= |
| 3479 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3480 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3481 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3482 | |
| 3483 | if( expected_status == PSA_SUCCESS ) |
| 3484 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3485 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3486 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3487 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3488 | } |
| 3489 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3490 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3491 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3492 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3493 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3494 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3495 | } |
| 3496 | /* END_CASE */ |
| 3497 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3498 | /* BEGIN_CASE */ |
| 3499 | void cipher_verify_output( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3500 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3501 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3502 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3503 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3504 | psa_key_type_t key_type = key_type_arg; |
| 3505 | psa_algorithm_t alg = alg_arg; |
mohammad1603 | e6b67a1 | 2018-03-12 10:38:49 -0700 | [diff] [blame] | 3506 | unsigned char iv[16] = {0}; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3507 | size_t iv_size = 16; |
| 3508 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3509 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3510 | size_t output1_size = 0; |
| 3511 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3512 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3513 | size_t output2_size = 0; |
| 3514 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3515 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3516 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3517 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3518 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3519 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3520 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3521 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3522 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3523 | psa_set_key_algorithm( &attributes, alg ); |
| 3524 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3525 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3526 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3527 | &key ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3528 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3529 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 3530 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3531 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3532 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3533 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3534 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3535 | iv, iv_size, |
| 3536 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3537 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3538 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3539 | TEST_ASSERT( output1_size <= |
| 3540 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3541 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3542 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3543 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len, |
| 3544 | output1, output1_size, |
| 3545 | &output1_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3546 | TEST_ASSERT( output1_length <= |
| 3547 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 3548 | TEST_ASSERT( output1_length <= |
| 3549 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
| 3550 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3551 | PSA_ASSERT( psa_cipher_finish( &operation1, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3552 | output1 + output1_length, |
| 3553 | output1_size - output1_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3554 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3555 | TEST_ASSERT( function_output_length <= |
| 3556 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3557 | TEST_ASSERT( function_output_length <= |
| 3558 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3559 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3560 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3561 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3562 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3563 | |
| 3564 | output2_size = output1_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3565 | TEST_ASSERT( output2_size <= |
| 3566 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 3567 | TEST_ASSERT( output2_size <= |
| 3568 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3569 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3570 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3571 | if( iv_length > 0 ) |
| 3572 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3573 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3574 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3575 | } |
| 3576 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3577 | PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length, |
| 3578 | output2, output2_size, |
| 3579 | &output2_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3580 | TEST_ASSERT( output2_length <= |
| 3581 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 3582 | TEST_ASSERT( output2_length <= |
| 3583 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) ); |
| 3584 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3585 | function_output_length = 0; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3586 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3587 | output2 + output2_length, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3588 | output2_size - output2_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3589 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3590 | TEST_ASSERT( function_output_length <= |
| 3591 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3592 | TEST_ASSERT( function_output_length <= |
| 3593 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3594 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3595 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3596 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3597 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3598 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3599 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3600 | |
| 3601 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3602 | psa_cipher_abort( &operation1 ); |
| 3603 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3604 | mbedtls_free( output1 ); |
| 3605 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3606 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3607 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3608 | } |
| 3609 | /* END_CASE */ |
| 3610 | |
| 3611 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3612 | void cipher_verify_output_multipart( int alg_arg, |
| 3613 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3614 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3615 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3616 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3617 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3618 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3619 | psa_key_type_t key_type = key_type_arg; |
| 3620 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3621 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3622 | unsigned char iv[16] = {0}; |
| 3623 | size_t iv_size = 16; |
| 3624 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3625 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3626 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3627 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3628 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3629 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3630 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3631 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3632 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3633 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3634 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3635 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3636 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3637 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3638 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3639 | psa_set_key_algorithm( &attributes, alg ); |
| 3640 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3641 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3642 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3643 | &key ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3644 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3645 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 3646 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3647 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3648 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3649 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3650 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3651 | iv, iv_size, |
| 3652 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3653 | } |
| 3654 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3655 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3656 | TEST_ASSERT( output1_buffer_size <= |
| 3657 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3658 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3659 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3660 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3661 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3662 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 3663 | output1, output1_buffer_size, |
| 3664 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3665 | TEST_ASSERT( function_output_length <= |
| 3666 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3667 | TEST_ASSERT( function_output_length <= |
| 3668 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3669 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3670 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3671 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 3672 | input->x + first_part_size, |
| 3673 | input->len - first_part_size, |
| 3674 | output1, output1_buffer_size, |
| 3675 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3676 | TEST_ASSERT( function_output_length <= |
| 3677 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3678 | alg, |
| 3679 | input->len - first_part_size ) ); |
| 3680 | TEST_ASSERT( function_output_length <= |
| 3681 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3682 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3683 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3684 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 3685 | output1 + output1_length, |
| 3686 | output1_buffer_size - output1_length, |
| 3687 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3688 | TEST_ASSERT( function_output_length <= |
| 3689 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3690 | TEST_ASSERT( function_output_length <= |
| 3691 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3692 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3693 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3694 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3695 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3696 | output2_buffer_size = output1_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3697 | TEST_ASSERT( output2_buffer_size <= |
| 3698 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 3699 | TEST_ASSERT( output2_buffer_size <= |
| 3700 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3701 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3702 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3703 | if( iv_length > 0 ) |
| 3704 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3705 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3706 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3707 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3708 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3709 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 3710 | output2, output2_buffer_size, |
| 3711 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3712 | TEST_ASSERT( function_output_length <= |
| 3713 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3714 | TEST_ASSERT( function_output_length <= |
| 3715 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3716 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3717 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3718 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 3719 | output1 + first_part_size, |
| 3720 | output1_length - first_part_size, |
| 3721 | output2, output2_buffer_size, |
| 3722 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3723 | TEST_ASSERT( function_output_length <= |
| 3724 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3725 | alg, |
| 3726 | output1_length - first_part_size ) ); |
| 3727 | TEST_ASSERT( function_output_length <= |
| 3728 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3729 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3730 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3731 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3732 | output2 + output2_length, |
| 3733 | output2_buffer_size - output2_length, |
| 3734 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3735 | TEST_ASSERT( function_output_length <= |
| 3736 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3737 | TEST_ASSERT( function_output_length <= |
| 3738 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3739 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3740 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3741 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3742 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3743 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3744 | |
| 3745 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3746 | psa_cipher_abort( &operation1 ); |
| 3747 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3748 | mbedtls_free( output1 ); |
| 3749 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3750 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3751 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3752 | } |
| 3753 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 3754 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3755 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3756 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3757 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3758 | data_t *nonce, |
| 3759 | data_t *additional_data, |
| 3760 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3761 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3762 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3763 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3764 | psa_key_type_t key_type = key_type_arg; |
| 3765 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3766 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3767 | unsigned char *output_data = NULL; |
| 3768 | size_t output_size = 0; |
| 3769 | size_t output_length = 0; |
| 3770 | unsigned char *output_data2 = NULL; |
| 3771 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 3772 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3773 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3774 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3775 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3776 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3777 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3778 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3779 | psa_set_key_algorithm( &attributes, alg ); |
| 3780 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3781 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3782 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3783 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3784 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3785 | key_bits = psa_get_key_bits( &attributes ); |
| 3786 | |
| 3787 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3788 | alg ); |
| 3789 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3790 | * should be exact. */ |
| 3791 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 3792 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 3793 | { |
| 3794 | TEST_EQUAL( output_size, |
| 3795 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3796 | TEST_ASSERT( output_size <= |
| 3797 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3798 | } |
| 3799 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3800 | |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 3801 | status = psa_aead_encrypt( key, alg, |
| 3802 | nonce->x, nonce->len, |
| 3803 | additional_data->x, |
| 3804 | additional_data->len, |
| 3805 | input_data->x, input_data->len, |
| 3806 | output_data, output_size, |
| 3807 | &output_length ); |
| 3808 | |
| 3809 | /* If the operation is not supported, just skip and not fail in case the |
| 3810 | * encryption involves a common limitation of cryptography hardwares and |
| 3811 | * an alternative implementation. */ |
| 3812 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 3813 | { |
| 3814 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3815 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 3816 | } |
| 3817 | |
| 3818 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3819 | |
| 3820 | if( PSA_SUCCESS == expected_result ) |
| 3821 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3822 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3823 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3824 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3825 | * should be exact. */ |
| 3826 | TEST_EQUAL( input_data->len, |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3827 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) ); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3828 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3829 | TEST_ASSERT( input_data->len <= |
| 3830 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) ); |
| 3831 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3832 | TEST_EQUAL( psa_aead_decrypt( key, alg, |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3833 | nonce->x, nonce->len, |
| 3834 | additional_data->x, |
| 3835 | additional_data->len, |
| 3836 | output_data, output_length, |
| 3837 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3838 | &output_length2 ), |
| 3839 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3840 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3841 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 3842 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3843 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3844 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3845 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3846 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3847 | mbedtls_free( output_data ); |
| 3848 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3849 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3850 | } |
| 3851 | /* END_CASE */ |
| 3852 | |
| 3853 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3854 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 3855 | int alg_arg, |
| 3856 | data_t *nonce, |
| 3857 | data_t *additional_data, |
| 3858 | data_t *input_data, |
| 3859 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3860 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3861 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3862 | psa_key_type_t key_type = key_type_arg; |
| 3863 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3864 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3865 | unsigned char *output_data = NULL; |
| 3866 | size_t output_size = 0; |
| 3867 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3868 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3869 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3870 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3871 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3872 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3873 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3874 | psa_set_key_algorithm( &attributes, alg ); |
| 3875 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3876 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3877 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3878 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3879 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3880 | key_bits = psa_get_key_bits( &attributes ); |
| 3881 | |
| 3882 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3883 | alg ); |
| 3884 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3885 | * should be exact. */ |
| 3886 | TEST_EQUAL( output_size, |
| 3887 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3888 | TEST_ASSERT( output_size <= |
| 3889 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3890 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3891 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3892 | status = psa_aead_encrypt( key, alg, |
| 3893 | nonce->x, nonce->len, |
| 3894 | additional_data->x, additional_data->len, |
| 3895 | input_data->x, input_data->len, |
| 3896 | output_data, output_size, |
| 3897 | &output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3898 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3899 | /* If the operation is not supported, just skip and not fail in case the |
| 3900 | * encryption involves a common limitation of cryptography hardwares and |
| 3901 | * an alternative implementation. */ |
| 3902 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3903 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3904 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3905 | 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] | 3906 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3907 | |
| 3908 | PSA_ASSERT( status ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3909 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 3910 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3911 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3912 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3913 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3914 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3915 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3916 | } |
| 3917 | /* END_CASE */ |
| 3918 | |
| 3919 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3920 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 3921 | int alg_arg, |
| 3922 | data_t *nonce, |
| 3923 | data_t *additional_data, |
| 3924 | data_t *input_data, |
| 3925 | data_t *expected_data, |
| 3926 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3927 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3928 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3929 | psa_key_type_t key_type = key_type_arg; |
| 3930 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3931 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3932 | unsigned char *output_data = NULL; |
| 3933 | size_t output_size = 0; |
| 3934 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3935 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3936 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3937 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3938 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3939 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3940 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3941 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3942 | psa_set_key_algorithm( &attributes, alg ); |
| 3943 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3944 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3945 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3946 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3947 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3948 | key_bits = psa_get_key_bits( &attributes ); |
| 3949 | |
| 3950 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3951 | alg ); |
| 3952 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 3953 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 3954 | { |
| 3955 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3956 | * should be exact. */ |
| 3957 | TEST_EQUAL( output_size, |
| 3958 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3959 | TEST_ASSERT( output_size <= |
| 3960 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3961 | } |
| 3962 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3963 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3964 | status = psa_aead_decrypt( key, alg, |
| 3965 | nonce->x, nonce->len, |
| 3966 | additional_data->x, |
| 3967 | additional_data->len, |
| 3968 | input_data->x, input_data->len, |
| 3969 | output_data, output_size, |
| 3970 | &output_length ); |
| 3971 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3972 | /* If the operation is not supported, just skip and not fail in case the |
| 3973 | * decryption involves a common limitation of cryptography hardwares and |
| 3974 | * an alternative implementation. */ |
| 3975 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3976 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3977 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3978 | 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] | 3979 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3980 | |
| 3981 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3982 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3983 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3984 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 3985 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3986 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3987 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3988 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3989 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3990 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3991 | } |
| 3992 | /* END_CASE */ |
| 3993 | |
| 3994 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3995 | void aead_multipart_encrypt( int key_type_arg, data_t *key_data, |
| 3996 | int alg_arg, |
| 3997 | data_t *nonce, |
| 3998 | data_t *additional_data, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3999 | int test_ad_mp_arg, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4000 | data_t *input_data, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4001 | int test_data_mp_arg, |
| 4002 | data_t *expected_result_arg ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4003 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4004 | size_t ad_part_len = 0; |
| 4005 | size_t data_part_len = 0; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4006 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4007 | if( test_ad_mp_arg == 1 ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4008 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4009 | for( ad_part_len = 1; ad_part_len <= additional_data->len; |
| 4010 | ad_part_len++ ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4011 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4012 | mbedtls_test_set_step( ad_part_len ); |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4013 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4014 | aead_multipart_encrypt_internal( key_type_arg, key_data, |
| 4015 | alg_arg,nonce, |
| 4016 | additional_data, |
| 4017 | ad_part_len, |
| 4018 | input_data, -1, |
| 4019 | expected_result_arg ); |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4020 | } |
| 4021 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4022 | |
| 4023 | if( test_data_mp_arg == 1 ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4024 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4025 | for( data_part_len = 1; data_part_len <= input_data->len; |
| 4026 | data_part_len++ ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4027 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4028 | aead_multipart_encrypt_internal( key_type_arg, key_data, |
| 4029 | alg_arg, nonce, |
| 4030 | additional_data, -1, |
| 4031 | input_data, data_part_len, |
| 4032 | expected_result_arg ); |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4033 | } |
| 4034 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4035 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4036 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4037 | |
| 4038 | exit: |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4039 | } |
| 4040 | /* END_CASE */ |
| 4041 | |
| 4042 | /* BEGIN_CASE */ |
| 4043 | void aead_multipart_encrypt_decrypt( int key_type_arg, data_t *key_data, |
| 4044 | int alg_arg, |
| 4045 | data_t *nonce, |
| 4046 | data_t *additional_data, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4047 | int test_ad_mp_arg, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4048 | data_t *input_data, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4049 | int test_data_mp_arg, |
| 4050 | int expected_status_arg ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4051 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4052 | size_t ad_part_len = 0; |
| 4053 | size_t data_part_len = 0; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4054 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4055 | if( test_ad_mp_arg == 1 ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4056 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4057 | for( ad_part_len = 1; ad_part_len <= additional_data->len; |
| 4058 | ad_part_len++ ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4059 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4060 | mbedtls_test_set_step( ad_part_len ); |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4061 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4062 | aead_multipart_encrypt_decrypt_internal( key_type_arg, key_data, |
| 4063 | alg_arg, nonce, |
| 4064 | additional_data, |
| 4065 | ad_part_len, |
| 4066 | input_data, -1, |
| 4067 | expected_status_arg ); |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4068 | } |
| 4069 | } |
| 4070 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4071 | if( test_data_mp_arg == 1 ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4072 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4073 | for( data_part_len = 1; data_part_len <= input_data->len; |
| 4074 | data_part_len++ ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4075 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4076 | aead_multipart_encrypt_decrypt_internal( key_type_arg, key_data, |
| 4077 | alg_arg, nonce, |
| 4078 | additional_data, -1, |
| 4079 | input_data, data_part_len, |
| 4080 | expected_status_arg ); |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4081 | } |
| 4082 | } |
| 4083 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4084 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4085 | |
| 4086 | exit: |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4087 | } |
| 4088 | /* END_CASE */ |
| 4089 | |
| 4090 | /* BEGIN_CASE */ |
| 4091 | void aead_multipart_decrypt( int key_type_arg, data_t *key_data, |
| 4092 | int alg_arg, |
| 4093 | data_t *nonce, |
| 4094 | data_t *additional_data, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4095 | int test_ad_mp_arg, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4096 | data_t *input_data, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4097 | int test_data_mp_arg, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4098 | data_t *expected_data, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4099 | int expected_status ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4100 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4101 | size_t ad_part_len = 0; |
| 4102 | size_t data_part_len = 0; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4103 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4104 | if( test_ad_mp_arg == 1 ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4105 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4106 | for( ad_part_len = 1; ad_part_len <= additional_data->len; |
| 4107 | ad_part_len++ ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4108 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4109 | mbedtls_test_set_step( ad_part_len ); |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4110 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4111 | aead_multipart_decrypt_internal( key_type_arg, key_data, |
| 4112 | alg_arg, nonce, |
| 4113 | additional_data, |
| 4114 | ad_part_len, |
| 4115 | input_data, -1, |
| 4116 | expected_data, expected_status ); |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4117 | } |
| 4118 | } |
| 4119 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4120 | if( test_data_mp_arg == 1 ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4121 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4122 | for( data_part_len = 1; data_part_len <= input_data->len; |
| 4123 | data_part_len++ ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4124 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4125 | aead_multipart_decrypt_internal( key_type_arg, key_data, |
| 4126 | alg_arg, nonce, |
| 4127 | additional_data, -1, |
| 4128 | input_data, data_part_len, |
| 4129 | expected_data, expected_status ); |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4130 | } |
| 4131 | } |
| 4132 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4133 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4134 | |
| 4135 | exit: |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4136 | } |
| 4137 | /* END_CASE */ |
| 4138 | |
| 4139 | /* BEGIN_CASE */ |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4140 | void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data, |
| 4141 | int alg_arg, |
| 4142 | int nonce_len, |
| 4143 | int expected_result_arg ) |
| 4144 | { |
| 4145 | |
| 4146 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4147 | psa_key_type_t key_type = key_type_arg; |
| 4148 | psa_algorithm_t alg = alg_arg; |
| 4149 | psa_aead_operation_t operation; |
| 4150 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 4151 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4152 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 4153 | size_t nonce_generated_len = 0; |
| 4154 | |
| 4155 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4156 | |
| 4157 | psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4158 | psa_set_key_algorithm( & attributes, alg ); |
| 4159 | psa_set_key_type( & attributes, key_type ); |
| 4160 | |
| 4161 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4162 | &key ) ); |
| 4163 | |
| 4164 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4165 | |
| 4166 | operation = psa_aead_operation_init( ); |
| 4167 | |
| 4168 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 4169 | |
| 4170 | /* If the operation is not supported, just skip and not fail in case the |
| 4171 | * encryption involves a common limitation of cryptography hardwares and |
| 4172 | * an alternative implementation. */ |
| 4173 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4174 | { |
| 4175 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4176 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_len ); |
| 4177 | } |
| 4178 | |
| 4179 | PSA_ASSERT( status ); |
| 4180 | |
| 4181 | TEST_ASSERT( nonce_len < PSA_AEAD_NONCE_MAX_SIZE ); |
| 4182 | |
| 4183 | status = psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4184 | nonce_len, |
| 4185 | &nonce_generated_len ); |
| 4186 | |
| 4187 | TEST_ASSERT( status == expected_result_arg ); |
| 4188 | |
| 4189 | exit: |
| 4190 | psa_destroy_key( key ); |
| 4191 | psa_aead_abort( &operation ); |
| 4192 | PSA_DONE( ); |
| 4193 | } |
| 4194 | /* END_CASE */ |
| 4195 | |
| 4196 | /* BEGIN_CASE */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame^] | 4197 | void aead_multipart_state_test( int key_type_arg, data_t *key_data, |
| 4198 | int alg_arg, |
| 4199 | data_t *nonce, |
| 4200 | data_t *additional_data, |
| 4201 | data_t *input_data ) |
| 4202 | { |
| 4203 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4204 | psa_key_type_t key_type = key_type_arg; |
| 4205 | psa_algorithm_t alg = alg_arg; |
| 4206 | psa_aead_operation_t operation; |
| 4207 | unsigned char *output_data = NULL; |
| 4208 | unsigned char *final_data = NULL; |
| 4209 | size_t output_size = 0; |
| 4210 | size_t finish_output_size = 0; |
| 4211 | size_t output_length = 0; |
| 4212 | size_t key_bits = 0; |
| 4213 | size_t tag_length = 0; |
| 4214 | size_t tag_size = 0; |
| 4215 | size_t nonce_length = 0; |
| 4216 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 4217 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 4218 | size_t output_part_length = 0; |
| 4219 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4220 | |
| 4221 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4222 | |
| 4223 | psa_set_key_usage_flags( & attributes, |
| 4224 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4225 | psa_set_key_algorithm( & attributes, alg ); |
| 4226 | psa_set_key_type( & attributes, key_type ); |
| 4227 | |
| 4228 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4229 | &key ) ); |
| 4230 | |
| 4231 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4232 | key_bits = psa_get_key_bits( &attributes ); |
| 4233 | |
| 4234 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 4235 | |
| 4236 | TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE ); |
| 4237 | |
| 4238 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 4239 | |
| 4240 | ASSERT_ALLOC( output_data, output_size ); |
| 4241 | |
| 4242 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 4243 | |
| 4244 | TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
| 4245 | |
| 4246 | ASSERT_ALLOC( final_data, finish_output_size ); |
| 4247 | |
| 4248 | /* Test all operations error without calling setup first. */ |
| 4249 | |
| 4250 | operation = psa_aead_operation_init( ); |
| 4251 | |
| 4252 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 4253 | PSA_ERROR_BAD_STATE ); |
| 4254 | |
| 4255 | psa_aead_abort( &operation ); |
| 4256 | |
| 4257 | operation = psa_aead_operation_init( ); |
| 4258 | |
| 4259 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4260 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4261 | &nonce_length ), |
| 4262 | PSA_ERROR_BAD_STATE ); |
| 4263 | |
| 4264 | psa_aead_abort( &operation ); |
| 4265 | |
| 4266 | operation = psa_aead_operation_init( ); |
| 4267 | |
| 4268 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 4269 | input_data->len ), |
| 4270 | PSA_ERROR_BAD_STATE ); |
| 4271 | |
| 4272 | psa_aead_abort( &operation ); |
| 4273 | |
| 4274 | operation = psa_aead_operation_init( ); |
| 4275 | |
| 4276 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 4277 | additional_data->len ), |
| 4278 | PSA_ERROR_BAD_STATE ); |
| 4279 | |
| 4280 | psa_aead_abort( &operation ); |
| 4281 | |
| 4282 | operation = psa_aead_operation_init( ); |
| 4283 | |
| 4284 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 4285 | input_data->len, output_data, |
| 4286 | output_size, &output_length ), |
| 4287 | PSA_ERROR_BAD_STATE ); |
| 4288 | |
| 4289 | psa_aead_abort( &operation ); |
| 4290 | |
| 4291 | operation = psa_aead_operation_init( ); |
| 4292 | |
| 4293 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 4294 | finish_output_size, |
| 4295 | &output_part_length, |
| 4296 | tag_buffer, tag_length, |
| 4297 | &tag_size ), |
| 4298 | PSA_ERROR_BAD_STATE ); |
| 4299 | |
| 4300 | psa_aead_abort( &operation ); |
| 4301 | |
| 4302 | operation = psa_aead_operation_init( ); |
| 4303 | |
| 4304 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 4305 | finish_output_size, |
| 4306 | &output_part_length, |
| 4307 | tag_buffer, |
| 4308 | tag_length ), |
| 4309 | PSA_ERROR_BAD_STATE ); |
| 4310 | |
| 4311 | psa_aead_abort( &operation ); |
| 4312 | |
| 4313 | /* Test for double setups. */ |
| 4314 | |
| 4315 | operation = psa_aead_operation_init( ); |
| 4316 | |
| 4317 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4318 | |
| 4319 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 4320 | PSA_ERROR_BAD_STATE ); |
| 4321 | |
| 4322 | psa_aead_abort( &operation ); |
| 4323 | |
| 4324 | operation = psa_aead_operation_init( ); |
| 4325 | |
| 4326 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 4327 | |
| 4328 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 4329 | PSA_ERROR_BAD_STATE ); |
| 4330 | |
| 4331 | psa_aead_abort( &operation ); |
| 4332 | |
| 4333 | /* Test for not setting a nonce. */ |
| 4334 | |
| 4335 | operation = psa_aead_operation_init( ); |
| 4336 | |
| 4337 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4338 | |
| 4339 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 4340 | additional_data->len ), |
| 4341 | PSA_ERROR_BAD_STATE ); |
| 4342 | |
| 4343 | psa_aead_abort( &operation ); |
| 4344 | |
| 4345 | /* Test for double setting nonce. */ |
| 4346 | |
| 4347 | operation = psa_aead_operation_init( ); |
| 4348 | |
| 4349 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4350 | |
| 4351 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4352 | |
| 4353 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 4354 | PSA_ERROR_BAD_STATE ); |
| 4355 | |
| 4356 | psa_aead_abort( &operation ); |
| 4357 | |
| 4358 | /* Test for setting lengths twice. */ |
| 4359 | |
| 4360 | operation = psa_aead_operation_init( ); |
| 4361 | |
| 4362 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4363 | |
| 4364 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4365 | |
| 4366 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4367 | input_data->len ) ); |
| 4368 | |
| 4369 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 4370 | input_data->len ), |
| 4371 | PSA_ERROR_BAD_STATE ); |
| 4372 | |
| 4373 | psa_aead_abort( &operation ); |
| 4374 | |
| 4375 | /* Test for setting lengths after already starting data. */ |
| 4376 | |
| 4377 | operation = psa_aead_operation_init( ); |
| 4378 | |
| 4379 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4380 | |
| 4381 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4382 | |
| 4383 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 4384 | input_data->len, output_data, |
| 4385 | output_size, &output_length ) ); |
| 4386 | |
| 4387 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 4388 | input_data->len ), |
| 4389 | PSA_ERROR_BAD_STATE ); |
| 4390 | |
| 4391 | psa_aead_abort( &operation ); |
| 4392 | |
| 4393 | /* Test for not sending any additional data or data (encrypt) */ |
| 4394 | |
| 4395 | operation = psa_aead_operation_init( ); |
| 4396 | |
| 4397 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4398 | |
| 4399 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4400 | |
| 4401 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4402 | input_data->len ) ); |
| 4403 | |
| 4404 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 4405 | finish_output_size, |
| 4406 | &output_part_length, |
| 4407 | tag_buffer, tag_length, |
| 4408 | &tag_size ), |
| 4409 | PSA_ERROR_INVALID_ARGUMENT ); |
| 4410 | |
| 4411 | psa_aead_abort( &operation ); |
| 4412 | |
| 4413 | /* Test for not sending any additional data or data (decrypt) */ |
| 4414 | |
| 4415 | operation = psa_aead_operation_init( ); |
| 4416 | |
| 4417 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 4418 | |
| 4419 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4420 | |
| 4421 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4422 | input_data->len ) ); |
| 4423 | |
| 4424 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 4425 | finish_output_size, |
| 4426 | &output_part_length, |
| 4427 | tag_buffer, |
| 4428 | tag_length ), |
| 4429 | PSA_ERROR_INVALID_ARGUMENT ); |
| 4430 | |
| 4431 | psa_aead_abort( &operation ); |
| 4432 | |
| 4433 | /* Test for not sending any additional data. */ |
| 4434 | |
| 4435 | operation = psa_aead_operation_init( ); |
| 4436 | |
| 4437 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4438 | |
| 4439 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4440 | |
| 4441 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4442 | input_data->len ) ); |
| 4443 | |
| 4444 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 4445 | input_data->len, output_data, |
| 4446 | output_size, &output_length ), |
| 4447 | PSA_ERROR_INVALID_ARGUMENT ); |
| 4448 | |
| 4449 | psa_aead_abort( &operation ); |
| 4450 | |
| 4451 | /* Test sending additional data after data. */ |
| 4452 | |
| 4453 | operation = psa_aead_operation_init( ); |
| 4454 | |
| 4455 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4456 | |
| 4457 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4458 | |
| 4459 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 4460 | input_data->len, output_data, |
| 4461 | output_size, &output_length ) ); |
| 4462 | |
| 4463 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 4464 | additional_data->len ), |
| 4465 | PSA_ERROR_BAD_STATE ); |
| 4466 | |
| 4467 | psa_aead_abort( &operation ); |
| 4468 | |
| 4469 | exit: |
| 4470 | psa_destroy_key( key ); |
| 4471 | psa_aead_abort( &operation ); |
| 4472 | mbedtls_free( output_data ); |
| 4473 | mbedtls_free( final_data ); |
| 4474 | PSA_DONE( ); |
| 4475 | } |
| 4476 | /* END_CASE */ |
| 4477 | |
| 4478 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4479 | void signature_size( int type_arg, |
| 4480 | int bits, |
| 4481 | int alg_arg, |
| 4482 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4483 | { |
| 4484 | psa_key_type_t type = type_arg; |
| 4485 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4486 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 4487 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4488 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 4489 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4490 | exit: |
| 4491 | ; |
| 4492 | } |
| 4493 | /* END_CASE */ |
| 4494 | |
| 4495 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 4496 | void sign_hash_deterministic( int key_type_arg, data_t *key_data, |
| 4497 | int alg_arg, data_t *input_data, |
| 4498 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4499 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4500 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4501 | psa_key_type_t key_type = key_type_arg; |
| 4502 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4503 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4504 | unsigned char *signature = NULL; |
| 4505 | size_t signature_size; |
| 4506 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4507 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4508 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4509 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4510 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4511 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4512 | psa_set_key_algorithm( &attributes, alg ); |
| 4513 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 4514 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4515 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4516 | &key ) ); |
| 4517 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4518 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4519 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4520 | /* Allocate a buffer which has the size advertized by the |
| 4521 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4522 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4523 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4524 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4525 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4526 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4527 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4528 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4529 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4530 | input_data->x, input_data->len, |
| 4531 | signature, signature_size, |
| 4532 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4533 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4534 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 4535 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4536 | |
| 4537 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4538 | /* |
| 4539 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4540 | * thus reset them as required. |
| 4541 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4542 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4543 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4544 | psa_destroy_key( key ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 4545 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4546 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4547 | } |
| 4548 | /* END_CASE */ |
| 4549 | |
| 4550 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 4551 | void sign_hash_fail( int key_type_arg, data_t *key_data, |
| 4552 | int alg_arg, data_t *input_data, |
| 4553 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4554 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4555 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4556 | psa_key_type_t key_type = key_type_arg; |
| 4557 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4558 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4559 | psa_status_t actual_status; |
| 4560 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 4561 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 4562 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4563 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4564 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4565 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4566 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4567 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4568 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4569 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4570 | psa_set_key_algorithm( &attributes, alg ); |
| 4571 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 4572 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4573 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4574 | &key ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4575 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4576 | actual_status = psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4577 | input_data->x, input_data->len, |
| 4578 | signature, signature_size, |
| 4579 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4580 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4581 | /* The value of *signature_length is unspecified on error, but |
| 4582 | * whatever it is, it should be less than signature_size, so that |
| 4583 | * if the caller tries to read *signature_length bytes without |
| 4584 | * checking the error code then they don't overflow a buffer. */ |
| 4585 | TEST_ASSERT( signature_length <= signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4586 | |
| 4587 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4588 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4589 | psa_destroy_key( key ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4590 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4591 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4592 | } |
| 4593 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 4594 | |
| 4595 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 4596 | void sign_verify_hash( int key_type_arg, data_t *key_data, |
| 4597 | int alg_arg, data_t *input_data ) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4598 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4599 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4600 | psa_key_type_t key_type = key_type_arg; |
| 4601 | psa_algorithm_t alg = alg_arg; |
| 4602 | size_t key_bits; |
| 4603 | unsigned char *signature = NULL; |
| 4604 | size_t signature_size; |
| 4605 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4606 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4607 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4608 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4609 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4610 | 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] | 4611 | psa_set_key_algorithm( &attributes, alg ); |
| 4612 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4613 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4614 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4615 | &key ) ); |
| 4616 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4617 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4618 | |
| 4619 | /* Allocate a buffer which has the size advertized by the |
| 4620 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4621 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4622 | key_bits, alg ); |
| 4623 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4624 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4625 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4626 | |
| 4627 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4628 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4629 | input_data->x, input_data->len, |
| 4630 | signature, signature_size, |
| 4631 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4632 | /* Check that the signature length looks sensible. */ |
| 4633 | TEST_ASSERT( signature_length <= signature_size ); |
| 4634 | TEST_ASSERT( signature_length > 0 ); |
| 4635 | |
| 4636 | /* Use the library to verify that the signature is correct. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4637 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4638 | input_data->x, input_data->len, |
| 4639 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4640 | |
| 4641 | if( input_data->len != 0 ) |
| 4642 | { |
| 4643 | /* Flip a bit in the input and verify that the signature is now |
| 4644 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 4645 | * because ECDSA may ignore the last few bits of the input. */ |
| 4646 | input_data->x[0] ^= 1; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4647 | TEST_EQUAL( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4648 | input_data->x, input_data->len, |
| 4649 | signature, signature_length ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4650 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4651 | } |
| 4652 | |
| 4653 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4654 | /* |
| 4655 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4656 | * thus reset them as required. |
| 4657 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4658 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4659 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4660 | psa_destroy_key( key ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4661 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4662 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4663 | } |
| 4664 | /* END_CASE */ |
| 4665 | |
| 4666 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 4667 | void verify_hash( int key_type_arg, data_t *key_data, |
| 4668 | int alg_arg, data_t *hash_data, |
| 4669 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4670 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4671 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4672 | psa_key_type_t key_type = key_type_arg; |
| 4673 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4674 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4675 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4676 | TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 4677 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4678 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4679 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4680 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4681 | psa_set_key_algorithm( &attributes, alg ); |
| 4682 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4683 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4684 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4685 | &key ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4686 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4687 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4688 | hash_data->x, hash_data->len, |
| 4689 | signature_data->x, signature_data->len ) ); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 4690 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4691 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4692 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4693 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4694 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4695 | } |
| 4696 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4697 | |
| 4698 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 4699 | void verify_hash_fail( int key_type_arg, data_t *key_data, |
| 4700 | int alg_arg, data_t *hash_data, |
| 4701 | data_t *signature_data, |
| 4702 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4703 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4704 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4705 | psa_key_type_t key_type = key_type_arg; |
| 4706 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4707 | psa_status_t actual_status; |
| 4708 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4709 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4710 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4711 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4712 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4713 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4714 | psa_set_key_algorithm( &attributes, alg ); |
| 4715 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4716 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4717 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4718 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4719 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4720 | actual_status = psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4721 | hash_data->x, hash_data->len, |
| 4722 | signature_data->x, signature_data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4723 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4724 | |
| 4725 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4726 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4727 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4728 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4729 | } |
| 4730 | /* END_CASE */ |
| 4731 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4732 | /* BEGIN_CASE */ |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 4733 | void sign_message_deterministic( int key_type_arg, |
| 4734 | data_t *key_data, |
| 4735 | int alg_arg, |
| 4736 | data_t *input_data, |
| 4737 | data_t *output_data ) |
| 4738 | { |
| 4739 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4740 | psa_key_type_t key_type = key_type_arg; |
| 4741 | psa_algorithm_t alg = alg_arg; |
| 4742 | size_t key_bits; |
| 4743 | unsigned char *signature = NULL; |
| 4744 | size_t signature_size; |
| 4745 | size_t signature_length = 0xdeadbeef; |
| 4746 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4747 | |
| 4748 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4749 | |
| 4750 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 4751 | psa_set_key_algorithm( &attributes, alg ); |
| 4752 | psa_set_key_type( &attributes, key_type ); |
| 4753 | |
| 4754 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4755 | &key ) ); |
| 4756 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4757 | key_bits = psa_get_key_bits( &attributes ); |
| 4758 | |
| 4759 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 4760 | TEST_ASSERT( signature_size != 0 ); |
| 4761 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
| 4762 | ASSERT_ALLOC( signature, signature_size ); |
| 4763 | |
| 4764 | PSA_ASSERT( psa_sign_message( key, alg, |
| 4765 | input_data->x, input_data->len, |
| 4766 | signature, signature_size, |
| 4767 | &signature_length ) ); |
| 4768 | |
| 4769 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 4770 | signature, signature_length ); |
| 4771 | |
| 4772 | exit: |
| 4773 | psa_reset_key_attributes( &attributes ); |
| 4774 | |
| 4775 | psa_destroy_key( key ); |
| 4776 | mbedtls_free( signature ); |
| 4777 | PSA_DONE( ); |
| 4778 | |
| 4779 | } |
| 4780 | /* END_CASE */ |
| 4781 | |
| 4782 | /* BEGIN_CASE */ |
| 4783 | void sign_message_fail( int key_type_arg, |
| 4784 | data_t *key_data, |
| 4785 | int alg_arg, |
| 4786 | data_t *input_data, |
| 4787 | int signature_size_arg, |
| 4788 | int expected_status_arg ) |
| 4789 | { |
| 4790 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4791 | psa_key_type_t key_type = key_type_arg; |
| 4792 | psa_algorithm_t alg = alg_arg; |
| 4793 | size_t signature_size = signature_size_arg; |
| 4794 | psa_status_t actual_status; |
| 4795 | psa_status_t expected_status = expected_status_arg; |
| 4796 | unsigned char *signature = NULL; |
| 4797 | size_t signature_length = 0xdeadbeef; |
| 4798 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4799 | |
| 4800 | ASSERT_ALLOC( signature, signature_size ); |
| 4801 | |
| 4802 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4803 | |
| 4804 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 4805 | psa_set_key_algorithm( &attributes, alg ); |
| 4806 | psa_set_key_type( &attributes, key_type ); |
| 4807 | |
| 4808 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4809 | &key ) ); |
| 4810 | |
| 4811 | actual_status = psa_sign_message( key, alg, |
| 4812 | input_data->x, input_data->len, |
| 4813 | signature, signature_size, |
| 4814 | &signature_length ); |
| 4815 | TEST_EQUAL( actual_status, expected_status ); |
| 4816 | /* The value of *signature_length is unspecified on error, but |
| 4817 | * whatever it is, it should be less than signature_size, so that |
| 4818 | * if the caller tries to read *signature_length bytes without |
| 4819 | * checking the error code then they don't overflow a buffer. */ |
| 4820 | TEST_ASSERT( signature_length <= signature_size ); |
| 4821 | |
| 4822 | exit: |
| 4823 | psa_reset_key_attributes( &attributes ); |
| 4824 | psa_destroy_key( key ); |
| 4825 | mbedtls_free( signature ); |
| 4826 | PSA_DONE( ); |
| 4827 | } |
| 4828 | /* END_CASE */ |
| 4829 | |
| 4830 | /* BEGIN_CASE */ |
| 4831 | void sign_verify_message( int key_type_arg, |
| 4832 | data_t *key_data, |
| 4833 | int alg_arg, |
| 4834 | data_t *input_data ) |
| 4835 | { |
| 4836 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4837 | psa_key_type_t key_type = key_type_arg; |
| 4838 | psa_algorithm_t alg = alg_arg; |
| 4839 | size_t key_bits; |
| 4840 | unsigned char *signature = NULL; |
| 4841 | size_t signature_size; |
| 4842 | size_t signature_length = 0xdeadbeef; |
| 4843 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4844 | |
| 4845 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4846 | |
| 4847 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 4848 | PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 4849 | psa_set_key_algorithm( &attributes, alg ); |
| 4850 | psa_set_key_type( &attributes, key_type ); |
| 4851 | |
| 4852 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4853 | &key ) ); |
| 4854 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4855 | key_bits = psa_get_key_bits( &attributes ); |
| 4856 | |
| 4857 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 4858 | TEST_ASSERT( signature_size != 0 ); |
| 4859 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
| 4860 | ASSERT_ALLOC( signature, signature_size ); |
| 4861 | |
| 4862 | PSA_ASSERT( psa_sign_message( key, alg, |
| 4863 | input_data->x, input_data->len, |
| 4864 | signature, signature_size, |
| 4865 | &signature_length ) ); |
| 4866 | TEST_ASSERT( signature_length <= signature_size ); |
| 4867 | TEST_ASSERT( signature_length > 0 ); |
| 4868 | |
| 4869 | PSA_ASSERT( psa_verify_message( key, alg, |
| 4870 | input_data->x, input_data->len, |
| 4871 | signature, signature_length ) ); |
| 4872 | |
| 4873 | if( input_data->len != 0 ) |
| 4874 | { |
| 4875 | /* Flip a bit in the input and verify that the signature is now |
| 4876 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 4877 | * because ECDSA may ignore the last few bits of the input. */ |
| 4878 | input_data->x[0] ^= 1; |
| 4879 | TEST_EQUAL( psa_verify_message( key, alg, |
| 4880 | input_data->x, input_data->len, |
| 4881 | signature, signature_length ), |
| 4882 | PSA_ERROR_INVALID_SIGNATURE ); |
| 4883 | } |
| 4884 | |
| 4885 | exit: |
| 4886 | psa_reset_key_attributes( &attributes ); |
| 4887 | |
| 4888 | psa_destroy_key( key ); |
| 4889 | mbedtls_free( signature ); |
| 4890 | PSA_DONE( ); |
| 4891 | } |
| 4892 | /* END_CASE */ |
| 4893 | |
| 4894 | /* BEGIN_CASE */ |
| 4895 | void verify_message( int key_type_arg, |
| 4896 | data_t *key_data, |
| 4897 | int alg_arg, |
| 4898 | data_t *input_data, |
| 4899 | data_t *signature_data ) |
| 4900 | { |
| 4901 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4902 | psa_key_type_t key_type = key_type_arg; |
| 4903 | psa_algorithm_t alg = alg_arg; |
| 4904 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4905 | |
| 4906 | TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE ); |
| 4907 | |
| 4908 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4909 | |
| 4910 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 4911 | psa_set_key_algorithm( &attributes, alg ); |
| 4912 | psa_set_key_type( &attributes, key_type ); |
| 4913 | |
| 4914 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4915 | &key ) ); |
| 4916 | |
| 4917 | PSA_ASSERT( psa_verify_message( key, alg, |
| 4918 | input_data->x, input_data->len, |
| 4919 | signature_data->x, signature_data->len ) ); |
| 4920 | |
| 4921 | exit: |
| 4922 | psa_reset_key_attributes( &attributes ); |
| 4923 | psa_destroy_key( key ); |
| 4924 | PSA_DONE( ); |
| 4925 | } |
| 4926 | /* END_CASE */ |
| 4927 | |
| 4928 | /* BEGIN_CASE */ |
| 4929 | void verify_message_fail( int key_type_arg, |
| 4930 | data_t *key_data, |
| 4931 | int alg_arg, |
| 4932 | data_t *hash_data, |
| 4933 | data_t *signature_data, |
| 4934 | int expected_status_arg ) |
| 4935 | { |
| 4936 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4937 | psa_key_type_t key_type = key_type_arg; |
| 4938 | psa_algorithm_t alg = alg_arg; |
| 4939 | psa_status_t actual_status; |
| 4940 | psa_status_t expected_status = expected_status_arg; |
| 4941 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4942 | |
| 4943 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4944 | |
| 4945 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 4946 | psa_set_key_algorithm( &attributes, alg ); |
| 4947 | psa_set_key_type( &attributes, key_type ); |
| 4948 | |
| 4949 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4950 | &key ) ); |
| 4951 | |
| 4952 | actual_status = psa_verify_message( key, alg, |
| 4953 | hash_data->x, hash_data->len, |
| 4954 | signature_data->x, |
| 4955 | signature_data->len ); |
| 4956 | TEST_EQUAL( actual_status, expected_status ); |
| 4957 | |
| 4958 | exit: |
| 4959 | psa_reset_key_attributes( &attributes ); |
| 4960 | psa_destroy_key( key ); |
| 4961 | PSA_DONE( ); |
| 4962 | } |
| 4963 | /* END_CASE */ |
| 4964 | |
| 4965 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4966 | void asymmetric_encrypt( int key_type_arg, |
| 4967 | data_t *key_data, |
| 4968 | int alg_arg, |
| 4969 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4970 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4971 | int expected_output_length_arg, |
| 4972 | int expected_status_arg ) |
| 4973 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4974 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4975 | psa_key_type_t key_type = key_type_arg; |
| 4976 | psa_algorithm_t alg = alg_arg; |
| 4977 | size_t expected_output_length = expected_output_length_arg; |
| 4978 | size_t key_bits; |
| 4979 | unsigned char *output = NULL; |
| 4980 | size_t output_size; |
| 4981 | size_t output_length = ~0; |
| 4982 | psa_status_t actual_status; |
| 4983 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4984 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4985 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4986 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4987 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4988 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4989 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4990 | psa_set_key_algorithm( &attributes, alg ); |
| 4991 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4992 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4993 | &key ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4994 | |
| 4995 | /* Determine the maximum output length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4996 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4997 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4998 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4999 | 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] | 5000 | TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5001 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5002 | |
| 5003 | /* Encrypt the input */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5004 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5005 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5006 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5007 | output, output_size, |
| 5008 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5009 | TEST_EQUAL( actual_status, expected_status ); |
| 5010 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5011 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5012 | /* If the label is empty, the test framework puts a non-null pointer |
| 5013 | * in label->x. Test that a null pointer works as well. */ |
| 5014 | if( label->len == 0 ) |
| 5015 | { |
| 5016 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 5017 | if( output_size != 0 ) |
| 5018 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5019 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5020 | input_data->x, input_data->len, |
| 5021 | NULL, label->len, |
| 5022 | output, output_size, |
| 5023 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5024 | TEST_EQUAL( actual_status, expected_status ); |
| 5025 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5026 | } |
| 5027 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5028 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5029 | /* |
| 5030 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5031 | * thus reset them as required. |
| 5032 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5033 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5034 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5035 | psa_destroy_key( key ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5036 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5037 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5038 | } |
| 5039 | /* END_CASE */ |
| 5040 | |
| 5041 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5042 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 5043 | data_t *key_data, |
| 5044 | int alg_arg, |
| 5045 | data_t *input_data, |
| 5046 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5047 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5048 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5049 | psa_key_type_t key_type = key_type_arg; |
| 5050 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5051 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5052 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5053 | size_t output_size; |
| 5054 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 5055 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5056 | size_t output2_size; |
| 5057 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5058 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5059 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5060 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5061 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5062 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 5063 | psa_set_key_algorithm( &attributes, alg ); |
| 5064 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 5065 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5066 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5067 | &key ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5068 | |
| 5069 | /* Determine the maximum ciphertext length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5070 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5071 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5072 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5073 | 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] | 5074 | TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5075 | ASSERT_ALLOC( output, output_size ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5076 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5077 | output2_size = input_data->len; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5078 | TEST_ASSERT( output2_size <= |
| 5079 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) ); |
| 5080 | TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5081 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5082 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 5083 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 5084 | * the original plaintext because of the non-optional random |
| 5085 | * part of encryption process which prevents using fixed vectors. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5086 | PSA_ASSERT( psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5087 | input_data->x, input_data->len, |
| 5088 | label->x, label->len, |
| 5089 | output, output_size, |
| 5090 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5091 | /* We don't know what ciphertext length to expect, but check that |
| 5092 | * it looks sensible. */ |
| 5093 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 5094 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5095 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5096 | output, output_length, |
| 5097 | label->x, label->len, |
| 5098 | output2, output2_size, |
| 5099 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 5100 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 5101 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5102 | |
| 5103 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5104 | /* |
| 5105 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5106 | * thus reset them as required. |
| 5107 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5108 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5109 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5110 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 5111 | mbedtls_free( output ); |
| 5112 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5113 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5114 | } |
| 5115 | /* END_CASE */ |
| 5116 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5117 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5118 | void asymmetric_decrypt( int key_type_arg, |
| 5119 | data_t *key_data, |
| 5120 | int alg_arg, |
| 5121 | data_t *input_data, |
| 5122 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 5123 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5124 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5125 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5126 | psa_key_type_t key_type = key_type_arg; |
| 5127 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5128 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5129 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 5130 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5131 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5132 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5133 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5134 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5135 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5136 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 5137 | psa_set_key_algorithm( &attributes, alg ); |
| 5138 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 5139 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5140 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5141 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5142 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5143 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5144 | key_bits = psa_get_key_bits( &attributes ); |
| 5145 | |
| 5146 | /* Determine the maximum ciphertext length */ |
| 5147 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 5148 | TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
| 5149 | ASSERT_ALLOC( output, output_size ); |
| 5150 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5151 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5152 | input_data->x, input_data->len, |
| 5153 | label->x, label->len, |
| 5154 | output, |
| 5155 | output_size, |
| 5156 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 5157 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 5158 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5159 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5160 | /* If the label is empty, the test framework puts a non-null pointer |
| 5161 | * in label->x. Test that a null pointer works as well. */ |
| 5162 | if( label->len == 0 ) |
| 5163 | { |
| 5164 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 5165 | if( output_size != 0 ) |
| 5166 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5167 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5168 | input_data->x, input_data->len, |
| 5169 | NULL, label->len, |
| 5170 | output, |
| 5171 | output_size, |
| 5172 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 5173 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 5174 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5175 | } |
| 5176 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5177 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5178 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5179 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 5180 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5181 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5182 | } |
| 5183 | /* END_CASE */ |
| 5184 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5185 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5186 | void asymmetric_decrypt_fail( int key_type_arg, |
| 5187 | data_t *key_data, |
| 5188 | int alg_arg, |
| 5189 | data_t *input_data, |
| 5190 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 5191 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5192 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5193 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5194 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5195 | psa_key_type_t key_type = key_type_arg; |
| 5196 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5197 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 5198 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5199 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5200 | psa_status_t actual_status; |
| 5201 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5202 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5203 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5204 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 5205 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5206 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5207 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5208 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 5209 | psa_set_key_algorithm( &attributes, alg ); |
| 5210 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 5211 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5212 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5213 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5214 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5215 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 5216 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5217 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 5218 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5219 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5220 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5221 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5222 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5223 | /* If the label is empty, the test framework puts a non-null pointer |
| 5224 | * in label->x. Test that a null pointer works as well. */ |
| 5225 | if( label->len == 0 ) |
| 5226 | { |
| 5227 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 5228 | if( output_size != 0 ) |
| 5229 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5230 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5231 | input_data->x, input_data->len, |
| 5232 | NULL, label->len, |
| 5233 | output, output_size, |
| 5234 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5235 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5236 | TEST_ASSERT( output_length <= output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5237 | } |
| 5238 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5239 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5240 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5241 | psa_destroy_key( key ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5242 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5243 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5244 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 5245 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5246 | |
| 5247 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5248 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 5249 | { |
| 5250 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 5251 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 5252 | * though it's OK by the C standard. We could test for this, but we'd need |
| 5253 | * to supress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 5254 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5255 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 5256 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5257 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 5258 | |
| 5259 | memset( &zero, 0, sizeof( zero ) ); |
| 5260 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5261 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5262 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5263 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5264 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5265 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5266 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5267 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 5268 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5269 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5270 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 5271 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 5272 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 5273 | } |
| 5274 | /* END_CASE */ |
| 5275 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 5276 | /* BEGIN_CASE */ |
| 5277 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5278 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5279 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5280 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5281 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5282 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5283 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5284 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 5285 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 5286 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5287 | |
| 5288 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5289 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5290 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5291 | } |
| 5292 | /* END_CASE */ |
| 5293 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5294 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 5295 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 5296 | int expected_status_arg ) |
| 5297 | { |
| 5298 | psa_algorithm_t alg = alg_arg; |
| 5299 | size_t capacity = capacity_arg; |
| 5300 | psa_status_t expected_status = expected_status_arg; |
| 5301 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5302 | |
| 5303 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5304 | |
| 5305 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 5306 | |
| 5307 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 5308 | expected_status ); |
| 5309 | |
| 5310 | exit: |
| 5311 | psa_key_derivation_abort( &operation ); |
| 5312 | PSA_DONE( ); |
| 5313 | } |
| 5314 | /* END_CASE */ |
| 5315 | |
| 5316 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5317 | void derive_input( int alg_arg, |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 5318 | int step_arg1, int key_type_arg1, data_t *input1, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5319 | int expected_status_arg1, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 5320 | int step_arg2, int key_type_arg2, data_t *input2, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5321 | int expected_status_arg2, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 5322 | int step_arg3, int key_type_arg3, data_t *input3, |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 5323 | int expected_status_arg3, |
| 5324 | int output_key_type_arg, int expected_output_status_arg ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5325 | { |
| 5326 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 5327 | psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3}; |
| 5328 | 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] | 5329 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 5330 | expected_status_arg2, |
| 5331 | expected_status_arg3}; |
| 5332 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5333 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 5334 | MBEDTLS_SVC_KEY_ID_INIT, |
| 5335 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5336 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5337 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5338 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 5339 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5340 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 5341 | psa_status_t expected_output_status = expected_output_status_arg; |
| 5342 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5343 | |
| 5344 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5345 | |
| 5346 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5347 | psa_set_key_algorithm( &attributes, alg ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5348 | |
| 5349 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 5350 | |
| 5351 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 5352 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5353 | if( key_types[i] != PSA_KEY_TYPE_NONE ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5354 | { |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 5355 | psa_set_key_type( &attributes, key_types[i] ); |
| 5356 | PSA_ASSERT( psa_import_key( &attributes, |
| 5357 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5358 | &keys[i] ) ); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 5359 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) && |
| 5360 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5361 | { |
| 5362 | // When taking a private key as secret input, use key agreement |
| 5363 | // to add the shared secret to the derivation |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5364 | TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self( |
| 5365 | &operation, keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 5366 | expected_statuses[i] ); |
| 5367 | } |
| 5368 | else |
| 5369 | { |
| 5370 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5371 | keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 5372 | expected_statuses[i] ); |
| 5373 | } |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 5374 | } |
| 5375 | else |
| 5376 | { |
| 5377 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 5378 | &operation, steps[i], |
| 5379 | inputs[i]->x, inputs[i]->len ), |
| 5380 | expected_statuses[i] ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5381 | } |
| 5382 | } |
| 5383 | |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 5384 | if( output_key_type != PSA_KEY_TYPE_NONE ) |
| 5385 | { |
| 5386 | psa_reset_key_attributes( &attributes ); |
| 5387 | psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA ); |
| 5388 | psa_set_key_bits( &attributes, 8 ); |
| 5389 | actual_output_status = |
| 5390 | psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5391 | &output_key ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 5392 | } |
| 5393 | else |
| 5394 | { |
| 5395 | uint8_t buffer[1]; |
| 5396 | actual_output_status = |
| 5397 | psa_key_derivation_output_bytes( &operation, |
| 5398 | buffer, sizeof( buffer ) ); |
| 5399 | } |
| 5400 | TEST_EQUAL( actual_output_status, expected_output_status ); |
| 5401 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5402 | exit: |
| 5403 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5404 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 5405 | psa_destroy_key( keys[i] ); |
| 5406 | psa_destroy_key( output_key ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5407 | PSA_DONE( ); |
| 5408 | } |
| 5409 | /* END_CASE */ |
| 5410 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 5411 | /* BEGIN_CASE */ |
| 5412 | void test_derive_invalid_key_derivation_state( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 5413 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 5414 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5415 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 5416 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5417 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 5418 | unsigned char input1[] = "Input 1"; |
| 5419 | size_t input1_length = sizeof( input1 ); |
| 5420 | unsigned char input2[] = "Input 2"; |
| 5421 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5422 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 5423 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 5424 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 5425 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 5426 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5427 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 5428 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5429 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5430 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5431 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5432 | psa_set_key_algorithm( &attributes, alg ); |
| 5433 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5434 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 5435 | PSA_ASSERT( psa_import_key( &attributes, |
| 5436 | key_data, sizeof( key_data ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5437 | &key ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5438 | |
| 5439 | /* valid key derivation */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5440 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 5441 | input1, input1_length, |
| 5442 | input2, input2_length, |
| 5443 | capacity ) ) |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 5444 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5445 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5446 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 5447 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 5448 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5449 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5450 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5451 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5452 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5453 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5454 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5455 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5456 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5457 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5458 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5459 | } |
| 5460 | /* END_CASE */ |
| 5461 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5462 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5463 | void test_derive_invalid_key_derivation_tests( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5464 | { |
| 5465 | uint8_t output_buffer[16]; |
| 5466 | size_t buffer_size = 16; |
| 5467 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5468 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5469 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5470 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 5471 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5472 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5473 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5474 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5475 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5476 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5477 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5478 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5479 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 5480 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5481 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5482 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5483 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5484 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5485 | |
| 5486 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5487 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 5488 | } |
| 5489 | /* END_CASE */ |
| 5490 | |
| 5491 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5492 | void derive_output( int alg_arg, |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5493 | int step1_arg, data_t *input1, |
| 5494 | int step2_arg, data_t *input2, |
| 5495 | int step3_arg, data_t *input3, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5496 | int requested_capacity_arg, |
| 5497 | data_t *expected_output1, |
| 5498 | data_t *expected_output2 ) |
| 5499 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5500 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5501 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg}; |
| 5502 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5503 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 5504 | MBEDTLS_SVC_KEY_ID_INIT, |
| 5505 | MBEDTLS_SVC_KEY_ID_INIT }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5506 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5507 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5508 | uint8_t *expected_outputs[2] = |
| 5509 | {expected_output1->x, expected_output2->x}; |
| 5510 | size_t output_sizes[2] = |
| 5511 | {expected_output1->len, expected_output2->len}; |
| 5512 | size_t output_buffer_size = 0; |
| 5513 | uint8_t *output_buffer = NULL; |
| 5514 | size_t expected_capacity; |
| 5515 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5516 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5517 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5518 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5519 | |
| 5520 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 5521 | { |
| 5522 | if( output_sizes[i] > output_buffer_size ) |
| 5523 | output_buffer_size = output_sizes[i]; |
| 5524 | if( output_sizes[i] == 0 ) |
| 5525 | expected_outputs[i] = NULL; |
| 5526 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5527 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5528 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5529 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5530 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5531 | psa_set_key_algorithm( &attributes, alg ); |
| 5532 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5533 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5534 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5535 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 5536 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 5537 | requested_capacity ) ); |
| 5538 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5539 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5540 | switch( steps[i] ) |
| 5541 | { |
| 5542 | case 0: |
| 5543 | break; |
| 5544 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5545 | PSA_ASSERT( psa_import_key( &attributes, |
| 5546 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5547 | &keys[i] ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5548 | |
| 5549 | if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 5550 | { |
| 5551 | PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) ); |
| 5552 | TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <= |
| 5553 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ); |
| 5554 | } |
| 5555 | |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5556 | PSA_ASSERT( psa_key_derivation_input_key( |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5557 | &operation, steps[i], keys[i] ) ); |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5558 | break; |
| 5559 | default: |
| 5560 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 5561 | &operation, steps[i], |
| 5562 | inputs[i]->x, inputs[i]->len ) ); |
| 5563 | break; |
| 5564 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5565 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5566 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5567 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5568 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5569 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5570 | expected_capacity = requested_capacity; |
| 5571 | |
| 5572 | /* Expansion phase. */ |
| 5573 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 5574 | { |
| 5575 | /* Read some bytes. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5576 | status = psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5577 | output_buffer, output_sizes[i] ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5578 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 5579 | { |
| 5580 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 5581 | TEST_ASSERT( status == PSA_SUCCESS || |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5582 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5583 | continue; |
| 5584 | } |
| 5585 | else if( expected_capacity == 0 || |
| 5586 | output_sizes[i] > expected_capacity ) |
| 5587 | { |
| 5588 | /* Capacity exceeded. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5589 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5590 | expected_capacity = 0; |
| 5591 | continue; |
| 5592 | } |
| 5593 | /* Success. Check the read data. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5594 | PSA_ASSERT( status ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5595 | if( output_sizes[i] != 0 ) |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5596 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 5597 | expected_outputs[i], output_sizes[i] ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5598 | /* Check the operation status. */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5599 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5600 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5601 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5602 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5603 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5604 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5605 | |
| 5606 | exit: |
| 5607 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5608 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5609 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 5610 | psa_destroy_key( keys[i] ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5611 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5612 | } |
| 5613 | /* END_CASE */ |
| 5614 | |
| 5615 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5616 | void derive_full( int alg_arg, |
| 5617 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 5618 | data_t *input1, |
| 5619 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5620 | int requested_capacity_arg ) |
| 5621 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5622 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5623 | psa_algorithm_t alg = alg_arg; |
| 5624 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5625 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5626 | unsigned char output_buffer[16]; |
| 5627 | size_t expected_capacity = requested_capacity; |
| 5628 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5629 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5630 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5631 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5632 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5633 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5634 | psa_set_key_algorithm( &attributes, alg ); |
| 5635 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5636 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5637 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5638 | &key ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5639 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5640 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 5641 | input1->x, input1->len, |
| 5642 | input2->x, input2->len, |
| 5643 | requested_capacity ) ) |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 5644 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 5645 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5646 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5647 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5648 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5649 | |
| 5650 | /* Expansion phase. */ |
| 5651 | while( current_capacity > 0 ) |
| 5652 | { |
| 5653 | size_t read_size = sizeof( output_buffer ); |
| 5654 | if( read_size > current_capacity ) |
| 5655 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5656 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5657 | output_buffer, |
| 5658 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5659 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5660 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5661 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5662 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5663 | } |
| 5664 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5665 | /* Check that the operation refuses to go over capacity. */ |
| 5666 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5667 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5668 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5669 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5670 | |
| 5671 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5672 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5673 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5674 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5675 | } |
| 5676 | /* END_CASE */ |
| 5677 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 5678 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5679 | void derive_key_exercise( int alg_arg, |
| 5680 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 5681 | data_t *input1, |
| 5682 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5683 | int derived_type_arg, |
| 5684 | int derived_bits_arg, |
| 5685 | int derived_usage_arg, |
| 5686 | int derived_alg_arg ) |
| 5687 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5688 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5689 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5690 | psa_algorithm_t alg = alg_arg; |
| 5691 | psa_key_type_t derived_type = derived_type_arg; |
| 5692 | size_t derived_bits = derived_bits_arg; |
| 5693 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 5694 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 5695 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5696 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5697 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5698 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5699 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5700 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5701 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5702 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5703 | psa_set_key_algorithm( &attributes, alg ); |
| 5704 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5705 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5706 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5707 | |
| 5708 | /* Derive a key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5709 | if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 5710 | input1->x, input1->len, |
| 5711 | input2->x, input2->len, |
| 5712 | capacity ) ) |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 5713 | goto exit; |
| 5714 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5715 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 5716 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 5717 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5718 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5719 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5720 | &derived_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5721 | |
| 5722 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5723 | PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5724 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 5725 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5726 | |
| 5727 | /* Exercise the derived key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5728 | if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5729 | goto exit; |
| 5730 | |
| 5731 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5732 | /* |
| 5733 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5734 | * thus reset them as required. |
| 5735 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5736 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5737 | |
| 5738 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5739 | psa_destroy_key( base_key ); |
| 5740 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5741 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5742 | } |
| 5743 | /* END_CASE */ |
| 5744 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 5745 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5746 | void derive_key_export( int alg_arg, |
| 5747 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 5748 | data_t *input1, |
| 5749 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5750 | int bytes1_arg, |
| 5751 | int bytes2_arg ) |
| 5752 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5753 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5754 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5755 | psa_algorithm_t alg = alg_arg; |
| 5756 | size_t bytes1 = bytes1_arg; |
| 5757 | size_t bytes2 = bytes2_arg; |
| 5758 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5759 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5760 | uint8_t *output_buffer = NULL; |
| 5761 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5762 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5763 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5764 | size_t length; |
| 5765 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5766 | ASSERT_ALLOC( output_buffer, capacity ); |
| 5767 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5768 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5769 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5770 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 5771 | psa_set_key_algorithm( &base_attributes, alg ); |
| 5772 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5773 | 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] | 5774 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5775 | |
| 5776 | /* Derive some material and output it. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5777 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 5778 | input1->x, input1->len, |
| 5779 | input2->x, input2->len, |
| 5780 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 5781 | goto exit; |
| 5782 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5783 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5784 | output_buffer, |
| 5785 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5786 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5787 | |
| 5788 | /* 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] | 5789 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 5790 | input1->x, input1->len, |
| 5791 | input2->x, input2->len, |
| 5792 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 5793 | goto exit; |
| 5794 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5795 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 5796 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 5797 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5798 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5799 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5800 | &derived_key ) ); |
| 5801 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5802 | export_buffer, bytes1, |
| 5803 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5804 | TEST_EQUAL( length, bytes1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5805 | PSA_ASSERT( psa_destroy_key( derived_key ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5806 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5807 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5808 | &derived_key ) ); |
| 5809 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5810 | export_buffer + bytes1, bytes2, |
| 5811 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5812 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5813 | |
| 5814 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5815 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 5816 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5817 | |
| 5818 | exit: |
| 5819 | mbedtls_free( output_buffer ); |
| 5820 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5821 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5822 | psa_destroy_key( base_key ); |
| 5823 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5824 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5825 | } |
| 5826 | /* END_CASE */ |
| 5827 | |
| 5828 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 5829 | void derive_key( int alg_arg, |
| 5830 | data_t *key_data, data_t *input1, data_t *input2, |
| 5831 | int type_arg, int bits_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 5832 | int expected_status_arg, |
| 5833 | int is_large_output ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5834 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5835 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5836 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5837 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 5838 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5839 | size_t bits = bits_arg; |
| 5840 | psa_status_t expected_status = expected_status_arg; |
| 5841 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5842 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5843 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5844 | |
| 5845 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5846 | |
| 5847 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 5848 | psa_set_key_algorithm( &base_attributes, alg ); |
| 5849 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 5850 | 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] | 5851 | &base_key ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5852 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5853 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 5854 | input1->x, input1->len, |
| 5855 | input2->x, input2->len, |
| 5856 | SIZE_MAX ) ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5857 | goto exit; |
| 5858 | |
| 5859 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 5860 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 5861 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5862 | psa_set_key_bits( &derived_attributes, bits ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 5863 | |
| 5864 | psa_status_t status = |
| 5865 | psa_key_derivation_output_key( &derived_attributes, |
| 5866 | &operation, |
| 5867 | &derived_key ); |
| 5868 | if( is_large_output > 0 ) |
| 5869 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5870 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5871 | |
| 5872 | exit: |
| 5873 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5874 | psa_destroy_key( base_key ); |
| 5875 | psa_destroy_key( derived_key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5876 | PSA_DONE( ); |
| 5877 | } |
| 5878 | /* END_CASE */ |
| 5879 | |
| 5880 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5881 | void key_agreement_setup( int alg_arg, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 5882 | int our_key_type_arg, int our_key_alg_arg, |
| 5883 | data_t *our_key_data, data_t *peer_key_data, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5884 | int expected_status_arg ) |
| 5885 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5886 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5887 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5888 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5889 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5890 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5891 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5892 | psa_status_t expected_status = expected_status_arg; |
| 5893 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5894 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5895 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5896 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5897 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5898 | psa_set_key_algorithm( &attributes, our_key_alg ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5899 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5900 | PSA_ASSERT( psa_import_key( &attributes, |
| 5901 | our_key_data->x, our_key_data->len, |
| 5902 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5903 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5904 | /* The tests currently include inputs that should fail at either step. |
| 5905 | * Test cases that fail at the setup step should be changed to call |
| 5906 | * key_derivation_setup instead, and this function should be renamed |
| 5907 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5908 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5909 | if( status == PSA_SUCCESS ) |
| 5910 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5911 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 5912 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 5913 | our_key, |
| 5914 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5915 | expected_status ); |
| 5916 | } |
| 5917 | else |
| 5918 | { |
| 5919 | TEST_ASSERT( status == expected_status ); |
| 5920 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5921 | |
| 5922 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5923 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5924 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5925 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5926 | } |
| 5927 | /* END_CASE */ |
| 5928 | |
| 5929 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5930 | void raw_key_agreement( int alg_arg, |
| 5931 | int our_key_type_arg, data_t *our_key_data, |
| 5932 | data_t *peer_key_data, |
| 5933 | data_t *expected_output ) |
| 5934 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5935 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5936 | psa_algorithm_t alg = alg_arg; |
| 5937 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5938 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5939 | unsigned char *output = NULL; |
| 5940 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5941 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5942 | |
| 5943 | ASSERT_ALLOC( output, expected_output->len ); |
| 5944 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5945 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5946 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5947 | psa_set_key_algorithm( &attributes, alg ); |
| 5948 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5949 | PSA_ASSERT( psa_import_key( &attributes, |
| 5950 | our_key_data->x, our_key_data->len, |
| 5951 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5952 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5953 | PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) ); |
| 5954 | key_bits = psa_get_key_bits( &attributes ); |
| 5955 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5956 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 5957 | peer_key_data->x, peer_key_data->len, |
| 5958 | output, expected_output->len, |
| 5959 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5960 | ASSERT_COMPARE( output, output_length, |
| 5961 | expected_output->x, expected_output->len ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5962 | TEST_ASSERT( output_length <= |
| 5963 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) ); |
| 5964 | TEST_ASSERT( output_length <= |
| 5965 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5966 | |
| 5967 | exit: |
| 5968 | mbedtls_free( output ); |
| 5969 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5970 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5971 | } |
| 5972 | /* END_CASE */ |
| 5973 | |
| 5974 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5975 | void key_agreement_capacity( int alg_arg, |
| 5976 | int our_key_type_arg, data_t *our_key_data, |
| 5977 | data_t *peer_key_data, |
| 5978 | int expected_capacity_arg ) |
| 5979 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5980 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5981 | psa_algorithm_t alg = alg_arg; |
| 5982 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5983 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5984 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5985 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5986 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5987 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5988 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5989 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5990 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5991 | psa_set_key_algorithm( &attributes, alg ); |
| 5992 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5993 | PSA_ASSERT( psa_import_key( &attributes, |
| 5994 | our_key_data->x, our_key_data->len, |
| 5995 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5996 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5997 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5998 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 5999 | &operation, |
| 6000 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 6001 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 6002 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 6003 | { |
| 6004 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6005 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 6006 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 6007 | NULL, 0 ) ); |
| 6008 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6009 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 6010 | /* Test the advertized capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 6011 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6012 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6013 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6014 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 6015 | /* Test the actual capacity by reading the output. */ |
| 6016 | while( actual_capacity > sizeof( output ) ) |
| 6017 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6018 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6019 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 6020 | actual_capacity -= sizeof( output ); |
| 6021 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6022 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6023 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6024 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 6025 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 6026 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6027 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6028 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6029 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6030 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6031 | } |
| 6032 | /* END_CASE */ |
| 6033 | |
| 6034 | /* BEGIN_CASE */ |
| 6035 | void key_agreement_output( int alg_arg, |
| 6036 | int our_key_type_arg, data_t *our_key_data, |
| 6037 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 6038 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6039 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6040 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6041 | psa_algorithm_t alg = alg_arg; |
| 6042 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6043 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6044 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 6045 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6046 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 6047 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 6048 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6049 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6050 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6051 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6052 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 6053 | psa_set_key_algorithm( &attributes, alg ); |
| 6054 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6055 | PSA_ASSERT( psa_import_key( &attributes, |
| 6056 | our_key_data->x, our_key_data->len, |
| 6057 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6058 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6059 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6060 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 6061 | &operation, |
| 6062 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 6063 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 6064 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 6065 | { |
| 6066 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6067 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 6068 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 6069 | NULL, 0 ) ); |
| 6070 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6071 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6072 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6073 | actual_output, |
| 6074 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 6075 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 6076 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 6077 | if( expected_output2->len != 0 ) |
| 6078 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6079 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6080 | actual_output, |
| 6081 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 6082 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 6083 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 6084 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6085 | |
| 6086 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6087 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6088 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6089 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6090 | mbedtls_free( actual_output ); |
| 6091 | } |
| 6092 | /* END_CASE */ |
| 6093 | |
| 6094 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6095 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6096 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6097 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6098 | unsigned char *output = NULL; |
| 6099 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6100 | size_t i; |
| 6101 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6102 | |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 6103 | TEST_ASSERT( bytes_arg >= 0 ); |
| 6104 | |
Gilles Peskine | 9189202 | 2021-02-08 19:50:26 +0100 | [diff] [blame] | 6105 | ASSERT_ALLOC( output, bytes ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6106 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6107 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6108 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6109 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6110 | /* Run several times, to ensure that every output byte will be |
| 6111 | * nonzero at least once with overwhelming probability |
| 6112 | * (2^(-8*number_of_runs)). */ |
| 6113 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6114 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 6115 | if( bytes != 0 ) |
| 6116 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6117 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6118 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6119 | for( i = 0; i < bytes; i++ ) |
| 6120 | { |
| 6121 | if( output[i] != 0 ) |
| 6122 | ++changed[i]; |
| 6123 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6124 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6125 | |
| 6126 | /* Check that every byte was changed to nonzero at least once. This |
| 6127 | * validates that psa_generate_random is overwriting every byte of |
| 6128 | * the output buffer. */ |
| 6129 | for( i = 0; i < bytes; i++ ) |
| 6130 | { |
| 6131 | TEST_ASSERT( changed[i] != 0 ); |
| 6132 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6133 | |
| 6134 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6135 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6136 | mbedtls_free( output ); |
| 6137 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6138 | } |
| 6139 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6140 | |
| 6141 | /* BEGIN_CASE */ |
| 6142 | void generate_key( int type_arg, |
| 6143 | int bits_arg, |
| 6144 | int usage_arg, |
| 6145 | int alg_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 6146 | int expected_status_arg, |
| 6147 | int is_large_key ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6148 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6149 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6150 | psa_key_type_t type = type_arg; |
| 6151 | psa_key_usage_t usage = usage_arg; |
| 6152 | size_t bits = bits_arg; |
| 6153 | psa_algorithm_t alg = alg_arg; |
| 6154 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6155 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6156 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6157 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6158 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6159 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6160 | psa_set_key_usage_flags( &attributes, usage ); |
| 6161 | psa_set_key_algorithm( &attributes, alg ); |
| 6162 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 6163 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6164 | |
| 6165 | /* Generate a key */ |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 6166 | psa_status_t status = psa_generate_key( &attributes, &key ); |
| 6167 | |
| 6168 | if( is_large_key > 0 ) |
| 6169 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 6170 | TEST_EQUAL( status , expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6171 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6172 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6173 | |
| 6174 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6175 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6176 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 6177 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6178 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 6179 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 6180 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 6181 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6182 | |
| 6183 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6184 | /* |
| 6185 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6186 | * thus reset them as required. |
| 6187 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6188 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6189 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6190 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6191 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6192 | } |
| 6193 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 6194 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 6195 | /* 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] | 6196 | void generate_key_rsa( int bits_arg, |
| 6197 | data_t *e_arg, |
| 6198 | int expected_status_arg ) |
| 6199 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6200 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6201 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6202 | size_t bits = bits_arg; |
| 6203 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 6204 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 6205 | psa_status_t expected_status = expected_status_arg; |
| 6206 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6207 | uint8_t *exported = NULL; |
| 6208 | size_t exported_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 6209 | PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6210 | size_t exported_length = SIZE_MAX; |
| 6211 | uint8_t *e_read_buffer = NULL; |
| 6212 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 6213 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6214 | size_t e_read_length = SIZE_MAX; |
| 6215 | |
| 6216 | if( e_arg->len == 0 || |
| 6217 | ( e_arg->len == 3 && |
| 6218 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 6219 | { |
| 6220 | is_default_public_exponent = 1; |
| 6221 | e_read_size = 0; |
| 6222 | } |
| 6223 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 6224 | ASSERT_ALLOC( exported, exported_size ); |
| 6225 | |
| 6226 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6227 | |
| 6228 | psa_set_key_usage_flags( &attributes, usage ); |
| 6229 | psa_set_key_algorithm( &attributes, alg ); |
| 6230 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 6231 | e_arg->x, e_arg->len ) ); |
| 6232 | psa_set_key_bits( &attributes, bits ); |
| 6233 | |
| 6234 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6235 | TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6236 | if( expected_status != PSA_SUCCESS ) |
| 6237 | goto exit; |
| 6238 | |
| 6239 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6240 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6241 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 6242 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 6243 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 6244 | e_read_buffer, e_read_size, |
| 6245 | &e_read_length ) ); |
| 6246 | if( is_default_public_exponent ) |
| 6247 | TEST_EQUAL( e_read_length, 0 ); |
| 6248 | else |
| 6249 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 6250 | |
| 6251 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 6252 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6253 | goto exit; |
| 6254 | |
| 6255 | /* Export the key and check the public exponent. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6256 | PSA_ASSERT( psa_export_public_key( key, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6257 | exported, exported_size, |
| 6258 | &exported_length ) ); |
| 6259 | { |
| 6260 | uint8_t *p = exported; |
| 6261 | uint8_t *end = exported + exported_length; |
| 6262 | size_t len; |
| 6263 | /* RSAPublicKey ::= SEQUENCE { |
| 6264 | * modulus INTEGER, -- n |
| 6265 | * publicExponent INTEGER } -- e |
| 6266 | */ |
| 6267 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6268 | MBEDTLS_ASN1_SEQUENCE | |
| 6269 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 6270 | TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6271 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 6272 | MBEDTLS_ASN1_INTEGER ) ); |
| 6273 | if( len >= 1 && p[0] == 0 ) |
| 6274 | { |
| 6275 | ++p; |
| 6276 | --len; |
| 6277 | } |
| 6278 | if( e_arg->len == 0 ) |
| 6279 | { |
| 6280 | TEST_EQUAL( len, 3 ); |
| 6281 | TEST_EQUAL( p[0], 1 ); |
| 6282 | TEST_EQUAL( p[1], 0 ); |
| 6283 | TEST_EQUAL( p[2], 1 ); |
| 6284 | } |
| 6285 | else |
| 6286 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 6287 | } |
| 6288 | |
| 6289 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6290 | /* |
| 6291 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 6292 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 6293 | */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6294 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6295 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6296 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6297 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6298 | mbedtls_free( e_read_buffer ); |
| 6299 | mbedtls_free( exported ); |
| 6300 | } |
| 6301 | /* END_CASE */ |
| 6302 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6303 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6304 | void persistent_key_load_key_from_storage( data_t *data, |
| 6305 | int type_arg, int bits_arg, |
| 6306 | int usage_flags_arg, int alg_arg, |
| 6307 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6308 | { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 6309 | 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] | 6310 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6311 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6312 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6313 | psa_key_type_t type = type_arg; |
| 6314 | size_t bits = bits_arg; |
| 6315 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 6316 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6317 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6318 | unsigned char *first_export = NULL; |
| 6319 | unsigned char *second_export = NULL; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 6320 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6321 | size_t first_exported_length; |
| 6322 | size_t second_exported_length; |
| 6323 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6324 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 6325 | { |
| 6326 | ASSERT_ALLOC( first_export, export_size ); |
| 6327 | ASSERT_ALLOC( second_export, export_size ); |
| 6328 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6329 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6330 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6331 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 6332 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6333 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 6334 | psa_set_key_algorithm( &attributes, alg ); |
| 6335 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 6336 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6337 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6338 | switch( generation_method ) |
| 6339 | { |
| 6340 | case IMPORT_KEY: |
| 6341 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6342 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6343 | &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6344 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6345 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6346 | case GENERATE_KEY: |
| 6347 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6348 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6349 | break; |
| 6350 | |
| 6351 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 6352 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6353 | { |
| 6354 | /* Create base key */ |
| 6355 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 6356 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6357 | psa_set_key_usage_flags( &base_attributes, |
| 6358 | PSA_KEY_USAGE_DERIVE ); |
| 6359 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 6360 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6361 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 6362 | data->x, data->len, |
| 6363 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6364 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6365 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6366 | PSA_ASSERT( psa_key_derivation_input_key( |
| 6367 | &operation, |
| 6368 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6369 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6370 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6371 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6372 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 6373 | &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6374 | &key ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6375 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6376 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6377 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6378 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 6379 | #else |
| 6380 | TEST_ASSUME( ! "KDF not supported in this configuration" ); |
| 6381 | #endif |
| 6382 | break; |
| 6383 | |
| 6384 | default: |
| 6385 | TEST_ASSERT( ! "generation_method not implemented in test" ); |
| 6386 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6387 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6388 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6389 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6390 | /* Export the key if permitted by the key policy. */ |
| 6391 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 6392 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6393 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6394 | first_export, export_size, |
| 6395 | &first_exported_length ) ); |
| 6396 | if( generation_method == IMPORT_KEY ) |
| 6397 | ASSERT_COMPARE( data->x, data->len, |
| 6398 | first_export, first_exported_length ); |
| 6399 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6400 | |
| 6401 | /* Shutdown and restart */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6402 | PSA_ASSERT( psa_purge_key( key ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6403 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6404 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6405 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6406 | /* Check key slot still contains key data */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6407 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 6408 | TEST_ASSERT( mbedtls_svc_key_id_equal( |
| 6409 | psa_get_key_id( &attributes ), key_id ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6410 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 6411 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 6412 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 6413 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 6414 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags ); |
| 6415 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6416 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6417 | /* Export the key again if permitted by the key policy. */ |
| 6418 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6419 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6420 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6421 | second_export, export_size, |
| 6422 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6423 | ASSERT_COMPARE( first_export, first_exported_length, |
| 6424 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6425 | } |
| 6426 | |
| 6427 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 6428 | if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6429 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6430 | |
| 6431 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6432 | /* |
| 6433 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6434 | * thus reset them as required. |
| 6435 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6436 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6437 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6438 | mbedtls_free( first_export ); |
| 6439 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6440 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6441 | psa_destroy_key( base_key ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6442 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6443 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6444 | } |
| 6445 | /* END_CASE */ |