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 | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 267 | /*! |
| 268 | * \brief Internal Function for AEAD multipart tests. |
| 269 | * |
| 270 | * \param key_type_arg Type of key passed in |
| 271 | * \param key_data The encryption / decryption key data |
| 272 | * \param alg_arg The type of algorithm used |
| 273 | * \param nonce Nonce data |
| 274 | * \param additional_data Additional data |
| 275 | * \param ad_part_len If not -1, the length of chunks to |
| 276 | * feed additional data in to be encrypted / |
| 277 | * decrypted. If -1, no chunking. |
| 278 | * \param input_data Data to encrypt / decrypt |
| 279 | * \param data_part_len If not -1, the length of chunks to feed the |
| 280 | * data in to be encrypted / decrypted. If -1, |
| 281 | * no chunking |
| 282 | * \param do_set_lengths If non-zero, then set lengths prior to |
| 283 | * calling encryption / decryption. |
| 284 | * \param expected_output Expected output |
Paul Elliott | 41ffae1 | 2021-07-22 21:52:01 +0100 | [diff] [blame] | 285 | * \param expect_valid_signature If non zero, we expect the signature to be |
| 286 | * valid |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 287 | * \param is_encrypt If non-zero this is an encryption operation. |
Paul Elliott | 41ffae1 | 2021-07-22 21:52:01 +0100 | [diff] [blame] | 288 | * \param do_zero_parts If non-zero, interleave zero length chunks |
| 289 | * with normal length chunks |
| 290 | * \param swap_set_functions If non-zero, swap the order of set lengths |
| 291 | * and set nonce. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 292 | * |
| 293 | * \return int Zero on failure, non-zero on success. |
| 294 | * |
| 295 | */ |
| 296 | static int aead_multipart_internal_func( int key_type_arg, data_t *key_data, |
| 297 | int alg_arg, |
| 298 | data_t *nonce, |
| 299 | data_t *additional_data, |
| 300 | int ad_part_len, |
| 301 | data_t *input_data, |
| 302 | int data_part_len, |
| 303 | int do_set_lengths, |
| 304 | data_t *expected_output, |
| 305 | int expect_valid_signature, |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 306 | int is_encrypt, |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 307 | int do_zero_parts, |
| 308 | int swap_set_functions ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 309 | { |
| 310 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 311 | psa_key_type_t key_type = key_type_arg; |
| 312 | psa_algorithm_t alg = alg_arg; |
| 313 | psa_aead_operation_t operation; |
| 314 | unsigned char *output_data = NULL; |
| 315 | unsigned char *part_data = NULL; |
| 316 | unsigned char *final_data = NULL; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 317 | size_t data_true_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 318 | size_t part_data_size = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 319 | size_t output_size = 0; |
| 320 | size_t final_output_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 321 | size_t output_length = 0; |
| 322 | size_t key_bits = 0; |
| 323 | size_t tag_length = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 324 | uint32_t part_offset = 0; |
| 325 | size_t part_length = 0; |
| 326 | size_t output_part_length = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 327 | size_t tag_size = 0; |
| 328 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 329 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 330 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 331 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 332 | int test_ok = 0; |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 333 | uint32_t part_count = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 334 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 335 | PSA_ASSERT( psa_crypto_init( ) ); |
| 336 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 337 | if( is_encrypt ) |
| 338 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 339 | else |
| 340 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 341 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 342 | psa_set_key_algorithm( &attributes, alg ); |
| 343 | psa_set_key_type( &attributes, key_type ); |
| 344 | |
| 345 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 346 | &key ) ); |
| 347 | |
| 348 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 349 | key_bits = psa_get_key_bits( &attributes ); |
| 350 | |
| 351 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 352 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 353 | if( is_encrypt ) |
| 354 | { |
| 355 | /* Tag gets written at end of buffer. */ |
| 356 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 357 | ( input_data->len + |
| 358 | tag_length ) ); |
| 359 | data_true_size = input_data->len; |
| 360 | } |
| 361 | else |
| 362 | { |
| 363 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 364 | ( input_data->len - |
| 365 | tag_length ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 366 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 367 | /* Do not want to attempt to decrypt tag. */ |
| 368 | data_true_size = input_data->len - tag_length; |
| 369 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 370 | |
| 371 | ASSERT_ALLOC( output_data, output_size ); |
| 372 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 373 | if( is_encrypt ) |
| 374 | { |
| 375 | final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg ); |
| 376 | TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE ); |
| 377 | } |
| 378 | else |
| 379 | { |
| 380 | final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 381 | TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
| 382 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 383 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 384 | ASSERT_ALLOC( final_data, final_output_size ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 385 | |
| 386 | operation = psa_aead_operation_init( ); |
| 387 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 388 | |
| 389 | if( is_encrypt ) |
| 390 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 391 | else |
| 392 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 393 | |
| 394 | /* If the operation is not supported, just skip and not fail in case the |
| 395 | * encryption involves a common limitation of cryptography hardwares and |
| 396 | * an alternative implementation. */ |
| 397 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 398 | { |
| 399 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 400 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 401 | } |
| 402 | |
| 403 | PSA_ASSERT( status ); |
| 404 | |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 405 | if( swap_set_functions ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 406 | { |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 407 | if( do_set_lengths ) |
| 408 | { |
| 409 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 410 | data_true_size ) ); |
| 411 | } |
| 412 | |
| 413 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 414 | } |
| 415 | else |
| 416 | { |
| 417 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 418 | |
| 419 | if( do_set_lengths ) |
| 420 | { |
| 421 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 422 | data_true_size ) ); |
| 423 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 424 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 425 | |
| 426 | if( ad_part_len != -1 ) |
| 427 | { |
| 428 | /* Pass additional data in parts */ |
| 429 | part_offset = 0; |
| 430 | |
| 431 | while( part_offset < additional_data->len ) |
| 432 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 433 | if( do_zero_parts && part_count++ & 0x01 ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 434 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 435 | part_length = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 436 | } |
| 437 | else |
| 438 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 439 | if( additional_data->len - part_offset < |
| 440 | ( uint32_t ) ad_part_len ) |
| 441 | { |
| 442 | part_length = additional_data->len - part_offset; |
| 443 | } |
| 444 | else |
| 445 | { |
| 446 | part_length = ad_part_len; |
| 447 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | PSA_ASSERT( psa_aead_update_ad( &operation, |
| 451 | additional_data->x + part_offset, |
| 452 | part_length ) ); |
| 453 | |
| 454 | part_offset += part_length; |
| 455 | } |
| 456 | } |
| 457 | else |
| 458 | { |
| 459 | /* Pass additional data in one go. */ |
| 460 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 461 | additional_data->len ) ); |
| 462 | } |
| 463 | |
| 464 | if( data_part_len != -1 ) |
| 465 | { |
| 466 | /* Pass data in parts */ |
| 467 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 468 | ( size_t ) data_part_len ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 469 | |
| 470 | ASSERT_ALLOC( part_data, part_data_size ); |
| 471 | |
| 472 | part_offset = 0; |
| 473 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 474 | while( part_offset < data_true_size ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 475 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 476 | if( do_zero_parts && part_count++ & 0x01 ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 477 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 478 | part_length = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 479 | } |
| 480 | else |
| 481 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 482 | if( ( data_true_size - part_offset ) < ( uint32_t ) data_part_len ) |
| 483 | { |
| 484 | part_length = ( data_true_size - part_offset ); |
| 485 | } |
| 486 | else |
| 487 | { |
| 488 | part_length = data_part_len; |
| 489 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | PSA_ASSERT( psa_aead_update( &operation, |
| 493 | ( input_data->x + part_offset ), |
| 494 | part_length, part_data, |
| 495 | part_data_size, |
| 496 | &output_part_length ) ); |
| 497 | |
| 498 | if( output_data && output_part_length ) |
| 499 | { |
| 500 | memcpy( ( output_data + part_offset ), part_data, |
| 501 | output_part_length ); |
| 502 | } |
| 503 | |
| 504 | part_offset += part_length; |
| 505 | output_length += output_part_length; |
| 506 | } |
| 507 | } |
| 508 | else |
| 509 | { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 510 | /* Pass all data in one go. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 511 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 512 | data_true_size, output_data, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 513 | output_size, &output_length ) ); |
| 514 | } |
| 515 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 516 | if( is_encrypt ) |
| 517 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 518 | final_output_size, |
| 519 | &output_part_length, |
| 520 | tag_buffer, tag_length, |
| 521 | &tag_size ) ); |
| 522 | else |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 523 | { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 524 | status = psa_aead_verify( &operation, final_data, |
| 525 | final_output_size, |
| 526 | &output_part_length, |
| 527 | ( input_data->x + data_true_size ), |
| 528 | tag_length ); |
| 529 | |
| 530 | if( status != PSA_SUCCESS ) |
| 531 | { |
| 532 | if( !expect_valid_signature ) |
| 533 | { |
| 534 | /* Expected failure. */ |
| 535 | test_ok = 1; |
| 536 | goto exit; |
| 537 | } |
| 538 | else |
| 539 | PSA_ASSERT( status ); |
| 540 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 541 | } |
| 542 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 543 | if( output_data && output_part_length ) |
| 544 | memcpy( ( output_data + output_length ), final_data, |
| 545 | output_part_length ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 546 | |
| 547 | output_length += output_part_length; |
| 548 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 549 | |
| 550 | /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE |
| 551 | * should be exact.*/ |
| 552 | if( is_encrypt ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 553 | { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 554 | TEST_EQUAL( tag_length, tag_size ); |
| 555 | |
| 556 | if( output_data && tag_length ) |
| 557 | memcpy( ( output_data + output_length ), tag_buffer, |
| 558 | tag_length ); |
| 559 | |
| 560 | output_length += tag_length; |
| 561 | |
| 562 | TEST_EQUAL( output_length, |
| 563 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 564 | input_data->len ) ); |
| 565 | TEST_ASSERT( output_length <= |
| 566 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 567 | } |
| 568 | else |
| 569 | { |
| 570 | TEST_EQUAL( output_length, |
| 571 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, |
| 572 | input_data->len ) ); |
| 573 | TEST_ASSERT( output_length <= |
| 574 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 575 | } |
| 576 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 577 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 578 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 579 | output_data, output_length ); |
| 580 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 581 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 582 | test_ok = 1; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 583 | |
| 584 | exit: |
| 585 | psa_destroy_key( key ); |
| 586 | psa_aead_abort( &operation ); |
| 587 | mbedtls_free( output_data ); |
| 588 | mbedtls_free( part_data ); |
| 589 | mbedtls_free( final_data ); |
| 590 | PSA_DONE( ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 591 | |
| 592 | return( test_ok ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 593 | } |
| 594 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 595 | /* END_HEADER */ |
| 596 | |
| 597 | /* BEGIN_DEPENDENCIES |
| 598 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 599 | * END_DEPENDENCIES |
| 600 | */ |
| 601 | |
| 602 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 603 | void static_checks( ) |
| 604 | { |
| 605 | size_t max_truncated_mac_size = |
| 606 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 607 | |
| 608 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 609 | * encoding. The shifted mask is the maximum truncated value. The |
| 610 | * untruncated algorithm may be one byte larger. */ |
| 611 | TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size ); |
| 612 | } |
| 613 | /* END_CASE */ |
| 614 | |
| 615 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 616 | void import_with_policy( int type_arg, |
| 617 | int usage_arg, int alg_arg, |
| 618 | int expected_status_arg ) |
| 619 | { |
| 620 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 621 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 622 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 623 | psa_key_type_t type = type_arg; |
| 624 | psa_key_usage_t usage = usage_arg; |
| 625 | psa_algorithm_t alg = alg_arg; |
| 626 | psa_status_t expected_status = expected_status_arg; |
| 627 | const uint8_t key_material[16] = {0}; |
| 628 | psa_status_t status; |
| 629 | |
| 630 | PSA_ASSERT( psa_crypto_init( ) ); |
| 631 | |
| 632 | psa_set_key_type( &attributes, type ); |
| 633 | psa_set_key_usage_flags( &attributes, usage ); |
| 634 | psa_set_key_algorithm( &attributes, alg ); |
| 635 | |
| 636 | status = psa_import_key( &attributes, |
| 637 | key_material, sizeof( key_material ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 638 | &key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 639 | TEST_EQUAL( status, expected_status ); |
| 640 | if( status != PSA_SUCCESS ) |
| 641 | goto exit; |
| 642 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 643 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 644 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 645 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 646 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 647 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 648 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 649 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 650 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 651 | |
| 652 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 653 | /* |
| 654 | * Key attributes may have been returned by psa_get_key_attributes() |
| 655 | * thus reset them as required. |
| 656 | */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 657 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 658 | |
| 659 | psa_destroy_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 660 | PSA_DONE( ); |
| 661 | } |
| 662 | /* END_CASE */ |
| 663 | |
| 664 | /* BEGIN_CASE */ |
| 665 | void import_with_data( data_t *data, int type_arg, |
| 666 | int attr_bits_arg, |
| 667 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 668 | { |
| 669 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 670 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 671 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 672 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 673 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 674 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 675 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 676 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 677 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 678 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 679 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 680 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 681 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 682 | status = psa_import_key( &attributes, data->x, data->len, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 683 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 684 | if( status != PSA_SUCCESS ) |
| 685 | goto exit; |
| 686 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 687 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 688 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 689 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 690 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 691 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 692 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 693 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 694 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 695 | |
| 696 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 697 | /* |
| 698 | * Key attributes may have been returned by psa_get_key_attributes() |
| 699 | * thus reset them as required. |
| 700 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 701 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 702 | |
| 703 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 704 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 705 | } |
| 706 | /* END_CASE */ |
| 707 | |
| 708 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 709 | void import_large_key( int type_arg, int byte_size_arg, |
| 710 | int expected_status_arg ) |
| 711 | { |
| 712 | psa_key_type_t type = type_arg; |
| 713 | size_t byte_size = byte_size_arg; |
| 714 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 715 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 716 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 717 | psa_status_t status; |
| 718 | uint8_t *buffer = NULL; |
| 719 | size_t buffer_size = byte_size + 1; |
| 720 | size_t n; |
| 721 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 722 | /* Skip the test case if the target running the test cannot |
| 723 | * accomodate large keys due to heap size constraints */ |
| 724 | ASSERT_ALLOC_WEAK( buffer, buffer_size ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 725 | memset( buffer, 'K', byte_size ); |
| 726 | |
| 727 | PSA_ASSERT( psa_crypto_init( ) ); |
| 728 | |
| 729 | /* Try importing the key */ |
| 730 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 731 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 732 | status = psa_import_key( &attributes, buffer, byte_size, &key ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 733 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 734 | TEST_EQUAL( status, expected_status ); |
| 735 | |
| 736 | if( status == PSA_SUCCESS ) |
| 737 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 738 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 739 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 740 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 741 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 742 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 743 | memset( buffer, 0, byte_size + 1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 744 | PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 745 | for( n = 0; n < byte_size; n++ ) |
| 746 | TEST_EQUAL( buffer[n], 'K' ); |
| 747 | for( n = byte_size; n < buffer_size; n++ ) |
| 748 | TEST_EQUAL( buffer[n], 0 ); |
| 749 | } |
| 750 | |
| 751 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 752 | /* |
| 753 | * Key attributes may have been returned by psa_get_key_attributes() |
| 754 | * thus reset them as required. |
| 755 | */ |
| 756 | psa_reset_key_attributes( &attributes ); |
| 757 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 758 | psa_destroy_key( key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 759 | PSA_DONE( ); |
| 760 | mbedtls_free( buffer ); |
| 761 | } |
| 762 | /* END_CASE */ |
| 763 | |
| 764 | /* BEGIN_CASE */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 765 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 766 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 767 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 768 | size_t bits = bits_arg; |
| 769 | psa_status_t expected_status = expected_status_arg; |
| 770 | psa_status_t status; |
| 771 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 772 | 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] | 773 | size_t buffer_size = /* Slight overapproximations */ |
| 774 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 775 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 776 | unsigned char *p; |
| 777 | int ret; |
| 778 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 779 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 780 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 781 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 782 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 783 | |
| 784 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 785 | bits, keypair ) ) >= 0 ); |
| 786 | length = ret; |
| 787 | |
| 788 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 789 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 790 | status = psa_import_key( &attributes, p, length, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 791 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 792 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 793 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 794 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 795 | |
| 796 | exit: |
| 797 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 798 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 799 | } |
| 800 | /* END_CASE */ |
| 801 | |
| 802 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 803 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 804 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 805 | int usage_arg, int alg_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 806 | int expected_bits, |
| 807 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 808 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 809 | int canonical_input ) |
| 810 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 811 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 812 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 813 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 814 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 815 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 816 | unsigned char *exported = NULL; |
| 817 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 818 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 819 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 820 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 821 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 822 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 823 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 824 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 825 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 826 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 827 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 828 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 829 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 830 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 831 | psa_set_key_algorithm( &attributes, alg ); |
| 832 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 833 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 834 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 835 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 836 | |
| 837 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 838 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 839 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 840 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 841 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 842 | |
| 843 | /* Export the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 844 | status = psa_export_key( key, exported, export_size, &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 845 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 846 | |
| 847 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 848 | * and export_size. On errors, the exported length must be 0. */ |
| 849 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 850 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
| 851 | TEST_ASSERT( exported_length <= export_size ); |
| 852 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 853 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 854 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 855 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 856 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 857 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 858 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 859 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 860 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 861 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 862 | * this validates the canonical representations. For canonical inputs, |
| 863 | * this doesn't directly validate the implementation, but it still helps |
| 864 | * by cross-validating the test data with the sanity check code. */ |
| 865 | if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) ) |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 866 | goto exit; |
| 867 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 868 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 869 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 870 | else |
| 871 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 872 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 873 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 874 | &key2 ) ); |
| 875 | PSA_ASSERT( psa_export_key( key2, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 876 | reexported, |
| 877 | export_size, |
| 878 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 879 | ASSERT_COMPARE( exported, exported_length, |
| 880 | reexported, reexported_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 881 | PSA_ASSERT( psa_destroy_key( key2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 882 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 883 | TEST_ASSERT( exported_length <= |
| 884 | PSA_EXPORT_KEY_OUTPUT_SIZE( type, |
| 885 | psa_get_key_bits( &got_attributes ) ) ); |
| 886 | TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 887 | |
| 888 | destroy: |
| 889 | /* Destroy the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 890 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 891 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 892 | |
| 893 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 894 | /* |
| 895 | * Key attributes may have been returned by psa_get_key_attributes() |
| 896 | * thus reset them as required. |
| 897 | */ |
| 898 | psa_reset_key_attributes( &got_attributes ); |
| 899 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 900 | mbedtls_free( exported ); |
| 901 | mbedtls_free( reexported ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 902 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 903 | } |
| 904 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 905 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 906 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 907 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 908 | int type_arg, |
| 909 | int alg_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 910 | int export_size_delta, |
| 911 | int expected_export_status_arg, |
| 912 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 913 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 914 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 915 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 916 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 917 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 918 | psa_status_t status; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 919 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 920 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 921 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 922 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 923 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 924 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 925 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 926 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 927 | psa_set_key_algorithm( &attributes, alg ); |
| 928 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 929 | |
| 930 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 931 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 932 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 933 | /* Export the public key */ |
| 934 | ASSERT_ALLOC( exported, export_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 935 | status = psa_export_public_key( key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 936 | exported, export_size, |
| 937 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 938 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 939 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 940 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 941 | 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] | 942 | size_t bits; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 943 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 944 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 945 | TEST_ASSERT( expected_public_key->len <= |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 946 | PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 947 | TEST_ASSERT( expected_public_key->len <= |
| 948 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 949 | TEST_ASSERT( expected_public_key->len <= |
| 950 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 951 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 952 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 953 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 954 | |
| 955 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 956 | /* |
| 957 | * Key attributes may have been returned by psa_get_key_attributes() |
| 958 | * thus reset them as required. |
| 959 | */ |
| 960 | psa_reset_key_attributes( &attributes ); |
| 961 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 962 | mbedtls_free( exported ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 963 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 964 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 965 | } |
| 966 | /* END_CASE */ |
| 967 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 968 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 969 | void import_and_exercise_key( data_t *data, |
| 970 | int type_arg, |
| 971 | int bits_arg, |
| 972 | int alg_arg ) |
| 973 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 974 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 975 | psa_key_type_t type = type_arg; |
| 976 | size_t bits = bits_arg; |
| 977 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 978 | 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] | 979 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 980 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 981 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 982 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 983 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 984 | psa_set_key_usage_flags( &attributes, usage ); |
| 985 | psa_set_key_algorithm( &attributes, alg ); |
| 986 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 987 | |
| 988 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 989 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 990 | |
| 991 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 992 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 993 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 994 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 995 | |
| 996 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 997 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 998 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 999 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1000 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1001 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1002 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1003 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1004 | /* |
| 1005 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1006 | * thus reset them as required. |
| 1007 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1008 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1009 | |
| 1010 | psa_reset_key_attributes( &attributes ); |
| 1011 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1012 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1013 | } |
| 1014 | /* END_CASE */ |
| 1015 | |
| 1016 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1017 | void effective_key_attributes( int type_arg, int expected_type_arg, |
| 1018 | int bits_arg, int expected_bits_arg, |
| 1019 | int usage_arg, int expected_usage_arg, |
| 1020 | int alg_arg, int expected_alg_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1021 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1022 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1023 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1024 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1025 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1026 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1027 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1028 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1029 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1030 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1031 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1032 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1033 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1034 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1035 | psa_set_key_usage_flags( &attributes, usage ); |
| 1036 | psa_set_key_algorithm( &attributes, alg ); |
| 1037 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1038 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1039 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1040 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1041 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1042 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1043 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1044 | TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type ); |
| 1045 | TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits ); |
| 1046 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1047 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1048 | |
| 1049 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1050 | /* |
| 1051 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1052 | * thus reset them as required. |
| 1053 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1054 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1055 | |
| 1056 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1057 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1058 | } |
| 1059 | /* END_CASE */ |
| 1060 | |
| 1061 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1062 | void check_key_policy( int type_arg, int bits_arg, |
| 1063 | int usage_arg, int alg_arg ) |
| 1064 | { |
| 1065 | test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg, |
| 1066 | usage_arg, usage_arg, alg_arg, alg_arg ); |
| 1067 | goto exit; |
| 1068 | } |
| 1069 | /* END_CASE */ |
| 1070 | |
| 1071 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1072 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1073 | { |
| 1074 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1075 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1076 | * though it's OK by the C standard. We could test for this, but we'd need |
| 1077 | * to supress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1078 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 1079 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1080 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1081 | |
| 1082 | memset( &zero, 0, sizeof( zero ) ); |
| 1083 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1084 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1085 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1086 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1087 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1088 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 1089 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 1090 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 1091 | |
| 1092 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 1093 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 1094 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 1095 | |
| 1096 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 1097 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 1098 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 1099 | |
| 1100 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 1101 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 1102 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1103 | } |
| 1104 | /* END_CASE */ |
| 1105 | |
| 1106 | /* BEGIN_CASE */ |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1107 | void mac_key_policy( int policy_usage, |
| 1108 | int policy_alg, |
| 1109 | int key_type, |
| 1110 | data_t *key_data, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1111 | int exercise_alg, |
| 1112 | int expected_status_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1113 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1114 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1115 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1116 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1117 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1118 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1119 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1120 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1121 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1122 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1123 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1124 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1125 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1126 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1127 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1128 | &key ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1129 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1130 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1131 | if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1132 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1133 | else |
| 1134 | TEST_EQUAL( status, expected_status ); |
| 1135 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1136 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1137 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1138 | memset( mac, 0, sizeof( mac ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1139 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1140 | if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1141 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1142 | else |
| 1143 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1144 | |
| 1145 | exit: |
| 1146 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1147 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1148 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1149 | } |
| 1150 | /* END_CASE */ |
| 1151 | |
| 1152 | /* BEGIN_CASE */ |
| 1153 | void cipher_key_policy( int policy_usage, |
| 1154 | int policy_alg, |
| 1155 | int key_type, |
| 1156 | data_t *key_data, |
| 1157 | int exercise_alg ) |
| 1158 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1159 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1160 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1161 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1162 | psa_status_t status; |
| 1163 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1164 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1165 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1166 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1167 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1168 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1169 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1170 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1171 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1172 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1173 | status = psa_cipher_encrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1174 | if( policy_alg == exercise_alg && |
| 1175 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1176 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1177 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1178 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1179 | psa_cipher_abort( &operation ); |
| 1180 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1181 | status = psa_cipher_decrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1182 | if( policy_alg == exercise_alg && |
| 1183 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1184 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1185 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1186 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1187 | |
| 1188 | exit: |
| 1189 | psa_cipher_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1190 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1191 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1192 | } |
| 1193 | /* END_CASE */ |
| 1194 | |
| 1195 | /* BEGIN_CASE */ |
| 1196 | void aead_key_policy( int policy_usage, |
| 1197 | int policy_alg, |
| 1198 | int key_type, |
| 1199 | data_t *key_data, |
| 1200 | int nonce_length_arg, |
| 1201 | int tag_length_arg, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1202 | int exercise_alg, |
| 1203 | int expected_status_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1204 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1205 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1206 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1207 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1208 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1209 | unsigned char nonce[16] = {0}; |
| 1210 | size_t nonce_length = nonce_length_arg; |
| 1211 | unsigned char tag[16]; |
| 1212 | size_t tag_length = tag_length_arg; |
| 1213 | size_t output_length; |
| 1214 | |
| 1215 | TEST_ASSERT( nonce_length <= sizeof( nonce ) ); |
| 1216 | TEST_ASSERT( tag_length <= sizeof( tag ) ); |
| 1217 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1218 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1219 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1220 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1221 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1222 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1223 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1224 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1225 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1226 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1227 | status = psa_aead_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1228 | nonce, nonce_length, |
| 1229 | NULL, 0, |
| 1230 | NULL, 0, |
| 1231 | tag, tag_length, |
| 1232 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1233 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1234 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1235 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1236 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1237 | |
| 1238 | memset( tag, 0, sizeof( tag ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1239 | status = psa_aead_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1240 | nonce, nonce_length, |
| 1241 | NULL, 0, |
| 1242 | tag, tag_length, |
| 1243 | NULL, 0, |
| 1244 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1245 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 1246 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1247 | else if( expected_status == PSA_SUCCESS ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1248 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1249 | else |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1250 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1251 | |
| 1252 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1253 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1254 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1255 | } |
| 1256 | /* END_CASE */ |
| 1257 | |
| 1258 | /* BEGIN_CASE */ |
| 1259 | void asymmetric_encryption_key_policy( int policy_usage, |
| 1260 | int policy_alg, |
| 1261 | int key_type, |
| 1262 | data_t *key_data, |
| 1263 | int exercise_alg ) |
| 1264 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1265 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1266 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1267 | psa_status_t status; |
| 1268 | size_t key_bits; |
| 1269 | size_t buffer_length; |
| 1270 | unsigned char *buffer = NULL; |
| 1271 | size_t output_length; |
| 1272 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1273 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1274 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1275 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1276 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1277 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1278 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1279 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1280 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1281 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1282 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1283 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1284 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 1285 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1286 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1287 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1288 | status = psa_asymmetric_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1289 | NULL, 0, |
| 1290 | NULL, 0, |
| 1291 | buffer, buffer_length, |
| 1292 | &output_length ); |
| 1293 | if( policy_alg == exercise_alg && |
| 1294 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1295 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1296 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1297 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1298 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 1299 | if( buffer_length != 0 ) |
| 1300 | memset( buffer, 0, buffer_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1301 | status = psa_asymmetric_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1302 | buffer, buffer_length, |
| 1303 | NULL, 0, |
| 1304 | buffer, buffer_length, |
| 1305 | &output_length ); |
| 1306 | if( policy_alg == exercise_alg && |
| 1307 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1308 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1309 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1310 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1311 | |
| 1312 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1313 | /* |
| 1314 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1315 | * thus reset them as required. |
| 1316 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1317 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1318 | |
| 1319 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1320 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1321 | mbedtls_free( buffer ); |
| 1322 | } |
| 1323 | /* END_CASE */ |
| 1324 | |
| 1325 | /* BEGIN_CASE */ |
| 1326 | void asymmetric_signature_key_policy( int policy_usage, |
| 1327 | int policy_alg, |
| 1328 | int key_type, |
| 1329 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1330 | int exercise_alg, |
| 1331 | int payload_length_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1332 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1333 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1334 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1335 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1336 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 1337 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 1338 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 1339 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 1340 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 1341 | int compatible_alg = payload_length_arg > 0; |
| 1342 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1343 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1344 | size_t signature_length; |
| 1345 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1346 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1347 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1348 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1349 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1350 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1351 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1352 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1353 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1354 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1355 | status = psa_sign_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1356 | payload, payload_length, |
| 1357 | signature, sizeof( signature ), |
| 1358 | &signature_length ); |
| 1359 | if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1360 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1361 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1362 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1363 | |
| 1364 | memset( signature, 0, sizeof( signature ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1365 | status = psa_verify_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1366 | payload, payload_length, |
| 1367 | signature, sizeof( signature ) ); |
| 1368 | if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1369 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1370 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1371 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1372 | |
| 1373 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1374 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1375 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1376 | } |
| 1377 | /* END_CASE */ |
| 1378 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1379 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1380 | void derive_key_policy( int policy_usage, |
| 1381 | int policy_alg, |
| 1382 | int key_type, |
| 1383 | data_t *key_data, |
| 1384 | int exercise_alg ) |
| 1385 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1386 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1387 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1388 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1389 | psa_status_t status; |
| 1390 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1391 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1392 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1393 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1394 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1395 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1396 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1397 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1398 | &key ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1399 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1400 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 1401 | |
| 1402 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 1403 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1404 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1405 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 1406 | &operation, |
| 1407 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 1408 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1409 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1410 | |
| 1411 | status = psa_key_derivation_input_key( &operation, |
| 1412 | PSA_KEY_DERIVATION_INPUT_SECRET, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1413 | key ); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1414 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1415 | if( policy_alg == exercise_alg && |
| 1416 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1417 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1418 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1419 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1420 | |
| 1421 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1422 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1423 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1424 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1425 | } |
| 1426 | /* END_CASE */ |
| 1427 | |
| 1428 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1429 | void agreement_key_policy( int policy_usage, |
| 1430 | int policy_alg, |
| 1431 | int key_type_arg, |
| 1432 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1433 | int exercise_alg, |
| 1434 | int expected_status_arg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1435 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1436 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1437 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1438 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1439 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1440 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1441 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1442 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1443 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1444 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1445 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1446 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1447 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1448 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1449 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1450 | &key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1451 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1452 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1453 | status = mbedtls_test_psa_key_agreement_with_self( &operation, key ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1454 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1455 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1456 | |
| 1457 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1458 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1459 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1460 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1461 | } |
| 1462 | /* END_CASE */ |
| 1463 | |
| 1464 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1465 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 1466 | int usage_arg, int alg_arg, int alg2_arg ) |
| 1467 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1468 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1469 | psa_key_type_t key_type = key_type_arg; |
| 1470 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1471 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1472 | psa_key_usage_t usage = usage_arg; |
| 1473 | psa_algorithm_t alg = alg_arg; |
| 1474 | psa_algorithm_t alg2 = alg2_arg; |
| 1475 | |
| 1476 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1477 | |
| 1478 | psa_set_key_usage_flags( &attributes, usage ); |
| 1479 | psa_set_key_algorithm( &attributes, alg ); |
| 1480 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 1481 | psa_set_key_type( &attributes, key_type ); |
| 1482 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1483 | &key ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1484 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1485 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1486 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 1487 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 1488 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 1489 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1490 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1491 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1492 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1493 | goto exit; |
| 1494 | |
| 1495 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1496 | /* |
| 1497 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1498 | * thus reset them as required. |
| 1499 | */ |
| 1500 | psa_reset_key_attributes( &got_attributes ); |
| 1501 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1502 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1503 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1504 | } |
| 1505 | /* END_CASE */ |
| 1506 | |
| 1507 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1508 | void raw_agreement_key_policy( int policy_usage, |
| 1509 | int policy_alg, |
| 1510 | int key_type_arg, |
| 1511 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1512 | int exercise_alg, |
| 1513 | int expected_status_arg ) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1514 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1515 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1516 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1517 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1518 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1519 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1520 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1521 | |
| 1522 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1523 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1524 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1525 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1526 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1527 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1528 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1529 | &key ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1530 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1531 | status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1532 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1533 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1534 | |
| 1535 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1536 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1537 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1538 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1539 | } |
| 1540 | /* END_CASE */ |
| 1541 | |
| 1542 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1543 | void copy_success( int source_usage_arg, |
| 1544 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1545 | int type_arg, data_t *material, |
| 1546 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1547 | int target_usage_arg, |
| 1548 | int target_alg_arg, int target_alg2_arg, |
| 1549 | int expected_usage_arg, |
| 1550 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1551 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1552 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1553 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1554 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 1555 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1556 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1557 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1558 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1559 | uint8_t *export_buffer = NULL; |
| 1560 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1561 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1562 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1563 | /* Prepare the source key. */ |
| 1564 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 1565 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1566 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1567 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1568 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 1569 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1570 | &source_key ) ); |
| 1571 | PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1572 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1573 | /* Prepare the target attributes. */ |
| 1574 | if( copy_attributes ) |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1575 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1576 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1577 | /* Set volatile lifetime to reset the key identifier to 0. */ |
| 1578 | psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE ); |
| 1579 | } |
| 1580 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1581 | if( target_usage_arg != -1 ) |
| 1582 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 1583 | if( target_alg_arg != -1 ) |
| 1584 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1585 | if( target_alg2_arg != -1 ) |
| 1586 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1587 | |
| 1588 | /* Copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1589 | PSA_ASSERT( psa_copy_key( source_key, |
| 1590 | &target_attributes, &target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1591 | |
| 1592 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1593 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1594 | |
| 1595 | /* Test that the target slot has the expected content and policy. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1596 | PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1597 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 1598 | psa_get_key_type( &target_attributes ) ); |
| 1599 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 1600 | psa_get_key_bits( &target_attributes ) ); |
| 1601 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 1602 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1603 | TEST_EQUAL( expected_alg2, |
| 1604 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1605 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 1606 | { |
| 1607 | size_t length; |
| 1608 | ASSERT_ALLOC( export_buffer, material->len ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1609 | PSA_ASSERT( psa_export_key( target_key, export_buffer, |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1610 | material->len, &length ) ); |
| 1611 | ASSERT_COMPARE( material->x, material->len, |
| 1612 | export_buffer, length ); |
| 1613 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1614 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1615 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1616 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1617 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) ) |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1618 | goto exit; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1619 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1620 | PSA_ASSERT( psa_destroy_key( target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1621 | |
| 1622 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1623 | /* |
| 1624 | * Source and target key attributes may have been returned by |
| 1625 | * psa_get_key_attributes() thus reset them as required. |
| 1626 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1627 | psa_reset_key_attributes( &source_attributes ); |
| 1628 | psa_reset_key_attributes( &target_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1629 | |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1630 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1631 | mbedtls_free( export_buffer ); |
| 1632 | } |
| 1633 | /* END_CASE */ |
| 1634 | |
| 1635 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1636 | void copy_fail( int source_usage_arg, |
| 1637 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1638 | int type_arg, data_t *material, |
| 1639 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1640 | int target_usage_arg, |
| 1641 | int target_alg_arg, int target_alg2_arg, |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 1642 | int target_id_arg, int target_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1643 | int expected_status_arg ) |
| 1644 | { |
| 1645 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1646 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1647 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1648 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 1649 | 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] | 1650 | |
| 1651 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1652 | |
| 1653 | /* Prepare the source key. */ |
| 1654 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 1655 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1656 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1657 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1658 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 1659 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1660 | &source_key ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1661 | |
| 1662 | /* Prepare the target attributes. */ |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 1663 | psa_set_key_id( &target_attributes, key_id ); |
| 1664 | psa_set_key_lifetime( &target_attributes, target_lifetime_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1665 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 1666 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 1667 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 1668 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1669 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1670 | |
| 1671 | /* Try to copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1672 | TEST_EQUAL( psa_copy_key( source_key, |
| 1673 | &target_attributes, &target_key ), |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1674 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1675 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1676 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1677 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1678 | exit: |
| 1679 | psa_reset_key_attributes( &source_attributes ); |
| 1680 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1681 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1682 | } |
| 1683 | /* END_CASE */ |
| 1684 | |
| 1685 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1686 | void hash_operation_init( ) |
| 1687 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1688 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1689 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1690 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1691 | * though it's OK by the C standard. We could test for this, but we'd need |
| 1692 | * to supress the Clang warning for the test. */ |
| 1693 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 1694 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 1695 | psa_hash_operation_t zero; |
| 1696 | |
| 1697 | memset( &zero, 0, sizeof( zero ) ); |
| 1698 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1699 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1700 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 1701 | PSA_ERROR_BAD_STATE ); |
| 1702 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 1703 | PSA_ERROR_BAD_STATE ); |
| 1704 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 1705 | PSA_ERROR_BAD_STATE ); |
| 1706 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1707 | /* A default hash operation should be abortable without error. */ |
| 1708 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 1709 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 1710 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1711 | } |
| 1712 | /* END_CASE */ |
| 1713 | |
| 1714 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1715 | void hash_setup( int alg_arg, |
| 1716 | int expected_status_arg ) |
| 1717 | { |
| 1718 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1719 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1720 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1721 | psa_status_t status; |
| 1722 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1723 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1724 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1725 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1726 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1727 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 1728 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 1729 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1730 | |
| 1731 | /* If setup failed, reproduce the failure, so as to |
| 1732 | * test the resulting state of the operation object. */ |
| 1733 | if( status != PSA_SUCCESS ) |
| 1734 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 1735 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1736 | /* Now the operation object should be reusable. */ |
| 1737 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 1738 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 1739 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1740 | #endif |
| 1741 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1742 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1743 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1744 | } |
| 1745 | /* END_CASE */ |
| 1746 | |
| 1747 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1748 | void hash_compute_fail( int alg_arg, data_t *input, |
| 1749 | int output_size_arg, int expected_status_arg ) |
| 1750 | { |
| 1751 | psa_algorithm_t alg = alg_arg; |
| 1752 | uint8_t *output = NULL; |
| 1753 | size_t output_size = output_size_arg; |
| 1754 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 1755 | psa_status_t expected_status = expected_status_arg; |
| 1756 | psa_status_t status; |
| 1757 | |
| 1758 | ASSERT_ALLOC( output, output_size ); |
| 1759 | |
| 1760 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1761 | |
| 1762 | status = psa_hash_compute( alg, input->x, input->len, |
| 1763 | output, output_size, &output_length ); |
| 1764 | TEST_EQUAL( status, expected_status ); |
| 1765 | TEST_ASSERT( output_length <= output_size ); |
| 1766 | |
| 1767 | exit: |
| 1768 | mbedtls_free( output ); |
| 1769 | PSA_DONE( ); |
| 1770 | } |
| 1771 | /* END_CASE */ |
| 1772 | |
| 1773 | /* BEGIN_CASE */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 1774 | void hash_compare_fail( int alg_arg, data_t *input, |
| 1775 | data_t *reference_hash, |
| 1776 | int expected_status_arg ) |
| 1777 | { |
| 1778 | psa_algorithm_t alg = alg_arg; |
| 1779 | psa_status_t expected_status = expected_status_arg; |
| 1780 | psa_status_t status; |
| 1781 | |
| 1782 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1783 | |
| 1784 | status = psa_hash_compare( alg, input->x, input->len, |
| 1785 | reference_hash->x, reference_hash->len ); |
| 1786 | TEST_EQUAL( status, expected_status ); |
| 1787 | |
| 1788 | exit: |
| 1789 | PSA_DONE( ); |
| 1790 | } |
| 1791 | /* END_CASE */ |
| 1792 | |
| 1793 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1794 | void hash_compute_compare( int alg_arg, data_t *input, |
| 1795 | data_t *expected_output ) |
| 1796 | { |
| 1797 | psa_algorithm_t alg = alg_arg; |
| 1798 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 1799 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 1800 | size_t i; |
| 1801 | |
| 1802 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1803 | |
| 1804 | /* Compute with tight buffer */ |
| 1805 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1806 | output, PSA_HASH_LENGTH( alg ), |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1807 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1808 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1809 | ASSERT_COMPARE( output, output_length, |
| 1810 | expected_output->x, expected_output->len ); |
| 1811 | |
| 1812 | /* Compute with larger buffer */ |
| 1813 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
| 1814 | output, sizeof( output ), |
| 1815 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1816 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1817 | ASSERT_COMPARE( output, output_length, |
| 1818 | expected_output->x, expected_output->len ); |
| 1819 | |
| 1820 | /* Compare with correct hash */ |
| 1821 | PSA_ASSERT( psa_hash_compare( alg, input->x, input->len, |
| 1822 | output, output_length ) ); |
| 1823 | |
| 1824 | /* Compare with trailing garbage */ |
| 1825 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 1826 | output, output_length + 1 ), |
| 1827 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1828 | |
| 1829 | /* Compare with truncated hash */ |
| 1830 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 1831 | output, output_length - 1 ), |
| 1832 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1833 | |
| 1834 | /* Compare with corrupted value */ |
| 1835 | for( i = 0; i < output_length; i++ ) |
| 1836 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 1837 | mbedtls_test_set_step( i ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1838 | output[i] ^= 1; |
| 1839 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 1840 | output, output_length ), |
| 1841 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1842 | output[i] ^= 1; |
| 1843 | } |
| 1844 | |
| 1845 | exit: |
| 1846 | PSA_DONE( ); |
| 1847 | } |
| 1848 | /* END_CASE */ |
| 1849 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 1850 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1851 | void hash_bad_order( ) |
| 1852 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1853 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1854 | unsigned char input[] = ""; |
| 1855 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1856 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1857 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 1858 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 1859 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1860 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1861 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1862 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1863 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1864 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1865 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1866 | /* Call setup twice in a row. */ |
| 1867 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1868 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 1869 | PSA_ERROR_BAD_STATE ); |
| 1870 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1871 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1872 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1873 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1874 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1875 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1876 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1877 | /* Call update after finish. */ |
| 1878 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1879 | PSA_ASSERT( psa_hash_finish( &operation, |
| 1880 | hash, sizeof( hash ), &hash_len ) ); |
| 1881 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1882 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1883 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1884 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1885 | /* Call verify without calling setup beforehand. */ |
| 1886 | TEST_EQUAL( psa_hash_verify( &operation, |
| 1887 | valid_hash, sizeof( valid_hash ) ), |
| 1888 | PSA_ERROR_BAD_STATE ); |
| 1889 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1890 | |
| 1891 | /* Call verify after finish. */ |
| 1892 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1893 | PSA_ASSERT( psa_hash_finish( &operation, |
| 1894 | hash, sizeof( hash ), &hash_len ) ); |
| 1895 | TEST_EQUAL( psa_hash_verify( &operation, |
| 1896 | valid_hash, sizeof( valid_hash ) ), |
| 1897 | PSA_ERROR_BAD_STATE ); |
| 1898 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1899 | |
| 1900 | /* Call verify twice in a row. */ |
| 1901 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1902 | PSA_ASSERT( psa_hash_verify( &operation, |
| 1903 | valid_hash, sizeof( valid_hash ) ) ); |
| 1904 | TEST_EQUAL( psa_hash_verify( &operation, |
| 1905 | valid_hash, sizeof( valid_hash ) ), |
| 1906 | PSA_ERROR_BAD_STATE ); |
| 1907 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1908 | |
| 1909 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1910 | TEST_EQUAL( psa_hash_finish( &operation, |
| 1911 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1912 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1913 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1914 | |
| 1915 | /* Call finish twice in a row. */ |
| 1916 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1917 | PSA_ASSERT( psa_hash_finish( &operation, |
| 1918 | hash, sizeof( hash ), &hash_len ) ); |
| 1919 | TEST_EQUAL( psa_hash_finish( &operation, |
| 1920 | hash, sizeof( hash ), &hash_len ), |
| 1921 | PSA_ERROR_BAD_STATE ); |
| 1922 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1923 | |
| 1924 | /* Call finish after calling verify. */ |
| 1925 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1926 | PSA_ASSERT( psa_hash_verify( &operation, |
| 1927 | valid_hash, sizeof( valid_hash ) ) ); |
| 1928 | TEST_EQUAL( psa_hash_finish( &operation, |
| 1929 | hash, sizeof( hash ), &hash_len ), |
| 1930 | PSA_ERROR_BAD_STATE ); |
| 1931 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1932 | |
| 1933 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1934 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1935 | } |
| 1936 | /* END_CASE */ |
| 1937 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 1938 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1939 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1940 | { |
| 1941 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1942 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 1943 | * appended to it */ |
| 1944 | unsigned char hash[] = { |
| 1945 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 1946 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 1947 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1948 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1949 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1950 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1951 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1952 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1953 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1954 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1955 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1956 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1957 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1958 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1959 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1960 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1961 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1962 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1963 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1964 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1965 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1966 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 1967 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1968 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1969 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1970 | } |
| 1971 | /* END_CASE */ |
| 1972 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 1973 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 1974 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1975 | { |
| 1976 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 1977 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1978 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1979 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1980 | size_t hash_len; |
| 1981 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1982 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1983 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1984 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1985 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1986 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1987 | hash, expected_size - 1, &hash_len ), |
| 1988 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1989 | |
| 1990 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1991 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1992 | } |
| 1993 | /* END_CASE */ |
| 1994 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 1995 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 1996 | void hash_clone_source_state( ) |
| 1997 | { |
| 1998 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 1999 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2000 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 2001 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2002 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2003 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2004 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2005 | size_t hash_len; |
| 2006 | |
| 2007 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2008 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 2009 | |
| 2010 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2011 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2012 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2013 | hash, sizeof( hash ), &hash_len ) ); |
| 2014 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2015 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2016 | |
| 2017 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 2018 | PSA_ERROR_BAD_STATE ); |
| 2019 | |
| 2020 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 2021 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 2022 | hash, sizeof( hash ), &hash_len ) ); |
| 2023 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 2024 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2025 | hash, sizeof( hash ), &hash_len ) ); |
| 2026 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 2027 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 2028 | hash, sizeof( hash ), &hash_len ) ); |
| 2029 | |
| 2030 | exit: |
| 2031 | psa_hash_abort( &op_source ); |
| 2032 | psa_hash_abort( &op_init ); |
| 2033 | psa_hash_abort( &op_setup ); |
| 2034 | psa_hash_abort( &op_finished ); |
| 2035 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2036 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2037 | } |
| 2038 | /* END_CASE */ |
| 2039 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2040 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2041 | void hash_clone_target_state( ) |
| 2042 | { |
| 2043 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2044 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2045 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2046 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2047 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2048 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2049 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 2050 | size_t hash_len; |
| 2051 | |
| 2052 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2053 | |
| 2054 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2055 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2056 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2057 | hash, sizeof( hash ), &hash_len ) ); |
| 2058 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2059 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2060 | |
| 2061 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 2062 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 2063 | hash, sizeof( hash ), &hash_len ) ); |
| 2064 | |
| 2065 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 2066 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 2067 | PSA_ERROR_BAD_STATE ); |
| 2068 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 2069 | PSA_ERROR_BAD_STATE ); |
| 2070 | |
| 2071 | exit: |
| 2072 | psa_hash_abort( &op_target ); |
| 2073 | psa_hash_abort( &op_init ); |
| 2074 | psa_hash_abort( &op_setup ); |
| 2075 | psa_hash_abort( &op_finished ); |
| 2076 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2077 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2078 | } |
| 2079 | /* END_CASE */ |
| 2080 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2081 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2082 | void mac_operation_init( ) |
| 2083 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2084 | const uint8_t input[1] = { 0 }; |
| 2085 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2086 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2087 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2088 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2089 | * to supress the Clang warning for the test. */ |
| 2090 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 2091 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 2092 | psa_mac_operation_t zero; |
| 2093 | |
| 2094 | memset( &zero, 0, sizeof( zero ) ); |
| 2095 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2096 | /* A freshly-initialized MAC operation should not be usable. */ |
| 2097 | TEST_EQUAL( psa_mac_update( &func, |
| 2098 | input, sizeof( input ) ), |
| 2099 | PSA_ERROR_BAD_STATE ); |
| 2100 | TEST_EQUAL( psa_mac_update( &init, |
| 2101 | input, sizeof( input ) ), |
| 2102 | PSA_ERROR_BAD_STATE ); |
| 2103 | TEST_EQUAL( psa_mac_update( &zero, |
| 2104 | input, sizeof( input ) ), |
| 2105 | PSA_ERROR_BAD_STATE ); |
| 2106 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2107 | /* A default MAC operation should be abortable without error. */ |
| 2108 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 2109 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 2110 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2111 | } |
| 2112 | /* END_CASE */ |
| 2113 | |
| 2114 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2115 | void mac_setup( int key_type_arg, |
| 2116 | data_t *key, |
| 2117 | int alg_arg, |
| 2118 | int expected_status_arg ) |
| 2119 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2120 | psa_key_type_t key_type = key_type_arg; |
| 2121 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2122 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2123 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2124 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 2125 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2126 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2127 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2128 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2129 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2130 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2131 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 2132 | &operation, &status ) ) |
| 2133 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2134 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2135 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2136 | /* The operation object should be reusable. */ |
| 2137 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2138 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 2139 | smoke_test_key_data, |
| 2140 | sizeof( smoke_test_key_data ), |
| 2141 | KNOWN_SUPPORTED_MAC_ALG, |
| 2142 | &operation, &status ) ) |
| 2143 | goto exit; |
| 2144 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2145 | #endif |
| 2146 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2147 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2148 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2149 | } |
| 2150 | /* END_CASE */ |
| 2151 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2152 | /* 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] | 2153 | void mac_bad_order( ) |
| 2154 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2155 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2156 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 2157 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2158 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2159 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2160 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2161 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2162 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2163 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 2164 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 2165 | size_t sign_mac_length = 0; |
| 2166 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 2167 | const uint8_t verify_mac[] = { |
| 2168 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 2169 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 2170 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 2171 | |
| 2172 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2173 | 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] | 2174 | psa_set_key_algorithm( &attributes, alg ); |
| 2175 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2176 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2177 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 2178 | &key ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2179 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2180 | /* Call update without calling setup beforehand. */ |
| 2181 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2182 | PSA_ERROR_BAD_STATE ); |
| 2183 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2184 | |
| 2185 | /* Call sign finish without calling setup beforehand. */ |
| 2186 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 2187 | &sign_mac_length), |
| 2188 | PSA_ERROR_BAD_STATE ); |
| 2189 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2190 | |
| 2191 | /* Call verify finish without calling setup beforehand. */ |
| 2192 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2193 | verify_mac, sizeof( verify_mac ) ), |
| 2194 | PSA_ERROR_BAD_STATE ); |
| 2195 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2196 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2197 | /* Call setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2198 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
| 2199 | TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2200 | PSA_ERROR_BAD_STATE ); |
| 2201 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2202 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2203 | /* Call update after sign finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2204 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2205 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2206 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2207 | sign_mac, sizeof( sign_mac ), |
| 2208 | &sign_mac_length ) ); |
| 2209 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2210 | PSA_ERROR_BAD_STATE ); |
| 2211 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2212 | |
| 2213 | /* Call update after verify finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2214 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2215 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2216 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2217 | verify_mac, sizeof( verify_mac ) ) ); |
| 2218 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2219 | PSA_ERROR_BAD_STATE ); |
| 2220 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2221 | |
| 2222 | /* Call sign finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2223 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2224 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2225 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2226 | sign_mac, sizeof( sign_mac ), |
| 2227 | &sign_mac_length ) ); |
| 2228 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2229 | sign_mac, sizeof( sign_mac ), |
| 2230 | &sign_mac_length ), |
| 2231 | PSA_ERROR_BAD_STATE ); |
| 2232 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2233 | |
| 2234 | /* Call verify finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2235 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2236 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2237 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2238 | verify_mac, sizeof( verify_mac ) ) ); |
| 2239 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2240 | verify_mac, sizeof( verify_mac ) ), |
| 2241 | PSA_ERROR_BAD_STATE ); |
| 2242 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2243 | |
| 2244 | /* Setup sign but try verify. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2245 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2246 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2247 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2248 | verify_mac, sizeof( verify_mac ) ), |
| 2249 | PSA_ERROR_BAD_STATE ); |
| 2250 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2251 | |
| 2252 | /* Setup verify but try sign. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2253 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2254 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2255 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2256 | sign_mac, sizeof( sign_mac ), |
| 2257 | &sign_mac_length ), |
| 2258 | PSA_ERROR_BAD_STATE ); |
| 2259 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2260 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2261 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2262 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2263 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2264 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2265 | } |
| 2266 | /* END_CASE */ |
| 2267 | |
| 2268 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2269 | void mac_sign( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2270 | data_t *key_data, |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2271 | int alg_arg, |
| 2272 | data_t *input, |
| 2273 | data_t *expected_mac ) |
| 2274 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2275 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2276 | psa_key_type_t key_type = key_type_arg; |
| 2277 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2278 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2279 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2280 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2281 | size_t mac_buffer_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2282 | 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] | 2283 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2284 | const size_t output_sizes_to_test[] = { |
| 2285 | 0, |
| 2286 | 1, |
| 2287 | expected_mac->len - 1, |
| 2288 | expected_mac->len, |
| 2289 | expected_mac->len + 1, |
| 2290 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2291 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2292 | TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2293 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 3d404d6 | 2020-08-25 23:47:36 +0200 | [diff] [blame] | 2294 | TEST_ASSERT( expected_mac->len == mac_buffer_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2295 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2296 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2297 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2298 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2299 | psa_set_key_algorithm( &attributes, alg ); |
| 2300 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2301 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2302 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2303 | &key ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2304 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2305 | for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ ) |
| 2306 | { |
| 2307 | const size_t output_size = output_sizes_to_test[i]; |
| 2308 | psa_status_t expected_status = |
| 2309 | ( output_size >= expected_mac->len ? PSA_SUCCESS : |
| 2310 | PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2311 | |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2312 | mbedtls_test_set_step( output_size ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2313 | ASSERT_ALLOC( actual_mac, output_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2314 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2315 | /* Calculate the MAC. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2316 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2317 | PSA_ASSERT( psa_mac_update( &operation, |
| 2318 | input->x, input->len ) ); |
| 2319 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2320 | actual_mac, output_size, |
| 2321 | &mac_length ), |
| 2322 | expected_status ); |
| 2323 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2324 | |
| 2325 | if( expected_status == PSA_SUCCESS ) |
| 2326 | { |
| 2327 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 2328 | actual_mac, mac_length ); |
| 2329 | } |
| 2330 | mbedtls_free( actual_mac ); |
| 2331 | actual_mac = NULL; |
| 2332 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2333 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2334 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2335 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2336 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2337 | PSA_DONE( ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2338 | mbedtls_free( actual_mac ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2339 | } |
| 2340 | /* END_CASE */ |
| 2341 | |
| 2342 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2343 | void mac_verify( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2344 | data_t *key_data, |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2345 | int alg_arg, |
| 2346 | data_t *input, |
| 2347 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2348 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2349 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2350 | psa_key_type_t key_type = key_type_arg; |
| 2351 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2352 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2353 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2354 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2355 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 2356 | TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE ); |
| 2357 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2358 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2359 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2360 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2361 | psa_set_key_algorithm( &attributes, alg ); |
| 2362 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2363 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2364 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2365 | &key ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2366 | |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2367 | /* Test the correct MAC. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2368 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2369 | PSA_ASSERT( psa_mac_update( &operation, |
| 2370 | input->x, input->len ) ); |
| 2371 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2372 | expected_mac->x, |
| 2373 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2374 | |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2375 | /* Test a MAC that's too short. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2376 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2377 | PSA_ASSERT( psa_mac_update( &operation, |
| 2378 | input->x, input->len ) ); |
| 2379 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2380 | expected_mac->x, |
| 2381 | expected_mac->len - 1 ), |
| 2382 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2383 | |
| 2384 | /* Test a MAC that's too long. */ |
| 2385 | ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 ); |
| 2386 | memcpy( perturbed_mac, expected_mac->x, expected_mac->len ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2387 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2388 | PSA_ASSERT( psa_mac_update( &operation, |
| 2389 | input->x, input->len ) ); |
| 2390 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2391 | perturbed_mac, |
| 2392 | expected_mac->len + 1 ), |
| 2393 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2394 | |
| 2395 | /* Test changing one byte. */ |
| 2396 | for( size_t i = 0; i < expected_mac->len; i++ ) |
| 2397 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2398 | mbedtls_test_set_step( i ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2399 | perturbed_mac[i] ^= 1; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2400 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2401 | PSA_ASSERT( psa_mac_update( &operation, |
| 2402 | input->x, input->len ) ); |
| 2403 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2404 | perturbed_mac, |
| 2405 | expected_mac->len ), |
| 2406 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2407 | perturbed_mac[i] ^= 1; |
| 2408 | } |
| 2409 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2410 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2411 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2412 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2413 | PSA_DONE( ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2414 | mbedtls_free( perturbed_mac ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2415 | } |
| 2416 | /* END_CASE */ |
| 2417 | |
| 2418 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2419 | void cipher_operation_init( ) |
| 2420 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2421 | const uint8_t input[1] = { 0 }; |
| 2422 | unsigned char output[1] = { 0 }; |
| 2423 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2424 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2425 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2426 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2427 | * to supress the Clang warning for the test. */ |
| 2428 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 2429 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 2430 | psa_cipher_operation_t zero; |
| 2431 | |
| 2432 | memset( &zero, 0, sizeof( zero ) ); |
| 2433 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2434 | /* A freshly-initialized cipher operation should not be usable. */ |
| 2435 | TEST_EQUAL( psa_cipher_update( &func, |
| 2436 | input, sizeof( input ), |
| 2437 | output, sizeof( output ), |
| 2438 | &output_length ), |
| 2439 | PSA_ERROR_BAD_STATE ); |
| 2440 | TEST_EQUAL( psa_cipher_update( &init, |
| 2441 | input, sizeof( input ), |
| 2442 | output, sizeof( output ), |
| 2443 | &output_length ), |
| 2444 | PSA_ERROR_BAD_STATE ); |
| 2445 | TEST_EQUAL( psa_cipher_update( &zero, |
| 2446 | input, sizeof( input ), |
| 2447 | output, sizeof( output ), |
| 2448 | &output_length ), |
| 2449 | PSA_ERROR_BAD_STATE ); |
| 2450 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2451 | /* A default cipher operation should be abortable without error. */ |
| 2452 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 2453 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 2454 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2455 | } |
| 2456 | /* END_CASE */ |
| 2457 | |
| 2458 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2459 | void cipher_setup( int key_type_arg, |
| 2460 | data_t *key, |
| 2461 | int alg_arg, |
| 2462 | int expected_status_arg ) |
| 2463 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2464 | psa_key_type_t key_type = key_type_arg; |
| 2465 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2466 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2467 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2468 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 2469 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2470 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2471 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2472 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2473 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2474 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2475 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 2476 | &operation, &status ) ) |
| 2477 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2478 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2479 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2480 | /* The operation object should be reusable. */ |
| 2481 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 2482 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 2483 | smoke_test_key_data, |
| 2484 | sizeof( smoke_test_key_data ), |
| 2485 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 2486 | &operation, &status ) ) |
| 2487 | goto exit; |
| 2488 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2489 | #endif |
| 2490 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2491 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2492 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2493 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2494 | } |
| 2495 | /* END_CASE */ |
| 2496 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2497 | /* 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] | 2498 | void cipher_bad_order( ) |
| 2499 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2500 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2501 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 2502 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2503 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2504 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2505 | 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] | 2506 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2507 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2508 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 2509 | const uint8_t text[] = { |
| 2510 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 2511 | 0xbb, 0xbb, 0xbb, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2512 | 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] | 2513 | size_t length = 0; |
| 2514 | |
| 2515 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2516 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 2517 | psa_set_key_algorithm( &attributes, alg ); |
| 2518 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2519 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 2520 | &key ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2521 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2522 | /* Call encrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2523 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 2524 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2525 | PSA_ERROR_BAD_STATE ); |
| 2526 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2527 | |
| 2528 | /* Call decrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2529 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 2530 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2531 | PSA_ERROR_BAD_STATE ); |
| 2532 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2533 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2534 | /* Generate an IV without calling setup beforehand. */ |
| 2535 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2536 | buffer, sizeof( buffer ), |
| 2537 | &length ), |
| 2538 | PSA_ERROR_BAD_STATE ); |
| 2539 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2540 | |
| 2541 | /* Generate an IV twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2542 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2543 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 2544 | buffer, sizeof( buffer ), |
| 2545 | &length ) ); |
| 2546 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2547 | buffer, sizeof( buffer ), |
| 2548 | &length ), |
| 2549 | PSA_ERROR_BAD_STATE ); |
| 2550 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2551 | |
| 2552 | /* Generate an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2553 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2554 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2555 | iv, sizeof( iv ) ) ); |
| 2556 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2557 | buffer, sizeof( buffer ), |
| 2558 | &length ), |
| 2559 | PSA_ERROR_BAD_STATE ); |
| 2560 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2561 | |
| 2562 | /* Set an IV without calling setup beforehand. */ |
| 2563 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2564 | iv, sizeof( iv ) ), |
| 2565 | PSA_ERROR_BAD_STATE ); |
| 2566 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2567 | |
| 2568 | /* Set an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2569 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2570 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2571 | iv, sizeof( iv ) ) ); |
| 2572 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2573 | iv, sizeof( iv ) ), |
| 2574 | PSA_ERROR_BAD_STATE ); |
| 2575 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2576 | |
| 2577 | /* Set an IV after it's already generated. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2578 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2579 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 2580 | buffer, sizeof( buffer ), |
| 2581 | &length ) ); |
| 2582 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2583 | iv, sizeof( iv ) ), |
| 2584 | PSA_ERROR_BAD_STATE ); |
| 2585 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2586 | |
| 2587 | /* Call update without calling setup beforehand. */ |
| 2588 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2589 | text, sizeof( text ), |
| 2590 | buffer, sizeof( buffer ), |
| 2591 | &length ), |
| 2592 | PSA_ERROR_BAD_STATE ); |
| 2593 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2594 | |
| 2595 | /* Call update without an IV where an IV is required. */ |
| 2596 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2597 | text, sizeof( text ), |
| 2598 | buffer, sizeof( buffer ), |
| 2599 | &length ), |
| 2600 | PSA_ERROR_BAD_STATE ); |
| 2601 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2602 | |
| 2603 | /* Call update after finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2604 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2605 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2606 | iv, sizeof( iv ) ) ); |
| 2607 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2608 | buffer, sizeof( buffer ), &length ) ); |
| 2609 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2610 | text, sizeof( text ), |
| 2611 | buffer, sizeof( buffer ), |
| 2612 | &length ), |
| 2613 | PSA_ERROR_BAD_STATE ); |
| 2614 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2615 | |
| 2616 | /* Call finish without calling setup beforehand. */ |
| 2617 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2618 | buffer, sizeof( buffer ), &length ), |
| 2619 | PSA_ERROR_BAD_STATE ); |
| 2620 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2621 | |
| 2622 | /* Call finish without an IV where an IV is required. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2623 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2624 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 2625 | * for cipher modes with padding. */ |
| 2626 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2627 | buffer, sizeof( buffer ), &length ), |
| 2628 | PSA_ERROR_BAD_STATE ); |
| 2629 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2630 | |
| 2631 | /* Call finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2632 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2633 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2634 | iv, sizeof( iv ) ) ); |
| 2635 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2636 | buffer, sizeof( buffer ), &length ) ); |
| 2637 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2638 | buffer, sizeof( buffer ), &length ), |
| 2639 | PSA_ERROR_BAD_STATE ); |
| 2640 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2641 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2642 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2643 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2644 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2645 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2646 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2647 | } |
| 2648 | /* END_CASE */ |
| 2649 | |
| 2650 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2651 | void cipher_encrypt( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2652 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2653 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2654 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2655 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2656 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2657 | psa_status_t status; |
| 2658 | psa_key_type_t key_type = key_type_arg; |
| 2659 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2660 | psa_status_t expected_status = expected_status_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2661 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2662 | size_t output_buffer_size = 0; |
| 2663 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2664 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2665 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2666 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2667 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2668 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2669 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2670 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2671 | psa_set_key_algorithm( &attributes, alg ); |
| 2672 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2673 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2674 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2675 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2676 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2677 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2678 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2679 | if( iv->len > 0 ) |
| 2680 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 2681 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2682 | } |
| 2683 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2684 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 2685 | TEST_ASSERT( output_buffer_size <= |
| 2686 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2687 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2688 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2689 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2690 | input->x, input->len, |
| 2691 | output, output_buffer_size, |
| 2692 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2693 | TEST_ASSERT( function_output_length <= |
| 2694 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 2695 | TEST_ASSERT( function_output_length <= |
| 2696 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2697 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2698 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2699 | status = psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 2700 | ( output_buffer_size == 0 ? NULL : |
| 2701 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2702 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2703 | &function_output_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2704 | TEST_ASSERT( function_output_length <= |
| 2705 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 2706 | TEST_ASSERT( function_output_length <= |
| 2707 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2708 | total_output_length += function_output_length; |
| 2709 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2710 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2711 | if( expected_status == PSA_SUCCESS ) |
| 2712 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2713 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2714 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2715 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2716 | } |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2717 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2718 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2719 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2720 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2721 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2722 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2723 | } |
| 2724 | /* END_CASE */ |
| 2725 | |
| 2726 | /* BEGIN_CASE */ |
| 2727 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2728 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2729 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2730 | int first_part_size_arg, |
| 2731 | int output1_length_arg, int output2_length_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2732 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2733 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2734 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2735 | psa_key_type_t key_type = key_type_arg; |
| 2736 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2737 | size_t first_part_size = first_part_size_arg; |
| 2738 | size_t output1_length = output1_length_arg; |
| 2739 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2740 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2741 | size_t output_buffer_size = 0; |
| 2742 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2743 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2744 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2745 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2746 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2747 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2748 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2749 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2750 | psa_set_key_algorithm( &attributes, alg ); |
| 2751 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2752 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2753 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2754 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2755 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2756 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2757 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2758 | if( iv->len > 0 ) |
| 2759 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 2760 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2761 | } |
| 2762 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2763 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 2764 | TEST_ASSERT( output_buffer_size <= |
| 2765 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2766 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2767 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2768 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2769 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 2770 | output, output_buffer_size, |
| 2771 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2772 | TEST_ASSERT( function_output_length == output1_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2773 | TEST_ASSERT( function_output_length <= |
| 2774 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 2775 | TEST_ASSERT( function_output_length <= |
| 2776 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2777 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2778 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2779 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2780 | input->x + first_part_size, |
| 2781 | input->len - first_part_size, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 2782 | ( output_buffer_size == 0 ? NULL : |
| 2783 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2784 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2785 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2786 | TEST_ASSERT( function_output_length == output2_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2787 | TEST_ASSERT( function_output_length <= |
| 2788 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 2789 | alg, |
| 2790 | input->len - first_part_size ) ); |
| 2791 | TEST_ASSERT( function_output_length <= |
| 2792 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2793 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2794 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2795 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 2796 | ( output_buffer_size == 0 ? NULL : |
| 2797 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2798 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2799 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2800 | TEST_ASSERT( function_output_length <= |
| 2801 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 2802 | TEST_ASSERT( function_output_length <= |
| 2803 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2804 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2805 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2806 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2807 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2808 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2809 | |
| 2810 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2811 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2812 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2813 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2814 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2815 | } |
| 2816 | /* END_CASE */ |
| 2817 | |
| 2818 | /* BEGIN_CASE */ |
| 2819 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2820 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2821 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2822 | int first_part_size_arg, |
| 2823 | int output1_length_arg, int output2_length_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2824 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2825 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2826 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2827 | psa_key_type_t key_type = key_type_arg; |
| 2828 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2829 | size_t first_part_size = first_part_size_arg; |
| 2830 | size_t output1_length = output1_length_arg; |
| 2831 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2832 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2833 | size_t output_buffer_size = 0; |
| 2834 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2835 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2836 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2837 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2838 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2839 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2840 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2841 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 2842 | psa_set_key_algorithm( &attributes, alg ); |
| 2843 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2844 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2845 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2846 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2847 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2848 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2849 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 2850 | if( iv->len > 0 ) |
| 2851 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 2852 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2853 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2854 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2855 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 2856 | TEST_ASSERT( output_buffer_size <= |
| 2857 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2858 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2859 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2860 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2861 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2862 | input->x, first_part_size, |
| 2863 | output, output_buffer_size, |
| 2864 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2865 | TEST_ASSERT( function_output_length == output1_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2866 | TEST_ASSERT( function_output_length <= |
| 2867 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 2868 | TEST_ASSERT( function_output_length <= |
| 2869 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2870 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2871 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2872 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2873 | input->x + first_part_size, |
| 2874 | input->len - first_part_size, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 2875 | ( output_buffer_size == 0 ? NULL : |
| 2876 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2877 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2878 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 2879 | TEST_ASSERT( function_output_length == output2_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2880 | TEST_ASSERT( function_output_length <= |
| 2881 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 2882 | alg, |
| 2883 | input->len - first_part_size ) ); |
| 2884 | TEST_ASSERT( function_output_length <= |
| 2885 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2886 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2887 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2888 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 2889 | ( output_buffer_size == 0 ? NULL : |
| 2890 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2891 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2892 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2893 | TEST_ASSERT( function_output_length <= |
| 2894 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 2895 | TEST_ASSERT( function_output_length <= |
| 2896 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2897 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2898 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2899 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2900 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2901 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2902 | |
| 2903 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2904 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2905 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2906 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2907 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2908 | } |
| 2909 | /* END_CASE */ |
| 2910 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2911 | /* BEGIN_CASE */ |
| 2912 | void cipher_decrypt( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2913 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2914 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2915 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2916 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2917 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2918 | psa_status_t status; |
| 2919 | psa_key_type_t key_type = key_type_arg; |
| 2920 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2921 | psa_status_t expected_status = expected_status_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2922 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2923 | size_t output_buffer_size = 0; |
| 2924 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2925 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2926 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2927 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2928 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2929 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2930 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2931 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 2932 | psa_set_key_algorithm( &attributes, alg ); |
| 2933 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2934 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2935 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2936 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2937 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2938 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2939 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 2940 | if( iv->len > 0 ) |
| 2941 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 2942 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 2943 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2944 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2945 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 2946 | TEST_ASSERT( output_buffer_size <= |
| 2947 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2948 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2949 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2950 | PSA_ASSERT( psa_cipher_update( &operation, |
| 2951 | input->x, input->len, |
| 2952 | output, output_buffer_size, |
| 2953 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2954 | TEST_ASSERT( function_output_length <= |
| 2955 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 2956 | TEST_ASSERT( function_output_length <= |
| 2957 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2958 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2959 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2960 | status = psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 2961 | ( output_buffer_size == 0 ? NULL : |
| 2962 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 2963 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2964 | &function_output_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 2965 | TEST_ASSERT( function_output_length <= |
| 2966 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 2967 | TEST_ASSERT( function_output_length <= |
| 2968 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2969 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2970 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2971 | |
| 2972 | if( expected_status == PSA_SUCCESS ) |
| 2973 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2974 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2975 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2976 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2977 | } |
| 2978 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2979 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2980 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2981 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2982 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2983 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2984 | } |
| 2985 | /* END_CASE */ |
| 2986 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2987 | /* BEGIN_CASE */ |
| 2988 | void cipher_verify_output( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2989 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2990 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2991 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2992 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2993 | psa_key_type_t key_type = key_type_arg; |
| 2994 | psa_algorithm_t alg = alg_arg; |
mohammad1603 | e6b67a1 | 2018-03-12 10:38:49 -0700 | [diff] [blame] | 2995 | unsigned char iv[16] = {0}; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2996 | size_t iv_size = 16; |
| 2997 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2998 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2999 | size_t output1_size = 0; |
| 3000 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3001 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3002 | size_t output2_size = 0; |
| 3003 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3004 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3005 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3006 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3007 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3008 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3009 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3010 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3011 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3012 | psa_set_key_algorithm( &attributes, alg ); |
| 3013 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3014 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3015 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3016 | &key ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3017 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3018 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 3019 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3020 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3021 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3022 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3023 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3024 | iv, iv_size, |
| 3025 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3026 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3027 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3028 | TEST_ASSERT( output1_size <= |
| 3029 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3030 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3031 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3032 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len, |
| 3033 | output1, output1_size, |
| 3034 | &output1_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3035 | TEST_ASSERT( output1_length <= |
| 3036 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 3037 | TEST_ASSERT( output1_length <= |
| 3038 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
| 3039 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3040 | PSA_ASSERT( psa_cipher_finish( &operation1, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3041 | output1 + output1_length, |
| 3042 | output1_size - output1_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3043 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3044 | TEST_ASSERT( function_output_length <= |
| 3045 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3046 | TEST_ASSERT( function_output_length <= |
| 3047 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3048 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3049 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3050 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3051 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3052 | |
| 3053 | output2_size = output1_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3054 | TEST_ASSERT( output2_size <= |
| 3055 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 3056 | TEST_ASSERT( output2_size <= |
| 3057 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3058 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3059 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3060 | if( iv_length > 0 ) |
| 3061 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3062 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3063 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3064 | } |
| 3065 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3066 | PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length, |
| 3067 | output2, output2_size, |
| 3068 | &output2_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3069 | TEST_ASSERT( output2_length <= |
| 3070 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 3071 | TEST_ASSERT( output2_length <= |
| 3072 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) ); |
| 3073 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3074 | function_output_length = 0; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3075 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3076 | output2 + output2_length, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3077 | output2_size - output2_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3078 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3079 | TEST_ASSERT( function_output_length <= |
| 3080 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3081 | TEST_ASSERT( function_output_length <= |
| 3082 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3083 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3084 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3085 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3086 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3087 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3088 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3089 | |
| 3090 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3091 | psa_cipher_abort( &operation1 ); |
| 3092 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3093 | mbedtls_free( output1 ); |
| 3094 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3095 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3096 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3097 | } |
| 3098 | /* END_CASE */ |
| 3099 | |
| 3100 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3101 | void cipher_verify_output_multipart( int alg_arg, |
| 3102 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3103 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3104 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3105 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3106 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3107 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3108 | psa_key_type_t key_type = key_type_arg; |
| 3109 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3110 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3111 | unsigned char iv[16] = {0}; |
| 3112 | size_t iv_size = 16; |
| 3113 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3114 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3115 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3116 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3117 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3118 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3119 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3120 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3121 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3122 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3123 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3124 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3125 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3126 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3127 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3128 | psa_set_key_algorithm( &attributes, alg ); |
| 3129 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3130 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3131 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3132 | &key ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3133 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3134 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 3135 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3136 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3137 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3138 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3139 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3140 | iv, iv_size, |
| 3141 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3142 | } |
| 3143 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3144 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3145 | TEST_ASSERT( output1_buffer_size <= |
| 3146 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3147 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3148 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3149 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3150 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3151 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 3152 | output1, output1_buffer_size, |
| 3153 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3154 | TEST_ASSERT( function_output_length <= |
| 3155 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3156 | TEST_ASSERT( function_output_length <= |
| 3157 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3158 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3159 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3160 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 3161 | input->x + first_part_size, |
| 3162 | input->len - first_part_size, |
| 3163 | output1, output1_buffer_size, |
| 3164 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3165 | TEST_ASSERT( function_output_length <= |
| 3166 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3167 | alg, |
| 3168 | input->len - first_part_size ) ); |
| 3169 | TEST_ASSERT( function_output_length <= |
| 3170 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3171 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3172 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3173 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 3174 | output1 + output1_length, |
| 3175 | output1_buffer_size - output1_length, |
| 3176 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3177 | TEST_ASSERT( function_output_length <= |
| 3178 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3179 | TEST_ASSERT( function_output_length <= |
| 3180 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3181 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3182 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3183 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3184 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3185 | output2_buffer_size = output1_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3186 | TEST_ASSERT( output2_buffer_size <= |
| 3187 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 3188 | TEST_ASSERT( output2_buffer_size <= |
| 3189 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3190 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3191 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3192 | if( iv_length > 0 ) |
| 3193 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3194 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3195 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3196 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3197 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3198 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 3199 | output2, output2_buffer_size, |
| 3200 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3201 | TEST_ASSERT( function_output_length <= |
| 3202 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3203 | TEST_ASSERT( function_output_length <= |
| 3204 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3205 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3206 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3207 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 3208 | output1 + first_part_size, |
| 3209 | output1_length - first_part_size, |
| 3210 | output2, output2_buffer_size, |
| 3211 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3212 | TEST_ASSERT( function_output_length <= |
| 3213 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3214 | alg, |
| 3215 | output1_length - first_part_size ) ); |
| 3216 | TEST_ASSERT( function_output_length <= |
| 3217 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3218 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3219 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3220 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3221 | output2 + output2_length, |
| 3222 | output2_buffer_size - output2_length, |
| 3223 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3224 | TEST_ASSERT( function_output_length <= |
| 3225 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3226 | TEST_ASSERT( function_output_length <= |
| 3227 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3228 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3229 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3230 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3231 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3232 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3233 | |
| 3234 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3235 | psa_cipher_abort( &operation1 ); |
| 3236 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3237 | mbedtls_free( output1 ); |
| 3238 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3239 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3240 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3241 | } |
| 3242 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 3243 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3244 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3245 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3246 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3247 | data_t *nonce, |
| 3248 | data_t *additional_data, |
| 3249 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3250 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3251 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3252 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3253 | psa_key_type_t key_type = key_type_arg; |
| 3254 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3255 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3256 | unsigned char *output_data = NULL; |
| 3257 | size_t output_size = 0; |
| 3258 | size_t output_length = 0; |
| 3259 | unsigned char *output_data2 = NULL; |
| 3260 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 3261 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3262 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3263 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3264 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3265 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3266 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3267 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3268 | psa_set_key_algorithm( &attributes, alg ); |
| 3269 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3270 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3271 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3272 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3273 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3274 | key_bits = psa_get_key_bits( &attributes ); |
| 3275 | |
| 3276 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3277 | alg ); |
| 3278 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3279 | * should be exact. */ |
| 3280 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 3281 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 3282 | { |
| 3283 | TEST_EQUAL( output_size, |
| 3284 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3285 | TEST_ASSERT( output_size <= |
| 3286 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3287 | } |
| 3288 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3289 | |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 3290 | status = psa_aead_encrypt( key, alg, |
| 3291 | nonce->x, nonce->len, |
| 3292 | additional_data->x, |
| 3293 | additional_data->len, |
| 3294 | input_data->x, input_data->len, |
| 3295 | output_data, output_size, |
| 3296 | &output_length ); |
| 3297 | |
| 3298 | /* If the operation is not supported, just skip and not fail in case the |
| 3299 | * encryption involves a common limitation of cryptography hardwares and |
| 3300 | * an alternative implementation. */ |
| 3301 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 3302 | { |
| 3303 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3304 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 3305 | } |
| 3306 | |
| 3307 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3308 | |
| 3309 | if( PSA_SUCCESS == expected_result ) |
| 3310 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3311 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3312 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3313 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3314 | * should be exact. */ |
| 3315 | TEST_EQUAL( input_data->len, |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3316 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) ); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3317 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3318 | TEST_ASSERT( input_data->len <= |
| 3319 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) ); |
| 3320 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3321 | TEST_EQUAL( psa_aead_decrypt( key, alg, |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3322 | nonce->x, nonce->len, |
| 3323 | additional_data->x, |
| 3324 | additional_data->len, |
| 3325 | output_data, output_length, |
| 3326 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3327 | &output_length2 ), |
| 3328 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3329 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3330 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 3331 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3332 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3333 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3334 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3335 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3336 | mbedtls_free( output_data ); |
| 3337 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3338 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3339 | } |
| 3340 | /* END_CASE */ |
| 3341 | |
| 3342 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3343 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 3344 | int alg_arg, |
| 3345 | data_t *nonce, |
| 3346 | data_t *additional_data, |
| 3347 | data_t *input_data, |
| 3348 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3349 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3350 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3351 | psa_key_type_t key_type = key_type_arg; |
| 3352 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3353 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3354 | unsigned char *output_data = NULL; |
| 3355 | size_t output_size = 0; |
| 3356 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3357 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3358 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3359 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3360 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3361 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3362 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3363 | psa_set_key_algorithm( &attributes, alg ); |
| 3364 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3365 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3366 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3367 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3368 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3369 | key_bits = psa_get_key_bits( &attributes ); |
| 3370 | |
| 3371 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3372 | alg ); |
| 3373 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3374 | * should be exact. */ |
| 3375 | TEST_EQUAL( output_size, |
| 3376 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3377 | TEST_ASSERT( output_size <= |
| 3378 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3379 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3380 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3381 | status = psa_aead_encrypt( key, alg, |
| 3382 | nonce->x, nonce->len, |
| 3383 | additional_data->x, additional_data->len, |
| 3384 | input_data->x, input_data->len, |
| 3385 | output_data, output_size, |
| 3386 | &output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3387 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3388 | /* If the operation is not supported, just skip and not fail in case the |
| 3389 | * encryption involves a common limitation of cryptography hardwares and |
| 3390 | * an alternative implementation. */ |
| 3391 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3392 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3393 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3394 | 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] | 3395 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3396 | |
| 3397 | PSA_ASSERT( status ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3398 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 3399 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3400 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3401 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3402 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3403 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3404 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3405 | } |
| 3406 | /* END_CASE */ |
| 3407 | |
| 3408 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3409 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 3410 | int alg_arg, |
| 3411 | data_t *nonce, |
| 3412 | data_t *additional_data, |
| 3413 | data_t *input_data, |
| 3414 | data_t *expected_data, |
| 3415 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3416 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3417 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3418 | psa_key_type_t key_type = key_type_arg; |
| 3419 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3420 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3421 | unsigned char *output_data = NULL; |
| 3422 | size_t output_size = 0; |
| 3423 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3424 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3425 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3426 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3427 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3428 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3429 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3430 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3431 | psa_set_key_algorithm( &attributes, alg ); |
| 3432 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3433 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3434 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3435 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3436 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3437 | key_bits = psa_get_key_bits( &attributes ); |
| 3438 | |
| 3439 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3440 | alg ); |
| 3441 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 3442 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 3443 | { |
| 3444 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3445 | * should be exact. */ |
| 3446 | TEST_EQUAL( output_size, |
| 3447 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3448 | TEST_ASSERT( output_size <= |
| 3449 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3450 | } |
| 3451 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3452 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3453 | status = psa_aead_decrypt( key, alg, |
| 3454 | nonce->x, nonce->len, |
| 3455 | additional_data->x, |
| 3456 | additional_data->len, |
| 3457 | input_data->x, input_data->len, |
| 3458 | output_data, output_size, |
| 3459 | &output_length ); |
| 3460 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3461 | /* If the operation is not supported, just skip and not fail in case the |
| 3462 | * decryption involves a common limitation of cryptography hardwares and |
| 3463 | * an alternative implementation. */ |
| 3464 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3465 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3466 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3467 | 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] | 3468 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3469 | |
| 3470 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3471 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3472 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3473 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 3474 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3475 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3476 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3477 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3478 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3479 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3480 | } |
| 3481 | /* END_CASE */ |
| 3482 | |
| 3483 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3484 | void aead_multipart_encrypt( int key_type_arg, data_t *key_data, |
| 3485 | int alg_arg, |
| 3486 | data_t *nonce, |
| 3487 | data_t *additional_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3488 | int do_test_ad_chunked, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3489 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3490 | int do_test_data_chunked, |
| 3491 | int do_set_lengths, |
| 3492 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3493 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3494 | size_t ad_part_len = 0; |
| 3495 | size_t data_part_len = 0; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3496 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3497 | TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked ); |
| 3498 | |
| 3499 | /* Temporary whilst we have algorithms that cannot support chunking */ |
| 3500 | if( do_test_ad_chunked == 1 ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3501 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3502 | for( ad_part_len = 1; ad_part_len <= additional_data->len; |
| 3503 | ad_part_len++ ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3504 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3505 | mbedtls_test_set_step( ad_part_len ); |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3506 | |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 3507 | /* Split ad into length(ad_part_len) parts. */ |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3508 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3509 | alg_arg, nonce, |
| 3510 | additional_data, |
| 3511 | ad_part_len, |
| 3512 | input_data, -1, |
| 3513 | do_set_lengths, |
| 3514 | expected_output, |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 3515 | 1, 1, 0, |
| 3516 | ( ad_part_len & 0x01 ) ) ) |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 3517 | break; |
| 3518 | |
| 3519 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 3520 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 3521 | |
| 3522 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3523 | alg_arg, nonce, |
| 3524 | additional_data, |
| 3525 | ad_part_len, |
| 3526 | input_data, -1, |
| 3527 | do_set_lengths, |
| 3528 | expected_output, |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 3529 | 1, 1, 1, |
| 3530 | ( ad_part_len & 0x01 ) ) ) |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3531 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3532 | } |
| 3533 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3534 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3535 | /* Temporary whilst we have algorithms that cannot support chunking */ |
| 3536 | if( do_test_data_chunked == 1 ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3537 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3538 | for( data_part_len = 1; data_part_len <= input_data->len; |
| 3539 | data_part_len++ ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3540 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 3541 | /* Split data into length(data_part_len) parts. */ |
| 3542 | mbedtls_test_set_step( 2000 + data_part_len ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3543 | |
| 3544 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3545 | alg_arg, nonce, |
| 3546 | additional_data, -1, |
| 3547 | input_data, data_part_len, |
| 3548 | do_set_lengths, |
| 3549 | expected_output, |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 3550 | 1, 1, 0, |
| 3551 | ( data_part_len & 0x01 ) ) ) |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 3552 | break; |
| 3553 | |
| 3554 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 3555 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 3556 | |
| 3557 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3558 | alg_arg, nonce, |
| 3559 | additional_data, -1, |
| 3560 | input_data, data_part_len, |
| 3561 | do_set_lengths, |
| 3562 | expected_output, |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 3563 | 1, 1, 1, |
| 3564 | ( data_part_len & 0x01 ) ) ) |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3565 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3566 | } |
| 3567 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3568 | |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3569 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 3570 | /* Goto is required to silence warnings about unused labels, as we |
| 3571 | * don't actually do any test assertions in this function. */ |
| 3572 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3573 | } |
| 3574 | /* END_CASE */ |
| 3575 | |
| 3576 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3577 | void aead_multipart_decrypt( int key_type_arg, data_t *key_data, |
| 3578 | int alg_arg, |
| 3579 | data_t *nonce, |
| 3580 | data_t *additional_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3581 | int do_test_ad_chunked, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3582 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3583 | int do_test_data_chunked, |
| 3584 | int do_set_lengths, |
| 3585 | data_t *expected_output, |
| 3586 | int expect_valid_signature ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3587 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3588 | size_t ad_part_len = 0; |
| 3589 | size_t data_part_len = 0; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3590 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3591 | /* Temporary whilst we have algorithms that cannot support chunking */ |
| 3592 | if( do_test_ad_chunked == 1 ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3593 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3594 | for( ad_part_len = 1; ad_part_len <= additional_data->len; |
| 3595 | ad_part_len++ ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3596 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 3597 | /* Split ad into length(ad_part_len) parts. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3598 | mbedtls_test_set_step( ad_part_len ); |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3599 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3600 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3601 | alg_arg, nonce, |
| 3602 | additional_data, |
| 3603 | ad_part_len, |
| 3604 | input_data, -1, |
| 3605 | do_set_lengths, |
| 3606 | expected_output, |
| 3607 | expect_valid_signature, |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 3608 | 0, 0, |
| 3609 | ( ad_part_len & 0x01 ) ) ) |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 3610 | break; |
| 3611 | |
| 3612 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 3613 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 3614 | |
| 3615 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3616 | alg_arg, nonce, |
| 3617 | additional_data, |
| 3618 | ad_part_len, |
| 3619 | input_data, -1, |
| 3620 | do_set_lengths, |
| 3621 | expected_output, |
| 3622 | expect_valid_signature, |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 3623 | 0, 1, |
| 3624 | ( ad_part_len & 0x01 ) ) ) |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3625 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3626 | } |
| 3627 | } |
| 3628 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3629 | /* Temporary whilst we have algorithms that cannot support chunking */ |
| 3630 | if( do_test_data_chunked == 1 ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3631 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3632 | for( data_part_len = 1; data_part_len <= input_data->len; |
| 3633 | data_part_len++ ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3634 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 3635 | /* Split data into length(data_part_len) parts. */ |
| 3636 | mbedtls_test_set_step( 2000 + data_part_len ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3637 | |
| 3638 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3639 | alg_arg, nonce, |
| 3640 | additional_data, -1, |
| 3641 | input_data, data_part_len, |
| 3642 | do_set_lengths, |
| 3643 | expected_output, |
| 3644 | expect_valid_signature, |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 3645 | 0, 0, |
| 3646 | ( data_part_len & 0x01 ) ) ) |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 3647 | break; |
| 3648 | |
| 3649 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 3650 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 3651 | |
| 3652 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3653 | alg_arg, nonce, |
| 3654 | additional_data, -1, |
| 3655 | input_data, data_part_len, |
| 3656 | do_set_lengths, |
| 3657 | expected_output, |
| 3658 | expect_valid_signature, |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 3659 | 0, 1, |
| 3660 | ( data_part_len & 0x01 ) ) ) |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3661 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3662 | } |
| 3663 | } |
| 3664 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 3665 | /* Goto is required to silence warnings about unused labels, as we |
| 3666 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3667 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3668 | } |
| 3669 | /* END_CASE */ |
| 3670 | |
| 3671 | /* BEGIN_CASE */ |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3672 | void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data, |
| 3673 | int alg_arg, |
| 3674 | int nonce_len, |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 3675 | int expected_generated_len_arg, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 3676 | data_t *additional_data, |
| 3677 | data_t *input_data, |
| 3678 | int expected_status_arg ) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3679 | { |
| 3680 | |
| 3681 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3682 | psa_key_type_t key_type = key_type_arg; |
| 3683 | psa_algorithm_t alg = alg_arg; |
| 3684 | psa_aead_operation_t operation; |
| 3685 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 3686 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3687 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 3688 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3689 | size_t nonce_generated_len = 0; |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 3690 | size_t expected_generated_len = expected_generated_len_arg; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 3691 | unsigned char *output_data = NULL; |
| 3692 | unsigned char *final_data = NULL; |
| 3693 | size_t output_size = 0; |
| 3694 | size_t finish_output_size = 0; |
| 3695 | size_t output_length = 0; |
| 3696 | size_t tag_length = 0; |
| 3697 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3698 | |
| 3699 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3700 | |
| 3701 | psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3702 | psa_set_key_algorithm( & attributes, alg ); |
| 3703 | psa_set_key_type( & attributes, key_type ); |
| 3704 | |
| 3705 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3706 | &key ) ); |
| 3707 | |
| 3708 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3709 | |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 3710 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 3711 | |
| 3712 | ASSERT_ALLOC( output_data, output_size ); |
| 3713 | |
| 3714 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 3715 | |
| 3716 | TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
| 3717 | |
| 3718 | ASSERT_ALLOC( final_data, finish_output_size ); |
| 3719 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3720 | operation = psa_aead_operation_init( ); |
| 3721 | |
| 3722 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 3723 | |
| 3724 | /* If the operation is not supported, just skip and not fail in case the |
| 3725 | * encryption involves a common limitation of cryptography hardwares and |
| 3726 | * an alternative implementation. */ |
| 3727 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 3728 | { |
| 3729 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3730 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_len ); |
| 3731 | } |
| 3732 | |
| 3733 | PSA_ASSERT( status ); |
| 3734 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3735 | status = psa_aead_generate_nonce( &operation, nonce_buffer, |
| 3736 | nonce_len, |
| 3737 | &nonce_generated_len ); |
| 3738 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 3739 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 3740 | |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 3741 | TEST_EQUAL( nonce_generated_len, expected_generated_len ); |
| 3742 | |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 3743 | TEST_ASSERT( nonce_generated_len < PSA_AEAD_NONCE_MAX_SIZE ); |
| 3744 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 3745 | if( expected_status == PSA_SUCCESS ) |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 3746 | { |
| 3747 | |
| 3748 | /* Ensure we can still complete operation. */ |
| 3749 | |
| 3750 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 3751 | additional_data->len ) ); |
| 3752 | |
| 3753 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
| 3754 | output_data, output_size, &output_length ) ); |
| 3755 | |
| 3756 | PSA_ASSERT( psa_aead_finish( &operation, final_data, finish_output_size, |
| 3757 | &output_length, tag_buffer, |
| 3758 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 3759 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3760 | |
| 3761 | exit: |
| 3762 | psa_destroy_key( key ); |
Paul Elliott | 16906f9 | 2021-06-24 09:57:01 +0100 | [diff] [blame] | 3763 | mbedtls_free( output_data ); |
| 3764 | mbedtls_free( final_data ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 3765 | psa_aead_abort( &operation ); |
| 3766 | PSA_DONE( ); |
| 3767 | } |
| 3768 | /* END_CASE */ |
| 3769 | |
| 3770 | /* BEGIN_CASE */ |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 3771 | void aead_multipart_set_nonce( int key_type_arg, data_t *key_data, |
| 3772 | int alg_arg, |
| 3773 | int nonce_len, |
| 3774 | data_t *additional_data, |
| 3775 | data_t *input_data, |
| 3776 | int expected_status_arg ) |
| 3777 | { |
| 3778 | |
| 3779 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3780 | psa_key_type_t key_type = key_type_arg; |
| 3781 | psa_algorithm_t alg = alg_arg; |
| 3782 | psa_aead_operation_t operation; |
| 3783 | uint8_t *nonce_buffer = NULL; |
| 3784 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3785 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3786 | psa_status_t expected_status = expected_status_arg; |
| 3787 | unsigned char *output_data = NULL; |
| 3788 | unsigned char *final_data = NULL; |
| 3789 | size_t output_size = 0; |
| 3790 | size_t finish_output_size = 0; |
| 3791 | size_t output_length = 0; |
| 3792 | size_t tag_length = 0; |
| 3793 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 3794 | int index = 0; |
| 3795 | |
| 3796 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3797 | |
| 3798 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3799 | psa_set_key_algorithm( &attributes, alg ); |
| 3800 | psa_set_key_type( &attributes, key_type ); |
| 3801 | |
| 3802 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3803 | &key ) ); |
| 3804 | |
| 3805 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3806 | |
| 3807 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 3808 | |
| 3809 | ASSERT_ALLOC( output_data, output_size ); |
| 3810 | |
| 3811 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 3812 | |
| 3813 | TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
| 3814 | |
| 3815 | ASSERT_ALLOC( final_data, finish_output_size ); |
| 3816 | |
| 3817 | operation = psa_aead_operation_init( ); |
| 3818 | |
| 3819 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 3820 | |
| 3821 | /* If the operation is not supported, just skip and not fail in case the |
| 3822 | * encryption involves a common limitation of cryptography hardwares and |
| 3823 | * an alternative implementation. */ |
| 3824 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 3825 | { |
| 3826 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3827 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_len ); |
| 3828 | } |
| 3829 | |
| 3830 | PSA_ASSERT( status ); |
| 3831 | |
| 3832 | ASSERT_ALLOC( nonce_buffer, nonce_len ); |
| 3833 | |
| 3834 | for( index = 0; index < nonce_len - 1; ++index) |
| 3835 | { |
| 3836 | nonce_buffer[index] = 'a' + index; |
| 3837 | } |
| 3838 | |
| 3839 | status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_len ); |
| 3840 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 3841 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 3842 | |
| 3843 | if( expected_status == PSA_SUCCESS ) |
| 3844 | { |
| 3845 | /* Ensure we can still complete operation. */ |
| 3846 | |
| 3847 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 3848 | additional_data->len ) ); |
| 3849 | |
| 3850 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
| 3851 | output_data, output_size, &output_length ) ); |
| 3852 | |
| 3853 | PSA_ASSERT( psa_aead_finish( &operation, final_data, finish_output_size, |
| 3854 | &output_length, tag_buffer, |
| 3855 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 3856 | } |
| 3857 | |
| 3858 | exit: |
| 3859 | psa_destroy_key( key ); |
| 3860 | mbedtls_free( output_data ); |
| 3861 | mbedtls_free( final_data ); |
| 3862 | mbedtls_free( nonce_buffer ); |
| 3863 | psa_aead_abort( &operation ); |
| 3864 | PSA_DONE( ); |
| 3865 | } |
| 3866 | /* END_CASE */ |
| 3867 | |
| 3868 | /* BEGIN_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame^] | 3869 | void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data, |
| 3870 | int alg_arg, |
| 3871 | int buffer_size, |
| 3872 | data_t *nonce, |
| 3873 | data_t *additional_data, |
| 3874 | data_t *input_data, |
| 3875 | int expected_status_arg ) |
| 3876 | { |
| 3877 | |
| 3878 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3879 | psa_key_type_t key_type = key_type_arg; |
| 3880 | psa_algorithm_t alg = alg_arg; |
| 3881 | psa_aead_operation_t operation; |
| 3882 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3883 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3884 | psa_status_t expected_status = expected_status_arg; |
| 3885 | unsigned char *output_data = NULL; |
| 3886 | unsigned char *final_data = NULL; |
| 3887 | size_t finish_output_size = 0; |
| 3888 | size_t output_length = 0; |
| 3889 | size_t tag_length = 0; |
| 3890 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 3891 | |
| 3892 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3893 | |
| 3894 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3895 | psa_set_key_algorithm( &attributes, alg ); |
| 3896 | psa_set_key_type( &attributes, key_type ); |
| 3897 | |
| 3898 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3899 | &key ) ); |
| 3900 | |
| 3901 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3902 | |
| 3903 | ASSERT_ALLOC( output_data, buffer_size ); |
| 3904 | |
| 3905 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 3906 | |
| 3907 | TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
| 3908 | |
| 3909 | ASSERT_ALLOC( final_data, finish_output_size ); |
| 3910 | |
| 3911 | operation = psa_aead_operation_init( ); |
| 3912 | |
| 3913 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 3914 | |
| 3915 | /* If the operation is not supported, just skip and not fail in case the |
| 3916 | * encryption involves a common limitation of cryptography hardwares and |
| 3917 | * an alternative implementation. */ |
| 3918 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 3919 | { |
| 3920 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3921 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 3922 | } |
| 3923 | |
| 3924 | PSA_ASSERT( status ); |
| 3925 | |
| 3926 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 3927 | |
| 3928 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 3929 | additional_data->len ) ); |
| 3930 | |
| 3931 | status = psa_aead_update( &operation, input_data->x, input_data->len, |
| 3932 | output_data, buffer_size, &output_length ); |
| 3933 | |
| 3934 | TEST_EQUAL( status, expected_status ); |
| 3935 | |
| 3936 | if( expected_status == PSA_SUCCESS ) |
| 3937 | { |
| 3938 | /* Ensure we can still complete operation. */ |
| 3939 | PSA_ASSERT( psa_aead_finish( &operation, final_data, finish_output_size, |
| 3940 | &output_length, tag_buffer, |
| 3941 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 3942 | } |
| 3943 | |
| 3944 | exit: |
| 3945 | psa_destroy_key( key ); |
| 3946 | mbedtls_free( output_data ); |
| 3947 | mbedtls_free( final_data ); |
| 3948 | psa_aead_abort( &operation ); |
| 3949 | PSA_DONE( ); |
| 3950 | } |
| 3951 | /* END_CASE */ |
| 3952 | |
| 3953 | |
| 3954 | /* BEGIN_CASE */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 3955 | void aead_multipart_state_test( int key_type_arg, data_t *key_data, |
| 3956 | int alg_arg, |
| 3957 | data_t *nonce, |
| 3958 | data_t *additional_data, |
| 3959 | data_t *input_data ) |
| 3960 | { |
| 3961 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3962 | psa_key_type_t key_type = key_type_arg; |
| 3963 | psa_algorithm_t alg = alg_arg; |
| 3964 | psa_aead_operation_t operation; |
| 3965 | unsigned char *output_data = NULL; |
| 3966 | unsigned char *final_data = NULL; |
| 3967 | size_t output_size = 0; |
| 3968 | size_t finish_output_size = 0; |
| 3969 | size_t output_length = 0; |
| 3970 | size_t key_bits = 0; |
| 3971 | size_t tag_length = 0; |
| 3972 | size_t tag_size = 0; |
| 3973 | size_t nonce_length = 0; |
| 3974 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 3975 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 3976 | size_t output_part_length = 0; |
| 3977 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3978 | |
| 3979 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3980 | |
| 3981 | psa_set_key_usage_flags( & attributes, |
| 3982 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3983 | psa_set_key_algorithm( & attributes, alg ); |
| 3984 | psa_set_key_type( & attributes, key_type ); |
| 3985 | |
| 3986 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3987 | &key ) ); |
| 3988 | |
| 3989 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3990 | key_bits = psa_get_key_bits( &attributes ); |
| 3991 | |
| 3992 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 3993 | |
| 3994 | TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE ); |
| 3995 | |
| 3996 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 3997 | |
| 3998 | ASSERT_ALLOC( output_data, output_size ); |
| 3999 | |
| 4000 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 4001 | |
| 4002 | TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
| 4003 | |
| 4004 | ASSERT_ALLOC( final_data, finish_output_size ); |
| 4005 | |
| 4006 | /* Test all operations error without calling setup first. */ |
| 4007 | |
| 4008 | operation = psa_aead_operation_init( ); |
| 4009 | |
| 4010 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 4011 | PSA_ERROR_BAD_STATE ); |
| 4012 | |
| 4013 | psa_aead_abort( &operation ); |
| 4014 | |
| 4015 | operation = psa_aead_operation_init( ); |
| 4016 | |
| 4017 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4018 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4019 | &nonce_length ), |
| 4020 | PSA_ERROR_BAD_STATE ); |
| 4021 | |
| 4022 | psa_aead_abort( &operation ); |
| 4023 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 4024 | /* ------------------------------------------------------- */ |
| 4025 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4026 | operation = psa_aead_operation_init( ); |
| 4027 | |
| 4028 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 4029 | input_data->len ), |
| 4030 | PSA_ERROR_BAD_STATE ); |
| 4031 | |
| 4032 | psa_aead_abort( &operation ); |
| 4033 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 4034 | /* ------------------------------------------------------- */ |
| 4035 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4036 | operation = psa_aead_operation_init( ); |
| 4037 | |
| 4038 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 4039 | additional_data->len ), |
| 4040 | PSA_ERROR_BAD_STATE ); |
| 4041 | |
| 4042 | psa_aead_abort( &operation ); |
| 4043 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 4044 | /* ------------------------------------------------------- */ |
| 4045 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4046 | operation = psa_aead_operation_init( ); |
| 4047 | |
| 4048 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 4049 | input_data->len, output_data, |
| 4050 | output_size, &output_length ), |
| 4051 | PSA_ERROR_BAD_STATE ); |
| 4052 | |
| 4053 | psa_aead_abort( &operation ); |
| 4054 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 4055 | /* ------------------------------------------------------- */ |
| 4056 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4057 | operation = psa_aead_operation_init( ); |
| 4058 | |
| 4059 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 4060 | finish_output_size, |
| 4061 | &output_part_length, |
| 4062 | tag_buffer, tag_length, |
| 4063 | &tag_size ), |
| 4064 | PSA_ERROR_BAD_STATE ); |
| 4065 | |
| 4066 | psa_aead_abort( &operation ); |
| 4067 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 4068 | /* ------------------------------------------------------- */ |
| 4069 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4070 | operation = psa_aead_operation_init( ); |
| 4071 | |
| 4072 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 4073 | finish_output_size, |
| 4074 | &output_part_length, |
| 4075 | tag_buffer, |
| 4076 | tag_length ), |
| 4077 | PSA_ERROR_BAD_STATE ); |
| 4078 | |
| 4079 | psa_aead_abort( &operation ); |
| 4080 | |
| 4081 | /* Test for double setups. */ |
| 4082 | |
| 4083 | operation = psa_aead_operation_init( ); |
| 4084 | |
| 4085 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4086 | |
| 4087 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 4088 | PSA_ERROR_BAD_STATE ); |
| 4089 | |
| 4090 | psa_aead_abort( &operation ); |
| 4091 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 4092 | /* ------------------------------------------------------- */ |
| 4093 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4094 | operation = psa_aead_operation_init( ); |
| 4095 | |
| 4096 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 4097 | |
| 4098 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 4099 | PSA_ERROR_BAD_STATE ); |
| 4100 | |
| 4101 | psa_aead_abort( &operation ); |
| 4102 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 4103 | /* ------------------------------------------------------- */ |
| 4104 | |
| 4105 | operation = psa_aead_operation_init( ); |
| 4106 | |
| 4107 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4108 | |
| 4109 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 4110 | PSA_ERROR_BAD_STATE ); |
| 4111 | |
| 4112 | psa_aead_abort( &operation ); |
| 4113 | |
| 4114 | /* ------------------------------------------------------- */ |
| 4115 | |
| 4116 | operation = psa_aead_operation_init( ); |
| 4117 | |
| 4118 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 4119 | |
| 4120 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 4121 | PSA_ERROR_BAD_STATE ); |
| 4122 | |
| 4123 | psa_aead_abort( &operation ); |
| 4124 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4125 | /* Test for not setting a nonce. */ |
| 4126 | |
| 4127 | operation = psa_aead_operation_init( ); |
| 4128 | |
| 4129 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4130 | |
| 4131 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 4132 | additional_data->len ), |
| 4133 | PSA_ERROR_BAD_STATE ); |
| 4134 | |
| 4135 | psa_aead_abort( &operation ); |
| 4136 | |
| 4137 | /* Test for double setting nonce. */ |
| 4138 | |
| 4139 | operation = psa_aead_operation_init( ); |
| 4140 | |
| 4141 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4142 | |
| 4143 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4144 | |
| 4145 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 4146 | PSA_ERROR_BAD_STATE ); |
| 4147 | |
| 4148 | psa_aead_abort( &operation ); |
| 4149 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 4150 | /* Test for double generating nonce. */ |
| 4151 | |
| 4152 | operation = psa_aead_operation_init( ); |
| 4153 | |
| 4154 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4155 | |
| 4156 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4157 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4158 | &nonce_length ) ); |
| 4159 | |
| 4160 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4161 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4162 | &nonce_length ), |
| 4163 | PSA_ERROR_BAD_STATE ); |
| 4164 | |
| 4165 | |
| 4166 | psa_aead_abort( &operation ); |
| 4167 | |
| 4168 | /* Test for generate nonce then set and vice versa */ |
| 4169 | |
| 4170 | operation = psa_aead_operation_init( ); |
| 4171 | |
| 4172 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4173 | |
| 4174 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4175 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4176 | &nonce_length ) ); |
| 4177 | |
| 4178 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 4179 | PSA_ERROR_BAD_STATE ); |
| 4180 | |
| 4181 | psa_aead_abort( &operation ); |
| 4182 | |
| 4183 | /* ------------------------------------------------------- */ |
| 4184 | |
| 4185 | operation = psa_aead_operation_init( ); |
| 4186 | |
| 4187 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4188 | |
| 4189 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4190 | |
| 4191 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4192 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4193 | &nonce_length ), |
| 4194 | PSA_ERROR_BAD_STATE ); |
| 4195 | |
| 4196 | psa_aead_abort( &operation ); |
| 4197 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 4198 | /* Test for generating nonce in decrypt setup. */ |
| 4199 | |
| 4200 | operation = psa_aead_operation_init( ); |
| 4201 | |
| 4202 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 4203 | |
| 4204 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4205 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4206 | &nonce_length ), |
| 4207 | PSA_ERROR_BAD_STATE ); |
| 4208 | |
| 4209 | psa_aead_abort( &operation ); |
| 4210 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4211 | /* Test for setting lengths twice. */ |
| 4212 | |
| 4213 | operation = psa_aead_operation_init( ); |
| 4214 | |
| 4215 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4216 | |
| 4217 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4218 | |
| 4219 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4220 | input_data->len ) ); |
| 4221 | |
| 4222 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 4223 | input_data->len ), |
| 4224 | PSA_ERROR_BAD_STATE ); |
| 4225 | |
| 4226 | psa_aead_abort( &operation ); |
| 4227 | |
| 4228 | /* Test for setting lengths after already starting data. */ |
| 4229 | |
| 4230 | operation = psa_aead_operation_init( ); |
| 4231 | |
| 4232 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4233 | |
| 4234 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4235 | |
| 4236 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 4237 | input_data->len, output_data, |
| 4238 | output_size, &output_length ) ); |
| 4239 | |
| 4240 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 4241 | input_data->len ), |
| 4242 | PSA_ERROR_BAD_STATE ); |
| 4243 | |
| 4244 | psa_aead_abort( &operation ); |
| 4245 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 4246 | /* Test for not sending any additional data or data after setting non zero |
| 4247 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4248 | |
| 4249 | operation = psa_aead_operation_init( ); |
| 4250 | |
| 4251 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4252 | |
| 4253 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4254 | |
| 4255 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4256 | input_data->len ) ); |
| 4257 | |
| 4258 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 4259 | finish_output_size, |
| 4260 | &output_part_length, |
| 4261 | tag_buffer, tag_length, |
| 4262 | &tag_size ), |
| 4263 | PSA_ERROR_INVALID_ARGUMENT ); |
| 4264 | |
| 4265 | psa_aead_abort( &operation ); |
| 4266 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 4267 | /* Test for not sending any additional data or data after setting non-zero |
| 4268 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4269 | |
| 4270 | operation = psa_aead_operation_init( ); |
| 4271 | |
| 4272 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 4273 | |
| 4274 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4275 | |
| 4276 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4277 | input_data->len ) ); |
| 4278 | |
| 4279 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 4280 | finish_output_size, |
| 4281 | &output_part_length, |
| 4282 | tag_buffer, |
| 4283 | tag_length ), |
| 4284 | PSA_ERROR_INVALID_ARGUMENT ); |
| 4285 | |
| 4286 | psa_aead_abort( &operation ); |
| 4287 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 4288 | /* Test for not sending any additional data after setting a non-zero length |
| 4289 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4290 | |
| 4291 | operation = psa_aead_operation_init( ); |
| 4292 | |
| 4293 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4294 | |
| 4295 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4296 | |
| 4297 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4298 | input_data->len ) ); |
| 4299 | |
| 4300 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 4301 | input_data->len, output_data, |
| 4302 | output_size, &output_length ), |
| 4303 | PSA_ERROR_INVALID_ARGUMENT ); |
| 4304 | |
| 4305 | psa_aead_abort( &operation ); |
| 4306 | |
| 4307 | /* Test sending additional data after data. */ |
| 4308 | |
| 4309 | operation = psa_aead_operation_init( ); |
| 4310 | |
| 4311 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4312 | |
| 4313 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4314 | |
| 4315 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 4316 | input_data->len, output_data, |
| 4317 | output_size, &output_length ) ); |
| 4318 | |
| 4319 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 4320 | additional_data->len ), |
| 4321 | PSA_ERROR_BAD_STATE ); |
| 4322 | |
| 4323 | psa_aead_abort( &operation ); |
| 4324 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 4325 | /* Test calling finish on decryption. */ |
| 4326 | |
| 4327 | operation = psa_aead_operation_init( ); |
| 4328 | |
| 4329 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 4330 | |
| 4331 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4332 | |
| 4333 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 4334 | finish_output_size, |
| 4335 | &output_part_length, |
| 4336 | tag_buffer, tag_length, |
| 4337 | &tag_size ), |
| 4338 | PSA_ERROR_BAD_STATE ); |
| 4339 | |
| 4340 | psa_aead_abort( &operation ); |
| 4341 | |
| 4342 | /* Test calling verify on encryption. */ |
| 4343 | |
| 4344 | operation = psa_aead_operation_init( ); |
| 4345 | |
| 4346 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4347 | |
| 4348 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4349 | |
| 4350 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 4351 | finish_output_size, |
| 4352 | &output_part_length, |
| 4353 | tag_buffer, |
| 4354 | tag_length ), |
Paul Elliott | 5b065cb | 2021-06-23 08:33:22 +0100 | [diff] [blame] | 4355 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 4356 | |
| 4357 | psa_aead_abort( &operation ); |
| 4358 | |
| 4359 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4360 | exit: |
| 4361 | psa_destroy_key( key ); |
| 4362 | psa_aead_abort( &operation ); |
| 4363 | mbedtls_free( output_data ); |
| 4364 | mbedtls_free( final_data ); |
| 4365 | PSA_DONE( ); |
| 4366 | } |
| 4367 | /* END_CASE */ |
| 4368 | |
| 4369 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4370 | void signature_size( int type_arg, |
| 4371 | int bits, |
| 4372 | int alg_arg, |
| 4373 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4374 | { |
| 4375 | psa_key_type_t type = type_arg; |
| 4376 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4377 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 4378 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4379 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 4380 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4381 | exit: |
| 4382 | ; |
| 4383 | } |
| 4384 | /* END_CASE */ |
| 4385 | |
| 4386 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 4387 | void sign_hash_deterministic( int key_type_arg, data_t *key_data, |
| 4388 | int alg_arg, data_t *input_data, |
| 4389 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4390 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4391 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4392 | psa_key_type_t key_type = key_type_arg; |
| 4393 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4394 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4395 | unsigned char *signature = NULL; |
| 4396 | size_t signature_size; |
| 4397 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4398 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4399 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4400 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4401 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4402 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4403 | psa_set_key_algorithm( &attributes, alg ); |
| 4404 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 4405 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4406 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4407 | &key ) ); |
| 4408 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4409 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4410 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4411 | /* Allocate a buffer which has the size advertized by the |
| 4412 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4413 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4414 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4415 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4416 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4417 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4418 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4419 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4420 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4421 | input_data->x, input_data->len, |
| 4422 | signature, signature_size, |
| 4423 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4424 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4425 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 4426 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4427 | |
| 4428 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4429 | /* |
| 4430 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4431 | * thus reset them as required. |
| 4432 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4433 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4434 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4435 | psa_destroy_key( key ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 4436 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4437 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4438 | } |
| 4439 | /* END_CASE */ |
| 4440 | |
| 4441 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 4442 | void sign_hash_fail( int key_type_arg, data_t *key_data, |
| 4443 | int alg_arg, data_t *input_data, |
| 4444 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4445 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4446 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4447 | psa_key_type_t key_type = key_type_arg; |
| 4448 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4449 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4450 | psa_status_t actual_status; |
| 4451 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 4452 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 4453 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4454 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4455 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4456 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4457 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4458 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4459 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4460 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4461 | psa_set_key_algorithm( &attributes, alg ); |
| 4462 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 4463 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4464 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4465 | &key ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4466 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4467 | actual_status = psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4468 | input_data->x, input_data->len, |
| 4469 | signature, signature_size, |
| 4470 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4471 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4472 | /* The value of *signature_length is unspecified on error, but |
| 4473 | * whatever it is, it should be less than signature_size, so that |
| 4474 | * if the caller tries to read *signature_length bytes without |
| 4475 | * checking the error code then they don't overflow a buffer. */ |
| 4476 | TEST_ASSERT( signature_length <= signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4477 | |
| 4478 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4479 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4480 | psa_destroy_key( key ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4481 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4482 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4483 | } |
| 4484 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 4485 | |
| 4486 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 4487 | void sign_verify_hash( int key_type_arg, data_t *key_data, |
| 4488 | int alg_arg, data_t *input_data ) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4489 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4490 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4491 | psa_key_type_t key_type = key_type_arg; |
| 4492 | psa_algorithm_t alg = alg_arg; |
| 4493 | size_t key_bits; |
| 4494 | unsigned char *signature = NULL; |
| 4495 | size_t signature_size; |
| 4496 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4497 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4498 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4499 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4500 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4501 | 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] | 4502 | psa_set_key_algorithm( &attributes, alg ); |
| 4503 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4504 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4505 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4506 | &key ) ); |
| 4507 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4508 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4509 | |
| 4510 | /* Allocate a buffer which has the size advertized by the |
| 4511 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4512 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4513 | key_bits, alg ); |
| 4514 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4515 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4516 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4517 | |
| 4518 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4519 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4520 | input_data->x, input_data->len, |
| 4521 | signature, signature_size, |
| 4522 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4523 | /* Check that the signature length looks sensible. */ |
| 4524 | TEST_ASSERT( signature_length <= signature_size ); |
| 4525 | TEST_ASSERT( signature_length > 0 ); |
| 4526 | |
| 4527 | /* Use the library to verify that the signature is correct. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4528 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4529 | input_data->x, input_data->len, |
| 4530 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4531 | |
| 4532 | if( input_data->len != 0 ) |
| 4533 | { |
| 4534 | /* Flip a bit in the input and verify that the signature is now |
| 4535 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 4536 | * because ECDSA may ignore the last few bits of the input. */ |
| 4537 | input_data->x[0] ^= 1; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4538 | TEST_EQUAL( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4539 | input_data->x, input_data->len, |
| 4540 | signature, signature_length ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4541 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4542 | } |
| 4543 | |
| 4544 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4545 | /* |
| 4546 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4547 | * thus reset them as required. |
| 4548 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4549 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4550 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4551 | psa_destroy_key( key ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4552 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4553 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4554 | } |
| 4555 | /* END_CASE */ |
| 4556 | |
| 4557 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 4558 | void verify_hash( int key_type_arg, data_t *key_data, |
| 4559 | int alg_arg, data_t *hash_data, |
| 4560 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4561 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4562 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4563 | psa_key_type_t key_type = key_type_arg; |
| 4564 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4565 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4566 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4567 | TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 4568 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4569 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4570 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4571 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4572 | psa_set_key_algorithm( &attributes, alg ); |
| 4573 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4574 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4575 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4576 | &key ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4577 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4578 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4579 | hash_data->x, hash_data->len, |
| 4580 | signature_data->x, signature_data->len ) ); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 4581 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4582 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4583 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4584 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4585 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4586 | } |
| 4587 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4588 | |
| 4589 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 4590 | void verify_hash_fail( int key_type_arg, data_t *key_data, |
| 4591 | int alg_arg, data_t *hash_data, |
| 4592 | data_t *signature_data, |
| 4593 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4594 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4595 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4596 | psa_key_type_t key_type = key_type_arg; |
| 4597 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4598 | psa_status_t actual_status; |
| 4599 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4600 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4601 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4602 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4603 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4604 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4605 | psa_set_key_algorithm( &attributes, alg ); |
| 4606 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4607 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4608 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4609 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4610 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4611 | actual_status = psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4612 | hash_data->x, hash_data->len, |
| 4613 | signature_data->x, signature_data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4614 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4615 | |
| 4616 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4617 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4618 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4619 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4620 | } |
| 4621 | /* END_CASE */ |
| 4622 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4623 | /* BEGIN_CASE */ |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 4624 | void sign_message_deterministic( int key_type_arg, |
| 4625 | data_t *key_data, |
| 4626 | int alg_arg, |
| 4627 | data_t *input_data, |
| 4628 | data_t *output_data ) |
| 4629 | { |
| 4630 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4631 | psa_key_type_t key_type = key_type_arg; |
| 4632 | psa_algorithm_t alg = alg_arg; |
| 4633 | size_t key_bits; |
| 4634 | unsigned char *signature = NULL; |
| 4635 | size_t signature_size; |
| 4636 | size_t signature_length = 0xdeadbeef; |
| 4637 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4638 | |
| 4639 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4640 | |
| 4641 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 4642 | psa_set_key_algorithm( &attributes, alg ); |
| 4643 | psa_set_key_type( &attributes, key_type ); |
| 4644 | |
| 4645 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4646 | &key ) ); |
| 4647 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4648 | key_bits = psa_get_key_bits( &attributes ); |
| 4649 | |
| 4650 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 4651 | TEST_ASSERT( signature_size != 0 ); |
| 4652 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
| 4653 | ASSERT_ALLOC( signature, signature_size ); |
| 4654 | |
| 4655 | PSA_ASSERT( psa_sign_message( key, alg, |
| 4656 | input_data->x, input_data->len, |
| 4657 | signature, signature_size, |
| 4658 | &signature_length ) ); |
| 4659 | |
| 4660 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 4661 | signature, signature_length ); |
| 4662 | |
| 4663 | exit: |
| 4664 | psa_reset_key_attributes( &attributes ); |
| 4665 | |
| 4666 | psa_destroy_key( key ); |
| 4667 | mbedtls_free( signature ); |
| 4668 | PSA_DONE( ); |
| 4669 | |
| 4670 | } |
| 4671 | /* END_CASE */ |
| 4672 | |
| 4673 | /* BEGIN_CASE */ |
| 4674 | void sign_message_fail( int key_type_arg, |
| 4675 | data_t *key_data, |
| 4676 | int alg_arg, |
| 4677 | data_t *input_data, |
| 4678 | int signature_size_arg, |
| 4679 | int expected_status_arg ) |
| 4680 | { |
| 4681 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4682 | psa_key_type_t key_type = key_type_arg; |
| 4683 | psa_algorithm_t alg = alg_arg; |
| 4684 | size_t signature_size = signature_size_arg; |
| 4685 | psa_status_t actual_status; |
| 4686 | psa_status_t expected_status = expected_status_arg; |
| 4687 | unsigned char *signature = NULL; |
| 4688 | size_t signature_length = 0xdeadbeef; |
| 4689 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4690 | |
| 4691 | ASSERT_ALLOC( signature, signature_size ); |
| 4692 | |
| 4693 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4694 | |
| 4695 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 4696 | psa_set_key_algorithm( &attributes, alg ); |
| 4697 | psa_set_key_type( &attributes, key_type ); |
| 4698 | |
| 4699 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4700 | &key ) ); |
| 4701 | |
| 4702 | actual_status = psa_sign_message( key, alg, |
| 4703 | input_data->x, input_data->len, |
| 4704 | signature, signature_size, |
| 4705 | &signature_length ); |
| 4706 | TEST_EQUAL( actual_status, expected_status ); |
| 4707 | /* The value of *signature_length is unspecified on error, but |
| 4708 | * whatever it is, it should be less than signature_size, so that |
| 4709 | * if the caller tries to read *signature_length bytes without |
| 4710 | * checking the error code then they don't overflow a buffer. */ |
| 4711 | TEST_ASSERT( signature_length <= signature_size ); |
| 4712 | |
| 4713 | exit: |
| 4714 | psa_reset_key_attributes( &attributes ); |
| 4715 | psa_destroy_key( key ); |
| 4716 | mbedtls_free( signature ); |
| 4717 | PSA_DONE( ); |
| 4718 | } |
| 4719 | /* END_CASE */ |
| 4720 | |
| 4721 | /* BEGIN_CASE */ |
| 4722 | void sign_verify_message( int key_type_arg, |
| 4723 | data_t *key_data, |
| 4724 | int alg_arg, |
| 4725 | data_t *input_data ) |
| 4726 | { |
| 4727 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4728 | psa_key_type_t key_type = key_type_arg; |
| 4729 | psa_algorithm_t alg = alg_arg; |
| 4730 | size_t key_bits; |
| 4731 | unsigned char *signature = NULL; |
| 4732 | size_t signature_size; |
| 4733 | size_t signature_length = 0xdeadbeef; |
| 4734 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4735 | |
| 4736 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4737 | |
| 4738 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 4739 | PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 4740 | psa_set_key_algorithm( &attributes, alg ); |
| 4741 | psa_set_key_type( &attributes, key_type ); |
| 4742 | |
| 4743 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4744 | &key ) ); |
| 4745 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4746 | key_bits = psa_get_key_bits( &attributes ); |
| 4747 | |
| 4748 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 4749 | TEST_ASSERT( signature_size != 0 ); |
| 4750 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
| 4751 | ASSERT_ALLOC( signature, signature_size ); |
| 4752 | |
| 4753 | PSA_ASSERT( psa_sign_message( key, alg, |
| 4754 | input_data->x, input_data->len, |
| 4755 | signature, signature_size, |
| 4756 | &signature_length ) ); |
| 4757 | TEST_ASSERT( signature_length <= signature_size ); |
| 4758 | TEST_ASSERT( signature_length > 0 ); |
| 4759 | |
| 4760 | PSA_ASSERT( psa_verify_message( key, alg, |
| 4761 | input_data->x, input_data->len, |
| 4762 | signature, signature_length ) ); |
| 4763 | |
| 4764 | if( input_data->len != 0 ) |
| 4765 | { |
| 4766 | /* Flip a bit in the input and verify that the signature is now |
| 4767 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 4768 | * because ECDSA may ignore the last few bits of the input. */ |
| 4769 | input_data->x[0] ^= 1; |
| 4770 | TEST_EQUAL( psa_verify_message( key, alg, |
| 4771 | input_data->x, input_data->len, |
| 4772 | signature, signature_length ), |
| 4773 | PSA_ERROR_INVALID_SIGNATURE ); |
| 4774 | } |
| 4775 | |
| 4776 | exit: |
| 4777 | psa_reset_key_attributes( &attributes ); |
| 4778 | |
| 4779 | psa_destroy_key( key ); |
| 4780 | mbedtls_free( signature ); |
| 4781 | PSA_DONE( ); |
| 4782 | } |
| 4783 | /* END_CASE */ |
| 4784 | |
| 4785 | /* BEGIN_CASE */ |
| 4786 | void verify_message( int key_type_arg, |
| 4787 | data_t *key_data, |
| 4788 | int alg_arg, |
| 4789 | data_t *input_data, |
| 4790 | data_t *signature_data ) |
| 4791 | { |
| 4792 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4793 | psa_key_type_t key_type = key_type_arg; |
| 4794 | psa_algorithm_t alg = alg_arg; |
| 4795 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4796 | |
| 4797 | TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE ); |
| 4798 | |
| 4799 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4800 | |
| 4801 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 4802 | psa_set_key_algorithm( &attributes, alg ); |
| 4803 | psa_set_key_type( &attributes, key_type ); |
| 4804 | |
| 4805 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4806 | &key ) ); |
| 4807 | |
| 4808 | PSA_ASSERT( psa_verify_message( key, alg, |
| 4809 | input_data->x, input_data->len, |
| 4810 | signature_data->x, signature_data->len ) ); |
| 4811 | |
| 4812 | exit: |
| 4813 | psa_reset_key_attributes( &attributes ); |
| 4814 | psa_destroy_key( key ); |
| 4815 | PSA_DONE( ); |
| 4816 | } |
| 4817 | /* END_CASE */ |
| 4818 | |
| 4819 | /* BEGIN_CASE */ |
| 4820 | void verify_message_fail( int key_type_arg, |
| 4821 | data_t *key_data, |
| 4822 | int alg_arg, |
| 4823 | data_t *hash_data, |
| 4824 | data_t *signature_data, |
| 4825 | int expected_status_arg ) |
| 4826 | { |
| 4827 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4828 | psa_key_type_t key_type = key_type_arg; |
| 4829 | psa_algorithm_t alg = alg_arg; |
| 4830 | psa_status_t actual_status; |
| 4831 | psa_status_t expected_status = expected_status_arg; |
| 4832 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4833 | |
| 4834 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4835 | |
| 4836 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 4837 | psa_set_key_algorithm( &attributes, alg ); |
| 4838 | psa_set_key_type( &attributes, key_type ); |
| 4839 | |
| 4840 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4841 | &key ) ); |
| 4842 | |
| 4843 | actual_status = psa_verify_message( key, alg, |
| 4844 | hash_data->x, hash_data->len, |
| 4845 | signature_data->x, |
| 4846 | signature_data->len ); |
| 4847 | TEST_EQUAL( actual_status, expected_status ); |
| 4848 | |
| 4849 | exit: |
| 4850 | psa_reset_key_attributes( &attributes ); |
| 4851 | psa_destroy_key( key ); |
| 4852 | PSA_DONE( ); |
| 4853 | } |
| 4854 | /* END_CASE */ |
| 4855 | |
| 4856 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4857 | void asymmetric_encrypt( int key_type_arg, |
| 4858 | data_t *key_data, |
| 4859 | int alg_arg, |
| 4860 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4861 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4862 | int expected_output_length_arg, |
| 4863 | int expected_status_arg ) |
| 4864 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4865 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4866 | psa_key_type_t key_type = key_type_arg; |
| 4867 | psa_algorithm_t alg = alg_arg; |
| 4868 | size_t expected_output_length = expected_output_length_arg; |
| 4869 | size_t key_bits; |
| 4870 | unsigned char *output = NULL; |
| 4871 | size_t output_size; |
| 4872 | size_t output_length = ~0; |
| 4873 | psa_status_t actual_status; |
| 4874 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4875 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4876 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4877 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4878 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4879 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4880 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4881 | psa_set_key_algorithm( &attributes, alg ); |
| 4882 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4883 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4884 | &key ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4885 | |
| 4886 | /* Determine the maximum output length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4887 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4888 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4889 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4890 | 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] | 4891 | TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4892 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4893 | |
| 4894 | /* Encrypt the input */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4895 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4896 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4897 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4898 | output, output_size, |
| 4899 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4900 | TEST_EQUAL( actual_status, expected_status ); |
| 4901 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4902 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4903 | /* If the label is empty, the test framework puts a non-null pointer |
| 4904 | * in label->x. Test that a null pointer works as well. */ |
| 4905 | if( label->len == 0 ) |
| 4906 | { |
| 4907 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4908 | if( output_size != 0 ) |
| 4909 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4910 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4911 | input_data->x, input_data->len, |
| 4912 | NULL, label->len, |
| 4913 | output, output_size, |
| 4914 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4915 | TEST_EQUAL( actual_status, expected_status ); |
| 4916 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4917 | } |
| 4918 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4919 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4920 | /* |
| 4921 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4922 | * thus reset them as required. |
| 4923 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4924 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4925 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4926 | psa_destroy_key( key ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4927 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4928 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4929 | } |
| 4930 | /* END_CASE */ |
| 4931 | |
| 4932 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4933 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 4934 | data_t *key_data, |
| 4935 | int alg_arg, |
| 4936 | data_t *input_data, |
| 4937 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4938 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4939 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4940 | psa_key_type_t key_type = key_type_arg; |
| 4941 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4942 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4943 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4944 | size_t output_size; |
| 4945 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4946 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4947 | size_t output2_size; |
| 4948 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4949 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4950 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4951 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4952 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4953 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4954 | psa_set_key_algorithm( &attributes, alg ); |
| 4955 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4956 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4957 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4958 | &key ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4959 | |
| 4960 | /* Determine the maximum ciphertext length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4961 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4962 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4963 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4964 | 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] | 4965 | TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4966 | ASSERT_ALLOC( output, output_size ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4967 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4968 | output2_size = input_data->len; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4969 | TEST_ASSERT( output2_size <= |
| 4970 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) ); |
| 4971 | TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4972 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4973 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 4974 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 4975 | * the original plaintext because of the non-optional random |
| 4976 | * part of encryption process which prevents using fixed vectors. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4977 | PSA_ASSERT( psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4978 | input_data->x, input_data->len, |
| 4979 | label->x, label->len, |
| 4980 | output, output_size, |
| 4981 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4982 | /* We don't know what ciphertext length to expect, but check that |
| 4983 | * it looks sensible. */ |
| 4984 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4985 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4986 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4987 | output, output_length, |
| 4988 | label->x, label->len, |
| 4989 | output2, output2_size, |
| 4990 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4991 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 4992 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4993 | |
| 4994 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4995 | /* |
| 4996 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4997 | * thus reset them as required. |
| 4998 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4999 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5000 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5001 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 5002 | mbedtls_free( output ); |
| 5003 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5004 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5005 | } |
| 5006 | /* END_CASE */ |
| 5007 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5008 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5009 | void asymmetric_decrypt( int key_type_arg, |
| 5010 | data_t *key_data, |
| 5011 | int alg_arg, |
| 5012 | data_t *input_data, |
| 5013 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 5014 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5015 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5016 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5017 | psa_key_type_t key_type = key_type_arg; |
| 5018 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5019 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5020 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 5021 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5022 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5023 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5024 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5025 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5026 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5027 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 5028 | psa_set_key_algorithm( &attributes, alg ); |
| 5029 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 5030 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5031 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5032 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5033 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5034 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5035 | key_bits = psa_get_key_bits( &attributes ); |
| 5036 | |
| 5037 | /* Determine the maximum ciphertext length */ |
| 5038 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 5039 | TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
| 5040 | ASSERT_ALLOC( output, output_size ); |
| 5041 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5042 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5043 | input_data->x, input_data->len, |
| 5044 | label->x, label->len, |
| 5045 | output, |
| 5046 | output_size, |
| 5047 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 5048 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 5049 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5050 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5051 | /* If the label is empty, the test framework puts a non-null pointer |
| 5052 | * in label->x. Test that a null pointer works as well. */ |
| 5053 | if( label->len == 0 ) |
| 5054 | { |
| 5055 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 5056 | if( output_size != 0 ) |
| 5057 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5058 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5059 | input_data->x, input_data->len, |
| 5060 | NULL, label->len, |
| 5061 | output, |
| 5062 | output_size, |
| 5063 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 5064 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 5065 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5066 | } |
| 5067 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5068 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5069 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5070 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 5071 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5072 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5073 | } |
| 5074 | /* END_CASE */ |
| 5075 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5076 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5077 | void asymmetric_decrypt_fail( int key_type_arg, |
| 5078 | data_t *key_data, |
| 5079 | int alg_arg, |
| 5080 | data_t *input_data, |
| 5081 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 5082 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5083 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5084 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5085 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5086 | psa_key_type_t key_type = key_type_arg; |
| 5087 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5088 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 5089 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5090 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5091 | psa_status_t actual_status; |
| 5092 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5093 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5094 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5095 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 5096 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5097 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5098 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5099 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 5100 | psa_set_key_algorithm( &attributes, alg ); |
| 5101 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 5102 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5103 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5104 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5105 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5106 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 5107 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5108 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 5109 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5110 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5111 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5112 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5113 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5114 | /* If the label is empty, the test framework puts a non-null pointer |
| 5115 | * in label->x. Test that a null pointer works as well. */ |
| 5116 | if( label->len == 0 ) |
| 5117 | { |
| 5118 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 5119 | if( output_size != 0 ) |
| 5120 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5121 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5122 | input_data->x, input_data->len, |
| 5123 | NULL, label->len, |
| 5124 | output, output_size, |
| 5125 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5126 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5127 | TEST_ASSERT( output_length <= output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5128 | } |
| 5129 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5130 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5131 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5132 | psa_destroy_key( key ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5133 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5134 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5135 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 5136 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5137 | |
| 5138 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5139 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 5140 | { |
| 5141 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 5142 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 5143 | * though it's OK by the C standard. We could test for this, but we'd need |
| 5144 | * to supress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 5145 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5146 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 5147 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5148 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 5149 | |
| 5150 | memset( &zero, 0, sizeof( zero ) ); |
| 5151 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5152 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5153 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5154 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5155 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5156 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5157 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5158 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 5159 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5160 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5161 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 5162 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 5163 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 5164 | } |
| 5165 | /* END_CASE */ |
| 5166 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 5167 | /* BEGIN_CASE */ |
| 5168 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5169 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5170 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5171 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5172 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5173 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5174 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5175 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 5176 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 5177 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5178 | |
| 5179 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5180 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5181 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 5182 | } |
| 5183 | /* END_CASE */ |
| 5184 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5185 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 5186 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 5187 | int expected_status_arg ) |
| 5188 | { |
| 5189 | psa_algorithm_t alg = alg_arg; |
| 5190 | size_t capacity = capacity_arg; |
| 5191 | psa_status_t expected_status = expected_status_arg; |
| 5192 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5193 | |
| 5194 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5195 | |
| 5196 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 5197 | |
| 5198 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 5199 | expected_status ); |
| 5200 | |
| 5201 | exit: |
| 5202 | psa_key_derivation_abort( &operation ); |
| 5203 | PSA_DONE( ); |
| 5204 | } |
| 5205 | /* END_CASE */ |
| 5206 | |
| 5207 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5208 | void derive_input( int alg_arg, |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 5209 | int step_arg1, int key_type_arg1, data_t *input1, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5210 | int expected_status_arg1, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 5211 | int step_arg2, int key_type_arg2, data_t *input2, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5212 | int expected_status_arg2, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 5213 | int step_arg3, int key_type_arg3, data_t *input3, |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 5214 | int expected_status_arg3, |
| 5215 | int output_key_type_arg, int expected_output_status_arg ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5216 | { |
| 5217 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 5218 | psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3}; |
| 5219 | 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] | 5220 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 5221 | expected_status_arg2, |
| 5222 | expected_status_arg3}; |
| 5223 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5224 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 5225 | MBEDTLS_SVC_KEY_ID_INIT, |
| 5226 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5227 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5228 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5229 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 5230 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5231 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 5232 | psa_status_t expected_output_status = expected_output_status_arg; |
| 5233 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5234 | |
| 5235 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5236 | |
| 5237 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5238 | psa_set_key_algorithm( &attributes, alg ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5239 | |
| 5240 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 5241 | |
| 5242 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 5243 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 5244 | if( key_types[i] != PSA_KEY_TYPE_NONE ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5245 | { |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 5246 | psa_set_key_type( &attributes, key_types[i] ); |
| 5247 | PSA_ASSERT( psa_import_key( &attributes, |
| 5248 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5249 | &keys[i] ) ); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 5250 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) && |
| 5251 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 5252 | { |
| 5253 | // When taking a private key as secret input, use key agreement |
| 5254 | // to add the shared secret to the derivation |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5255 | TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self( |
| 5256 | &operation, keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 5257 | expected_statuses[i] ); |
| 5258 | } |
| 5259 | else |
| 5260 | { |
| 5261 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5262 | keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 5263 | expected_statuses[i] ); |
| 5264 | } |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 5265 | } |
| 5266 | else |
| 5267 | { |
| 5268 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 5269 | &operation, steps[i], |
| 5270 | inputs[i]->x, inputs[i]->len ), |
| 5271 | expected_statuses[i] ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5272 | } |
| 5273 | } |
| 5274 | |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 5275 | if( output_key_type != PSA_KEY_TYPE_NONE ) |
| 5276 | { |
| 5277 | psa_reset_key_attributes( &attributes ); |
| 5278 | psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA ); |
| 5279 | psa_set_key_bits( &attributes, 8 ); |
| 5280 | actual_output_status = |
| 5281 | psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5282 | &output_key ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 5283 | } |
| 5284 | else |
| 5285 | { |
| 5286 | uint8_t buffer[1]; |
| 5287 | actual_output_status = |
| 5288 | psa_key_derivation_output_bytes( &operation, |
| 5289 | buffer, sizeof( buffer ) ); |
| 5290 | } |
| 5291 | TEST_EQUAL( actual_output_status, expected_output_status ); |
| 5292 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5293 | exit: |
| 5294 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5295 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 5296 | psa_destroy_key( keys[i] ); |
| 5297 | psa_destroy_key( output_key ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 5298 | PSA_DONE( ); |
| 5299 | } |
| 5300 | /* END_CASE */ |
| 5301 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 5302 | /* BEGIN_CASE */ |
| 5303 | void test_derive_invalid_key_derivation_state( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 5304 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 5305 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5306 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 5307 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5308 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 5309 | unsigned char input1[] = "Input 1"; |
| 5310 | size_t input1_length = sizeof( input1 ); |
| 5311 | unsigned char input2[] = "Input 2"; |
| 5312 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5313 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 5314 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 5315 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 5316 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 5317 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5318 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 5319 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5320 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5321 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5322 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5323 | psa_set_key_algorithm( &attributes, alg ); |
| 5324 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5325 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 5326 | PSA_ASSERT( psa_import_key( &attributes, |
| 5327 | key_data, sizeof( key_data ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5328 | &key ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5329 | |
| 5330 | /* valid key derivation */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5331 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 5332 | input1, input1_length, |
| 5333 | input2, input2_length, |
| 5334 | capacity ) ) |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 5335 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5336 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5337 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 5338 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 5339 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5340 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5341 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5342 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5343 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5344 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5345 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5346 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5347 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5348 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5349 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5350 | } |
| 5351 | /* END_CASE */ |
| 5352 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5353 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 5354 | void test_derive_invalid_key_derivation_tests( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5355 | { |
| 5356 | uint8_t output_buffer[16]; |
| 5357 | size_t buffer_size = 16; |
| 5358 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5359 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5360 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5361 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 5362 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5363 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5364 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5365 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5366 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5367 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5368 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5369 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5370 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 5371 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5372 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5373 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5374 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 5375 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 5376 | |
| 5377 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5378 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 5379 | } |
| 5380 | /* END_CASE */ |
| 5381 | |
| 5382 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5383 | void derive_output( int alg_arg, |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5384 | int step1_arg, data_t *input1, |
| 5385 | int step2_arg, data_t *input2, |
| 5386 | int step3_arg, data_t *input3, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5387 | int requested_capacity_arg, |
| 5388 | data_t *expected_output1, |
| 5389 | data_t *expected_output2 ) |
| 5390 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5391 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5392 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg}; |
| 5393 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5394 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 5395 | MBEDTLS_SVC_KEY_ID_INIT, |
| 5396 | MBEDTLS_SVC_KEY_ID_INIT }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5397 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5398 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5399 | uint8_t *expected_outputs[2] = |
| 5400 | {expected_output1->x, expected_output2->x}; |
| 5401 | size_t output_sizes[2] = |
| 5402 | {expected_output1->len, expected_output2->len}; |
| 5403 | size_t output_buffer_size = 0; |
| 5404 | uint8_t *output_buffer = NULL; |
| 5405 | size_t expected_capacity; |
| 5406 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5407 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5408 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5409 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5410 | |
| 5411 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 5412 | { |
| 5413 | if( output_sizes[i] > output_buffer_size ) |
| 5414 | output_buffer_size = output_sizes[i]; |
| 5415 | if( output_sizes[i] == 0 ) |
| 5416 | expected_outputs[i] = NULL; |
| 5417 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5418 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5419 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5420 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5421 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5422 | psa_set_key_algorithm( &attributes, alg ); |
| 5423 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5424 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5425 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5426 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 5427 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 5428 | requested_capacity ) ); |
| 5429 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5430 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5431 | switch( steps[i] ) |
| 5432 | { |
| 5433 | case 0: |
| 5434 | break; |
| 5435 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5436 | PSA_ASSERT( psa_import_key( &attributes, |
| 5437 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5438 | &keys[i] ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5439 | |
| 5440 | if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 5441 | { |
| 5442 | PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) ); |
| 5443 | TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <= |
| 5444 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ); |
| 5445 | } |
| 5446 | |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5447 | PSA_ASSERT( psa_key_derivation_input_key( |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5448 | &operation, steps[i], keys[i] ) ); |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5449 | break; |
| 5450 | default: |
| 5451 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 5452 | &operation, steps[i], |
| 5453 | inputs[i]->x, inputs[i]->len ) ); |
| 5454 | break; |
| 5455 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5456 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5457 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5458 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5459 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5460 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5461 | expected_capacity = requested_capacity; |
| 5462 | |
| 5463 | /* Expansion phase. */ |
| 5464 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 5465 | { |
| 5466 | /* Read some bytes. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5467 | status = psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5468 | output_buffer, output_sizes[i] ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5469 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 5470 | { |
| 5471 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 5472 | TEST_ASSERT( status == PSA_SUCCESS || |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5473 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5474 | continue; |
| 5475 | } |
| 5476 | else if( expected_capacity == 0 || |
| 5477 | output_sizes[i] > expected_capacity ) |
| 5478 | { |
| 5479 | /* Capacity exceeded. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5480 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5481 | expected_capacity = 0; |
| 5482 | continue; |
| 5483 | } |
| 5484 | /* Success. Check the read data. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5485 | PSA_ASSERT( status ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5486 | if( output_sizes[i] != 0 ) |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5487 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 5488 | expected_outputs[i], output_sizes[i] ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5489 | /* Check the operation status. */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5490 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5491 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5492 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5493 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5494 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5495 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5496 | |
| 5497 | exit: |
| 5498 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5499 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5500 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 5501 | psa_destroy_key( keys[i] ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5502 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5503 | } |
| 5504 | /* END_CASE */ |
| 5505 | |
| 5506 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5507 | void derive_full( int alg_arg, |
| 5508 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 5509 | data_t *input1, |
| 5510 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5511 | int requested_capacity_arg ) |
| 5512 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5513 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5514 | psa_algorithm_t alg = alg_arg; |
| 5515 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5516 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5517 | unsigned char output_buffer[16]; |
| 5518 | size_t expected_capacity = requested_capacity; |
| 5519 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5520 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5521 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5522 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5523 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5524 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5525 | psa_set_key_algorithm( &attributes, alg ); |
| 5526 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5527 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5528 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5529 | &key ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5530 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5531 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 5532 | input1->x, input1->len, |
| 5533 | input2->x, input2->len, |
| 5534 | requested_capacity ) ) |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 5535 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 5536 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5537 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5538 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5539 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5540 | |
| 5541 | /* Expansion phase. */ |
| 5542 | while( current_capacity > 0 ) |
| 5543 | { |
| 5544 | size_t read_size = sizeof( output_buffer ); |
| 5545 | if( read_size > current_capacity ) |
| 5546 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5547 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5548 | output_buffer, |
| 5549 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5550 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5551 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5552 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5553 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5554 | } |
| 5555 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5556 | /* Check that the operation refuses to go over capacity. */ |
| 5557 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5558 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5559 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5560 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5561 | |
| 5562 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5563 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5564 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5565 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5566 | } |
| 5567 | /* END_CASE */ |
| 5568 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 5569 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5570 | void derive_key_exercise( int alg_arg, |
| 5571 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 5572 | data_t *input1, |
| 5573 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5574 | int derived_type_arg, |
| 5575 | int derived_bits_arg, |
| 5576 | int derived_usage_arg, |
| 5577 | int derived_alg_arg ) |
| 5578 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5579 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5580 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5581 | psa_algorithm_t alg = alg_arg; |
| 5582 | psa_key_type_t derived_type = derived_type_arg; |
| 5583 | size_t derived_bits = derived_bits_arg; |
| 5584 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 5585 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 5586 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5587 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5588 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5589 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5590 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5591 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5592 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5593 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5594 | psa_set_key_algorithm( &attributes, alg ); |
| 5595 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5596 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5597 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5598 | |
| 5599 | /* Derive a key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5600 | if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 5601 | input1->x, input1->len, |
| 5602 | input2->x, input2->len, |
| 5603 | capacity ) ) |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 5604 | goto exit; |
| 5605 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5606 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 5607 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 5608 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5609 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5610 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5611 | &derived_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5612 | |
| 5613 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5614 | PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5615 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 5616 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5617 | |
| 5618 | /* Exercise the derived key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5619 | if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5620 | goto exit; |
| 5621 | |
| 5622 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5623 | /* |
| 5624 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5625 | * thus reset them as required. |
| 5626 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5627 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5628 | |
| 5629 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5630 | psa_destroy_key( base_key ); |
| 5631 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5632 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5633 | } |
| 5634 | /* END_CASE */ |
| 5635 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 5636 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5637 | void derive_key_export( int alg_arg, |
| 5638 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 5639 | data_t *input1, |
| 5640 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5641 | int bytes1_arg, |
| 5642 | int bytes2_arg ) |
| 5643 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5644 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5645 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5646 | psa_algorithm_t alg = alg_arg; |
| 5647 | size_t bytes1 = bytes1_arg; |
| 5648 | size_t bytes2 = bytes2_arg; |
| 5649 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5650 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5651 | uint8_t *output_buffer = NULL; |
| 5652 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5653 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5654 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5655 | size_t length; |
| 5656 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5657 | ASSERT_ALLOC( output_buffer, capacity ); |
| 5658 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5659 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5660 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5661 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 5662 | psa_set_key_algorithm( &base_attributes, alg ); |
| 5663 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5664 | 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] | 5665 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5666 | |
| 5667 | /* Derive some material and output it. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5668 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 5669 | input1->x, input1->len, |
| 5670 | input2->x, input2->len, |
| 5671 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 5672 | goto exit; |
| 5673 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5674 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5675 | output_buffer, |
| 5676 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5677 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5678 | |
| 5679 | /* 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] | 5680 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 5681 | input1->x, input1->len, |
| 5682 | input2->x, input2->len, |
| 5683 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 5684 | goto exit; |
| 5685 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5686 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 5687 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 5688 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5689 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5690 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5691 | &derived_key ) ); |
| 5692 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5693 | export_buffer, bytes1, |
| 5694 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5695 | TEST_EQUAL( length, bytes1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5696 | PSA_ASSERT( psa_destroy_key( derived_key ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5697 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5698 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5699 | &derived_key ) ); |
| 5700 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5701 | export_buffer + bytes1, bytes2, |
| 5702 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5703 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5704 | |
| 5705 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5706 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 5707 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5708 | |
| 5709 | exit: |
| 5710 | mbedtls_free( output_buffer ); |
| 5711 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5712 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5713 | psa_destroy_key( base_key ); |
| 5714 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5715 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5716 | } |
| 5717 | /* END_CASE */ |
| 5718 | |
| 5719 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 5720 | void derive_key( int alg_arg, |
| 5721 | data_t *key_data, data_t *input1, data_t *input2, |
| 5722 | int type_arg, int bits_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 5723 | int expected_status_arg, |
| 5724 | int is_large_output ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5725 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5726 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5727 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5728 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 5729 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5730 | size_t bits = bits_arg; |
| 5731 | psa_status_t expected_status = expected_status_arg; |
| 5732 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5733 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5734 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5735 | |
| 5736 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5737 | |
| 5738 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 5739 | psa_set_key_algorithm( &base_attributes, alg ); |
| 5740 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 5741 | 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] | 5742 | &base_key ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5743 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 5744 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 5745 | input1->x, input1->len, |
| 5746 | input2->x, input2->len, |
| 5747 | SIZE_MAX ) ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5748 | goto exit; |
| 5749 | |
| 5750 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 5751 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 5752 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5753 | psa_set_key_bits( &derived_attributes, bits ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 5754 | |
| 5755 | psa_status_t status = |
| 5756 | psa_key_derivation_output_key( &derived_attributes, |
| 5757 | &operation, |
| 5758 | &derived_key ); |
| 5759 | if( is_large_output > 0 ) |
| 5760 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 5761 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5762 | |
| 5763 | exit: |
| 5764 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5765 | psa_destroy_key( base_key ); |
| 5766 | psa_destroy_key( derived_key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5767 | PSA_DONE( ); |
| 5768 | } |
| 5769 | /* END_CASE */ |
| 5770 | |
| 5771 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5772 | void key_agreement_setup( int alg_arg, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 5773 | int our_key_type_arg, int our_key_alg_arg, |
| 5774 | data_t *our_key_data, data_t *peer_key_data, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5775 | int expected_status_arg ) |
| 5776 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5777 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5778 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5779 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5780 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5781 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5782 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5783 | psa_status_t expected_status = expected_status_arg; |
| 5784 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5785 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5786 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5787 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5788 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5789 | psa_set_key_algorithm( &attributes, our_key_alg ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5790 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5791 | PSA_ASSERT( psa_import_key( &attributes, |
| 5792 | our_key_data->x, our_key_data->len, |
| 5793 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5794 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5795 | /* The tests currently include inputs that should fail at either step. |
| 5796 | * Test cases that fail at the setup step should be changed to call |
| 5797 | * key_derivation_setup instead, and this function should be renamed |
| 5798 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5799 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5800 | if( status == PSA_SUCCESS ) |
| 5801 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5802 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 5803 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 5804 | our_key, |
| 5805 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5806 | expected_status ); |
| 5807 | } |
| 5808 | else |
| 5809 | { |
| 5810 | TEST_ASSERT( status == expected_status ); |
| 5811 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5812 | |
| 5813 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5814 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5815 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5816 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5817 | } |
| 5818 | /* END_CASE */ |
| 5819 | |
| 5820 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5821 | void raw_key_agreement( int alg_arg, |
| 5822 | int our_key_type_arg, data_t *our_key_data, |
| 5823 | data_t *peer_key_data, |
| 5824 | data_t *expected_output ) |
| 5825 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5826 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5827 | psa_algorithm_t alg = alg_arg; |
| 5828 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5829 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5830 | unsigned char *output = NULL; |
| 5831 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5832 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5833 | |
| 5834 | ASSERT_ALLOC( output, expected_output->len ); |
| 5835 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5836 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5837 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5838 | psa_set_key_algorithm( &attributes, alg ); |
| 5839 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5840 | PSA_ASSERT( psa_import_key( &attributes, |
| 5841 | our_key_data->x, our_key_data->len, |
| 5842 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5843 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5844 | PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) ); |
| 5845 | key_bits = psa_get_key_bits( &attributes ); |
| 5846 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5847 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 5848 | peer_key_data->x, peer_key_data->len, |
| 5849 | output, expected_output->len, |
| 5850 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5851 | ASSERT_COMPARE( output, output_length, |
| 5852 | expected_output->x, expected_output->len ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5853 | TEST_ASSERT( output_length <= |
| 5854 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) ); |
| 5855 | TEST_ASSERT( output_length <= |
| 5856 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5857 | |
| 5858 | exit: |
| 5859 | mbedtls_free( output ); |
| 5860 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5861 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5862 | } |
| 5863 | /* END_CASE */ |
| 5864 | |
| 5865 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5866 | void key_agreement_capacity( int alg_arg, |
| 5867 | int our_key_type_arg, data_t *our_key_data, |
| 5868 | data_t *peer_key_data, |
| 5869 | int expected_capacity_arg ) |
| 5870 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5871 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5872 | psa_algorithm_t alg = alg_arg; |
| 5873 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5874 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5875 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5876 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5877 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5878 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5879 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5880 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5881 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5882 | psa_set_key_algorithm( &attributes, alg ); |
| 5883 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5884 | PSA_ASSERT( psa_import_key( &attributes, |
| 5885 | our_key_data->x, our_key_data->len, |
| 5886 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5887 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5888 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5889 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 5890 | &operation, |
| 5891 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 5892 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5893 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 5894 | { |
| 5895 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5896 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5897 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5898 | NULL, 0 ) ); |
| 5899 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5900 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5901 | /* Test the advertized capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5902 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5903 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5904 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5905 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5906 | /* Test the actual capacity by reading the output. */ |
| 5907 | while( actual_capacity > sizeof( output ) ) |
| 5908 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5909 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5910 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5911 | actual_capacity -= sizeof( output ); |
| 5912 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5913 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5914 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5915 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5916 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5917 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5918 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5919 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5920 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5921 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5922 | } |
| 5923 | /* END_CASE */ |
| 5924 | |
| 5925 | /* BEGIN_CASE */ |
| 5926 | void key_agreement_output( int alg_arg, |
| 5927 | int our_key_type_arg, data_t *our_key_data, |
| 5928 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5929 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5930 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5931 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5932 | psa_algorithm_t alg = alg_arg; |
| 5933 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5934 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5935 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5936 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5937 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5938 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 5939 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5940 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5941 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5942 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5943 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5944 | psa_set_key_algorithm( &attributes, alg ); |
| 5945 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5946 | PSA_ASSERT( psa_import_key( &attributes, |
| 5947 | our_key_data->x, our_key_data->len, |
| 5948 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5949 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5950 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5951 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 5952 | &operation, |
| 5953 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 5954 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5955 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 5956 | { |
| 5957 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5958 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5959 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5960 | NULL, 0 ) ); |
| 5961 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5962 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5963 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5964 | actual_output, |
| 5965 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5966 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 5967 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5968 | if( expected_output2->len != 0 ) |
| 5969 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5970 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5971 | actual_output, |
| 5972 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5973 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 5974 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5975 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5976 | |
| 5977 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5978 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5979 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5980 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5981 | mbedtls_free( actual_output ); |
| 5982 | } |
| 5983 | /* END_CASE */ |
| 5984 | |
| 5985 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5986 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5987 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5988 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5989 | unsigned char *output = NULL; |
| 5990 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5991 | size_t i; |
| 5992 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5993 | |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 5994 | TEST_ASSERT( bytes_arg >= 0 ); |
| 5995 | |
Gilles Peskine | 9189202 | 2021-02-08 19:50:26 +0100 | [diff] [blame] | 5996 | ASSERT_ALLOC( output, bytes ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5997 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5998 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5999 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6000 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6001 | /* Run several times, to ensure that every output byte will be |
| 6002 | * nonzero at least once with overwhelming probability |
| 6003 | * (2^(-8*number_of_runs)). */ |
| 6004 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6005 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 6006 | if( bytes != 0 ) |
| 6007 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6008 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6009 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6010 | for( i = 0; i < bytes; i++ ) |
| 6011 | { |
| 6012 | if( output[i] != 0 ) |
| 6013 | ++changed[i]; |
| 6014 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6015 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6016 | |
| 6017 | /* Check that every byte was changed to nonzero at least once. This |
| 6018 | * validates that psa_generate_random is overwriting every byte of |
| 6019 | * the output buffer. */ |
| 6020 | for( i = 0; i < bytes; i++ ) |
| 6021 | { |
| 6022 | TEST_ASSERT( changed[i] != 0 ); |
| 6023 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6024 | |
| 6025 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6026 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6027 | mbedtls_free( output ); |
| 6028 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6029 | } |
| 6030 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6031 | |
| 6032 | /* BEGIN_CASE */ |
| 6033 | void generate_key( int type_arg, |
| 6034 | int bits_arg, |
| 6035 | int usage_arg, |
| 6036 | int alg_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 6037 | int expected_status_arg, |
| 6038 | int is_large_key ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6039 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6040 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6041 | psa_key_type_t type = type_arg; |
| 6042 | psa_key_usage_t usage = usage_arg; |
| 6043 | size_t bits = bits_arg; |
| 6044 | psa_algorithm_t alg = alg_arg; |
| 6045 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6046 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6047 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6048 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6049 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6050 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6051 | psa_set_key_usage_flags( &attributes, usage ); |
| 6052 | psa_set_key_algorithm( &attributes, alg ); |
| 6053 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 6054 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6055 | |
| 6056 | /* Generate a key */ |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 6057 | psa_status_t status = psa_generate_key( &attributes, &key ); |
| 6058 | |
| 6059 | if( is_large_key > 0 ) |
| 6060 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 6061 | TEST_EQUAL( status , expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6062 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6063 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6064 | |
| 6065 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6066 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6067 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 6068 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6069 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 6070 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 6071 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 6072 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6073 | |
| 6074 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6075 | /* |
| 6076 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6077 | * thus reset them as required. |
| 6078 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6079 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6080 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6081 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6082 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6083 | } |
| 6084 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 6085 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 6086 | /* 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] | 6087 | void generate_key_rsa( int bits_arg, |
| 6088 | data_t *e_arg, |
| 6089 | int expected_status_arg ) |
| 6090 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6091 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6092 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6093 | size_t bits = bits_arg; |
| 6094 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 6095 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 6096 | psa_status_t expected_status = expected_status_arg; |
| 6097 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6098 | uint8_t *exported = NULL; |
| 6099 | size_t exported_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 6100 | PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6101 | size_t exported_length = SIZE_MAX; |
| 6102 | uint8_t *e_read_buffer = NULL; |
| 6103 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 6104 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6105 | size_t e_read_length = SIZE_MAX; |
| 6106 | |
| 6107 | if( e_arg->len == 0 || |
| 6108 | ( e_arg->len == 3 && |
| 6109 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 6110 | { |
| 6111 | is_default_public_exponent = 1; |
| 6112 | e_read_size = 0; |
| 6113 | } |
| 6114 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 6115 | ASSERT_ALLOC( exported, exported_size ); |
| 6116 | |
| 6117 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6118 | |
| 6119 | psa_set_key_usage_flags( &attributes, usage ); |
| 6120 | psa_set_key_algorithm( &attributes, alg ); |
| 6121 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 6122 | e_arg->x, e_arg->len ) ); |
| 6123 | psa_set_key_bits( &attributes, bits ); |
| 6124 | |
| 6125 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6126 | TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6127 | if( expected_status != PSA_SUCCESS ) |
| 6128 | goto exit; |
| 6129 | |
| 6130 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6131 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6132 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 6133 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 6134 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 6135 | e_read_buffer, e_read_size, |
| 6136 | &e_read_length ) ); |
| 6137 | if( is_default_public_exponent ) |
| 6138 | TEST_EQUAL( e_read_length, 0 ); |
| 6139 | else |
| 6140 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 6141 | |
| 6142 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 6143 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6144 | goto exit; |
| 6145 | |
| 6146 | /* Export the key and check the public exponent. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6147 | PSA_ASSERT( psa_export_public_key( key, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6148 | exported, exported_size, |
| 6149 | &exported_length ) ); |
| 6150 | { |
| 6151 | uint8_t *p = exported; |
| 6152 | uint8_t *end = exported + exported_length; |
| 6153 | size_t len; |
| 6154 | /* RSAPublicKey ::= SEQUENCE { |
| 6155 | * modulus INTEGER, -- n |
| 6156 | * publicExponent INTEGER } -- e |
| 6157 | */ |
| 6158 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6159 | MBEDTLS_ASN1_SEQUENCE | |
| 6160 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 6161 | TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6162 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 6163 | MBEDTLS_ASN1_INTEGER ) ); |
| 6164 | if( len >= 1 && p[0] == 0 ) |
| 6165 | { |
| 6166 | ++p; |
| 6167 | --len; |
| 6168 | } |
| 6169 | if( e_arg->len == 0 ) |
| 6170 | { |
| 6171 | TEST_EQUAL( len, 3 ); |
| 6172 | TEST_EQUAL( p[0], 1 ); |
| 6173 | TEST_EQUAL( p[1], 0 ); |
| 6174 | TEST_EQUAL( p[2], 1 ); |
| 6175 | } |
| 6176 | else |
| 6177 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 6178 | } |
| 6179 | |
| 6180 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6181 | /* |
| 6182 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 6183 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 6184 | */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6185 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6186 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6187 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6188 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6189 | mbedtls_free( e_read_buffer ); |
| 6190 | mbedtls_free( exported ); |
| 6191 | } |
| 6192 | /* END_CASE */ |
| 6193 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6194 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6195 | void persistent_key_load_key_from_storage( data_t *data, |
| 6196 | int type_arg, int bits_arg, |
| 6197 | int usage_flags_arg, int alg_arg, |
| 6198 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6199 | { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 6200 | 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] | 6201 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6202 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6203 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6204 | psa_key_type_t type = type_arg; |
| 6205 | size_t bits = bits_arg; |
| 6206 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 6207 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6208 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6209 | unsigned char *first_export = NULL; |
| 6210 | unsigned char *second_export = NULL; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 6211 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6212 | size_t first_exported_length; |
| 6213 | size_t second_exported_length; |
| 6214 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6215 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 6216 | { |
| 6217 | ASSERT_ALLOC( first_export, export_size ); |
| 6218 | ASSERT_ALLOC( second_export, export_size ); |
| 6219 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6220 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6221 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6222 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 6223 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6224 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 6225 | psa_set_key_algorithm( &attributes, alg ); |
| 6226 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 6227 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6228 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6229 | switch( generation_method ) |
| 6230 | { |
| 6231 | case IMPORT_KEY: |
| 6232 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6233 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6234 | &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6235 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6236 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6237 | case GENERATE_KEY: |
| 6238 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6239 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6240 | break; |
| 6241 | |
| 6242 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 6243 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6244 | { |
| 6245 | /* Create base key */ |
| 6246 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 6247 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6248 | psa_set_key_usage_flags( &base_attributes, |
| 6249 | PSA_KEY_USAGE_DERIVE ); |
| 6250 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 6251 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6252 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 6253 | data->x, data->len, |
| 6254 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6255 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6256 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6257 | PSA_ASSERT( psa_key_derivation_input_key( |
| 6258 | &operation, |
| 6259 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6260 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6261 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6262 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6263 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 6264 | &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6265 | &key ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6266 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6267 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6268 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6269 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 6270 | #else |
| 6271 | TEST_ASSUME( ! "KDF not supported in this configuration" ); |
| 6272 | #endif |
| 6273 | break; |
| 6274 | |
| 6275 | default: |
| 6276 | TEST_ASSERT( ! "generation_method not implemented in test" ); |
| 6277 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6278 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6279 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6280 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6281 | /* Export the key if permitted by the key policy. */ |
| 6282 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 6283 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6284 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6285 | first_export, export_size, |
| 6286 | &first_exported_length ) ); |
| 6287 | if( generation_method == IMPORT_KEY ) |
| 6288 | ASSERT_COMPARE( data->x, data->len, |
| 6289 | first_export, first_exported_length ); |
| 6290 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6291 | |
| 6292 | /* Shutdown and restart */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6293 | PSA_ASSERT( psa_purge_key( key ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6294 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6295 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6296 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6297 | /* Check key slot still contains key data */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6298 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 6299 | TEST_ASSERT( mbedtls_svc_key_id_equal( |
| 6300 | psa_get_key_id( &attributes ), key_id ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6301 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 6302 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 6303 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 6304 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 6305 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags ); |
| 6306 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6307 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6308 | /* Export the key again if permitted by the key policy. */ |
| 6309 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6310 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6311 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6312 | second_export, export_size, |
| 6313 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6314 | ASSERT_COMPARE( first_export, first_exported_length, |
| 6315 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6316 | } |
| 6317 | |
| 6318 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 6319 | if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 6320 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6321 | |
| 6322 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6323 | /* |
| 6324 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6325 | * thus reset them as required. |
| 6326 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6327 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6328 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6329 | mbedtls_free( first_export ); |
| 6330 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6331 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 6332 | psa_destroy_key( base_key ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6333 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6334 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 6335 | } |
| 6336 | /* END_CASE */ |