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 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 15 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 16 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 17 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 18 | /* A hash algorithm that is known to be supported. |
| 19 | * |
| 20 | * This is used in some smoke tests. |
| 21 | */ |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 22 | #if defined(PSA_WANT_ALG_MD2) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 23 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2 |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 24 | #elif defined(PSA_WANT_ALG_MD4) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 25 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4 |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 26 | #elif defined(PSA_WANT_ALG_MD5) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 27 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5 |
| 28 | /* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of |
| 29 | * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160 |
| 30 | * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be |
| 31 | * implausible anyway. */ |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 32 | #elif defined(PSA_WANT_ALG_SHA_1) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 33 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1 |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 34 | #elif defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 35 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256 |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 36 | #elif defined(PSA_WANT_ALG_SHA_384) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 37 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384 |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 38 | #elif defined(PSA_WANT_ALG_SHA_512) |
| 39 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_512 |
| 40 | #elif defined(PSA_WANT_ALG_SHA3_256) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 41 | #define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256 |
| 42 | #else |
| 43 | #undef KNOWN_SUPPORTED_HASH_ALG |
| 44 | #endif |
| 45 | |
| 46 | /* A block cipher that is known to be supported. |
| 47 | * |
| 48 | * For simplicity's sake, stick to block ciphers with 16-byte blocks. |
| 49 | */ |
| 50 | #if defined(MBEDTLS_AES_C) |
| 51 | #define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES |
| 52 | #elif defined(MBEDTLS_ARIA_C) |
| 53 | #define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA |
| 54 | #elif defined(MBEDTLS_CAMELLIA_C) |
| 55 | #define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA |
| 56 | #undef KNOWN_SUPPORTED_BLOCK_CIPHER |
| 57 | #endif |
| 58 | |
| 59 | /* A MAC mode that is known to be supported. |
| 60 | * |
| 61 | * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or |
| 62 | * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER. |
| 63 | * |
| 64 | * This is used in some smoke tests. |
| 65 | */ |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 66 | #if defined(KNOWN_SUPPORTED_HASH_ALG) && defined(PSA_WANT_ALG_HMAC) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 67 | #define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) ) |
| 68 | #define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC |
| 69 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C) |
| 70 | #define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC |
| 71 | #define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER |
| 72 | #else |
| 73 | #undef KNOWN_SUPPORTED_MAC_ALG |
| 74 | #undef KNOWN_SUPPORTED_MAC_KEY_TYPE |
| 75 | #endif |
| 76 | |
| 77 | /* A cipher algorithm and key type that are known to be supported. |
| 78 | * |
| 79 | * This is used in some smoke tests. |
| 80 | */ |
| 81 | #if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR) |
| 82 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR |
| 83 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC) |
| 84 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING |
| 85 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB) |
| 86 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB |
| 87 | #elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB) |
| 88 | #define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB |
| 89 | #else |
| 90 | #undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG |
| 91 | #endif |
| 92 | #if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG) |
| 93 | #define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG |
| 94 | #define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER |
| 95 | #elif defined(MBEDTLS_RC4_C) |
| 96 | #define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4 |
| 97 | #define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4 |
| 98 | #else |
| 99 | #undef KNOWN_SUPPORTED_CIPHER_ALG |
| 100 | #undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE |
| 101 | #endif |
| 102 | |
Gilles Peskine | 667c111 | 2019-12-03 19:03:20 +0100 | [diff] [blame] | 103 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Ronald Cron | 9e12f8f | 2020-11-13 09:46:44 +0100 | [diff] [blame] | 104 | int lifetime_is_dynamic_secure_element( psa_key_lifetime_t lifetime ) |
Gilles Peskine | 667c111 | 2019-12-03 19:03:20 +0100 | [diff] [blame] | 105 | { |
Ronald Cron | 9e12f8f | 2020-11-13 09:46:44 +0100 | [diff] [blame] | 106 | return( PSA_KEY_LIFETIME_GET_LOCATION( lifetime ) != |
| 107 | PSA_KEY_LOCATION_LOCAL_STORAGE ); |
Gilles Peskine | 667c111 | 2019-12-03 19:03:20 +0100 | [diff] [blame] | 108 | } |
| 109 | #else |
| 110 | int lifetime_is_secure_element( psa_key_lifetime_t lifetime ) |
| 111 | { |
| 112 | (void) lifetime; |
| 113 | return( 0 ); |
| 114 | } |
| 115 | #endif |
| 116 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 117 | /** Test if a buffer contains a constant byte value. |
| 118 | * |
| 119 | * `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] | 120 | * |
| 121 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 122 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 123 | * \param size Size of the buffer in bytes. |
| 124 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 125 | * \return 1 if the buffer is all-bits-zero. |
| 126 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 127 | */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 128 | 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] | 129 | { |
| 130 | size_t i; |
| 131 | for( i = 0; i < size; i++ ) |
| 132 | { |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 133 | if( ( (unsigned char *) buffer )[i] != c ) |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 134 | return( 0 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 135 | } |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 136 | return( 1 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 137 | } |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 138 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 139 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
| 140 | static int asn1_write_10x( unsigned char **p, |
| 141 | unsigned char *start, |
| 142 | size_t bits, |
| 143 | unsigned char x ) |
| 144 | { |
| 145 | int ret; |
| 146 | int len = bits / 8 + 1; |
Gilles Peskine | 480416a | 2018-06-28 19:04:07 +0200 | [diff] [blame] | 147 | if( bits == 0 ) |
| 148 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
| 149 | if( bits <= 8 && x >= 1 << ( bits - 1 ) ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 150 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 151 | if( *p < start || *p - start < (ptrdiff_t) len ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 152 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 153 | *p -= len; |
| 154 | ( *p )[len-1] = x; |
| 155 | if( bits % 8 == 0 ) |
| 156 | ( *p )[1] |= 1; |
| 157 | else |
| 158 | ( *p )[0] |= 1 << ( bits % 8 ); |
| 159 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 160 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
| 161 | MBEDTLS_ASN1_INTEGER ) ); |
| 162 | return( len ); |
| 163 | } |
| 164 | |
| 165 | static int construct_fake_rsa_key( unsigned char *buffer, |
| 166 | size_t buffer_size, |
| 167 | unsigned char **p, |
| 168 | size_t bits, |
| 169 | int keypair ) |
| 170 | { |
| 171 | size_t half_bits = ( bits + 1 ) / 2; |
| 172 | int ret; |
| 173 | int len = 0; |
| 174 | /* Construct something that looks like a DER encoding of |
| 175 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 176 | * RSAPrivateKey ::= SEQUENCE { |
| 177 | * version Version, |
| 178 | * modulus INTEGER, -- n |
| 179 | * publicExponent INTEGER, -- e |
| 180 | * privateExponent INTEGER, -- d |
| 181 | * prime1 INTEGER, -- p |
| 182 | * prime2 INTEGER, -- q |
| 183 | * exponent1 INTEGER, -- d mod (p-1) |
| 184 | * exponent2 INTEGER, -- d mod (q-1) |
| 185 | * coefficient INTEGER, -- (inverse of q) mod p |
| 186 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 187 | * } |
| 188 | * Or, for a public key, the same structure with only |
| 189 | * version, modulus and publicExponent. |
| 190 | */ |
| 191 | *p = buffer + buffer_size; |
| 192 | if( keypair ) |
| 193 | { |
| 194 | MBEDTLS_ASN1_CHK_ADD( len, /* pq */ |
| 195 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 196 | MBEDTLS_ASN1_CHK_ADD( len, /* dq */ |
| 197 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 198 | MBEDTLS_ASN1_CHK_ADD( len, /* dp */ |
| 199 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 200 | MBEDTLS_ASN1_CHK_ADD( len, /* q */ |
| 201 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 202 | MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */ |
| 203 | asn1_write_10x( p, buffer, half_bits, 3 ) ); |
| 204 | MBEDTLS_ASN1_CHK_ADD( len, /* d */ |
| 205 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 206 | } |
| 207 | MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */ |
| 208 | asn1_write_10x( p, buffer, 17, 1 ) ); |
| 209 | MBEDTLS_ASN1_CHK_ADD( len, /* n */ |
| 210 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 211 | if( keypair ) |
| 212 | MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */ |
| 213 | mbedtls_asn1_write_int( p, buffer, 0 ) ); |
| 214 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) ); |
| 215 | { |
| 216 | const unsigned char tag = |
| 217 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
| 218 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) ); |
| 219 | } |
| 220 | return( len ); |
| 221 | } |
| 222 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 223 | int check_key_attributes_sanity( mbedtls_svc_key_id_t key ) |
Gilles Peskine | 667c111 | 2019-12-03 19:03:20 +0100 | [diff] [blame] | 224 | { |
| 225 | int ok = 0; |
| 226 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 227 | psa_key_lifetime_t lifetime; |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 228 | mbedtls_svc_key_id_t id; |
Gilles Peskine | 667c111 | 2019-12-03 19:03:20 +0100 | [diff] [blame] | 229 | psa_key_type_t type; |
| 230 | psa_key_type_t bits; |
| 231 | |
| 232 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 233 | lifetime = psa_get_key_lifetime( &attributes ); |
| 234 | id = psa_get_key_id( &attributes ); |
| 235 | type = psa_get_key_type( &attributes ); |
| 236 | bits = psa_get_key_bits( &attributes ); |
| 237 | |
| 238 | /* Persistence */ |
Ronald Cron | f1ff9a8 | 2020-10-19 08:44:19 +0200 | [diff] [blame] | 239 | if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) ) |
Ronald Cron | 4184107 | 2020-09-17 15:28:26 +0200 | [diff] [blame] | 240 | { |
| 241 | TEST_ASSERT( |
| 242 | ( PSA_KEY_ID_VOLATILE_MIN <= |
| 243 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) && |
| 244 | ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <= |
| 245 | PSA_KEY_ID_VOLATILE_MAX ) ); |
| 246 | } |
Gilles Peskine | 667c111 | 2019-12-03 19:03:20 +0100 | [diff] [blame] | 247 | else |
| 248 | { |
| 249 | TEST_ASSERT( |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 250 | ( PSA_KEY_ID_USER_MIN <= MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) && |
| 251 | ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <= PSA_KEY_ID_USER_MAX ) ); |
Gilles Peskine | 667c111 | 2019-12-03 19:03:20 +0100 | [diff] [blame] | 252 | } |
| 253 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 254 | /* randomly-generated 64-bit constant, should never appear in test data */ |
| 255 | psa_key_slot_number_t slot_number = 0xec94d4a5058a1a21; |
| 256 | psa_status_t status = psa_get_key_slot_number( &attributes, &slot_number ); |
Ronald Cron | 9e12f8f | 2020-11-13 09:46:44 +0100 | [diff] [blame] | 257 | if( lifetime_is_dynamic_secure_element( lifetime ) ) |
Gilles Peskine | 667c111 | 2019-12-03 19:03:20 +0100 | [diff] [blame] | 258 | { |
| 259 | /* Mbed Crypto currently always exposes the slot number to |
| 260 | * applications. This is not mandated by the PSA specification |
| 261 | * and may change in future versions. */ |
| 262 | TEST_EQUAL( status, 0 ); |
| 263 | TEST_ASSERT( slot_number != 0xec94d4a5058a1a21 ); |
| 264 | } |
| 265 | else |
| 266 | { |
| 267 | TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT ); |
| 268 | } |
| 269 | #endif |
| 270 | |
| 271 | /* Type and size */ |
| 272 | TEST_ASSERT( type != 0 ); |
| 273 | TEST_ASSERT( bits != 0 ); |
| 274 | TEST_ASSERT( bits <= PSA_MAX_KEY_BITS ); |
| 275 | if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ) |
| 276 | TEST_ASSERT( bits % 8 == 0 ); |
| 277 | |
| 278 | /* MAX macros concerning specific key types */ |
| 279 | if( PSA_KEY_TYPE_IS_ECC( type ) ) |
| 280 | TEST_ASSERT( bits <= PSA_VENDOR_ECC_MAX_CURVE_BITS ); |
| 281 | else if( PSA_KEY_TYPE_IS_RSA( type ) ) |
| 282 | TEST_ASSERT( bits <= PSA_VENDOR_RSA_MAX_KEY_BITS ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 283 | TEST_ASSERT( PSA_BLOCK_CIPHER_BLOCK_LENGTH( type ) <= PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE ); |
Gilles Peskine | 667c111 | 2019-12-03 19:03:20 +0100 | [diff] [blame] | 284 | |
| 285 | ok = 1; |
| 286 | |
| 287 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 288 | /* |
| 289 | * Key attributes may have been returned by psa_get_key_attributes() |
| 290 | * thus reset them as required. |
| 291 | */ |
Gilles Peskine | 667c111 | 2019-12-03 19:03:20 +0100 | [diff] [blame] | 292 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 293 | |
Gilles Peskine | 667c111 | 2019-12-03 19:03:20 +0100 | [diff] [blame] | 294 | return( ok ); |
| 295 | } |
| 296 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 297 | int exercise_mac_setup( psa_key_type_t key_type, |
| 298 | const unsigned char *key_bytes, |
| 299 | size_t key_length, |
| 300 | psa_algorithm_t alg, |
| 301 | psa_mac_operation_t *operation, |
| 302 | psa_status_t *status ) |
| 303 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 304 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 305 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 306 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 307 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 308 | psa_set_key_algorithm( &attributes, alg ); |
| 309 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 310 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 311 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 312 | *status = psa_mac_sign_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 313 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 314 | PSA_ASSERT( psa_mac_abort( operation ) ); |
| 315 | /* If setup failed, reproduce the failure, so that the caller can |
| 316 | * test the resulting state of the operation object. */ |
| 317 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 318 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 319 | TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 320 | } |
| 321 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 322 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 323 | return( 1 ); |
| 324 | |
| 325 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 326 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 327 | return( 0 ); |
| 328 | } |
| 329 | |
| 330 | int exercise_cipher_setup( psa_key_type_t key_type, |
| 331 | const unsigned char *key_bytes, |
| 332 | size_t key_length, |
| 333 | psa_algorithm_t alg, |
| 334 | psa_cipher_operation_t *operation, |
| 335 | psa_status_t *status ) |
| 336 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 337 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 338 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 339 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 340 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 341 | psa_set_key_algorithm( &attributes, alg ); |
| 342 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 343 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 344 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 345 | *status = psa_cipher_encrypt_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 346 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 347 | PSA_ASSERT( psa_cipher_abort( operation ) ); |
| 348 | /* If setup failed, reproduce the failure, so that the caller can |
| 349 | * test the resulting state of the operation object. */ |
| 350 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 351 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 352 | TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ), |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 353 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 354 | } |
| 355 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 356 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 357 | return( 1 ); |
| 358 | |
| 359 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 360 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 361 | return( 0 ); |
| 362 | } |
| 363 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 364 | static int exercise_mac_key( mbedtls_svc_key_id_t key, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 365 | psa_key_usage_t usage, |
| 366 | psa_algorithm_t alg ) |
| 367 | { |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 368 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 369 | const unsigned char input[] = "foo"; |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 370 | unsigned char mac[PSA_MAC_MAX_SIZE] = {0}; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 371 | size_t mac_length = sizeof( mac ); |
| 372 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 373 | if( usage & PSA_KEY_USAGE_SIGN_HASH ) |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 374 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 375 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 376 | PSA_ASSERT( psa_mac_update( &operation, |
| 377 | input, sizeof( input ) ) ); |
| 378 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 379 | mac, sizeof( mac ), |
| 380 | &mac_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 381 | } |
| 382 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 383 | if( usage & PSA_KEY_USAGE_VERIFY_HASH ) |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 384 | { |
| 385 | psa_status_t verify_status = |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 386 | ( usage & PSA_KEY_USAGE_SIGN_HASH ? |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 387 | PSA_SUCCESS : |
| 388 | PSA_ERROR_INVALID_SIGNATURE ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 389 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 390 | PSA_ASSERT( psa_mac_update( &operation, |
| 391 | input, sizeof( input ) ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 392 | TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ), |
| 393 | verify_status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | return( 1 ); |
| 397 | |
| 398 | exit: |
| 399 | psa_mac_abort( &operation ); |
| 400 | return( 0 ); |
| 401 | } |
| 402 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 403 | static int exercise_cipher_key( mbedtls_svc_key_id_t key, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 404 | psa_key_usage_t usage, |
| 405 | psa_algorithm_t alg ) |
| 406 | { |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 407 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 408 | unsigned char iv[16] = {0}; |
| 409 | size_t iv_length = sizeof( iv ); |
| 410 | const unsigned char plaintext[16] = "Hello, world..."; |
| 411 | unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)"; |
| 412 | size_t ciphertext_length = sizeof( ciphertext ); |
| 413 | unsigned char decrypted[sizeof( ciphertext )]; |
| 414 | size_t part_length; |
| 415 | |
| 416 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 417 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 418 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 419 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 420 | iv, sizeof( iv ), |
| 421 | &iv_length ) ); |
| 422 | PSA_ASSERT( psa_cipher_update( &operation, |
| 423 | plaintext, sizeof( plaintext ), |
| 424 | ciphertext, sizeof( ciphertext ), |
| 425 | &ciphertext_length ) ); |
| 426 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 427 | ciphertext + ciphertext_length, |
| 428 | sizeof( ciphertext ) - ciphertext_length, |
| 429 | &part_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 430 | ciphertext_length += part_length; |
| 431 | } |
| 432 | |
| 433 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 434 | { |
| 435 | psa_status_t status; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 436 | int maybe_invalid_padding = 0; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 437 | if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) ) |
| 438 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 439 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 440 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 441 | /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't |
| 442 | * have this macro yet. */ |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 443 | iv_length = PSA_BLOCK_CIPHER_BLOCK_LENGTH( |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 444 | psa_get_key_type( &attributes ) ); |
| 445 | maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 446 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 447 | } |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 448 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 449 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 450 | iv, iv_length ) ); |
| 451 | PSA_ASSERT( psa_cipher_update( &operation, |
| 452 | ciphertext, ciphertext_length, |
| 453 | decrypted, sizeof( decrypted ), |
| 454 | &part_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 455 | status = psa_cipher_finish( &operation, |
| 456 | decrypted + part_length, |
| 457 | sizeof( decrypted ) - part_length, |
| 458 | &part_length ); |
| 459 | /* For a stream cipher, all inputs are valid. For a block cipher, |
| 460 | * if the input is some aribtrary data rather than an actual |
| 461 | ciphertext, a padding error is likely. */ |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 462 | if( maybe_invalid_padding ) |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 463 | TEST_ASSERT( status == PSA_SUCCESS || |
| 464 | status == PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 465 | else |
| 466 | PSA_ASSERT( status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | return( 1 ); |
| 470 | |
| 471 | exit: |
| 472 | psa_cipher_abort( &operation ); |
| 473 | return( 0 ); |
| 474 | } |
| 475 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 476 | static int exercise_aead_key( mbedtls_svc_key_id_t key, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 477 | psa_key_usage_t usage, |
| 478 | psa_algorithm_t alg ) |
| 479 | { |
| 480 | unsigned char nonce[16] = {0}; |
| 481 | size_t nonce_length = sizeof( nonce ); |
| 482 | unsigned char plaintext[16] = "Hello, world..."; |
| 483 | unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)"; |
| 484 | size_t ciphertext_length = sizeof( ciphertext ); |
| 485 | size_t plaintext_length = sizeof( ciphertext ); |
| 486 | |
| 487 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 488 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 489 | PSA_ASSERT( psa_aead_encrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 490 | nonce, nonce_length, |
| 491 | NULL, 0, |
| 492 | plaintext, sizeof( plaintext ), |
| 493 | ciphertext, sizeof( ciphertext ), |
| 494 | &ciphertext_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 498 | { |
| 499 | psa_status_t verify_status = |
| 500 | ( usage & PSA_KEY_USAGE_ENCRYPT ? |
| 501 | PSA_SUCCESS : |
| 502 | PSA_ERROR_INVALID_SIGNATURE ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 503 | TEST_EQUAL( psa_aead_decrypt( key, alg, |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 504 | nonce, nonce_length, |
| 505 | NULL, 0, |
| 506 | ciphertext, ciphertext_length, |
| 507 | plaintext, sizeof( plaintext ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 508 | &plaintext_length ), |
| 509 | verify_status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | return( 1 ); |
| 513 | |
| 514 | exit: |
| 515 | return( 0 ); |
| 516 | } |
| 517 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 518 | static int exercise_signature_key( mbedtls_svc_key_id_t key, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 519 | psa_key_usage_t usage, |
| 520 | psa_algorithm_t alg ) |
| 521 | { |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 522 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 523 | size_t payload_length = 16; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 524 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 525 | size_t signature_length = sizeof( signature ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 526 | psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg ); |
| 527 | |
| 528 | /* If the policy allows signing with any hash, just pick one. */ |
| 529 | if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH ) |
| 530 | { |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 531 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 532 | hash_alg = KNOWN_SUPPORTED_HASH_ALG; |
| 533 | alg ^= PSA_ALG_ANY_HASH ^ hash_alg; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 534 | #else |
| 535 | test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 536 | return( 1 ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 537 | #endif |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 538 | } |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 539 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 540 | if( usage & PSA_KEY_USAGE_SIGN_HASH ) |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 541 | { |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 542 | /* Some algorithms require the payload to have the size of |
| 543 | * the hash encoded in the algorithm. Use this input size |
| 544 | * even for algorithms that allow other input sizes. */ |
Gilles Peskine | f969b3a | 2018-06-30 00:20:25 +0200 | [diff] [blame] | 545 | if( hash_alg != 0 ) |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 546 | payload_length = PSA_HASH_LENGTH( hash_alg ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 547 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 548 | payload, payload_length, |
| 549 | signature, sizeof( signature ), |
| 550 | &signature_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 551 | } |
| 552 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 553 | if( usage & PSA_KEY_USAGE_VERIFY_HASH ) |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 554 | { |
| 555 | psa_status_t verify_status = |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 556 | ( usage & PSA_KEY_USAGE_SIGN_HASH ? |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 557 | PSA_SUCCESS : |
| 558 | PSA_ERROR_INVALID_SIGNATURE ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 559 | TEST_EQUAL( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 560 | payload, payload_length, |
| 561 | signature, signature_length ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 562 | verify_status ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | return( 1 ); |
| 566 | |
| 567 | exit: |
| 568 | return( 0 ); |
| 569 | } |
| 570 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 571 | static int exercise_asymmetric_encryption_key( mbedtls_svc_key_id_t key, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 572 | psa_key_usage_t usage, |
| 573 | psa_algorithm_t alg ) |
| 574 | { |
| 575 | unsigned char plaintext[256] = "Hello, world..."; |
| 576 | unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)"; |
| 577 | size_t ciphertext_length = sizeof( ciphertext ); |
| 578 | size_t plaintext_length = 16; |
| 579 | |
| 580 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 581 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 582 | PSA_ASSERT( psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 583 | plaintext, plaintext_length, |
| 584 | NULL, 0, |
| 585 | ciphertext, sizeof( ciphertext ), |
| 586 | &ciphertext_length ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 590 | { |
| 591 | psa_status_t status = |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 592 | psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 593 | ciphertext, ciphertext_length, |
| 594 | NULL, 0, |
| 595 | plaintext, sizeof( plaintext ), |
| 596 | &plaintext_length ); |
| 597 | TEST_ASSERT( status == PSA_SUCCESS || |
| 598 | ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 && |
| 599 | ( status == PSA_ERROR_INVALID_ARGUMENT || |
| 600 | status == PSA_ERROR_INVALID_PADDING ) ) ); |
| 601 | } |
| 602 | |
| 603 | return( 1 ); |
| 604 | |
| 605 | exit: |
| 606 | return( 0 ); |
| 607 | } |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 608 | |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 609 | static int setup_key_derivation_wrap( psa_key_derivation_operation_t* operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 610 | mbedtls_svc_key_id_t key, |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 611 | psa_algorithm_t alg, |
| 612 | unsigned char* input1, size_t input1_length, |
| 613 | unsigned char* input2, size_t input2_length, |
| 614 | size_t capacity ) |
| 615 | { |
| 616 | PSA_ASSERT( psa_key_derivation_setup( operation, alg ) ); |
| 617 | if( PSA_ALG_IS_HKDF( alg ) ) |
| 618 | { |
| 619 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 620 | PSA_KEY_DERIVATION_INPUT_SALT, |
| 621 | input1, input1_length ) ); |
| 622 | PSA_ASSERT( psa_key_derivation_input_key( operation, |
| 623 | PSA_KEY_DERIVATION_INPUT_SECRET, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 624 | key ) ); |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 625 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 626 | PSA_KEY_DERIVATION_INPUT_INFO, |
| 627 | input2, |
| 628 | input2_length ) ); |
| 629 | } |
| 630 | else if( PSA_ALG_IS_TLS12_PRF( alg ) || |
| 631 | PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 632 | { |
| 633 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 634 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 635 | input1, input1_length ) ); |
| 636 | PSA_ASSERT( psa_key_derivation_input_key( operation, |
| 637 | PSA_KEY_DERIVATION_INPUT_SECRET, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 638 | key ) ); |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 639 | PSA_ASSERT( psa_key_derivation_input_bytes( operation, |
| 640 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 641 | input2, input2_length ) ); |
| 642 | } |
| 643 | else |
| 644 | { |
| 645 | TEST_ASSERT( ! "Key derivation algorithm not supported" ); |
| 646 | } |
| 647 | |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 648 | if( capacity != SIZE_MAX ) |
| 649 | PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) ); |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 650 | |
| 651 | return( 1 ); |
| 652 | |
| 653 | exit: |
| 654 | return( 0 ); |
| 655 | } |
| 656 | |
| 657 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 658 | static int exercise_key_derivation_key( mbedtls_svc_key_id_t key, |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 659 | psa_key_usage_t usage, |
| 660 | psa_algorithm_t alg ) |
| 661 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 662 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 663 | unsigned char input1[] = "Input 1"; |
| 664 | size_t input1_length = sizeof( input1 ); |
| 665 | unsigned char input2[] = "Input 2"; |
| 666 | size_t input2_length = sizeof( input2 ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 667 | unsigned char output[1]; |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 668 | size_t capacity = sizeof( output ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 669 | |
| 670 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 671 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 672 | if( !setup_key_derivation_wrap( &operation, key, alg, |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 673 | input1, input1_length, |
| 674 | input2, input2_length, capacity ) ) |
| 675 | goto exit; |
Gilles Peskine | 7607cd6 | 2019-05-29 17:35:00 +0200 | [diff] [blame] | 676 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 677 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 678 | output, |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 679 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 680 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | return( 1 ); |
| 684 | |
| 685 | exit: |
| 686 | return( 0 ); |
| 687 | } |
| 688 | |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 689 | /* We need two keys to exercise key agreement. Exercise the |
| 690 | * private key against its own public key. */ |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 691 | static psa_status_t key_agreement_with_self( |
| 692 | psa_key_derivation_operation_t *operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 693 | mbedtls_svc_key_id_t key ) |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 694 | { |
| 695 | psa_key_type_t private_key_type; |
| 696 | psa_key_type_t public_key_type; |
| 697 | size_t key_bits; |
| 698 | uint8_t *public_key = NULL; |
| 699 | size_t public_key_length; |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 700 | /* Return GENERIC_ERROR if something other than the final call to |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 701 | * psa_key_derivation_key_agreement fails. This isn't fully satisfactory, |
| 702 | * but it's good enough: callers will report it as a failed test anyway. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 703 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 704 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 705 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 706 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 707 | private_key_type = psa_get_key_type( &attributes ); |
| 708 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 709 | public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 710 | public_key_length = PSA_EXPORT_KEY_OUTPUT_SIZE( public_key_type, key_bits ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 711 | ASSERT_ALLOC( public_key, public_key_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 712 | PSA_ASSERT( psa_export_public_key( key, public_key, public_key_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 713 | &public_key_length ) ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 714 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 715 | status = psa_key_derivation_key_agreement( |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 716 | operation, PSA_KEY_DERIVATION_INPUT_SECRET, key, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 717 | public_key, public_key_length ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 718 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 719 | /* |
| 720 | * Key attributes may have been returned by psa_get_key_attributes() |
| 721 | * thus reset them as required. |
| 722 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 723 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 724 | |
| 725 | mbedtls_free( public_key ); |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 726 | return( status ); |
| 727 | } |
| 728 | |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 729 | /* We need two keys to exercise key agreement. Exercise the |
| 730 | * private key against its own public key. */ |
| 731 | static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 732 | mbedtls_svc_key_id_t key ) |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 733 | { |
| 734 | psa_key_type_t private_key_type; |
| 735 | psa_key_type_t public_key_type; |
| 736 | size_t key_bits; |
| 737 | uint8_t *public_key = NULL; |
| 738 | size_t public_key_length; |
| 739 | uint8_t output[1024]; |
| 740 | size_t output_length; |
| 741 | /* Return GENERIC_ERROR if something other than the final call to |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 742 | * psa_key_derivation_key_agreement fails. This isn't fully satisfactory, |
| 743 | * but it's good enough: callers will report it as a failed test anyway. */ |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 744 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 745 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 746 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 747 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 748 | private_key_type = psa_get_key_type( &attributes ); |
| 749 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 750 | public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 751 | public_key_length = PSA_EXPORT_KEY_OUTPUT_SIZE( public_key_type, key_bits ); |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 752 | ASSERT_ALLOC( public_key, public_key_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 753 | PSA_ASSERT( psa_export_public_key( key, |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 754 | public_key, public_key_length, |
| 755 | &public_key_length ) ); |
| 756 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 757 | status = psa_raw_key_agreement( alg, key, |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 758 | public_key, public_key_length, |
| 759 | output, sizeof( output ), &output_length ); |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 760 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 761 | /* |
| 762 | * Key attributes may have been returned by psa_get_key_attributes() |
| 763 | * thus reset them as required. |
| 764 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 765 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 766 | |
| 767 | mbedtls_free( public_key ); |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 768 | return( status ); |
| 769 | } |
| 770 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 771 | static int exercise_raw_key_agreement_key( mbedtls_svc_key_id_t key, |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 772 | psa_key_usage_t usage, |
| 773 | psa_algorithm_t alg ) |
| 774 | { |
| 775 | int ok = 0; |
| 776 | |
| 777 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 778 | { |
| 779 | /* We need two keys to exercise key agreement. Exercise the |
| 780 | * private key against its own public key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 781 | PSA_ASSERT( raw_key_agreement_with_self( alg, key ) ); |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 782 | } |
| 783 | ok = 1; |
| 784 | |
| 785 | exit: |
| 786 | return( ok ); |
| 787 | } |
| 788 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 789 | static int exercise_key_agreement_key( mbedtls_svc_key_id_t key, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 790 | psa_key_usage_t usage, |
| 791 | psa_algorithm_t alg ) |
| 792 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 793 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 794 | unsigned char output[1]; |
| 795 | int ok = 0; |
| 796 | |
| 797 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 798 | { |
| 799 | /* We need two keys to exercise key agreement. Exercise the |
| 800 | * private key against its own public key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 801 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 802 | PSA_ASSERT( key_agreement_with_self( &operation, key ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 803 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 804 | output, |
| 805 | sizeof( output ) ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 806 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 807 | } |
| 808 | ok = 1; |
| 809 | |
| 810 | exit: |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 811 | return( ok ); |
| 812 | } |
| 813 | |
Jaeden Amero | f7dca86 | 2019-06-27 17:31:33 +0100 | [diff] [blame] | 814 | int asn1_skip_integer( unsigned char **p, const unsigned char *end, |
| 815 | size_t min_bits, size_t max_bits, |
| 816 | int must_be_odd ) |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 817 | { |
| 818 | size_t len; |
| 819 | size_t actual_bits; |
| 820 | unsigned char msb; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 821 | TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 822 | MBEDTLS_ASN1_INTEGER ), |
| 823 | 0 ); |
k-stachowiak | 9b88efc | 2019-09-13 15:26:53 +0200 | [diff] [blame] | 824 | |
| 825 | /* Check if the retrieved length doesn't extend the actual buffer's size. |
| 826 | * It is assumed here, that end >= p, which validates casting to size_t. */ |
| 827 | TEST_ASSERT( len <= (size_t)( end - *p) ); |
| 828 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 829 | /* Tolerate a slight departure from DER encoding: |
| 830 | * - 0 may be represented by an empty string or a 1-byte string. |
| 831 | * - The sign bit may be used as a value bit. */ |
| 832 | if( ( len == 1 && ( *p )[0] == 0 ) || |
| 833 | ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) ) |
| 834 | { |
| 835 | ++( *p ); |
| 836 | --len; |
| 837 | } |
| 838 | if( min_bits == 0 && len == 0 ) |
| 839 | return( 1 ); |
| 840 | msb = ( *p )[0]; |
| 841 | TEST_ASSERT( msb != 0 ); |
| 842 | actual_bits = 8 * ( len - 1 ); |
| 843 | while( msb != 0 ) |
| 844 | { |
| 845 | msb >>= 1; |
| 846 | ++actual_bits; |
| 847 | } |
| 848 | TEST_ASSERT( actual_bits >= min_bits ); |
| 849 | TEST_ASSERT( actual_bits <= max_bits ); |
| 850 | if( must_be_odd ) |
| 851 | TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 ); |
| 852 | *p += len; |
| 853 | return( 1 ); |
| 854 | exit: |
| 855 | return( 0 ); |
| 856 | } |
| 857 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 858 | static int exported_key_sanity_check( psa_key_type_t type, size_t bits, |
| 859 | uint8_t *exported, size_t exported_length ) |
| 860 | { |
| 861 | if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 862 | TEST_EQUAL( exported_length, ( bits + 7 ) / 8 ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 863 | else |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 864 | TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ) ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 865 | |
| 866 | #if defined(MBEDTLS_DES_C) |
| 867 | if( type == PSA_KEY_TYPE_DES ) |
| 868 | { |
| 869 | /* Check the parity bits. */ |
| 870 | unsigned i; |
| 871 | for( i = 0; i < bits / 8; i++ ) |
| 872 | { |
| 873 | unsigned bit_count = 0; |
| 874 | unsigned m; |
| 875 | for( m = 1; m <= 0x100; m <<= 1 ) |
| 876 | { |
| 877 | if( exported[i] & m ) |
| 878 | ++bit_count; |
| 879 | } |
| 880 | TEST_ASSERT( bit_count % 2 != 0 ); |
| 881 | } |
| 882 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 883 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 884 | #endif |
| 885 | |
| 886 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 887 | if( type == PSA_KEY_TYPE_RSA_KEY_PAIR ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 888 | { |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 889 | uint8_t *p = exported; |
| 890 | uint8_t *end = exported + exported_length; |
| 891 | size_t len; |
| 892 | /* RSAPrivateKey ::= SEQUENCE { |
Gilles Peskine | dea46cf | 2018-08-21 16:12:54 +0200 | [diff] [blame] | 893 | * version INTEGER, -- must be 0 |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 894 | * modulus INTEGER, -- n |
| 895 | * publicExponent INTEGER, -- e |
| 896 | * privateExponent INTEGER, -- d |
| 897 | * prime1 INTEGER, -- p |
| 898 | * prime2 INTEGER, -- q |
| 899 | * exponent1 INTEGER, -- d mod (p-1) |
| 900 | * exponent2 INTEGER, -- d mod (q-1) |
| 901 | * coefficient INTEGER, -- (inverse of q) mod p |
| 902 | * } |
| 903 | */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 904 | TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len, |
| 905 | MBEDTLS_ASN1_SEQUENCE | |
| 906 | MBEDTLS_ASN1_CONSTRUCTED ), 0 ); |
| 907 | TEST_EQUAL( p + len, end ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 908 | if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) ) |
| 909 | goto exit; |
| 910 | if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) ) |
| 911 | goto exit; |
| 912 | if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) ) |
| 913 | goto exit; |
| 914 | /* Require d to be at least half the size of n. */ |
| 915 | if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) ) |
| 916 | goto exit; |
| 917 | /* Require p and q to be at most half the size of n, rounded up. */ |
| 918 | if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) ) |
| 919 | goto exit; |
| 920 | if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) ) |
| 921 | goto exit; |
| 922 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 923 | goto exit; |
| 924 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 925 | goto exit; |
| 926 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 927 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 928 | TEST_EQUAL( p, end ); |
Gilles Peskine | 0f915f1 | 2018-12-17 23:35:42 +0100 | [diff] [blame] | 929 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 930 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 931 | #endif /* MBEDTLS_RSA_C */ |
| 932 | |
| 933 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 934 | if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 935 | { |
Gilles Peskine | 5b802a3 | 2018-10-29 19:21:41 +0100 | [diff] [blame] | 936 | /* Just the secret value */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 937 | TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) ); |
Gilles Peskine | 5b802a3 | 2018-10-29 19:21:41 +0100 | [diff] [blame] | 938 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 939 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 940 | #endif /* MBEDTLS_ECP_C */ |
| 941 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 942 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
| 943 | { |
| 944 | uint8_t *p = exported; |
| 945 | uint8_t *end = exported + exported_length; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 946 | #if defined(MBEDTLS_RSA_C) |
| 947 | if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ) |
| 948 | { |
Jaeden Amero | f7dca86 | 2019-06-27 17:31:33 +0100 | [diff] [blame] | 949 | size_t len; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 950 | /* RSAPublicKey ::= SEQUENCE { |
| 951 | * modulus INTEGER, -- n |
| 952 | * publicExponent INTEGER } -- e |
| 953 | */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 954 | TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len, |
| 955 | MBEDTLS_ASN1_SEQUENCE | |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 956 | MBEDTLS_ASN1_CONSTRUCTED ), |
| 957 | 0 ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 958 | TEST_EQUAL( p + len, end ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 959 | if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) ) |
| 960 | goto exit; |
| 961 | if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) ) |
| 962 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 963 | TEST_EQUAL( p, end ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 964 | } |
| 965 | else |
| 966 | #endif /* MBEDTLS_RSA_C */ |
| 967 | #if defined(MBEDTLS_ECP_C) |
| 968 | if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) ) |
| 969 | { |
Steven Cooreman | 3fa684e | 2020-07-30 15:04:07 +0200 | [diff] [blame] | 970 | if( PSA_KEY_TYPE_ECC_GET_FAMILY( type ) == PSA_ECC_FAMILY_MONTGOMERY ) |
| 971 | { |
| 972 | /* The representation of an ECC Montgomery public key is |
| 973 | * the raw compressed point */ |
| 974 | TEST_EQUAL( p + PSA_BITS_TO_BYTES( bits ), end ); |
| 975 | } |
| 976 | else |
| 977 | { |
| 978 | /* The representation of an ECC Weierstrass public key is: |
| 979 | * - The byte 0x04; |
| 980 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 981 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 982 | * - where m is the bit size associated with the curve. |
| 983 | */ |
| 984 | TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end ); |
| 985 | TEST_EQUAL( p[0], 4 ); |
| 986 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 987 | } |
| 988 | else |
| 989 | #endif /* MBEDTLS_ECP_C */ |
| 990 | { |
Jaeden Amero | 594a330 | 2018-10-26 17:07:22 +0100 | [diff] [blame] | 991 | char message[47]; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 992 | mbedtls_snprintf( message, sizeof( message ), |
| 993 | "No sanity check for public key type=0x%08lx", |
| 994 | (unsigned long) type ); |
| 995 | test_fail( message, __LINE__, __FILE__ ); |
Gilles Peskine | b16841e | 2019-10-10 20:36:12 +0200 | [diff] [blame] | 996 | (void) p; |
| 997 | (void) end; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 998 | return( 0 ); |
| 999 | } |
| 1000 | } |
| 1001 | else |
| 1002 | |
| 1003 | { |
| 1004 | /* No sanity checks for other types */ |
| 1005 | } |
| 1006 | |
| 1007 | return( 1 ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1008 | |
| 1009 | exit: |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 1010 | return( 0 ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1011 | } |
| 1012 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1013 | static int exercise_export_key( mbedtls_svc_key_id_t key, |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1014 | psa_key_usage_t usage ) |
| 1015 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1016 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1017 | uint8_t *exported = NULL; |
| 1018 | size_t exported_size = 0; |
| 1019 | size_t exported_length = 0; |
| 1020 | int ok = 0; |
| 1021 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1022 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | acec7b6f | 2018-09-13 20:34:11 +0200 | [diff] [blame] | 1023 | |
Ronald Cron | 1e87d5b | 2021-01-18 13:32:28 +0100 | [diff] [blame] | 1024 | exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
| 1025 | psa_get_key_type( &attributes ), |
| 1026 | psa_get_key_bits( &attributes ) ); |
| 1027 | ASSERT_ALLOC( exported, exported_size ); |
| 1028 | |
Gilles Peskine | acec7b6f | 2018-09-13 20:34:11 +0200 | [diff] [blame] | 1029 | if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 && |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1030 | ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1031 | { |
Ronald Cron | 1e87d5b | 2021-01-18 13:32:28 +0100 | [diff] [blame] | 1032 | TEST_EQUAL( psa_export_key( key, exported, |
| 1033 | exported_size, &exported_length ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1034 | PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1035 | ok = 1; |
| 1036 | goto exit; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1037 | } |
| 1038 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1039 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1040 | exported, exported_size, |
| 1041 | &exported_length ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1042 | ok = exported_key_sanity_check( psa_get_key_type( &attributes ), |
| 1043 | psa_get_key_bits( &attributes ), |
| 1044 | exported, exported_length ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1045 | |
| 1046 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1047 | /* |
| 1048 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1049 | * thus reset them as required. |
| 1050 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1051 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1052 | |
| 1053 | mbedtls_free( exported ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1054 | return( ok ); |
| 1055 | } |
| 1056 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1057 | static int exercise_export_public_key( mbedtls_svc_key_id_t key ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1058 | { |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1059 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1060 | psa_key_type_t public_type; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1061 | uint8_t *exported = NULL; |
| 1062 | size_t exported_size = 0; |
| 1063 | size_t exported_length = 0; |
| 1064 | int ok = 0; |
| 1065 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1066 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1067 | if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1068 | { |
Ronald Cron | 1e87d5b | 2021-01-18 13:32:28 +0100 | [diff] [blame] | 1069 | exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE( |
| 1070 | psa_get_key_type( &attributes ), |
| 1071 | psa_get_key_bits( &attributes ) ); |
| 1072 | ASSERT_ALLOC( exported, exported_size ); |
| 1073 | |
| 1074 | TEST_EQUAL( psa_export_public_key( key, exported, |
| 1075 | exported_size, &exported_length ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1076 | PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | 1e87d5b | 2021-01-18 13:32:28 +0100 | [diff] [blame] | 1077 | ok = 1; |
| 1078 | goto exit; |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1079 | } |
| 1080 | |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1081 | public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1082 | psa_get_key_type( &attributes ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1083 | exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, |
| 1084 | psa_get_key_bits( &attributes ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1085 | ASSERT_ALLOC( exported, exported_size ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1086 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1087 | PSA_ASSERT( psa_export_public_key( key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1088 | exported, exported_size, |
| 1089 | &exported_length ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1090 | ok = exported_key_sanity_check( public_type, |
| 1091 | psa_get_key_bits( &attributes ), |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1092 | exported, exported_length ); |
| 1093 | |
| 1094 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1095 | /* |
| 1096 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1097 | * thus reset them as required. |
| 1098 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1099 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1100 | |
| 1101 | mbedtls_free( exported ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1102 | return( ok ); |
| 1103 | } |
| 1104 | |
Gilles Peskine | c9516fb | 2019-02-05 20:32:06 +0100 | [diff] [blame] | 1105 | /** Do smoke tests on a key. |
| 1106 | * |
| 1107 | * Perform one of each operation indicated by \p alg (decrypt/encrypt, |
| 1108 | * sign/verify, or derivation) that is permitted according to \p usage. |
| 1109 | * \p usage and \p alg should correspond to the expected policy on the |
| 1110 | * key. |
| 1111 | * |
| 1112 | * Export the key if permitted by \p usage, and check that the output |
| 1113 | * looks sensible. If \p usage forbids export, check that |
| 1114 | * \p psa_export_key correctly rejects the attempt. If the key is |
| 1115 | * asymmetric, also check \p psa_export_public_key. |
| 1116 | * |
| 1117 | * If the key fails the tests, this function calls the test framework's |
| 1118 | * `test_fail` function and returns false. Otherwise this function returns |
| 1119 | * true. Therefore it should be used as follows: |
| 1120 | * ``` |
| 1121 | * if( ! exercise_key( ... ) ) goto exit; |
| 1122 | * ``` |
| 1123 | * |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1124 | * \param key The key to exercise. It should be capable of performing |
Gilles Peskine | c9516fb | 2019-02-05 20:32:06 +0100 | [diff] [blame] | 1125 | * \p alg. |
| 1126 | * \param usage The usage flags to assume. |
| 1127 | * \param alg The algorithm to exercise. |
| 1128 | * |
| 1129 | * \retval 0 The key failed the smoke tests. |
| 1130 | * \retval 1 The key passed the smoke tests. |
| 1131 | */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1132 | static int exercise_key( mbedtls_svc_key_id_t key, |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1133 | psa_key_usage_t usage, |
| 1134 | psa_algorithm_t alg ) |
| 1135 | { |
| 1136 | int ok; |
Gilles Peskine | 667c111 | 2019-12-03 19:03:20 +0100 | [diff] [blame] | 1137 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1138 | if( ! check_key_attributes_sanity( key ) ) |
Gilles Peskine | 667c111 | 2019-12-03 19:03:20 +0100 | [diff] [blame] | 1139 | return( 0 ); |
| 1140 | |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1141 | if( alg == 0 ) |
| 1142 | ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */ |
| 1143 | else if( PSA_ALG_IS_MAC( alg ) ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1144 | ok = exercise_mac_key( key, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1145 | else if( PSA_ALG_IS_CIPHER( alg ) ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1146 | ok = exercise_cipher_key( key, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1147 | else if( PSA_ALG_IS_AEAD( alg ) ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1148 | ok = exercise_aead_key( key, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1149 | else if( PSA_ALG_IS_SIGN( alg ) ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1150 | ok = exercise_signature_key( key, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1151 | else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1152 | ok = exercise_asymmetric_encryption_key( key, usage, alg ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1153 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1154 | ok = exercise_key_derivation_key( key, usage, alg ); |
Gilles Peskine | 2e46e9c | 2019-04-11 21:24:55 +0200 | [diff] [blame] | 1155 | else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1156 | ok = exercise_raw_key_agreement_key( key, usage, alg ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1157 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1158 | ok = exercise_key_agreement_key( key, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1159 | else |
| 1160 | { |
| 1161 | char message[40]; |
| 1162 | mbedtls_snprintf( message, sizeof( message ), |
| 1163 | "No code to exercise alg=0x%08lx", |
| 1164 | (unsigned long) alg ); |
| 1165 | test_fail( message, __LINE__, __FILE__ ); |
| 1166 | ok = 0; |
| 1167 | } |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1168 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1169 | ok = ok && exercise_export_key( key, usage ); |
| 1170 | ok = ok && exercise_export_public_key( key ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 1171 | |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1172 | return( ok ); |
| 1173 | } |
| 1174 | |
Gilles Peskine | 10df341 | 2018-10-25 22:35:43 +0200 | [diff] [blame] | 1175 | static psa_key_usage_t usage_to_exercise( psa_key_type_t type, |
| 1176 | psa_algorithm_t alg ) |
| 1177 | { |
| 1178 | if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ) |
| 1179 | { |
| 1180 | return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ? |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1181 | PSA_KEY_USAGE_VERIFY_HASH : |
| 1182 | PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 10df341 | 2018-10-25 22:35:43 +0200 | [diff] [blame] | 1183 | } |
| 1184 | else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) || |
| 1185 | PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ) |
| 1186 | { |
| 1187 | return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ? |
| 1188 | PSA_KEY_USAGE_ENCRYPT : |
| 1189 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 1190 | } |
| 1191 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) || |
| 1192 | PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 1193 | { |
| 1194 | return( PSA_KEY_USAGE_DERIVE ); |
| 1195 | } |
| 1196 | else |
| 1197 | { |
| 1198 | return( 0 ); |
| 1199 | } |
| 1200 | |
| 1201 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1202 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1203 | 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] | 1204 | { |
| 1205 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 1206 | 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] | 1207 | uint8_t buffer[1]; |
| 1208 | size_t length; |
| 1209 | int ok = 0; |
| 1210 | |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 1211 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1212 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 1213 | psa_set_key_algorithm( &attributes, PSA_ALG_CTR ); |
| 1214 | psa_set_key_type( &attributes, PSA_KEY_TYPE_AES ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1215 | TEST_EQUAL( psa_get_key_attributes( key, &attributes ), |
Ronald Cron | 432e19c | 2020-09-17 14:12:30 +0200 | [diff] [blame] | 1216 | PSA_ERROR_DOES_NOT_EXIST ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 1217 | TEST_EQUAL( |
| 1218 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 ); |
| 1219 | TEST_EQUAL( |
| 1220 | 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] | 1221 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1222 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 1223 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 1224 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
| 1225 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
| 1226 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1227 | TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ), |
Ronald Cron | 432e19c | 2020-09-17 14:12:30 +0200 | [diff] [blame] | 1228 | PSA_ERROR_DOES_NOT_EXIST ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1229 | TEST_EQUAL( psa_export_public_key( key, |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1230 | buffer, sizeof( buffer ), &length ), |
Ronald Cron | 432e19c | 2020-09-17 14:12:30 +0200 | [diff] [blame] | 1231 | PSA_ERROR_DOES_NOT_EXIST ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1232 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1233 | ok = 1; |
| 1234 | |
| 1235 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1236 | /* |
| 1237 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1238 | * thus reset them as required. |
| 1239 | */ |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1240 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1241 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1242 | return( ok ); |
| 1243 | } |
| 1244 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1245 | /* Assert that a key isn't reported as having a slot number. */ |
| 1246 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1247 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 1248 | do \ |
| 1249 | { \ |
| 1250 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
| 1251 | TEST_EQUAL( psa_get_key_slot_number( \ |
| 1252 | attributes, \ |
| 1253 | &ASSERT_NO_SLOT_NUMBER_slot_number ), \ |
| 1254 | PSA_ERROR_INVALID_ARGUMENT ); \ |
| 1255 | } \ |
| 1256 | while( 0 ) |
| 1257 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1258 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 1259 | ( (void) 0 ) |
| 1260 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1261 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1262 | /* An overapproximation of the amount of storage needed for a key of the |
| 1263 | * given type and with the given content. The API doesn't make it easy |
| 1264 | * to find a good value for the size. The current implementation doesn't |
| 1265 | * care about the value anyway. */ |
| 1266 | #define KEY_BITS_FROM_DATA( type, data ) \ |
| 1267 | ( data )->len |
| 1268 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 1269 | typedef enum { |
| 1270 | IMPORT_KEY = 0, |
| 1271 | GENERATE_KEY = 1, |
| 1272 | DERIVE_KEY = 2 |
| 1273 | } generate_method; |
| 1274 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1275 | /* END_HEADER */ |
| 1276 | |
| 1277 | /* BEGIN_DEPENDENCIES |
| 1278 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1279 | * END_DEPENDENCIES |
| 1280 | */ |
| 1281 | |
| 1282 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1283 | void static_checks( ) |
| 1284 | { |
| 1285 | size_t max_truncated_mac_size = |
| 1286 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1287 | |
| 1288 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1289 | * encoding. The shifted mask is the maximum truncated value. The |
| 1290 | * untruncated algorithm may be one byte larger. */ |
| 1291 | TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 1292 | |
| 1293 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 1294 | /* Check deprecated constants. */ |
| 1295 | TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR ); |
| 1296 | TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS ); |
| 1297 | TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST ); |
| 1298 | TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA ); |
| 1299 | TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED ); |
| 1300 | TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH ); |
| 1301 | TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH ); |
| 1302 | TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 1303 | |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 1304 | TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 1305 | TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 1306 | TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 1307 | TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 1308 | TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 1309 | TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 1310 | TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 1311 | TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 1312 | TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 1313 | TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 1314 | TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 ); |
| 1315 | TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 1316 | TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 1317 | TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 1318 | TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 1319 | TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 1320 | TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 1321 | TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 1322 | TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 1323 | TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 1324 | TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 1325 | TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 1326 | TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 1327 | TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 ); |
| 1328 | TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 ); |
| 1329 | TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 ); |
| 1330 | TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 ); |
| 1331 | TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 ); |
| 1332 | TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY ); |
| 1333 | TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY ); |
| 1334 | |
| 1335 | TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 ); |
| 1336 | TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 ); |
| 1337 | TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 ); |
| 1338 | TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 ); |
| 1339 | TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 ); |
| 1340 | TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 ); |
| 1341 | TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 ); |
| 1342 | TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY ); |
Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 1343 | |
Paul Elliott | 75e2703 | 2020-06-03 15:17:39 +0100 | [diff] [blame] | 1344 | TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 ); |
| 1345 | TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 ); |
| 1346 | TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 ); |
| 1347 | TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 ); |
| 1348 | TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 ); |
| 1349 | |
| 1350 | TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 ); |
| 1351 | TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM ); |
Gilles Peskine | b87b719 | 2019-12-04 16:24:10 +0100 | [diff] [blame] | 1352 | #endif |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1353 | } |
| 1354 | /* END_CASE */ |
| 1355 | |
| 1356 | /* BEGIN_CASE */ |
Ronald Cron | 81e0050 | 2020-07-28 15:06:14 +0200 | [diff] [blame] | 1357 | void attributes_set_get( int owner_id_arg, int id_arg, int lifetime_arg, |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1358 | int usage_flags_arg, int alg_arg, |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1359 | int type_arg, int bits_arg ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1360 | { |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1361 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 81e0050 | 2020-07-28 15:06:14 +0200 | [diff] [blame] | 1362 | mbedtls_svc_key_id_t id = mbedtls_svc_key_id_make( owner_id_arg, id_arg ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1363 | psa_key_lifetime_t lifetime = lifetime_arg; |
| 1364 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 1365 | psa_algorithm_t alg = alg_arg; |
| 1366 | psa_key_type_t type = type_arg; |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1367 | size_t bits = bits_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1368 | |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 1369 | TEST_EQUAL( |
| 1370 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 ); |
| 1371 | TEST_EQUAL( |
| 1372 | MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1373 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
| 1374 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 1375 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 1376 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1377 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1378 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 1379 | psa_set_key_id( &attributes, id ); |
| 1380 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1381 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 1382 | psa_set_key_algorithm( &attributes, alg ); |
| 1383 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1384 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1385 | |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 1386 | TEST_ASSERT( mbedtls_svc_key_id_equal( |
| 1387 | psa_get_key_id( &attributes ), id ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1388 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime ); |
| 1389 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags ); |
| 1390 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
| 1391 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1392 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1393 | |
| 1394 | psa_reset_key_attributes( &attributes ); |
| 1395 | |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 1396 | TEST_EQUAL( |
| 1397 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 ); |
| 1398 | TEST_EQUAL( |
| 1399 | MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1400 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
| 1401 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 1402 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 1403 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 1404 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1405 | } |
| 1406 | /* END_CASE */ |
| 1407 | |
| 1408 | /* BEGIN_CASE */ |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 1409 | void persistence_attributes( int id1_arg, int owner_id1_arg, int lifetime_arg, |
| 1410 | int id2_arg, int owner_id2_arg, |
| 1411 | int expected_id_arg, int expected_owner_id_arg, |
| 1412 | int expected_lifetime_arg ) |
Gilles Peskine | dd835cb | 2019-05-15 16:14:57 +0200 | [diff] [blame] | 1413 | { |
| 1414 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 1415 | mbedtls_svc_key_id_t id1 = |
| 1416 | mbedtls_svc_key_id_make( owner_id1_arg, id1_arg ); |
Gilles Peskine | dd835cb | 2019-05-15 16:14:57 +0200 | [diff] [blame] | 1417 | psa_key_lifetime_t lifetime = lifetime_arg; |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 1418 | mbedtls_svc_key_id_t id2 = |
| 1419 | mbedtls_svc_key_id_make( owner_id2_arg, id2_arg ); |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 1420 | mbedtls_svc_key_id_t expected_id = |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 1421 | mbedtls_svc_key_id_make( expected_owner_id_arg, expected_id_arg ); |
Gilles Peskine | dd835cb | 2019-05-15 16:14:57 +0200 | [diff] [blame] | 1422 | psa_key_lifetime_t expected_lifetime = expected_lifetime_arg; |
| 1423 | |
| 1424 | if( id1_arg != -1 ) |
| 1425 | psa_set_key_id( &attributes, id1 ); |
| 1426 | if( lifetime_arg != -1 ) |
| 1427 | psa_set_key_lifetime( &attributes, lifetime ); |
| 1428 | if( id2_arg != -1 ) |
| 1429 | psa_set_key_id( &attributes, id2 ); |
| 1430 | |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 1431 | TEST_ASSERT( mbedtls_svc_key_id_equal( |
| 1432 | psa_get_key_id( &attributes ), expected_id ) ); |
Gilles Peskine | dd835cb | 2019-05-15 16:14:57 +0200 | [diff] [blame] | 1433 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime ); |
| 1434 | } |
| 1435 | /* END_CASE */ |
| 1436 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1437 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1438 | void slot_number_attribute( ) |
| 1439 | { |
| 1440 | psa_key_slot_number_t slot_number = 0xdeadbeef; |
| 1441 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1442 | |
| 1443 | /* Initially, there is no slot number. */ |
| 1444 | TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ), |
| 1445 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1446 | |
| 1447 | /* Test setting a slot number. */ |
| 1448 | psa_set_key_slot_number( &attributes, 0 ); |
| 1449 | PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) ); |
| 1450 | TEST_EQUAL( slot_number, 0 ); |
| 1451 | |
| 1452 | /* Test changing the slot number. */ |
| 1453 | psa_set_key_slot_number( &attributes, 42 ); |
| 1454 | PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) ); |
| 1455 | TEST_EQUAL( slot_number, 42 ); |
| 1456 | |
| 1457 | /* Test clearing the slot number. */ |
| 1458 | psa_clear_key_slot_number( &attributes ); |
| 1459 | TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ), |
| 1460 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1461 | |
| 1462 | /* Clearing again should have no effect. */ |
| 1463 | psa_clear_key_slot_number( &attributes ); |
| 1464 | TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ), |
| 1465 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1466 | |
| 1467 | /* Test that reset clears the slot number. */ |
| 1468 | psa_set_key_slot_number( &attributes, 42 ); |
| 1469 | PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) ); |
| 1470 | TEST_EQUAL( slot_number, 42 ); |
| 1471 | psa_reset_key_attributes( &attributes ); |
| 1472 | TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ), |
| 1473 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1474 | } |
| 1475 | /* END_CASE */ |
| 1476 | |
Gilles Peskine | dd835cb | 2019-05-15 16:14:57 +0200 | [diff] [blame] | 1477 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1478 | void import_with_policy( int type_arg, |
| 1479 | int usage_arg, int alg_arg, |
| 1480 | int expected_status_arg ) |
| 1481 | { |
| 1482 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1483 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1484 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1485 | psa_key_type_t type = type_arg; |
| 1486 | psa_key_usage_t usage = usage_arg; |
| 1487 | psa_algorithm_t alg = alg_arg; |
| 1488 | psa_status_t expected_status = expected_status_arg; |
| 1489 | const uint8_t key_material[16] = {0}; |
| 1490 | psa_status_t status; |
| 1491 | |
| 1492 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1493 | |
| 1494 | psa_set_key_type( &attributes, type ); |
| 1495 | psa_set_key_usage_flags( &attributes, usage ); |
| 1496 | psa_set_key_algorithm( &attributes, alg ); |
| 1497 | |
| 1498 | status = psa_import_key( &attributes, |
| 1499 | key_material, sizeof( key_material ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1500 | &key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1501 | TEST_EQUAL( status, expected_status ); |
| 1502 | if( status != PSA_SUCCESS ) |
| 1503 | goto exit; |
| 1504 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1505 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1506 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1507 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 1508 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1509 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1510 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1511 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1512 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1513 | |
| 1514 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1515 | /* |
| 1516 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1517 | * thus reset them as required. |
| 1518 | */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1519 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1520 | |
| 1521 | psa_destroy_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1522 | PSA_DONE( ); |
| 1523 | } |
| 1524 | /* END_CASE */ |
| 1525 | |
| 1526 | /* BEGIN_CASE */ |
| 1527 | void import_with_data( data_t *data, int type_arg, |
| 1528 | int attr_bits_arg, |
| 1529 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1530 | { |
| 1531 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1532 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1533 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1534 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1535 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1536 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1537 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1538 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1539 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1540 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1541 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1542 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1543 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1544 | status = psa_import_key( &attributes, data->x, data->len, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1545 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1546 | if( status != PSA_SUCCESS ) |
| 1547 | goto exit; |
| 1548 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1549 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1550 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1551 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1552 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1553 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1554 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1555 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1556 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1557 | |
| 1558 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1559 | /* |
| 1560 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1561 | * thus reset them as required. |
| 1562 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1563 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1564 | |
| 1565 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1566 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1567 | } |
| 1568 | /* END_CASE */ |
| 1569 | |
| 1570 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1571 | void import_large_key( int type_arg, int byte_size_arg, |
| 1572 | int expected_status_arg ) |
| 1573 | { |
| 1574 | psa_key_type_t type = type_arg; |
| 1575 | size_t byte_size = byte_size_arg; |
| 1576 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1577 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1578 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1579 | psa_status_t status; |
| 1580 | uint8_t *buffer = NULL; |
| 1581 | size_t buffer_size = byte_size + 1; |
| 1582 | size_t n; |
| 1583 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame^] | 1584 | /* Skip the test case if the target running the test cannot |
| 1585 | * accomodate large keys due to heap size constraints */ |
| 1586 | ASSERT_ALLOC_WEAK( buffer, buffer_size ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1587 | memset( buffer, 'K', byte_size ); |
| 1588 | |
| 1589 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1590 | |
| 1591 | /* Try importing the key */ |
| 1592 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1593 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1594 | status = psa_import_key( &attributes, buffer, byte_size, &key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1595 | TEST_EQUAL( status, expected_status ); |
| 1596 | |
| 1597 | if( status == PSA_SUCCESS ) |
| 1598 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1599 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1600 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 1601 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 1602 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1603 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1604 | memset( buffer, 0, byte_size + 1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1605 | PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1606 | for( n = 0; n < byte_size; n++ ) |
| 1607 | TEST_EQUAL( buffer[n], 'K' ); |
| 1608 | for( n = byte_size; n < buffer_size; n++ ) |
| 1609 | TEST_EQUAL( buffer[n], 0 ); |
| 1610 | } |
| 1611 | |
| 1612 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1613 | /* |
| 1614 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1615 | * thus reset them as required. |
| 1616 | */ |
| 1617 | psa_reset_key_attributes( &attributes ); |
| 1618 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1619 | psa_destroy_key( key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1620 | PSA_DONE( ); |
| 1621 | mbedtls_free( buffer ); |
| 1622 | } |
| 1623 | /* END_CASE */ |
| 1624 | |
| 1625 | /* BEGIN_CASE */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1626 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 1627 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1628 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1629 | size_t bits = bits_arg; |
| 1630 | psa_status_t expected_status = expected_status_arg; |
| 1631 | psa_status_t status; |
| 1632 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1633 | 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] | 1634 | size_t buffer_size = /* Slight overapproximations */ |
| 1635 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1636 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1637 | unsigned char *p; |
| 1638 | int ret; |
| 1639 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1640 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1641 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1642 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1643 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1644 | |
| 1645 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 1646 | bits, keypair ) ) >= 0 ); |
| 1647 | length = ret; |
| 1648 | |
| 1649 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1650 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1651 | status = psa_import_key( &attributes, p, length, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1652 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1653 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1654 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1655 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1656 | |
| 1657 | exit: |
| 1658 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1659 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1660 | } |
| 1661 | /* END_CASE */ |
| 1662 | |
| 1663 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1664 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 1665 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 1666 | int usage_arg, int alg_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1667 | int expected_bits, |
| 1668 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1669 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1670 | int canonical_input ) |
| 1671 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1672 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1673 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1674 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1675 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1676 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1677 | unsigned char *exported = NULL; |
| 1678 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1679 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1680 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1681 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1682 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1683 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1684 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1685 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1686 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1687 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1688 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1689 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1690 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1691 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 1692 | psa_set_key_algorithm( &attributes, alg ); |
| 1693 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1694 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1695 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1696 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1697 | |
| 1698 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1699 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1700 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1701 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1702 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1703 | |
| 1704 | /* Export the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1705 | status = psa_export_key( key, exported, export_size, &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1706 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1707 | |
| 1708 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1709 | * and export_size. On errors, the exported length must be 0. */ |
| 1710 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 1711 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
| 1712 | TEST_ASSERT( exported_length <= export_size ); |
| 1713 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1714 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 1715 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1716 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1717 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1718 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1719 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1720 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1721 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1722 | if( ! exercise_export_key( key, usage_arg ) ) |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1723 | goto exit; |
| 1724 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1725 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1726 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1727 | else |
| 1728 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1729 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1730 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1731 | &key2 ) ); |
| 1732 | PSA_ASSERT( psa_export_key( key2, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1733 | reexported, |
| 1734 | export_size, |
| 1735 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1736 | ASSERT_COMPARE( exported, exported_length, |
| 1737 | reexported, reexported_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1738 | PSA_ASSERT( psa_destroy_key( key2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1739 | } |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1740 | TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, psa_get_key_bits( &got_attributes ) ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1741 | |
| 1742 | destroy: |
| 1743 | /* Destroy the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1744 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1745 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1746 | |
| 1747 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1748 | /* |
| 1749 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1750 | * thus reset them as required. |
| 1751 | */ |
| 1752 | psa_reset_key_attributes( &got_attributes ); |
| 1753 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1754 | mbedtls_free( exported ); |
| 1755 | mbedtls_free( reexported ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1756 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1757 | } |
| 1758 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1759 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1760 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1761 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1762 | int type_arg, |
| 1763 | int alg_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1764 | int export_size_delta, |
| 1765 | int expected_export_status_arg, |
| 1766 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1767 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1768 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1769 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1770 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1771 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1772 | psa_status_t status; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1773 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1774 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1775 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1776 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1777 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1778 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1779 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1780 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1781 | psa_set_key_algorithm( &attributes, alg ); |
| 1782 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1783 | |
| 1784 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1785 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1786 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1787 | /* Export the public key */ |
| 1788 | ASSERT_ALLOC( exported, export_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1789 | status = psa_export_public_key( key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1790 | exported, export_size, |
| 1791 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1792 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1793 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1794 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1795 | 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] | 1796 | size_t bits; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1797 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1798 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1799 | TEST_ASSERT( expected_public_key->len <= |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1800 | PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1801 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 1802 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1803 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1804 | |
| 1805 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1806 | /* |
| 1807 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1808 | * thus reset them as required. |
| 1809 | */ |
| 1810 | psa_reset_key_attributes( &attributes ); |
| 1811 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1812 | mbedtls_free( exported ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1813 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1814 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1815 | } |
| 1816 | /* END_CASE */ |
| 1817 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1818 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1819 | void import_and_exercise_key( data_t *data, |
| 1820 | int type_arg, |
| 1821 | int bits_arg, |
| 1822 | int alg_arg ) |
| 1823 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1824 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1825 | psa_key_type_t type = type_arg; |
| 1826 | size_t bits = bits_arg; |
| 1827 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 10df341 | 2018-10-25 22:35:43 +0200 | [diff] [blame] | 1828 | psa_key_usage_t usage = usage_to_exercise( type, alg ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1829 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1830 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1831 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1832 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1833 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1834 | psa_set_key_usage_flags( &attributes, usage ); |
| 1835 | psa_set_key_algorithm( &attributes, alg ); |
| 1836 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1837 | |
| 1838 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1839 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1840 | |
| 1841 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1842 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1843 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1844 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1845 | |
| 1846 | /* Do something with the key according to its type and permitted usage. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1847 | if( ! exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1848 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1849 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1850 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1851 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1852 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1853 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1854 | /* |
| 1855 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1856 | * thus reset them as required. |
| 1857 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1858 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1859 | |
| 1860 | psa_reset_key_attributes( &attributes ); |
| 1861 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1862 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1863 | } |
| 1864 | /* END_CASE */ |
| 1865 | |
| 1866 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1867 | void effective_key_attributes( int type_arg, int expected_type_arg, |
| 1868 | int bits_arg, int expected_bits_arg, |
| 1869 | int usage_arg, int expected_usage_arg, |
| 1870 | int alg_arg, int expected_alg_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1871 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1872 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1873 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1874 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1875 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1876 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1877 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1878 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1879 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1880 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1881 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1882 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1883 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1884 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1885 | psa_set_key_usage_flags( &attributes, usage ); |
| 1886 | psa_set_key_algorithm( &attributes, alg ); |
| 1887 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1888 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1889 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1890 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1891 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1892 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1893 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1894 | TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type ); |
| 1895 | TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits ); |
| 1896 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1897 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1898 | |
| 1899 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1900 | /* |
| 1901 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1902 | * thus reset them as required. |
| 1903 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1904 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1905 | |
| 1906 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1907 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1908 | } |
| 1909 | /* END_CASE */ |
| 1910 | |
| 1911 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1912 | void check_key_policy( int type_arg, int bits_arg, |
| 1913 | int usage_arg, int alg_arg ) |
| 1914 | { |
| 1915 | test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg, |
| 1916 | usage_arg, usage_arg, alg_arg, alg_arg ); |
| 1917 | goto exit; |
| 1918 | } |
| 1919 | /* END_CASE */ |
| 1920 | |
| 1921 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1922 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1923 | { |
| 1924 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1925 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1926 | * though it's OK by the C standard. We could test for this, but we'd need |
| 1927 | * to supress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1928 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 1929 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1930 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1931 | |
| 1932 | memset( &zero, 0, sizeof( zero ) ); |
| 1933 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1934 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1935 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1936 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1937 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1938 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 1939 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 1940 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 1941 | |
| 1942 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 1943 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 1944 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 1945 | |
| 1946 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 1947 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 1948 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 1949 | |
| 1950 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 1951 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 1952 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1953 | } |
| 1954 | /* END_CASE */ |
| 1955 | |
| 1956 | /* BEGIN_CASE */ |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1957 | void mac_key_policy( int policy_usage, |
| 1958 | int policy_alg, |
| 1959 | int key_type, |
| 1960 | data_t *key_data, |
| 1961 | int exercise_alg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1962 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1963 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1964 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1965 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1966 | psa_status_t status; |
| 1967 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1968 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1969 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1970 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1971 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1972 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1973 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1974 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1975 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1976 | &key ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1977 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1978 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1979 | if( policy_alg == exercise_alg && |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1980 | ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1981 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1982 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1983 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1984 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1985 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1986 | memset( mac, 0, sizeof( mac ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1987 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1988 | if( policy_alg == exercise_alg && |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1989 | ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1990 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1991 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1992 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1993 | |
| 1994 | exit: |
| 1995 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1996 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1997 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1998 | } |
| 1999 | /* END_CASE */ |
| 2000 | |
| 2001 | /* BEGIN_CASE */ |
| 2002 | void cipher_key_policy( int policy_usage, |
| 2003 | int policy_alg, |
| 2004 | int key_type, |
| 2005 | data_t *key_data, |
| 2006 | int exercise_alg ) |
| 2007 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2008 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2009 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2010 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2011 | psa_status_t status; |
| 2012 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2013 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2014 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2015 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2016 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2017 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2018 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2019 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2020 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2021 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2022 | status = psa_cipher_encrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2023 | if( policy_alg == exercise_alg && |
| 2024 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2025 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2026 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2027 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2028 | psa_cipher_abort( &operation ); |
| 2029 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2030 | status = psa_cipher_decrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2031 | if( policy_alg == exercise_alg && |
| 2032 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2033 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2034 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2035 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2036 | |
| 2037 | exit: |
| 2038 | psa_cipher_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2039 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2040 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2041 | } |
| 2042 | /* END_CASE */ |
| 2043 | |
| 2044 | /* BEGIN_CASE */ |
| 2045 | void aead_key_policy( int policy_usage, |
| 2046 | int policy_alg, |
| 2047 | int key_type, |
| 2048 | data_t *key_data, |
| 2049 | int nonce_length_arg, |
| 2050 | int tag_length_arg, |
| 2051 | int exercise_alg ) |
| 2052 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2053 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2054 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2055 | psa_status_t status; |
| 2056 | unsigned char nonce[16] = {0}; |
| 2057 | size_t nonce_length = nonce_length_arg; |
| 2058 | unsigned char tag[16]; |
| 2059 | size_t tag_length = tag_length_arg; |
| 2060 | size_t output_length; |
| 2061 | |
| 2062 | TEST_ASSERT( nonce_length <= sizeof( nonce ) ); |
| 2063 | TEST_ASSERT( tag_length <= sizeof( tag ) ); |
| 2064 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2065 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2066 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2067 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2068 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2069 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2070 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2071 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2072 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2073 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2074 | status = psa_aead_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2075 | nonce, nonce_length, |
| 2076 | NULL, 0, |
| 2077 | NULL, 0, |
| 2078 | tag, tag_length, |
| 2079 | &output_length ); |
| 2080 | if( policy_alg == exercise_alg && |
| 2081 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2082 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2083 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2084 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2085 | |
| 2086 | memset( tag, 0, sizeof( tag ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2087 | status = psa_aead_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2088 | nonce, nonce_length, |
| 2089 | NULL, 0, |
| 2090 | tag, tag_length, |
| 2091 | NULL, 0, |
| 2092 | &output_length ); |
| 2093 | if( policy_alg == exercise_alg && |
| 2094 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2095 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2096 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2097 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2098 | |
| 2099 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2100 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2101 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2102 | } |
| 2103 | /* END_CASE */ |
| 2104 | |
| 2105 | /* BEGIN_CASE */ |
| 2106 | void asymmetric_encryption_key_policy( int policy_usage, |
| 2107 | int policy_alg, |
| 2108 | int key_type, |
| 2109 | data_t *key_data, |
| 2110 | int exercise_alg ) |
| 2111 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2112 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2113 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2114 | psa_status_t status; |
| 2115 | size_t key_bits; |
| 2116 | size_t buffer_length; |
| 2117 | unsigned char *buffer = NULL; |
| 2118 | size_t output_length; |
| 2119 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2120 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2121 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2122 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2123 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2124 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2125 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2126 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2127 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2128 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2129 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2130 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2131 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 2132 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2133 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2134 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2135 | status = psa_asymmetric_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2136 | NULL, 0, |
| 2137 | NULL, 0, |
| 2138 | buffer, buffer_length, |
| 2139 | &output_length ); |
| 2140 | if( policy_alg == exercise_alg && |
| 2141 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2142 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2143 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2144 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2145 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 2146 | if( buffer_length != 0 ) |
| 2147 | memset( buffer, 0, buffer_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2148 | status = psa_asymmetric_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2149 | buffer, buffer_length, |
| 2150 | NULL, 0, |
| 2151 | buffer, buffer_length, |
| 2152 | &output_length ); |
| 2153 | if( policy_alg == exercise_alg && |
| 2154 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2155 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2156 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2157 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2158 | |
| 2159 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2160 | /* |
| 2161 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2162 | * thus reset them as required. |
| 2163 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2164 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2165 | |
| 2166 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2167 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2168 | mbedtls_free( buffer ); |
| 2169 | } |
| 2170 | /* END_CASE */ |
| 2171 | |
| 2172 | /* BEGIN_CASE */ |
| 2173 | void asymmetric_signature_key_policy( int policy_usage, |
| 2174 | int policy_alg, |
| 2175 | int key_type, |
| 2176 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2177 | int exercise_alg, |
| 2178 | int payload_length_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2179 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2180 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2181 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2182 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2183 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 2184 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2185 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2186 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2187 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2188 | int compatible_alg = payload_length_arg > 0; |
| 2189 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2190 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2191 | size_t signature_length; |
| 2192 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2193 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2194 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2195 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2196 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2197 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2198 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2199 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2200 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2201 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2202 | status = psa_sign_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2203 | payload, payload_length, |
| 2204 | signature, sizeof( signature ), |
| 2205 | &signature_length ); |
| 2206 | if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2207 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2208 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2209 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2210 | |
| 2211 | memset( signature, 0, sizeof( signature ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2212 | status = psa_verify_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2213 | payload, payload_length, |
| 2214 | signature, sizeof( signature ) ); |
| 2215 | if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2216 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2217 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2218 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2219 | |
| 2220 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2221 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2222 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2223 | } |
| 2224 | /* END_CASE */ |
| 2225 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2226 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2227 | void derive_key_policy( int policy_usage, |
| 2228 | int policy_alg, |
| 2229 | int key_type, |
| 2230 | data_t *key_data, |
| 2231 | int exercise_alg ) |
| 2232 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2233 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2234 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2235 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2236 | psa_status_t status; |
| 2237 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2238 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2239 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2240 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2241 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2242 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2243 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2244 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2245 | &key ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2246 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2247 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 2248 | |
| 2249 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 2250 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2251 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2252 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 2253 | &operation, |
| 2254 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2255 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2256 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2257 | |
| 2258 | status = psa_key_derivation_input_key( &operation, |
| 2259 | PSA_KEY_DERIVATION_INPUT_SECRET, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2260 | key ); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2261 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2262 | if( policy_alg == exercise_alg && |
| 2263 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2264 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2265 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2266 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2267 | |
| 2268 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2269 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2270 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2271 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2272 | } |
| 2273 | /* END_CASE */ |
| 2274 | |
| 2275 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2276 | void agreement_key_policy( int policy_usage, |
| 2277 | int policy_alg, |
| 2278 | int key_type_arg, |
| 2279 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2280 | int exercise_alg, |
| 2281 | int expected_status_arg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2282 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2283 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2284 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2285 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2286 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2287 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2288 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2289 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2290 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2291 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2292 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2293 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2294 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2295 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2296 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2297 | &key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2298 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2299 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2300 | status = key_agreement_with_self( &operation, key ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2301 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2302 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2303 | |
| 2304 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2305 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2306 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2307 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2308 | } |
| 2309 | /* END_CASE */ |
| 2310 | |
| 2311 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2312 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 2313 | int usage_arg, int alg_arg, int alg2_arg ) |
| 2314 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2315 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2316 | psa_key_type_t key_type = key_type_arg; |
| 2317 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2318 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2319 | psa_key_usage_t usage = usage_arg; |
| 2320 | psa_algorithm_t alg = alg_arg; |
| 2321 | psa_algorithm_t alg2 = alg2_arg; |
| 2322 | |
| 2323 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2324 | |
| 2325 | psa_set_key_usage_flags( &attributes, usage ); |
| 2326 | psa_set_key_algorithm( &attributes, alg ); |
| 2327 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 2328 | psa_set_key_type( &attributes, key_type ); |
| 2329 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2330 | &key ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2331 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2332 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2333 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 2334 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 2335 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 2336 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2337 | if( ! exercise_key( key, usage, alg ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2338 | goto exit; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2339 | if( ! exercise_key( key, usage, alg2 ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2340 | goto exit; |
| 2341 | |
| 2342 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2343 | /* |
| 2344 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2345 | * thus reset them as required. |
| 2346 | */ |
| 2347 | psa_reset_key_attributes( &got_attributes ); |
| 2348 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2349 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2350 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2351 | } |
| 2352 | /* END_CASE */ |
| 2353 | |
| 2354 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2355 | void raw_agreement_key_policy( int policy_usage, |
| 2356 | int policy_alg, |
| 2357 | int key_type_arg, |
| 2358 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2359 | int exercise_alg, |
| 2360 | int expected_status_arg ) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2361 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2362 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2363 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2364 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2365 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2366 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2367 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2368 | |
| 2369 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2370 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2371 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2372 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2373 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2374 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2375 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2376 | &key ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2377 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2378 | status = raw_key_agreement_with_self( exercise_alg, key ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2379 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2380 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2381 | |
| 2382 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2383 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2384 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2385 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2386 | } |
| 2387 | /* END_CASE */ |
| 2388 | |
| 2389 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2390 | void copy_success( int source_usage_arg, |
| 2391 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2392 | int type_arg, data_t *material, |
| 2393 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2394 | int target_usage_arg, |
| 2395 | int target_alg_arg, int target_alg2_arg, |
| 2396 | int expected_usage_arg, |
| 2397 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2398 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2399 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2400 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2401 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2402 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2403 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2404 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2405 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2406 | uint8_t *export_buffer = NULL; |
| 2407 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2408 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2409 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2410 | /* Prepare the source key. */ |
| 2411 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2412 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2413 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2414 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2415 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2416 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2417 | &source_key ) ); |
| 2418 | PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2419 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2420 | /* Prepare the target attributes. */ |
| 2421 | if( copy_attributes ) |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2422 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2423 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2424 | /* Set volatile lifetime to reset the key identifier to 0. */ |
| 2425 | psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE ); |
| 2426 | } |
| 2427 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2428 | if( target_usage_arg != -1 ) |
| 2429 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2430 | if( target_alg_arg != -1 ) |
| 2431 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2432 | if( target_alg2_arg != -1 ) |
| 2433 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2434 | |
| 2435 | /* Copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2436 | PSA_ASSERT( psa_copy_key( source_key, |
| 2437 | &target_attributes, &target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2438 | |
| 2439 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2440 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2441 | |
| 2442 | /* Test that the target slot has the expected content and policy. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2443 | PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2444 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 2445 | psa_get_key_type( &target_attributes ) ); |
| 2446 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 2447 | psa_get_key_bits( &target_attributes ) ); |
| 2448 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 2449 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2450 | TEST_EQUAL( expected_alg2, |
| 2451 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2452 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 2453 | { |
| 2454 | size_t length; |
| 2455 | ASSERT_ALLOC( export_buffer, material->len ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2456 | PSA_ASSERT( psa_export_key( target_key, export_buffer, |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2457 | material->len, &length ) ); |
| 2458 | ASSERT_COMPARE( material->x, material->len, |
| 2459 | export_buffer, length ); |
| 2460 | } |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2461 | if( ! exercise_key( target_key, expected_usage, expected_alg ) ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2462 | goto exit; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2463 | if( ! exercise_key( target_key, expected_usage, expected_alg2 ) ) |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2464 | goto exit; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2465 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2466 | PSA_ASSERT( psa_destroy_key( target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2467 | |
| 2468 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2469 | /* |
| 2470 | * Source and target key attributes may have been returned by |
| 2471 | * psa_get_key_attributes() thus reset them as required. |
| 2472 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2473 | psa_reset_key_attributes( &source_attributes ); |
| 2474 | psa_reset_key_attributes( &target_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2475 | |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2476 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2477 | mbedtls_free( export_buffer ); |
| 2478 | } |
| 2479 | /* END_CASE */ |
| 2480 | |
| 2481 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2482 | void copy_fail( int source_usage_arg, |
| 2483 | int source_alg_arg, int source_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2484 | int type_arg, data_t *material, |
| 2485 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2486 | int target_usage_arg, |
| 2487 | int target_alg_arg, int target_alg2_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2488 | int expected_status_arg ) |
| 2489 | { |
| 2490 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2491 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2492 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2493 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2494 | |
| 2495 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2496 | |
| 2497 | /* Prepare the source key. */ |
| 2498 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2499 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2500 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2501 | psa_set_key_type( &source_attributes, type_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2502 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2503 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2504 | &source_key ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2505 | |
| 2506 | /* Prepare the target attributes. */ |
| 2507 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 2508 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 2509 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2510 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2511 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2512 | |
| 2513 | /* Try to copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2514 | TEST_EQUAL( psa_copy_key( source_key, |
| 2515 | &target_attributes, &target_key ), |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2516 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2517 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2518 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2519 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2520 | exit: |
| 2521 | psa_reset_key_attributes( &source_attributes ); |
| 2522 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2523 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2524 | } |
| 2525 | /* END_CASE */ |
| 2526 | |
| 2527 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2528 | void hash_operation_init( ) |
| 2529 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2530 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2531 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2532 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2533 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2534 | * to supress the Clang warning for the test. */ |
| 2535 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 2536 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2537 | psa_hash_operation_t zero; |
| 2538 | |
| 2539 | memset( &zero, 0, sizeof( zero ) ); |
| 2540 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2541 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2542 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 2543 | PSA_ERROR_BAD_STATE ); |
| 2544 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 2545 | PSA_ERROR_BAD_STATE ); |
| 2546 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 2547 | PSA_ERROR_BAD_STATE ); |
| 2548 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2549 | /* A default hash operation should be abortable without error. */ |
| 2550 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 2551 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 2552 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2553 | } |
| 2554 | /* END_CASE */ |
| 2555 | |
| 2556 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2557 | void hash_setup( int alg_arg, |
| 2558 | int expected_status_arg ) |
| 2559 | { |
| 2560 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2561 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2562 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2563 | psa_status_t status; |
| 2564 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2565 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2566 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2567 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2568 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2569 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2570 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 2571 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2572 | |
| 2573 | /* If setup failed, reproduce the failure, so as to |
| 2574 | * test the resulting state of the operation object. */ |
| 2575 | if( status != PSA_SUCCESS ) |
| 2576 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 2577 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2578 | /* Now the operation object should be reusable. */ |
| 2579 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 2580 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 2581 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2582 | #endif |
| 2583 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2584 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2585 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2586 | } |
| 2587 | /* END_CASE */ |
| 2588 | |
| 2589 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2590 | void hash_compute_fail( int alg_arg, data_t *input, |
| 2591 | int output_size_arg, int expected_status_arg ) |
| 2592 | { |
| 2593 | psa_algorithm_t alg = alg_arg; |
| 2594 | uint8_t *output = NULL; |
| 2595 | size_t output_size = output_size_arg; |
| 2596 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 2597 | psa_status_t expected_status = expected_status_arg; |
| 2598 | psa_status_t status; |
| 2599 | |
| 2600 | ASSERT_ALLOC( output, output_size ); |
| 2601 | |
| 2602 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2603 | |
| 2604 | status = psa_hash_compute( alg, input->x, input->len, |
| 2605 | output, output_size, &output_length ); |
| 2606 | TEST_EQUAL( status, expected_status ); |
| 2607 | TEST_ASSERT( output_length <= output_size ); |
| 2608 | |
| 2609 | exit: |
| 2610 | mbedtls_free( output ); |
| 2611 | PSA_DONE( ); |
| 2612 | } |
| 2613 | /* END_CASE */ |
| 2614 | |
| 2615 | /* BEGIN_CASE */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2616 | void hash_compare_fail( int alg_arg, data_t *input, |
| 2617 | data_t *reference_hash, |
| 2618 | int expected_status_arg ) |
| 2619 | { |
| 2620 | psa_algorithm_t alg = alg_arg; |
| 2621 | psa_status_t expected_status = expected_status_arg; |
| 2622 | psa_status_t status; |
| 2623 | |
| 2624 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2625 | |
| 2626 | status = psa_hash_compare( alg, input->x, input->len, |
| 2627 | reference_hash->x, reference_hash->len ); |
| 2628 | TEST_EQUAL( status, expected_status ); |
| 2629 | |
| 2630 | exit: |
| 2631 | PSA_DONE( ); |
| 2632 | } |
| 2633 | /* END_CASE */ |
| 2634 | |
| 2635 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2636 | void hash_compute_compare( int alg_arg, data_t *input, |
| 2637 | data_t *expected_output ) |
| 2638 | { |
| 2639 | psa_algorithm_t alg = alg_arg; |
| 2640 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2641 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 2642 | size_t i; |
| 2643 | |
| 2644 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2645 | |
| 2646 | /* Compute with tight buffer */ |
| 2647 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2648 | output, PSA_HASH_LENGTH( alg ), |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2649 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2650 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2651 | ASSERT_COMPARE( output, output_length, |
| 2652 | expected_output->x, expected_output->len ); |
| 2653 | |
| 2654 | /* Compute with larger buffer */ |
| 2655 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
| 2656 | output, sizeof( output ), |
| 2657 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2658 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2659 | ASSERT_COMPARE( output, output_length, |
| 2660 | expected_output->x, expected_output->len ); |
| 2661 | |
| 2662 | /* Compare with correct hash */ |
| 2663 | PSA_ASSERT( psa_hash_compare( alg, input->x, input->len, |
| 2664 | output, output_length ) ); |
| 2665 | |
| 2666 | /* Compare with trailing garbage */ |
| 2667 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2668 | output, output_length + 1 ), |
| 2669 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2670 | |
| 2671 | /* Compare with truncated hash */ |
| 2672 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2673 | output, output_length - 1 ), |
| 2674 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2675 | |
| 2676 | /* Compare with corrupted value */ |
| 2677 | for( i = 0; i < output_length; i++ ) |
| 2678 | { |
| 2679 | test_set_step( i ); |
| 2680 | output[i] ^= 1; |
| 2681 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2682 | output, output_length ), |
| 2683 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2684 | output[i] ^= 1; |
| 2685 | } |
| 2686 | |
| 2687 | exit: |
| 2688 | PSA_DONE( ); |
| 2689 | } |
| 2690 | /* END_CASE */ |
| 2691 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2692 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2693 | void hash_bad_order( ) |
| 2694 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2695 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2696 | unsigned char input[] = ""; |
| 2697 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2698 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2699 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2700 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2701 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2702 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2703 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2704 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2705 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2706 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2707 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2708 | /* Call setup twice in a row. */ |
| 2709 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2710 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 2711 | PSA_ERROR_BAD_STATE ); |
| 2712 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2713 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2714 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2715 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2716 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2717 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2718 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2719 | /* Call update after finish. */ |
| 2720 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2721 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2722 | hash, sizeof( hash ), &hash_len ) ); |
| 2723 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2724 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2725 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2726 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2727 | /* Call verify without calling setup beforehand. */ |
| 2728 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2729 | valid_hash, sizeof( valid_hash ) ), |
| 2730 | PSA_ERROR_BAD_STATE ); |
| 2731 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2732 | |
| 2733 | /* Call verify after finish. */ |
| 2734 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2735 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2736 | hash, sizeof( hash ), &hash_len ) ); |
| 2737 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2738 | valid_hash, sizeof( valid_hash ) ), |
| 2739 | PSA_ERROR_BAD_STATE ); |
| 2740 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2741 | |
| 2742 | /* Call verify twice in a row. */ |
| 2743 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2744 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2745 | valid_hash, sizeof( valid_hash ) ) ); |
| 2746 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2747 | valid_hash, sizeof( valid_hash ) ), |
| 2748 | PSA_ERROR_BAD_STATE ); |
| 2749 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2750 | |
| 2751 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2752 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2753 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2754 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2755 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2756 | |
| 2757 | /* Call finish twice in a row. */ |
| 2758 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2759 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2760 | hash, sizeof( hash ), &hash_len ) ); |
| 2761 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2762 | hash, sizeof( hash ), &hash_len ), |
| 2763 | PSA_ERROR_BAD_STATE ); |
| 2764 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2765 | |
| 2766 | /* Call finish after calling verify. */ |
| 2767 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2768 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2769 | valid_hash, sizeof( valid_hash ) ) ); |
| 2770 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2771 | hash, sizeof( hash ), &hash_len ), |
| 2772 | PSA_ERROR_BAD_STATE ); |
| 2773 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2774 | |
| 2775 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2776 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2777 | } |
| 2778 | /* END_CASE */ |
| 2779 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2780 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2781 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2782 | { |
| 2783 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2784 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2785 | * appended to it */ |
| 2786 | unsigned char hash[] = { |
| 2787 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2788 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2789 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2790 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2791 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2792 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2793 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2794 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2795 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2796 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2797 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2798 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2799 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2800 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2801 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2802 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2803 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2804 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2805 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2806 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2807 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2808 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2809 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2810 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2811 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2812 | } |
| 2813 | /* END_CASE */ |
| 2814 | |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2815 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2816 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2817 | { |
| 2818 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2819 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2820 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2821 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2822 | size_t hash_len; |
| 2823 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2824 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2825 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2826 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2827 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2828 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2829 | hash, expected_size - 1, &hash_len ), |
| 2830 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2831 | |
| 2832 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2833 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2834 | } |
| 2835 | /* END_CASE */ |
| 2836 | |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2837 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2838 | void hash_clone_source_state( ) |
| 2839 | { |
| 2840 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2841 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2842 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 2843 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2844 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2845 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2846 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2847 | size_t hash_len; |
| 2848 | |
| 2849 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2850 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 2851 | |
| 2852 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2853 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2854 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2855 | hash, sizeof( hash ), &hash_len ) ); |
| 2856 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2857 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2858 | |
| 2859 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 2860 | PSA_ERROR_BAD_STATE ); |
| 2861 | |
| 2862 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 2863 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 2864 | hash, sizeof( hash ), &hash_len ) ); |
| 2865 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 2866 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2867 | hash, sizeof( hash ), &hash_len ) ); |
| 2868 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 2869 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 2870 | hash, sizeof( hash ), &hash_len ) ); |
| 2871 | |
| 2872 | exit: |
| 2873 | psa_hash_abort( &op_source ); |
| 2874 | psa_hash_abort( &op_init ); |
| 2875 | psa_hash_abort( &op_setup ); |
| 2876 | psa_hash_abort( &op_finished ); |
| 2877 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2878 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2879 | } |
| 2880 | /* END_CASE */ |
| 2881 | |
| 2882 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 2883 | void hash_clone_target_state( ) |
| 2884 | { |
| 2885 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2886 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2887 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2888 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2889 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2890 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2891 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 2892 | size_t hash_len; |
| 2893 | |
| 2894 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2895 | |
| 2896 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2897 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2898 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2899 | hash, sizeof( hash ), &hash_len ) ); |
| 2900 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2901 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2902 | |
| 2903 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 2904 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 2905 | hash, sizeof( hash ), &hash_len ) ); |
| 2906 | |
| 2907 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 2908 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 2909 | PSA_ERROR_BAD_STATE ); |
| 2910 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 2911 | PSA_ERROR_BAD_STATE ); |
| 2912 | |
| 2913 | exit: |
| 2914 | psa_hash_abort( &op_target ); |
| 2915 | psa_hash_abort( &op_init ); |
| 2916 | psa_hash_abort( &op_setup ); |
| 2917 | psa_hash_abort( &op_finished ); |
| 2918 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2919 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2920 | } |
| 2921 | /* END_CASE */ |
| 2922 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2923 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2924 | void mac_operation_init( ) |
| 2925 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2926 | const uint8_t input[1] = { 0 }; |
| 2927 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2928 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2929 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2930 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2931 | * to supress the Clang warning for the test. */ |
| 2932 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 2933 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 2934 | psa_mac_operation_t zero; |
| 2935 | |
| 2936 | memset( &zero, 0, sizeof( zero ) ); |
| 2937 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2938 | /* A freshly-initialized MAC operation should not be usable. */ |
| 2939 | TEST_EQUAL( psa_mac_update( &func, |
| 2940 | input, sizeof( input ) ), |
| 2941 | PSA_ERROR_BAD_STATE ); |
| 2942 | TEST_EQUAL( psa_mac_update( &init, |
| 2943 | input, sizeof( input ) ), |
| 2944 | PSA_ERROR_BAD_STATE ); |
| 2945 | TEST_EQUAL( psa_mac_update( &zero, |
| 2946 | input, sizeof( input ) ), |
| 2947 | PSA_ERROR_BAD_STATE ); |
| 2948 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2949 | /* A default MAC operation should be abortable without error. */ |
| 2950 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 2951 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 2952 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2953 | } |
| 2954 | /* END_CASE */ |
| 2955 | |
| 2956 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2957 | void mac_setup( int key_type_arg, |
| 2958 | data_t *key, |
| 2959 | int alg_arg, |
| 2960 | int expected_status_arg ) |
| 2961 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2962 | psa_key_type_t key_type = key_type_arg; |
| 2963 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2964 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2965 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2966 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 2967 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2968 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2969 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2970 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2971 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2972 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2973 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 2974 | &operation, &status ) ) |
| 2975 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2976 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2977 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2978 | /* The operation object should be reusable. */ |
| 2979 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2980 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 2981 | smoke_test_key_data, |
| 2982 | sizeof( smoke_test_key_data ), |
| 2983 | KNOWN_SUPPORTED_MAC_ALG, |
| 2984 | &operation, &status ) ) |
| 2985 | goto exit; |
| 2986 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2987 | #endif |
| 2988 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2989 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2990 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2991 | } |
| 2992 | /* END_CASE */ |
| 2993 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2994 | /* 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] | 2995 | void mac_bad_order( ) |
| 2996 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2997 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2998 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 2999 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3000 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3001 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3002 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3003 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3004 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3005 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3006 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3007 | size_t sign_mac_length = 0; |
| 3008 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3009 | const uint8_t verify_mac[] = { |
| 3010 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3011 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 3012 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 3013 | |
| 3014 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3015 | 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] | 3016 | psa_set_key_algorithm( &attributes, alg ); |
| 3017 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3018 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3019 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 3020 | &key ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3021 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3022 | /* Call update without calling setup beforehand. */ |
| 3023 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3024 | PSA_ERROR_BAD_STATE ); |
| 3025 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3026 | |
| 3027 | /* Call sign finish without calling setup beforehand. */ |
| 3028 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 3029 | &sign_mac_length), |
| 3030 | PSA_ERROR_BAD_STATE ); |
| 3031 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3032 | |
| 3033 | /* Call verify finish without calling setup beforehand. */ |
| 3034 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3035 | verify_mac, sizeof( verify_mac ) ), |
| 3036 | PSA_ERROR_BAD_STATE ); |
| 3037 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3038 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3039 | /* Call setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3040 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
| 3041 | TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3042 | PSA_ERROR_BAD_STATE ); |
| 3043 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3044 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3045 | /* Call update after sign finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3046 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3047 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3048 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 3049 | sign_mac, sizeof( sign_mac ), |
| 3050 | &sign_mac_length ) ); |
| 3051 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3052 | PSA_ERROR_BAD_STATE ); |
| 3053 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3054 | |
| 3055 | /* Call update after verify finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3056 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3057 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3058 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3059 | verify_mac, sizeof( verify_mac ) ) ); |
| 3060 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3061 | PSA_ERROR_BAD_STATE ); |
| 3062 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3063 | |
| 3064 | /* Call sign finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3065 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3066 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3067 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 3068 | sign_mac, sizeof( sign_mac ), |
| 3069 | &sign_mac_length ) ); |
| 3070 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3071 | sign_mac, sizeof( sign_mac ), |
| 3072 | &sign_mac_length ), |
| 3073 | PSA_ERROR_BAD_STATE ); |
| 3074 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3075 | |
| 3076 | /* Call verify finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3077 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3078 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3079 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3080 | verify_mac, sizeof( verify_mac ) ) ); |
| 3081 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3082 | verify_mac, sizeof( verify_mac ) ), |
| 3083 | PSA_ERROR_BAD_STATE ); |
| 3084 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3085 | |
| 3086 | /* Setup sign but try verify. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3087 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3088 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3089 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3090 | verify_mac, sizeof( verify_mac ) ), |
| 3091 | PSA_ERROR_BAD_STATE ); |
| 3092 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3093 | |
| 3094 | /* Setup verify but try sign. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3095 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3096 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3097 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3098 | sign_mac, sizeof( sign_mac ), |
| 3099 | &sign_mac_length ), |
| 3100 | PSA_ERROR_BAD_STATE ); |
| 3101 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3102 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3103 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3104 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3105 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3106 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3107 | } |
| 3108 | /* END_CASE */ |
| 3109 | |
| 3110 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3111 | void mac_sign( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3112 | data_t *key_data, |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3113 | int alg_arg, |
| 3114 | data_t *input, |
| 3115 | data_t *expected_mac ) |
| 3116 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3117 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3118 | psa_key_type_t key_type = key_type_arg; |
| 3119 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3120 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3121 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3122 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3123 | size_t mac_buffer_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3124 | 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] | 3125 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3126 | const size_t output_sizes_to_test[] = { |
| 3127 | 0, |
| 3128 | 1, |
| 3129 | expected_mac->len - 1, |
| 3130 | expected_mac->len, |
| 3131 | expected_mac->len + 1, |
| 3132 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3133 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3134 | TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3135 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 3d404d6 | 2020-08-25 23:47:36 +0200 | [diff] [blame] | 3136 | TEST_ASSERT( expected_mac->len == mac_buffer_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3137 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3138 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3139 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3140 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3141 | psa_set_key_algorithm( &attributes, alg ); |
| 3142 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3143 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3144 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3145 | &key ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3146 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3147 | for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ ) |
| 3148 | { |
| 3149 | const size_t output_size = output_sizes_to_test[i]; |
| 3150 | psa_status_t expected_status = |
| 3151 | ( output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3152 | PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3153 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3154 | test_set_step( output_size ); |
| 3155 | ASSERT_ALLOC( actual_mac, output_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3156 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3157 | /* Calculate the MAC. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3158 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3159 | PSA_ASSERT( psa_mac_update( &operation, |
| 3160 | input->x, input->len ) ); |
| 3161 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3162 | actual_mac, output_size, |
| 3163 | &mac_length ), |
| 3164 | expected_status ); |
| 3165 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3166 | |
| 3167 | if( expected_status == PSA_SUCCESS ) |
| 3168 | { |
| 3169 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 3170 | actual_mac, mac_length ); |
| 3171 | } |
| 3172 | mbedtls_free( actual_mac ); |
| 3173 | actual_mac = NULL; |
| 3174 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3175 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3176 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3177 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3178 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3179 | PSA_DONE( ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3180 | mbedtls_free( actual_mac ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3181 | } |
| 3182 | /* END_CASE */ |
| 3183 | |
| 3184 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3185 | void mac_verify( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3186 | data_t *key_data, |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3187 | int alg_arg, |
| 3188 | data_t *input, |
| 3189 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3190 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3191 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3192 | psa_key_type_t key_type = key_type_arg; |
| 3193 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3194 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3195 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3196 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3197 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3198 | TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE ); |
| 3199 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3200 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3201 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3202 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3203 | psa_set_key_algorithm( &attributes, alg ); |
| 3204 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3205 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3206 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3207 | &key ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3208 | |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3209 | /* Test the correct MAC. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3210 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3211 | PSA_ASSERT( psa_mac_update( &operation, |
| 3212 | input->x, input->len ) ); |
| 3213 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3214 | expected_mac->x, |
| 3215 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3216 | |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3217 | /* Test a MAC that's too short. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3218 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3219 | PSA_ASSERT( psa_mac_update( &operation, |
| 3220 | input->x, input->len ) ); |
| 3221 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3222 | expected_mac->x, |
| 3223 | expected_mac->len - 1 ), |
| 3224 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3225 | |
| 3226 | /* Test a MAC that's too long. */ |
| 3227 | ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 ); |
| 3228 | memcpy( perturbed_mac, expected_mac->x, expected_mac->len ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3229 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3230 | PSA_ASSERT( psa_mac_update( &operation, |
| 3231 | input->x, input->len ) ); |
| 3232 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3233 | perturbed_mac, |
| 3234 | expected_mac->len + 1 ), |
| 3235 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3236 | |
| 3237 | /* Test changing one byte. */ |
| 3238 | for( size_t i = 0; i < expected_mac->len; i++ ) |
| 3239 | { |
| 3240 | test_set_step( i ); |
| 3241 | perturbed_mac[i] ^= 1; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3242 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3243 | PSA_ASSERT( psa_mac_update( &operation, |
| 3244 | input->x, input->len ) ); |
| 3245 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3246 | perturbed_mac, |
| 3247 | expected_mac->len ), |
| 3248 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3249 | perturbed_mac[i] ^= 1; |
| 3250 | } |
| 3251 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3252 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3253 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3254 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3255 | PSA_DONE( ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3256 | mbedtls_free( perturbed_mac ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3257 | } |
| 3258 | /* END_CASE */ |
| 3259 | |
| 3260 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3261 | void cipher_operation_init( ) |
| 3262 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3263 | const uint8_t input[1] = { 0 }; |
| 3264 | unsigned char output[1] = { 0 }; |
| 3265 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3266 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3267 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3268 | * though it's OK by the C standard. We could test for this, but we'd need |
| 3269 | * to supress the Clang warning for the test. */ |
| 3270 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 3271 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3272 | psa_cipher_operation_t zero; |
| 3273 | |
| 3274 | memset( &zero, 0, sizeof( zero ) ); |
| 3275 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3276 | /* A freshly-initialized cipher operation should not be usable. */ |
| 3277 | TEST_EQUAL( psa_cipher_update( &func, |
| 3278 | input, sizeof( input ), |
| 3279 | output, sizeof( output ), |
| 3280 | &output_length ), |
| 3281 | PSA_ERROR_BAD_STATE ); |
| 3282 | TEST_EQUAL( psa_cipher_update( &init, |
| 3283 | input, sizeof( input ), |
| 3284 | output, sizeof( output ), |
| 3285 | &output_length ), |
| 3286 | PSA_ERROR_BAD_STATE ); |
| 3287 | TEST_EQUAL( psa_cipher_update( &zero, |
| 3288 | input, sizeof( input ), |
| 3289 | output, sizeof( output ), |
| 3290 | &output_length ), |
| 3291 | PSA_ERROR_BAD_STATE ); |
| 3292 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3293 | /* A default cipher operation should be abortable without error. */ |
| 3294 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 3295 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 3296 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3297 | } |
| 3298 | /* END_CASE */ |
| 3299 | |
| 3300 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3301 | void cipher_setup( int key_type_arg, |
| 3302 | data_t *key, |
| 3303 | int alg_arg, |
| 3304 | int expected_status_arg ) |
| 3305 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3306 | psa_key_type_t key_type = key_type_arg; |
| 3307 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3308 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3309 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3310 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3311 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3312 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3313 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3314 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3315 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3316 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3317 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 3318 | &operation, &status ) ) |
| 3319 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3320 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3321 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3322 | /* The operation object should be reusable. */ |
| 3323 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 3324 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3325 | smoke_test_key_data, |
| 3326 | sizeof( smoke_test_key_data ), |
| 3327 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3328 | &operation, &status ) ) |
| 3329 | goto exit; |
| 3330 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 3331 | #endif |
| 3332 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3333 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3334 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3335 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3336 | } |
| 3337 | /* END_CASE */ |
| 3338 | |
Steven Cooreman | 29eecbf | 2021-01-28 19:41:25 +0100 | [diff] [blame] | 3339 | /* BEGIN_CASE depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7 */ |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3340 | void cipher_bad_order( ) |
| 3341 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3342 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3343 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3344 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3345 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3346 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3347 | 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] | 3348 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3349 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3350 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 3351 | const uint8_t text[] = { |
| 3352 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 3353 | 0xbb, 0xbb, 0xbb, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3354 | 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] | 3355 | size_t length = 0; |
| 3356 | |
| 3357 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3358 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3359 | psa_set_key_algorithm( &attributes, alg ); |
| 3360 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3361 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 3362 | &key ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3363 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3364 | /* Call encrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3365 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3366 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3367 | PSA_ERROR_BAD_STATE ); |
| 3368 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3369 | |
| 3370 | /* Call decrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3371 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3372 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3373 | PSA_ERROR_BAD_STATE ); |
| 3374 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3375 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3376 | /* Generate an IV without calling setup beforehand. */ |
| 3377 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3378 | buffer, sizeof( buffer ), |
| 3379 | &length ), |
| 3380 | PSA_ERROR_BAD_STATE ); |
| 3381 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3382 | |
| 3383 | /* Generate an IV twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3384 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3385 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3386 | buffer, sizeof( buffer ), |
| 3387 | &length ) ); |
| 3388 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3389 | buffer, sizeof( buffer ), |
| 3390 | &length ), |
| 3391 | PSA_ERROR_BAD_STATE ); |
| 3392 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3393 | |
| 3394 | /* Generate an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3395 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3396 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3397 | iv, sizeof( iv ) ) ); |
| 3398 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3399 | buffer, sizeof( buffer ), |
| 3400 | &length ), |
| 3401 | PSA_ERROR_BAD_STATE ); |
| 3402 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3403 | |
| 3404 | /* Set an IV without calling setup beforehand. */ |
| 3405 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3406 | iv, sizeof( iv ) ), |
| 3407 | PSA_ERROR_BAD_STATE ); |
| 3408 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3409 | |
| 3410 | /* Set an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3411 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3412 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3413 | iv, sizeof( iv ) ) ); |
| 3414 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3415 | iv, sizeof( iv ) ), |
| 3416 | PSA_ERROR_BAD_STATE ); |
| 3417 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3418 | |
| 3419 | /* Set an IV after it's already generated. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3420 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3421 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3422 | buffer, sizeof( buffer ), |
| 3423 | &length ) ); |
| 3424 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3425 | iv, sizeof( iv ) ), |
| 3426 | PSA_ERROR_BAD_STATE ); |
| 3427 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3428 | |
| 3429 | /* Call update without calling setup beforehand. */ |
| 3430 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3431 | text, sizeof( text ), |
| 3432 | buffer, sizeof( buffer ), |
| 3433 | &length ), |
| 3434 | PSA_ERROR_BAD_STATE ); |
| 3435 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3436 | |
| 3437 | /* Call update without an IV where an IV is required. */ |
| 3438 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3439 | text, sizeof( text ), |
| 3440 | buffer, sizeof( buffer ), |
| 3441 | &length ), |
| 3442 | PSA_ERROR_BAD_STATE ); |
| 3443 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3444 | |
| 3445 | /* Call update after finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3446 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3447 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3448 | iv, sizeof( iv ) ) ); |
| 3449 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3450 | buffer, sizeof( buffer ), &length ) ); |
| 3451 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3452 | text, sizeof( text ), |
| 3453 | buffer, sizeof( buffer ), |
| 3454 | &length ), |
| 3455 | PSA_ERROR_BAD_STATE ); |
| 3456 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3457 | |
| 3458 | /* Call finish without calling setup beforehand. */ |
| 3459 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3460 | buffer, sizeof( buffer ), &length ), |
| 3461 | PSA_ERROR_BAD_STATE ); |
| 3462 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3463 | |
| 3464 | /* Call finish without an IV where an IV is required. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3465 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3466 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3467 | * for cipher modes with padding. */ |
| 3468 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3469 | buffer, sizeof( buffer ), &length ), |
| 3470 | PSA_ERROR_BAD_STATE ); |
| 3471 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3472 | |
| 3473 | /* Call finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3474 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3475 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3476 | iv, sizeof( iv ) ) ); |
| 3477 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3478 | buffer, sizeof( buffer ), &length ) ); |
| 3479 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3480 | buffer, sizeof( buffer ), &length ), |
| 3481 | PSA_ERROR_BAD_STATE ); |
| 3482 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3483 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3484 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3485 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3486 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3487 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3488 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3489 | } |
| 3490 | /* END_CASE */ |
| 3491 | |
| 3492 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3493 | void cipher_encrypt( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3494 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3495 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3496 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3497 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3498 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3499 | psa_status_t status; |
| 3500 | psa_key_type_t key_type = key_type_arg; |
| 3501 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3502 | psa_status_t expected_status = expected_status_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3503 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3504 | size_t output_buffer_size = 0; |
| 3505 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3506 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3507 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3508 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3509 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3510 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3511 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3512 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3513 | psa_set_key_algorithm( &attributes, alg ); |
| 3514 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3515 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3516 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3517 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3518 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3519 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3520 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3521 | if( iv->len > 0 ) |
| 3522 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3523 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3524 | } |
| 3525 | |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3526 | output_buffer_size = ( (size_t) input->len + |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3527 | PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3528 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3529 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3530 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3531 | input->x, input->len, |
| 3532 | output, output_buffer_size, |
| 3533 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3534 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3535 | status = psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3536 | output + total_output_length, |
| 3537 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3538 | &function_output_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3539 | total_output_length += function_output_length; |
| 3540 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3541 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3542 | if( expected_status == PSA_SUCCESS ) |
| 3543 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3544 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3545 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3546 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3547 | } |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3548 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3549 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3550 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3551 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3552 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3553 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3554 | } |
| 3555 | /* END_CASE */ |
| 3556 | |
| 3557 | /* BEGIN_CASE */ |
| 3558 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3559 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3560 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3561 | int first_part_size_arg, |
| 3562 | int output1_length_arg, int output2_length_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3563 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3564 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3565 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3566 | psa_key_type_t key_type = key_type_arg; |
| 3567 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3568 | size_t first_part_size = first_part_size_arg; |
| 3569 | size_t output1_length = output1_length_arg; |
| 3570 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3571 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3572 | size_t output_buffer_size = 0; |
| 3573 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3574 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3575 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3576 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3577 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3578 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3579 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3580 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3581 | psa_set_key_algorithm( &attributes, alg ); |
| 3582 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3583 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3584 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3585 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3586 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3587 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3588 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3589 | if( iv->len > 0 ) |
| 3590 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3591 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3592 | } |
| 3593 | |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3594 | output_buffer_size = ( (size_t) input->len + |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3595 | PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3596 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3597 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3598 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3599 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 3600 | output, output_buffer_size, |
| 3601 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3602 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3603 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3604 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3605 | input->x + first_part_size, |
| 3606 | input->len - first_part_size, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3607 | output + total_output_length, |
| 3608 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3609 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3610 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3611 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3612 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3613 | output + total_output_length, |
| 3614 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3615 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3616 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3617 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3618 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3619 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3620 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3621 | |
| 3622 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3623 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3624 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3625 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3626 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3627 | } |
| 3628 | /* END_CASE */ |
| 3629 | |
| 3630 | /* BEGIN_CASE */ |
| 3631 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3632 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3633 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3634 | int first_part_size_arg, |
| 3635 | int output1_length_arg, int output2_length_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3636 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3637 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3638 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3639 | psa_key_type_t key_type = key_type_arg; |
| 3640 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3641 | size_t first_part_size = first_part_size_arg; |
| 3642 | size_t output1_length = output1_length_arg; |
| 3643 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3644 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3645 | size_t output_buffer_size = 0; |
| 3646 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3647 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3648 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3649 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3650 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3651 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3652 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3653 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3654 | psa_set_key_algorithm( &attributes, alg ); |
| 3655 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3656 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3657 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3658 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3659 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3660 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3661 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3662 | if( iv->len > 0 ) |
| 3663 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3664 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3665 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3666 | |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3667 | output_buffer_size = ( (size_t) input->len + |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3668 | PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3669 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3670 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3671 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3672 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3673 | input->x, first_part_size, |
| 3674 | output, output_buffer_size, |
| 3675 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3676 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3677 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3678 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3679 | input->x + first_part_size, |
| 3680 | input->len - first_part_size, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3681 | output + total_output_length, |
| 3682 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3683 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3684 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3685 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3686 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3687 | output + total_output_length, |
| 3688 | output_buffer_size - total_output_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3689 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3690 | total_output_length += function_output_length; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3691 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3692 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3693 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3694 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3695 | |
| 3696 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3697 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3698 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3699 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3700 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3701 | } |
| 3702 | /* END_CASE */ |
| 3703 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3704 | /* BEGIN_CASE */ |
| 3705 | void cipher_decrypt( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3706 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3707 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3708 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3709 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3710 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3711 | psa_status_t status; |
| 3712 | psa_key_type_t key_type = key_type_arg; |
| 3713 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3714 | psa_status_t expected_status = expected_status_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3715 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3716 | size_t output_buffer_size = 0; |
| 3717 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3718 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3719 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3720 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3721 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3722 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3723 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3724 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3725 | psa_set_key_algorithm( &attributes, alg ); |
| 3726 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3727 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3728 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3729 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3730 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3731 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3732 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3733 | if( iv->len > 0 ) |
| 3734 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3735 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3736 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3737 | |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3738 | output_buffer_size = ( (size_t) input->len + |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3739 | PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3740 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3741 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3742 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3743 | input->x, input->len, |
| 3744 | output, output_buffer_size, |
| 3745 | &function_output_length ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3746 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3747 | status = psa_cipher_finish( &operation, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3748 | output + total_output_length, |
| 3749 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3750 | &function_output_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3751 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3752 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3753 | |
| 3754 | if( expected_status == PSA_SUCCESS ) |
| 3755 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3756 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3757 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3758 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3759 | } |
| 3760 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3761 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3762 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3763 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3764 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3765 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3766 | } |
| 3767 | /* END_CASE */ |
| 3768 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3769 | /* BEGIN_CASE */ |
| 3770 | void cipher_verify_output( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3771 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3772 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3773 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3774 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3775 | psa_key_type_t key_type = key_type_arg; |
| 3776 | psa_algorithm_t alg = alg_arg; |
mohammad1603 | e6b67a1 | 2018-03-12 10:38:49 -0700 | [diff] [blame] | 3777 | unsigned char iv[16] = {0}; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3778 | size_t iv_size = 16; |
| 3779 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3780 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3781 | size_t output1_size = 0; |
| 3782 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3783 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3784 | size_t output2_size = 0; |
| 3785 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3786 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3787 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3788 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3789 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3790 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3791 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3792 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3793 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3794 | psa_set_key_algorithm( &attributes, alg ); |
| 3795 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3796 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3797 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3798 | &key ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3799 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3800 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 3801 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3802 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3803 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3804 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3805 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3806 | iv, iv_size, |
| 3807 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3808 | } |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3809 | output1_size = ( (size_t) input->len + |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3810 | PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3811 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3812 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3813 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len, |
| 3814 | output1, output1_size, |
| 3815 | &output1_length ) ); |
| 3816 | PSA_ASSERT( psa_cipher_finish( &operation1, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3817 | output1 + output1_length, |
| 3818 | output1_size - output1_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3819 | &function_output_length ) ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3820 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3821 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3822 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3823 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3824 | |
| 3825 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3826 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3827 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3828 | if( iv_length > 0 ) |
| 3829 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3830 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3831 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3832 | } |
| 3833 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3834 | PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length, |
| 3835 | output2, output2_size, |
| 3836 | &output2_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3837 | function_output_length = 0; |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3838 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3839 | output2 + output2_length, |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3840 | output2_size - output2_length, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3841 | &function_output_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3842 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3843 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3844 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3845 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3846 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3847 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3848 | |
| 3849 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3850 | psa_cipher_abort( &operation1 ); |
| 3851 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3852 | mbedtls_free( output1 ); |
| 3853 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3854 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3855 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3856 | } |
| 3857 | /* END_CASE */ |
| 3858 | |
| 3859 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3860 | void cipher_verify_output_multipart( int alg_arg, |
| 3861 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3862 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3863 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3864 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3865 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3866 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3867 | psa_key_type_t key_type = key_type_arg; |
| 3868 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3869 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3870 | unsigned char iv[16] = {0}; |
| 3871 | size_t iv_size = 16; |
| 3872 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3873 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3874 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3875 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3876 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3877 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3878 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3879 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3880 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3881 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3882 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3883 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3884 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3885 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3886 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3887 | psa_set_key_algorithm( &attributes, alg ); |
| 3888 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3889 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3890 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3891 | &key ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3892 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3893 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 3894 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3895 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3896 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3897 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3898 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3899 | iv, iv_size, |
| 3900 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3901 | } |
| 3902 | |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame] | 3903 | output1_buffer_size = ( (size_t) input->len + |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3904 | PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3905 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3906 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3907 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3908 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3909 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 3910 | output1, output1_buffer_size, |
| 3911 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3912 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3913 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3914 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 3915 | input->x + first_part_size, |
| 3916 | input->len - first_part_size, |
| 3917 | output1, output1_buffer_size, |
| 3918 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3919 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3920 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3921 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 3922 | output1 + output1_length, |
| 3923 | output1_buffer_size - output1_length, |
| 3924 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3925 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3926 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3927 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3928 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3929 | output2_buffer_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3930 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3931 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3932 | if( iv_length > 0 ) |
| 3933 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3934 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3935 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3936 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3937 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3938 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 3939 | output2, output2_buffer_size, |
| 3940 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3941 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3942 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3943 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 3944 | output1 + first_part_size, |
| 3945 | output1_length - first_part_size, |
| 3946 | output2, output2_buffer_size, |
| 3947 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3948 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3949 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3950 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3951 | output2 + output2_length, |
| 3952 | output2_buffer_size - output2_length, |
| 3953 | &function_output_length ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3954 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3955 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3956 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3957 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3958 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3959 | |
| 3960 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3961 | psa_cipher_abort( &operation1 ); |
| 3962 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3963 | mbedtls_free( output1 ); |
| 3964 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3965 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3966 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3967 | } |
| 3968 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 3969 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3970 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3971 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3972 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3973 | data_t *nonce, |
| 3974 | data_t *additional_data, |
| 3975 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3976 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3977 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3978 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3979 | psa_key_type_t key_type = key_type_arg; |
| 3980 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3981 | unsigned char *output_data = NULL; |
| 3982 | size_t output_size = 0; |
| 3983 | size_t output_length = 0; |
| 3984 | unsigned char *output_data2 = NULL; |
| 3985 | size_t output_length2 = 0; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3986 | size_t tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3987 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3988 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3989 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3990 | output_size = input_data->len + tag_length; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3991 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3992 | * should be exact. */ |
| 3993 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT ) |
| 3994 | TEST_EQUAL( output_size, |
| 3995 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3996 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3997 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3998 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3999 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4000 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4001 | psa_set_key_algorithm( &attributes, alg ); |
| 4002 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4003 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4004 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4005 | &key ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4006 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4007 | TEST_EQUAL( psa_aead_encrypt( key, alg, |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4008 | nonce->x, nonce->len, |
| 4009 | additional_data->x, |
| 4010 | additional_data->len, |
| 4011 | input_data->x, input_data->len, |
| 4012 | output_data, output_size, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4013 | &output_length ), |
| 4014 | expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4015 | |
| 4016 | if( PSA_SUCCESS == expected_result ) |
| 4017 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4018 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4019 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4020 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4021 | * should be exact. */ |
| 4022 | TEST_EQUAL( input_data->len, |
| 4023 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) ); |
| 4024 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4025 | TEST_EQUAL( psa_aead_decrypt( key, alg, |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4026 | nonce->x, nonce->len, |
| 4027 | additional_data->x, |
| 4028 | additional_data->len, |
| 4029 | output_data, output_length, |
| 4030 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4031 | &output_length2 ), |
| 4032 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4033 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4034 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 4035 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4036 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4037 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4038 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4039 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4040 | mbedtls_free( output_data ); |
| 4041 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4042 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4043 | } |
| 4044 | /* END_CASE */ |
| 4045 | |
| 4046 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4047 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 4048 | int alg_arg, |
| 4049 | data_t *nonce, |
| 4050 | data_t *additional_data, |
| 4051 | data_t *input_data, |
| 4052 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4053 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4054 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4055 | psa_key_type_t key_type = key_type_arg; |
| 4056 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4057 | unsigned char *output_data = NULL; |
| 4058 | size_t output_size = 0; |
| 4059 | size_t output_length = 0; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4060 | size_t tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4061 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4062 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4063 | output_size = input_data->len + tag_length; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4064 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4065 | * should be exact. */ |
| 4066 | TEST_EQUAL( output_size, |
| 4067 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4068 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4069 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4070 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4071 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4072 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4073 | psa_set_key_algorithm( &attributes, alg ); |
| 4074 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4075 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4076 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4077 | &key ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4078 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4079 | PSA_ASSERT( psa_aead_encrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4080 | nonce->x, nonce->len, |
| 4081 | additional_data->x, additional_data->len, |
| 4082 | input_data->x, input_data->len, |
| 4083 | output_data, output_size, |
| 4084 | &output_length ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4085 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4086 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 4087 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4088 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4089 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4090 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4091 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4092 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4093 | } |
| 4094 | /* END_CASE */ |
| 4095 | |
| 4096 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4097 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 4098 | int alg_arg, |
| 4099 | data_t *nonce, |
| 4100 | data_t *additional_data, |
| 4101 | data_t *input_data, |
| 4102 | data_t *expected_data, |
| 4103 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4104 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4105 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4106 | psa_key_type_t key_type = key_type_arg; |
| 4107 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4108 | unsigned char *output_data = NULL; |
| 4109 | size_t output_size = 0; |
| 4110 | size_t output_length = 0; |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4111 | size_t tag_length = PSA_AEAD_TAG_LENGTH( alg ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4112 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4113 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4114 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4115 | output_size = input_data->len - tag_length; |
| 4116 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4117 | * should be exact. */ |
| 4118 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT ) |
| 4119 | TEST_EQUAL( output_size, |
| 4120 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4121 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4122 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4123 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4124 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4125 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4126 | psa_set_key_algorithm( &attributes, alg ); |
| 4127 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4128 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4129 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4130 | &key ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4131 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4132 | TEST_EQUAL( psa_aead_decrypt( key, alg, |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4133 | nonce->x, nonce->len, |
| 4134 | additional_data->x, |
| 4135 | additional_data->len, |
| 4136 | input_data->x, input_data->len, |
| 4137 | output_data, output_size, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4138 | &output_length ), |
| 4139 | expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4140 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4141 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4142 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4143 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4144 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4145 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4146 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4147 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4148 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4149 | } |
| 4150 | /* END_CASE */ |
| 4151 | |
| 4152 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4153 | void signature_size( int type_arg, |
| 4154 | int bits, |
| 4155 | int alg_arg, |
| 4156 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4157 | { |
| 4158 | psa_key_type_t type = type_arg; |
| 4159 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4160 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 4161 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4162 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 4163 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 4164 | TEST_EQUAL( actual_size, |
| 4165 | PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) ); |
| 4166 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 4167 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4168 | exit: |
| 4169 | ; |
| 4170 | } |
| 4171 | /* END_CASE */ |
| 4172 | |
| 4173 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4174 | void sign_deterministic( int key_type_arg, data_t *key_data, |
| 4175 | int alg_arg, data_t *input_data, |
| 4176 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4177 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4178 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4179 | psa_key_type_t key_type = key_type_arg; |
| 4180 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4181 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4182 | unsigned char *signature = NULL; |
| 4183 | size_t signature_size; |
| 4184 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4185 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4186 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4187 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4188 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4189 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4190 | psa_set_key_algorithm( &attributes, alg ); |
| 4191 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 4192 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4193 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4194 | &key ) ); |
| 4195 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4196 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4197 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4198 | /* Allocate a buffer which has the size advertized by the |
| 4199 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4200 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4201 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4202 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4203 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4204 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4205 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4206 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4207 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4208 | input_data->x, input_data->len, |
| 4209 | signature, signature_size, |
| 4210 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4211 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4212 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 4213 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4214 | |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 4215 | #if defined(MBEDTLS_TEST_DEPRECATED) |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 4216 | memset( signature, 0, signature_size ); |
| 4217 | signature_length = INVALID_EXPORT_LENGTH; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4218 | PSA_ASSERT( psa_asymmetric_sign( key, alg, |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 4219 | input_data->x, input_data->len, |
| 4220 | signature, signature_size, |
| 4221 | &signature_length ) ); |
| 4222 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 4223 | signature, signature_length ); |
| 4224 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 4225 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4226 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4227 | /* |
| 4228 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4229 | * thus reset them as required. |
| 4230 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4231 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4232 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4233 | psa_destroy_key( key ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 4234 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4235 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4236 | } |
| 4237 | /* END_CASE */ |
| 4238 | |
| 4239 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4240 | void sign_fail( int key_type_arg, data_t *key_data, |
| 4241 | int alg_arg, data_t *input_data, |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4242 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4243 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4244 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4245 | psa_key_type_t key_type = key_type_arg; |
| 4246 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4247 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4248 | psa_status_t actual_status; |
| 4249 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 4250 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 4251 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4252 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4253 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4254 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4255 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4256 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4257 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4258 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4259 | psa_set_key_algorithm( &attributes, alg ); |
| 4260 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 4261 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4262 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4263 | &key ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4264 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4265 | actual_status = psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4266 | input_data->x, input_data->len, |
| 4267 | signature, signature_size, |
| 4268 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4269 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 4270 | /* The value of *signature_length is unspecified on error, but |
| 4271 | * whatever it is, it should be less than signature_size, so that |
| 4272 | * if the caller tries to read *signature_length bytes without |
| 4273 | * checking the error code then they don't overflow a buffer. */ |
| 4274 | TEST_ASSERT( signature_length <= signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4275 | |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 4276 | #if defined(MBEDTLS_TEST_DEPRECATED) |
| 4277 | signature_length = INVALID_EXPORT_LENGTH; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4278 | TEST_EQUAL( psa_asymmetric_sign( key, alg, |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 4279 | input_data->x, input_data->len, |
| 4280 | signature, signature_size, |
| 4281 | &signature_length ), |
| 4282 | expected_status ); |
| 4283 | TEST_ASSERT( signature_length <= signature_size ); |
| 4284 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 4285 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4286 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4287 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4288 | psa_destroy_key( key ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4289 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4290 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4291 | } |
| 4292 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 4293 | |
| 4294 | /* BEGIN_CASE */ |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4295 | void sign_verify( int key_type_arg, data_t *key_data, |
| 4296 | int alg_arg, data_t *input_data ) |
| 4297 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4298 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4299 | psa_key_type_t key_type = key_type_arg; |
| 4300 | psa_algorithm_t alg = alg_arg; |
| 4301 | size_t key_bits; |
| 4302 | unsigned char *signature = NULL; |
| 4303 | size_t signature_size; |
| 4304 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4305 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4306 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4307 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4308 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4309 | 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] | 4310 | psa_set_key_algorithm( &attributes, alg ); |
| 4311 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4312 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4313 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4314 | &key ) ); |
| 4315 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4316 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4317 | |
| 4318 | /* Allocate a buffer which has the size advertized by the |
| 4319 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4320 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4321 | key_bits, alg ); |
| 4322 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4323 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4324 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4325 | |
| 4326 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4327 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4328 | input_data->x, input_data->len, |
| 4329 | signature, signature_size, |
| 4330 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4331 | /* Check that the signature length looks sensible. */ |
| 4332 | TEST_ASSERT( signature_length <= signature_size ); |
| 4333 | TEST_ASSERT( signature_length > 0 ); |
| 4334 | |
| 4335 | /* Use the library to verify that the signature is correct. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4336 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4337 | input_data->x, input_data->len, |
| 4338 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4339 | |
| 4340 | if( input_data->len != 0 ) |
| 4341 | { |
| 4342 | /* Flip a bit in the input and verify that the signature is now |
| 4343 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 4344 | * because ECDSA may ignore the last few bits of the input. */ |
| 4345 | input_data->x[0] ^= 1; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4346 | TEST_EQUAL( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4347 | input_data->x, input_data->len, |
| 4348 | signature, signature_length ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4349 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4350 | } |
| 4351 | |
| 4352 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4353 | /* |
| 4354 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4355 | * thus reset them as required. |
| 4356 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4357 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4358 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4359 | psa_destroy_key( key ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4360 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4361 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 4362 | } |
| 4363 | /* END_CASE */ |
| 4364 | |
| 4365 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4366 | void asymmetric_verify( int key_type_arg, data_t *key_data, |
| 4367 | int alg_arg, data_t *hash_data, |
| 4368 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4369 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4370 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4371 | psa_key_type_t key_type = key_type_arg; |
| 4372 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4373 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4374 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4375 | TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 4376 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4377 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4378 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4379 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4380 | psa_set_key_algorithm( &attributes, alg ); |
| 4381 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4382 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4383 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4384 | &key ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4385 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4386 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4387 | hash_data->x, hash_data->len, |
| 4388 | signature_data->x, signature_data->len ) ); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 4389 | |
| 4390 | #if defined(MBEDTLS_TEST_DEPRECATED) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4391 | PSA_ASSERT( psa_asymmetric_verify( key, alg, |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 4392 | hash_data->x, hash_data->len, |
| 4393 | signature_data->x, |
| 4394 | signature_data->len ) ); |
| 4395 | |
| 4396 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 4397 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4398 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4399 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4400 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4401 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 4402 | } |
| 4403 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4404 | |
| 4405 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4406 | void asymmetric_verify_fail( int key_type_arg, data_t *key_data, |
| 4407 | int alg_arg, data_t *hash_data, |
| 4408 | data_t *signature_data, |
| 4409 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4410 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4411 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4412 | psa_key_type_t key_type = key_type_arg; |
| 4413 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4414 | psa_status_t actual_status; |
| 4415 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4416 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4417 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4418 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4419 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4420 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4421 | psa_set_key_algorithm( &attributes, alg ); |
| 4422 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4423 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4424 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4425 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4426 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4427 | actual_status = psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 4428 | hash_data->x, hash_data->len, |
| 4429 | signature_data->x, signature_data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4430 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4431 | |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 4432 | #if defined(MBEDTLS_TEST_DEPRECATED) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4433 | TEST_EQUAL( psa_asymmetric_verify( key, alg, |
Gilles Peskine | 895242b | 2019-11-29 12:15:40 +0100 | [diff] [blame] | 4434 | hash_data->x, hash_data->len, |
| 4435 | signature_data->x, signature_data->len ), |
| 4436 | expected_status ); |
| 4437 | #endif /* MBEDTLS_TEST_DEPRECATED */ |
| 4438 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4439 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4440 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4441 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4442 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4443 | } |
| 4444 | /* END_CASE */ |
| 4445 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4446 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4447 | void asymmetric_encrypt( int key_type_arg, |
| 4448 | data_t *key_data, |
| 4449 | int alg_arg, |
| 4450 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4451 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4452 | int expected_output_length_arg, |
| 4453 | int expected_status_arg ) |
| 4454 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4455 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4456 | psa_key_type_t key_type = key_type_arg; |
| 4457 | psa_algorithm_t alg = alg_arg; |
| 4458 | size_t expected_output_length = expected_output_length_arg; |
| 4459 | size_t key_bits; |
| 4460 | unsigned char *output = NULL; |
| 4461 | size_t output_size; |
| 4462 | size_t output_length = ~0; |
| 4463 | psa_status_t actual_status; |
| 4464 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4465 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4466 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4467 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4468 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4469 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4470 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4471 | psa_set_key_algorithm( &attributes, alg ); |
| 4472 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4473 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4474 | &key ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4475 | |
| 4476 | /* Determine the maximum output length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4477 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4478 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4479 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4480 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4481 | |
| 4482 | /* Encrypt the input */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4483 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4484 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4485 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4486 | output, output_size, |
| 4487 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4488 | TEST_EQUAL( actual_status, expected_status ); |
| 4489 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4490 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4491 | /* If the label is empty, the test framework puts a non-null pointer |
| 4492 | * in label->x. Test that a null pointer works as well. */ |
| 4493 | if( label->len == 0 ) |
| 4494 | { |
| 4495 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4496 | if( output_size != 0 ) |
| 4497 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4498 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4499 | input_data->x, input_data->len, |
| 4500 | NULL, label->len, |
| 4501 | output, output_size, |
| 4502 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4503 | TEST_EQUAL( actual_status, expected_status ); |
| 4504 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4505 | } |
| 4506 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4507 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4508 | /* |
| 4509 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4510 | * thus reset them as required. |
| 4511 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4512 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4513 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4514 | psa_destroy_key( key ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4515 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4516 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 4517 | } |
| 4518 | /* END_CASE */ |
| 4519 | |
| 4520 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4521 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 4522 | data_t *key_data, |
| 4523 | int alg_arg, |
| 4524 | data_t *input_data, |
| 4525 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4526 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4527 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4528 | psa_key_type_t key_type = key_type_arg; |
| 4529 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4530 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4531 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4532 | size_t output_size; |
| 4533 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4534 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4535 | size_t output2_size; |
| 4536 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4537 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4538 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4539 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4540 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4541 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4542 | psa_set_key_algorithm( &attributes, alg ); |
| 4543 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4544 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4545 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4546 | &key ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4547 | |
| 4548 | /* Determine the maximum ciphertext length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4549 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 4550 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4551 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4552 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4553 | output2_size = input_data->len; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4554 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4555 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 4556 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 4557 | * the original plaintext because of the non-optional random |
| 4558 | * part of encryption process which prevents using fixed vectors. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4559 | PSA_ASSERT( psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4560 | input_data->x, input_data->len, |
| 4561 | label->x, label->len, |
| 4562 | output, output_size, |
| 4563 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4564 | /* We don't know what ciphertext length to expect, but check that |
| 4565 | * it looks sensible. */ |
| 4566 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 4567 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4568 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4569 | output, output_length, |
| 4570 | label->x, label->len, |
| 4571 | output2, output2_size, |
| 4572 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4573 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 4574 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4575 | |
| 4576 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4577 | /* |
| 4578 | * Key attributes may have been returned by psa_get_key_attributes() |
| 4579 | * thus reset them as required. |
| 4580 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4581 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 4582 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4583 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4584 | mbedtls_free( output ); |
| 4585 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4586 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4587 | } |
| 4588 | /* END_CASE */ |
| 4589 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4590 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4591 | void asymmetric_decrypt( int key_type_arg, |
| 4592 | data_t *key_data, |
| 4593 | int alg_arg, |
| 4594 | data_t *input_data, |
| 4595 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 4596 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4597 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4598 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4599 | psa_key_type_t key_type = key_type_arg; |
| 4600 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4601 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 4602 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4603 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4604 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4605 | |
Jaeden Amero | 412654a | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4606 | output_size = expected_data->len; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4607 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4608 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4609 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4610 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4611 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4612 | psa_set_key_algorithm( &attributes, alg ); |
| 4613 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4614 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4615 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4616 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4617 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4618 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4619 | input_data->x, input_data->len, |
| 4620 | label->x, label->len, |
| 4621 | output, |
| 4622 | output_size, |
| 4623 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4624 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4625 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4626 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4627 | /* If the label is empty, the test framework puts a non-null pointer |
| 4628 | * in label->x. Test that a null pointer works as well. */ |
| 4629 | if( label->len == 0 ) |
| 4630 | { |
| 4631 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4632 | if( output_size != 0 ) |
| 4633 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4634 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4635 | input_data->x, input_data->len, |
| 4636 | NULL, label->len, |
| 4637 | output, |
| 4638 | output_size, |
| 4639 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4640 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4641 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4642 | } |
| 4643 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4644 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4645 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4646 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4647 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4648 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4649 | } |
| 4650 | /* END_CASE */ |
| 4651 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4652 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4653 | void asymmetric_decrypt_fail( int key_type_arg, |
| 4654 | data_t *key_data, |
| 4655 | int alg_arg, |
| 4656 | data_t *input_data, |
| 4657 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4658 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4659 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4660 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4661 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4662 | psa_key_type_t key_type = key_type_arg; |
| 4663 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4664 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 4665 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4666 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4667 | psa_status_t actual_status; |
| 4668 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4669 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4670 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4671 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4672 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4673 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4674 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4675 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4676 | psa_set_key_algorithm( &attributes, alg ); |
| 4677 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 4678 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4679 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4680 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4681 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4682 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4683 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4684 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4685 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4686 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4687 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4688 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4689 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4690 | /* If the label is empty, the test framework puts a non-null pointer |
| 4691 | * in label->x. Test that a null pointer works as well. */ |
| 4692 | if( label->len == 0 ) |
| 4693 | { |
| 4694 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4695 | if( output_size != 0 ) |
| 4696 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4697 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4698 | input_data->x, input_data->len, |
| 4699 | NULL, label->len, |
| 4700 | output, output_size, |
| 4701 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4702 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 4703 | TEST_ASSERT( output_length <= output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 4704 | } |
| 4705 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4706 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 4707 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4708 | psa_destroy_key( key ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4709 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4710 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 4711 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 4712 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4713 | |
| 4714 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4715 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4716 | { |
| 4717 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 4718 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 4719 | * though it's OK by the C standard. We could test for this, but we'd need |
| 4720 | * to supress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4721 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4722 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 4723 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4724 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4725 | |
| 4726 | memset( &zero, 0, sizeof( zero ) ); |
| 4727 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4728 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4729 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4730 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4731 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4732 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4733 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4734 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 4735 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4736 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 4737 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 4738 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 4739 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 4740 | } |
| 4741 | /* END_CASE */ |
| 4742 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4743 | /* BEGIN_CASE */ |
| 4744 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4745 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4746 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4747 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4748 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4749 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4750 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4751 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 4752 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4753 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4754 | |
| 4755 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4756 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4757 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 4758 | } |
| 4759 | /* END_CASE */ |
| 4760 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4761 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 4762 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 4763 | int expected_status_arg ) |
| 4764 | { |
| 4765 | psa_algorithm_t alg = alg_arg; |
| 4766 | size_t capacity = capacity_arg; |
| 4767 | psa_status_t expected_status = expected_status_arg; |
| 4768 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4769 | |
| 4770 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4771 | |
| 4772 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4773 | |
| 4774 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 4775 | expected_status ); |
| 4776 | |
| 4777 | exit: |
| 4778 | psa_key_derivation_abort( &operation ); |
| 4779 | PSA_DONE( ); |
| 4780 | } |
| 4781 | /* END_CASE */ |
| 4782 | |
| 4783 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4784 | void derive_input( int alg_arg, |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4785 | int step_arg1, int key_type_arg1, data_t *input1, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4786 | int expected_status_arg1, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 4787 | int step_arg2, int key_type_arg2, data_t *input2, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4788 | int expected_status_arg2, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 4789 | int step_arg3, int key_type_arg3, data_t *input3, |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4790 | int expected_status_arg3, |
| 4791 | int output_key_type_arg, int expected_output_status_arg ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4792 | { |
| 4793 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4794 | psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3}; |
| 4795 | 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] | 4796 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 4797 | expected_status_arg2, |
| 4798 | expected_status_arg3}; |
| 4799 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4800 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 4801 | MBEDTLS_SVC_KEY_ID_INIT, |
| 4802 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4803 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4804 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4805 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4806 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4807 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4808 | psa_status_t expected_output_status = expected_output_status_arg; |
| 4809 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4810 | |
| 4811 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4812 | |
| 4813 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4814 | psa_set_key_algorithm( &attributes, alg ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4815 | |
| 4816 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 4817 | |
| 4818 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 4819 | { |
Gilles Peskine | b896519 | 2019-09-24 16:21:10 +0200 | [diff] [blame] | 4820 | if( key_types[i] != PSA_KEY_TYPE_NONE ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4821 | { |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4822 | psa_set_key_type( &attributes, key_types[i] ); |
| 4823 | PSA_ASSERT( psa_import_key( &attributes, |
| 4824 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4825 | &keys[i] ) ); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 4826 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) && |
| 4827 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 4828 | { |
| 4829 | // When taking a private key as secret input, use key agreement |
| 4830 | // to add the shared secret to the derivation |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4831 | TEST_EQUAL( key_agreement_with_self( &operation, keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 4832 | expected_statuses[i] ); |
| 4833 | } |
| 4834 | else |
| 4835 | { |
| 4836 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4837 | keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 4838 | expected_statuses[i] ); |
| 4839 | } |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 4840 | } |
| 4841 | else |
| 4842 | { |
| 4843 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 4844 | &operation, steps[i], |
| 4845 | inputs[i]->x, inputs[i]->len ), |
| 4846 | expected_statuses[i] ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4847 | } |
| 4848 | } |
| 4849 | |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4850 | if( output_key_type != PSA_KEY_TYPE_NONE ) |
| 4851 | { |
| 4852 | psa_reset_key_attributes( &attributes ); |
| 4853 | psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA ); |
| 4854 | psa_set_key_bits( &attributes, 8 ); |
| 4855 | actual_output_status = |
| 4856 | psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4857 | &output_key ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 4858 | } |
| 4859 | else |
| 4860 | { |
| 4861 | uint8_t buffer[1]; |
| 4862 | actual_output_status = |
| 4863 | psa_key_derivation_output_bytes( &operation, |
| 4864 | buffer, sizeof( buffer ) ); |
| 4865 | } |
| 4866 | TEST_EQUAL( actual_output_status, expected_output_status ); |
| 4867 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4868 | exit: |
| 4869 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4870 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 4871 | psa_destroy_key( keys[i] ); |
| 4872 | psa_destroy_key( output_key ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 4873 | PSA_DONE( ); |
| 4874 | } |
| 4875 | /* END_CASE */ |
| 4876 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4877 | /* BEGIN_CASE */ |
| 4878 | void test_derive_invalid_key_derivation_state( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4879 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4880 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4881 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 4882 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4883 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4884 | unsigned char input1[] = "Input 1"; |
| 4885 | size_t input1_length = sizeof( input1 ); |
| 4886 | unsigned char input2[] = "Input 2"; |
| 4887 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4888 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 4889 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 4890 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4891 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 4892 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4893 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4894 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4895 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4896 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4897 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4898 | psa_set_key_algorithm( &attributes, alg ); |
| 4899 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4900 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 4901 | PSA_ASSERT( psa_import_key( &attributes, |
| 4902 | key_data, sizeof( key_data ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4903 | &key ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4904 | |
| 4905 | /* valid key derivation */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4906 | if( !setup_key_derivation_wrap( &operation, key, alg, |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4907 | input1, input1_length, |
| 4908 | input2, input2_length, |
| 4909 | capacity ) ) |
| 4910 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4911 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4912 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 4913 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4914 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4915 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4916 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4917 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4918 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 4919 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4920 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4921 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4922 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4923 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4924 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4925 | } |
| 4926 | /* END_CASE */ |
| 4927 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4928 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 4929 | void test_derive_invalid_key_derivation_tests( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4930 | { |
| 4931 | uint8_t output_buffer[16]; |
| 4932 | size_t buffer_size = 16; |
| 4933 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4934 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4935 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4936 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 4937 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4938 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4939 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4940 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4941 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4942 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4943 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4944 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 4945 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 4946 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4947 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4948 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4949 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 4950 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 4951 | |
| 4952 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4953 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 4954 | } |
| 4955 | /* END_CASE */ |
| 4956 | |
| 4957 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4958 | void derive_output( int alg_arg, |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4959 | int step1_arg, data_t *input1, |
| 4960 | int step2_arg, data_t *input2, |
| 4961 | int step3_arg, data_t *input3, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4962 | int requested_capacity_arg, |
| 4963 | data_t *expected_output1, |
| 4964 | data_t *expected_output2 ) |
| 4965 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4966 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4967 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg}; |
| 4968 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4969 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 4970 | MBEDTLS_SVC_KEY_ID_INIT, |
| 4971 | MBEDTLS_SVC_KEY_ID_INIT }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4972 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 4973 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4974 | uint8_t *expected_outputs[2] = |
| 4975 | {expected_output1->x, expected_output2->x}; |
| 4976 | size_t output_sizes[2] = |
| 4977 | {expected_output1->len, expected_output2->len}; |
| 4978 | size_t output_buffer_size = 0; |
| 4979 | uint8_t *output_buffer = NULL; |
| 4980 | size_t expected_capacity; |
| 4981 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4982 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4983 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 4984 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4985 | |
| 4986 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 4987 | { |
| 4988 | if( output_sizes[i] > output_buffer_size ) |
| 4989 | output_buffer_size = output_sizes[i]; |
| 4990 | if( output_sizes[i] == 0 ) |
| 4991 | expected_outputs[i] = NULL; |
| 4992 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4993 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4994 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4995 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 4996 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 4997 | psa_set_key_algorithm( &attributes, alg ); |
| 4998 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 4999 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5000 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5001 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 5002 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 5003 | requested_capacity ) ); |
| 5004 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5005 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5006 | switch( steps[i] ) |
| 5007 | { |
| 5008 | case 0: |
| 5009 | break; |
| 5010 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 5011 | PSA_ASSERT( psa_import_key( &attributes, |
| 5012 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5013 | &keys[i] ) ); |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5014 | PSA_ASSERT( psa_key_derivation_input_key( |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5015 | &operation, steps[i], keys[i] ) ); |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5016 | break; |
| 5017 | default: |
| 5018 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 5019 | &operation, steps[i], |
| 5020 | inputs[i]->x, inputs[i]->len ) ); |
| 5021 | break; |
| 5022 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 5023 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 5024 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5025 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5026 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5027 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5028 | expected_capacity = requested_capacity; |
| 5029 | |
| 5030 | /* Expansion phase. */ |
| 5031 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 5032 | { |
| 5033 | /* Read some bytes. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5034 | status = psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5035 | output_buffer, output_sizes[i] ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5036 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 5037 | { |
| 5038 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 5039 | TEST_ASSERT( status == PSA_SUCCESS || |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5040 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5041 | continue; |
| 5042 | } |
| 5043 | else if( expected_capacity == 0 || |
| 5044 | output_sizes[i] > expected_capacity ) |
| 5045 | { |
| 5046 | /* Capacity exceeded. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5047 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5048 | expected_capacity = 0; |
| 5049 | continue; |
| 5050 | } |
| 5051 | /* Success. Check the read data. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5052 | PSA_ASSERT( status ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5053 | if( output_sizes[i] != 0 ) |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5054 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 5055 | expected_outputs[i], output_sizes[i] ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5056 | /* Check the operation status. */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5057 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5058 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5059 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5060 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5061 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5062 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5063 | |
| 5064 | exit: |
| 5065 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5066 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5067 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 5068 | psa_destroy_key( keys[i] ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5069 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 5070 | } |
| 5071 | /* END_CASE */ |
| 5072 | |
| 5073 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5074 | void derive_full( int alg_arg, |
| 5075 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 5076 | data_t *input1, |
| 5077 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5078 | int requested_capacity_arg ) |
| 5079 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5080 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5081 | psa_algorithm_t alg = alg_arg; |
| 5082 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5083 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5084 | unsigned char output_buffer[16]; |
| 5085 | size_t expected_capacity = requested_capacity; |
| 5086 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5087 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5088 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5089 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5090 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5091 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5092 | psa_set_key_algorithm( &attributes, alg ); |
| 5093 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5094 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5095 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5096 | &key ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5097 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5098 | if( !setup_key_derivation_wrap( &operation, key, alg, |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 5099 | input1->x, input1->len, |
| 5100 | input2->x, input2->len, |
| 5101 | requested_capacity ) ) |
| 5102 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 5103 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5104 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5105 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5106 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5107 | |
| 5108 | /* Expansion phase. */ |
| 5109 | while( current_capacity > 0 ) |
| 5110 | { |
| 5111 | size_t read_size = sizeof( output_buffer ); |
| 5112 | if( read_size > current_capacity ) |
| 5113 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5114 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5115 | output_buffer, |
| 5116 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5117 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5118 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5119 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5120 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5121 | } |
| 5122 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5123 | /* Check that the operation refuses to go over capacity. */ |
| 5124 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5125 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5126 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5127 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5128 | |
| 5129 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5130 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5131 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5132 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 5133 | } |
| 5134 | /* END_CASE */ |
| 5135 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 5136 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5137 | void derive_key_exercise( int alg_arg, |
| 5138 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 5139 | data_t *input1, |
| 5140 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5141 | int derived_type_arg, |
| 5142 | int derived_bits_arg, |
| 5143 | int derived_usage_arg, |
| 5144 | int derived_alg_arg ) |
| 5145 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5146 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5147 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5148 | psa_algorithm_t alg = alg_arg; |
| 5149 | psa_key_type_t derived_type = derived_type_arg; |
| 5150 | size_t derived_bits = derived_bits_arg; |
| 5151 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 5152 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 5153 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5154 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5155 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5156 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5157 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5158 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5159 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5160 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5161 | psa_set_key_algorithm( &attributes, alg ); |
| 5162 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5163 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5164 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5165 | |
| 5166 | /* Derive a key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5167 | if ( setup_key_derivation_wrap( &operation, base_key, alg, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 5168 | input1->x, input1->len, |
| 5169 | input2->x, input2->len, capacity ) ) |
| 5170 | goto exit; |
| 5171 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5172 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 5173 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 5174 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5175 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5176 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5177 | &derived_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5178 | |
| 5179 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5180 | PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5181 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 5182 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5183 | |
| 5184 | /* Exercise the derived key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5185 | if( ! exercise_key( derived_key, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5186 | goto exit; |
| 5187 | |
| 5188 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5189 | /* |
| 5190 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5191 | * thus reset them as required. |
| 5192 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5193 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5194 | |
| 5195 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5196 | psa_destroy_key( base_key ); |
| 5197 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5198 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5199 | } |
| 5200 | /* END_CASE */ |
| 5201 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 5202 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5203 | void derive_key_export( int alg_arg, |
| 5204 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 5205 | data_t *input1, |
| 5206 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5207 | int bytes1_arg, |
| 5208 | int bytes2_arg ) |
| 5209 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5210 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5211 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5212 | psa_algorithm_t alg = alg_arg; |
| 5213 | size_t bytes1 = bytes1_arg; |
| 5214 | size_t bytes2 = bytes2_arg; |
| 5215 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5216 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5217 | uint8_t *output_buffer = NULL; |
| 5218 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5219 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5220 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5221 | size_t length; |
| 5222 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5223 | ASSERT_ALLOC( output_buffer, capacity ); |
| 5224 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5225 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5226 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5227 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 5228 | psa_set_key_algorithm( &base_attributes, alg ); |
| 5229 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5230 | 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] | 5231 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5232 | |
| 5233 | /* Derive some material and output it. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5234 | if( !setup_key_derivation_wrap( &operation, base_key, alg, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 5235 | input1->x, input1->len, |
| 5236 | input2->x, input2->len, capacity ) ) |
| 5237 | goto exit; |
| 5238 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5239 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5240 | output_buffer, |
| 5241 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5242 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5243 | |
| 5244 | /* Derive the same output again, but this time store it in key objects. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5245 | if( !setup_key_derivation_wrap( &operation, base_key, alg, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 5246 | input1->x, input1->len, |
| 5247 | input2->x, input2->len, capacity ) ) |
| 5248 | goto exit; |
| 5249 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5250 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 5251 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 5252 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5253 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5254 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5255 | &derived_key ) ); |
| 5256 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5257 | export_buffer, bytes1, |
| 5258 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5259 | TEST_EQUAL( length, bytes1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5260 | PSA_ASSERT( psa_destroy_key( derived_key ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5261 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5262 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5263 | &derived_key ) ); |
| 5264 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5265 | export_buffer + bytes1, bytes2, |
| 5266 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5267 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5268 | |
| 5269 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5270 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 5271 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5272 | |
| 5273 | exit: |
| 5274 | mbedtls_free( output_buffer ); |
| 5275 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5276 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5277 | psa_destroy_key( base_key ); |
| 5278 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5279 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 5280 | } |
| 5281 | /* END_CASE */ |
| 5282 | |
| 5283 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 5284 | void derive_key( int alg_arg, |
| 5285 | data_t *key_data, data_t *input1, data_t *input2, |
| 5286 | int type_arg, int bits_arg, |
| 5287 | int expected_status_arg ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5288 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5289 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5290 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5291 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 5292 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5293 | size_t bits = bits_arg; |
| 5294 | psa_status_t expected_status = expected_status_arg; |
| 5295 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5296 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5297 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5298 | |
| 5299 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5300 | |
| 5301 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 5302 | psa_set_key_algorithm( &base_attributes, alg ); |
| 5303 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 5304 | 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] | 5305 | &base_key ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5306 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5307 | if( !setup_key_derivation_wrap( &operation, base_key, alg, |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5308 | input1->x, input1->len, |
| 5309 | input2->x, input2->len, SIZE_MAX ) ) |
| 5310 | goto exit; |
| 5311 | |
| 5312 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 5313 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 5314 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5315 | psa_set_key_bits( &derived_attributes, bits ); |
| 5316 | TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5317 | &derived_key ), |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5318 | expected_status ); |
| 5319 | |
| 5320 | exit: |
| 5321 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5322 | psa_destroy_key( base_key ); |
| 5323 | psa_destroy_key( derived_key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 5324 | PSA_DONE( ); |
| 5325 | } |
| 5326 | /* END_CASE */ |
| 5327 | |
| 5328 | /* BEGIN_CASE */ |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame^] | 5329 | void derive_large_key( int alg_arg, |
| 5330 | data_t *key_data, data_t *input1, data_t *input2, |
| 5331 | int type_arg, int bits_arg, |
| 5332 | int expected_status_arg ) |
| 5333 | { |
| 5334 | size_t key_bytes = PSA_BITS_TO_BYTES(bits_arg); |
| 5335 | uint8_t* buffer = NULL; |
| 5336 | |
| 5337 | /* Check that the target running this test can accomodate large |
| 5338 | * keys on its heap, before calling the actual generate_key test */ |
| 5339 | ASSERT_ALLOC_WEAK(buffer, key_bytes); |
| 5340 | mbedtls_free( buffer ); |
| 5341 | |
| 5342 | test_derive_key( alg_arg, key_data, input1, input2, type_arg, bits_arg, expected_status_arg ); |
| 5343 | exit: |
| 5344 | return; |
| 5345 | } |
| 5346 | /* END_CASE */ |
| 5347 | |
| 5348 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5349 | void key_agreement_setup( int alg_arg, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 5350 | int our_key_type_arg, int our_key_alg_arg, |
| 5351 | data_t *our_key_data, data_t *peer_key_data, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5352 | int expected_status_arg ) |
| 5353 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5354 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5355 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5356 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5357 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5358 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5359 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5360 | psa_status_t expected_status = expected_status_arg; |
| 5361 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5362 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5363 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5364 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5365 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 5366 | psa_set_key_algorithm( &attributes, our_key_alg ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5367 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5368 | PSA_ASSERT( psa_import_key( &attributes, |
| 5369 | our_key_data->x, our_key_data->len, |
| 5370 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5371 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5372 | /* The tests currently include inputs that should fail at either step. |
| 5373 | * Test cases that fail at the setup step should be changed to call |
| 5374 | * key_derivation_setup instead, and this function should be renamed |
| 5375 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5376 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5377 | if( status == PSA_SUCCESS ) |
| 5378 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5379 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 5380 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 5381 | our_key, |
| 5382 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 5383 | expected_status ); |
| 5384 | } |
| 5385 | else |
| 5386 | { |
| 5387 | TEST_ASSERT( status == expected_status ); |
| 5388 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5389 | |
| 5390 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5391 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5392 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5393 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 5394 | } |
| 5395 | /* END_CASE */ |
| 5396 | |
| 5397 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5398 | void raw_key_agreement( int alg_arg, |
| 5399 | int our_key_type_arg, data_t *our_key_data, |
| 5400 | data_t *peer_key_data, |
| 5401 | data_t *expected_output ) |
| 5402 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5403 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5404 | psa_algorithm_t alg = alg_arg; |
| 5405 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5406 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5407 | unsigned char *output = NULL; |
| 5408 | size_t output_length = ~0; |
| 5409 | |
| 5410 | ASSERT_ALLOC( output, expected_output->len ); |
| 5411 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5412 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5413 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5414 | psa_set_key_algorithm( &attributes, alg ); |
| 5415 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5416 | PSA_ASSERT( psa_import_key( &attributes, |
| 5417 | our_key_data->x, our_key_data->len, |
| 5418 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5419 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 5420 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 5421 | peer_key_data->x, peer_key_data->len, |
| 5422 | output, expected_output->len, |
| 5423 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5424 | ASSERT_COMPARE( output, output_length, |
| 5425 | expected_output->x, expected_output->len ); |
| 5426 | |
| 5427 | exit: |
| 5428 | mbedtls_free( output ); |
| 5429 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5430 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 5431 | } |
| 5432 | /* END_CASE */ |
| 5433 | |
| 5434 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5435 | void key_agreement_capacity( int alg_arg, |
| 5436 | int our_key_type_arg, data_t *our_key_data, |
| 5437 | data_t *peer_key_data, |
| 5438 | int expected_capacity_arg ) |
| 5439 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5440 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5441 | psa_algorithm_t alg = alg_arg; |
| 5442 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5443 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5444 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5445 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5446 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5447 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5448 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5449 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5450 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5451 | psa_set_key_algorithm( &attributes, alg ); |
| 5452 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5453 | PSA_ASSERT( psa_import_key( &attributes, |
| 5454 | our_key_data->x, our_key_data->len, |
| 5455 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5456 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5457 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5458 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 5459 | &operation, |
| 5460 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 5461 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5462 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 5463 | { |
| 5464 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5465 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5466 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5467 | NULL, 0 ) ); |
| 5468 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5469 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5470 | /* Test the advertized capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 5471 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5472 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5473 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5474 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5475 | /* Test the actual capacity by reading the output. */ |
| 5476 | while( actual_capacity > sizeof( output ) ) |
| 5477 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5478 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5479 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5480 | actual_capacity -= sizeof( output ); |
| 5481 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5482 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5483 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5484 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 5485 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 5486 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5487 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5488 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5489 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5490 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5491 | } |
| 5492 | /* END_CASE */ |
| 5493 | |
| 5494 | /* BEGIN_CASE */ |
| 5495 | void key_agreement_output( int alg_arg, |
| 5496 | int our_key_type_arg, data_t *our_key_data, |
| 5497 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5498 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5499 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5500 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5501 | psa_algorithm_t alg = alg_arg; |
| 5502 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5503 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5504 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5505 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5506 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5507 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 5508 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5509 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5510 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5511 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5512 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 5513 | psa_set_key_algorithm( &attributes, alg ); |
| 5514 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5515 | PSA_ASSERT( psa_import_key( &attributes, |
| 5516 | our_key_data->x, our_key_data->len, |
| 5517 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5518 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5519 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5520 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 5521 | &operation, |
| 5522 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 5523 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5524 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 5525 | { |
| 5526 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5527 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 5528 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 5529 | NULL, 0 ) ); |
| 5530 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5531 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5532 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5533 | actual_output, |
| 5534 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5535 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 5536 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5537 | if( expected_output2->len != 0 ) |
| 5538 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5539 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5540 | actual_output, |
| 5541 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5542 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 5543 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 5544 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5545 | |
| 5546 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5547 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5548 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5549 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 5550 | mbedtls_free( actual_output ); |
| 5551 | } |
| 5552 | /* END_CASE */ |
| 5553 | |
| 5554 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5555 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5556 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5557 | size_t bytes = bytes_arg; |
| 5558 | const unsigned char trail[] = "don't overwrite me"; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5559 | unsigned char *output = NULL; |
| 5560 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5561 | size_t i; |
| 5562 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5563 | |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 5564 | TEST_ASSERT( bytes_arg >= 0 ); |
| 5565 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5566 | ASSERT_ALLOC( output, bytes + sizeof( trail ) ); |
| 5567 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5568 | memcpy( output + bytes, trail, sizeof( trail ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5569 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5570 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5571 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5572 | /* Run several times, to ensure that every output byte will be |
| 5573 | * nonzero at least once with overwhelming probability |
| 5574 | * (2^(-8*number_of_runs)). */ |
| 5575 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5576 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 5577 | if( bytes != 0 ) |
| 5578 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5579 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5580 | |
| 5581 | /* Check that no more than bytes have been overwritten */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 5582 | ASSERT_COMPARE( output + bytes, sizeof( trail ), |
| 5583 | trail, sizeof( trail ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5584 | |
| 5585 | for( i = 0; i < bytes; i++ ) |
| 5586 | { |
| 5587 | if( output[i] != 0 ) |
| 5588 | ++changed[i]; |
| 5589 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5590 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5591 | |
| 5592 | /* Check that every byte was changed to nonzero at least once. This |
| 5593 | * validates that psa_generate_random is overwriting every byte of |
| 5594 | * the output buffer. */ |
| 5595 | for( i = 0; i < bytes; i++ ) |
| 5596 | { |
| 5597 | TEST_ASSERT( changed[i] != 0 ); |
| 5598 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5599 | |
| 5600 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5601 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 5602 | mbedtls_free( output ); |
| 5603 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 5604 | } |
| 5605 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5606 | |
| 5607 | /* BEGIN_CASE */ |
| 5608 | void generate_key( int type_arg, |
| 5609 | int bits_arg, |
| 5610 | int usage_arg, |
| 5611 | int alg_arg, |
| 5612 | int expected_status_arg ) |
| 5613 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5614 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5615 | psa_key_type_t type = type_arg; |
| 5616 | psa_key_usage_t usage = usage_arg; |
| 5617 | size_t bits = bits_arg; |
| 5618 | psa_algorithm_t alg = alg_arg; |
| 5619 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5620 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5621 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5622 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5623 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5624 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5625 | psa_set_key_usage_flags( &attributes, usage ); |
| 5626 | psa_set_key_algorithm( &attributes, alg ); |
| 5627 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5628 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5629 | |
| 5630 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5631 | TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5632 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 5633 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5634 | |
| 5635 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5636 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5637 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 5638 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5639 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 5640 | /* Do something with the key according to its type and permitted usage. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5641 | if( ! exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 5642 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5643 | |
| 5644 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5645 | /* |
| 5646 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5647 | * thus reset them as required. |
| 5648 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5649 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5650 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5651 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5652 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 5653 | } |
| 5654 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 5655 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame^] | 5656 | /* BEGIN_CASE */ |
| 5657 | void generate_large_key( int type_arg, |
| 5658 | int bits_arg, |
| 5659 | int usage_arg, |
| 5660 | int alg_arg, |
| 5661 | int expected_status_arg ) |
| 5662 | { |
| 5663 | size_t key_bytes = PSA_BITS_TO_BYTES(bits_arg); |
| 5664 | uint8_t* buffer = NULL; |
| 5665 | |
| 5666 | /* Check that the target running this test can accomodate large |
| 5667 | * keys on its heap, before calling the actual generate_key test */ |
| 5668 | ASSERT_ALLOC_WEAK(buffer, key_bytes); |
| 5669 | mbedtls_free( buffer ); |
| 5670 | |
| 5671 | test_generate_key( type_arg, bits_arg, usage_arg, alg_arg, expected_status_arg ); |
| 5672 | exit: |
| 5673 | return; |
| 5674 | } |
| 5675 | /* END_CASE */ |
| 5676 | |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5677 | /* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */ |
| 5678 | void generate_key_rsa( int bits_arg, |
| 5679 | data_t *e_arg, |
| 5680 | int expected_status_arg ) |
| 5681 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5682 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 5683 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5684 | size_t bits = bits_arg; |
| 5685 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 5686 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 5687 | psa_status_t expected_status = expected_status_arg; |
| 5688 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5689 | uint8_t *exported = NULL; |
| 5690 | size_t exported_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5691 | PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5692 | size_t exported_length = SIZE_MAX; |
| 5693 | uint8_t *e_read_buffer = NULL; |
| 5694 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 5695 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5696 | size_t e_read_length = SIZE_MAX; |
| 5697 | |
| 5698 | if( e_arg->len == 0 || |
| 5699 | ( e_arg->len == 3 && |
| 5700 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 5701 | { |
| 5702 | is_default_public_exponent = 1; |
| 5703 | e_read_size = 0; |
| 5704 | } |
| 5705 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 5706 | ASSERT_ALLOC( exported, exported_size ); |
| 5707 | |
| 5708 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5709 | |
| 5710 | psa_set_key_usage_flags( &attributes, usage ); |
| 5711 | psa_set_key_algorithm( &attributes, alg ); |
| 5712 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 5713 | e_arg->x, e_arg->len ) ); |
| 5714 | psa_set_key_bits( &attributes, bits ); |
| 5715 | |
| 5716 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5717 | TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5718 | if( expected_status != PSA_SUCCESS ) |
| 5719 | goto exit; |
| 5720 | |
| 5721 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5722 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5723 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 5724 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 5725 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 5726 | e_read_buffer, e_read_size, |
| 5727 | &e_read_length ) ); |
| 5728 | if( is_default_public_exponent ) |
| 5729 | TEST_EQUAL( e_read_length, 0 ); |
| 5730 | else |
| 5731 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 5732 | |
| 5733 | /* Do something with the key according to its type and permitted usage. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5734 | if( ! exercise_key( key, usage, alg ) ) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5735 | goto exit; |
| 5736 | |
| 5737 | /* Export the key and check the public exponent. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5738 | PSA_ASSERT( psa_export_public_key( key, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5739 | exported, exported_size, |
| 5740 | &exported_length ) ); |
| 5741 | { |
| 5742 | uint8_t *p = exported; |
| 5743 | uint8_t *end = exported + exported_length; |
| 5744 | size_t len; |
| 5745 | /* RSAPublicKey ::= SEQUENCE { |
| 5746 | * modulus INTEGER, -- n |
| 5747 | * publicExponent INTEGER } -- e |
| 5748 | */ |
| 5749 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5750 | MBEDTLS_ASN1_SEQUENCE | |
| 5751 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5752 | TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
| 5753 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 5754 | MBEDTLS_ASN1_INTEGER ) ); |
| 5755 | if( len >= 1 && p[0] == 0 ) |
| 5756 | { |
| 5757 | ++p; |
| 5758 | --len; |
| 5759 | } |
| 5760 | if( e_arg->len == 0 ) |
| 5761 | { |
| 5762 | TEST_EQUAL( len, 3 ); |
| 5763 | TEST_EQUAL( p[0], 1 ); |
| 5764 | TEST_EQUAL( p[1], 0 ); |
| 5765 | TEST_EQUAL( p[2], 1 ); |
| 5766 | } |
| 5767 | else |
| 5768 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 5769 | } |
| 5770 | |
| 5771 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5772 | /* |
| 5773 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 5774 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 5775 | */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5776 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5777 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5778 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5779 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 5780 | mbedtls_free( e_read_buffer ); |
| 5781 | mbedtls_free( exported ); |
| 5782 | } |
| 5783 | /* END_CASE */ |
| 5784 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5785 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5786 | void persistent_key_load_key_from_storage( data_t *data, |
| 5787 | int type_arg, int bits_arg, |
| 5788 | int usage_flags_arg, int alg_arg, |
| 5789 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5790 | { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 5791 | 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] | 5792 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5793 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5794 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5795 | psa_key_type_t type = type_arg; |
| 5796 | size_t bits = bits_arg; |
| 5797 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 5798 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5799 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5800 | unsigned char *first_export = NULL; |
| 5801 | unsigned char *second_export = NULL; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 5802 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5803 | size_t first_exported_length; |
| 5804 | size_t second_exported_length; |
| 5805 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5806 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 5807 | { |
| 5808 | ASSERT_ALLOC( first_export, export_size ); |
| 5809 | ASSERT_ALLOC( second_export, export_size ); |
| 5810 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5811 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5812 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5813 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 5814 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5815 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 5816 | psa_set_key_algorithm( &attributes, alg ); |
| 5817 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 5818 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5819 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5820 | switch( generation_method ) |
| 5821 | { |
| 5822 | case IMPORT_KEY: |
| 5823 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5824 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5825 | &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5826 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5827 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5828 | case GENERATE_KEY: |
| 5829 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5830 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5831 | break; |
| 5832 | |
| 5833 | case DERIVE_KEY: |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 5834 | #if PSA_WANT_ALG_HKDF && PSA_WANT_ALG_SHA_256 |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5835 | { |
| 5836 | /* Create base key */ |
| 5837 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 5838 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5839 | psa_set_key_usage_flags( &base_attributes, |
| 5840 | PSA_KEY_USAGE_DERIVE ); |
| 5841 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 5842 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5843 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 5844 | data->x, data->len, |
| 5845 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5846 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5847 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5848 | PSA_ASSERT( psa_key_derivation_input_key( |
| 5849 | &operation, |
| 5850 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5851 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5852 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5853 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 5854 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 5855 | &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5856 | &key ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5857 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5858 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5859 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5860 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 5861 | #else |
| 5862 | TEST_ASSUME( ! "KDF not supported in this configuration" ); |
| 5863 | #endif |
| 5864 | break; |
| 5865 | |
| 5866 | default: |
| 5867 | TEST_ASSERT( ! "generation_method not implemented in test" ); |
| 5868 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5869 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5870 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5871 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5872 | /* Export the key if permitted by the key policy. */ |
| 5873 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 5874 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5875 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5876 | first_export, export_size, |
| 5877 | &first_exported_length ) ); |
| 5878 | if( generation_method == IMPORT_KEY ) |
| 5879 | ASSERT_COMPARE( data->x, data->len, |
| 5880 | first_export, first_exported_length ); |
| 5881 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5882 | |
| 5883 | /* Shutdown and restart */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5884 | PSA_ASSERT( psa_purge_key( key ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5885 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5886 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5887 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5888 | /* Check key slot still contains key data */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5889 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 5890 | TEST_ASSERT( mbedtls_svc_key_id_equal( |
| 5891 | psa_get_key_id( &attributes ), key_id ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5892 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 5893 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 5894 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 5895 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 5896 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags ); |
| 5897 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5898 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5899 | /* Export the key again if permitted by the key policy. */ |
| 5900 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5901 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5902 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5903 | second_export, export_size, |
| 5904 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5905 | ASSERT_COMPARE( first_export, first_exported_length, |
| 5906 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5907 | } |
| 5908 | |
| 5909 | /* Do something with the key according to its type and permitted usage. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5910 | if( ! exercise_key( key, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 5911 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5912 | |
| 5913 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5914 | /* |
| 5915 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5916 | * thus reset them as required. |
| 5917 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5918 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5919 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5920 | mbedtls_free( first_export ); |
| 5921 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 5922 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 5923 | psa_destroy_key( base_key ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5924 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5925 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 5926 | } |
| 5927 | /* END_CASE */ |