| 1 | /* |
| 2 | * PSA crypto layer on top of Mbed TLS crypto |
| 3 | */ |
| 4 | /* |
| 5 | * Copyright The Mbed TLS Contributors |
| 6 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 7 | */ |
| 8 | |
| 9 | #include "common.h" |
| 10 | #include "psa_crypto_core_common.h" |
| 11 | |
| 12 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 13 | |
| 14 | #if defined(MBEDTLS_PSA_CRYPTO_CONFIG) |
| 15 | #include "check_crypto_config.h" |
| 16 | #endif |
| 17 | |
| 18 | #include "psa/crypto.h" |
| 19 | #include "psa/crypto_values.h" |
| 20 | |
| 21 | #include "psa_crypto_cipher.h" |
| 22 | #include "psa_crypto_core.h" |
| 23 | #include "psa_crypto_invasive.h" |
| 24 | #include "psa_crypto_driver_wrappers.h" |
| 25 | #include "psa_crypto_driver_wrappers_no_static.h" |
| 26 | #include "psa_crypto_ecp.h" |
| 27 | #include "psa_crypto_ffdh.h" |
| 28 | #include "psa_crypto_hash.h" |
| 29 | #include "psa_crypto_mac.h" |
| 30 | #include "psa_crypto_rsa.h" |
| 31 | #include "psa_crypto_ecp.h" |
| 32 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 33 | #include "psa_crypto_se.h" |
| 34 | #endif |
| 35 | #include "psa_crypto_slot_management.h" |
| 36 | /* Include internal declarations that are useful for implementing persistently |
| 37 | * stored keys. */ |
| 38 | #include "psa_crypto_storage.h" |
| 39 | |
| 40 | #include "psa_crypto_random.h" |
| 41 | #include "psa_crypto_random_impl.h" |
| 42 | |
| 43 | #include <stdlib.h> |
| 44 | #include <string.h> |
| 45 | #include "mbedtls/platform.h" |
| 46 | |
| 47 | #include "mbedtls/aes.h" |
| 48 | #include "mbedtls/asn1.h" |
| 49 | #include "mbedtls/asn1write.h" |
| 50 | #include "mbedtls/bignum.h" |
| 51 | #include "mbedtls/camellia.h" |
| 52 | #include "mbedtls/chacha20.h" |
| 53 | #include "mbedtls/chachapoly.h" |
| 54 | #include "mbedtls/cipher.h" |
| 55 | #include "mbedtls/ccm.h" |
| 56 | #include "mbedtls/cmac.h" |
| 57 | #include "mbedtls/constant_time.h" |
| 58 | #include "mbedtls/des.h" |
| 59 | #include "mbedtls/ecdh.h" |
| 60 | #include "mbedtls/ecp.h" |
| 61 | #include "mbedtls/entropy.h" |
| 62 | #include "mbedtls/error.h" |
| 63 | #include "mbedtls/gcm.h" |
| 64 | #include "mbedtls/md5.h" |
| 65 | #include "mbedtls/pk.h" |
| 66 | #include "pk_wrap.h" |
| 67 | #include "mbedtls/platform_util.h" |
| 68 | #include "mbedtls/error.h" |
| 69 | #include "mbedtls/ripemd160.h" |
| 70 | #include "mbedtls/rsa.h" |
| 71 | #include "mbedtls/sha1.h" |
| 72 | #include "mbedtls/sha256.h" |
| 73 | #include "mbedtls/sha512.h" |
| 74 | #include "mbedtls/psa_util.h" |
| 75 | #include "mbedtls/threading.h" |
| 76 | |
| 77 | #include "constant_time_internal.h" |
| 78 | |
| 79 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \ |
| 80 | defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \ |
| 81 | defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND) |
| 82 | #define BUILTIN_ALG_ANY_HKDF 1 |
| 83 | #endif |
| 84 | |
| 85 | /****************************************************************/ |
| 86 | /* Global data, support functions and library management */ |
| 87 | /****************************************************************/ |
| 88 | |
| 89 | static int key_type_is_raw_bytes(psa_key_type_t type) |
| 90 | { |
| 91 | return PSA_KEY_TYPE_IS_UNSTRUCTURED(type); |
| 92 | } |
| 93 | |
| 94 | /* Values for psa_global_data_t::rng_state */ |
| 95 | #define RNG_NOT_INITIALIZED 0 |
| 96 | #define RNG_INITIALIZED 1 |
| 97 | #define RNG_SEEDED 2 |
| 98 | |
| 99 | /* IDs for PSA crypto subsystems. Starts at 1 to catch potential uninitialized |
| 100 | * variables as arguments. */ |
| 101 | typedef enum { |
| 102 | PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS = 1, |
| 103 | PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS, |
| 104 | PSA_CRYPTO_SUBSYSTEM_RNG, |
| 105 | PSA_CRYPTO_SUBSYSTEM_TRANSACTION, |
| 106 | } mbedtls_psa_crypto_subsystem; |
| 107 | |
| 108 | /* Initialization flags for global_data::initialized */ |
| 109 | #define PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED 0x01 |
| 110 | #define PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED 0x02 |
| 111 | #define PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED 0x04 |
| 112 | |
| 113 | #define PSA_CRYPTO_SUBSYSTEM_ALL_INITIALISED ( \ |
| 114 | PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED | \ |
| 115 | PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED | \ |
| 116 | PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED) |
| 117 | |
| 118 | typedef struct { |
| 119 | uint8_t initialized; |
| 120 | uint8_t rng_state; |
| 121 | mbedtls_psa_random_context_t rng; |
| 122 | } psa_global_data_t; |
| 123 | |
| 124 | static psa_global_data_t global_data; |
| 125 | |
| 126 | static uint8_t psa_get_initialized(void) |
| 127 | { |
| 128 | uint8_t initialized; |
| 129 | |
| 130 | #if defined(MBEDTLS_THREADING_C) |
| 131 | mbedtls_mutex_lock(&mbedtls_threading_psa_rngdata_mutex); |
| 132 | #endif /* defined(MBEDTLS_THREADING_C) */ |
| 133 | |
| 134 | initialized = global_data.rng_state == RNG_SEEDED; |
| 135 | |
| 136 | #if defined(MBEDTLS_THREADING_C) |
| 137 | mbedtls_mutex_unlock(&mbedtls_threading_psa_rngdata_mutex); |
| 138 | #endif /* defined(MBEDTLS_THREADING_C) */ |
| 139 | |
| 140 | #if defined(MBEDTLS_THREADING_C) |
| 141 | mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex); |
| 142 | #endif /* defined(MBEDTLS_THREADING_C) */ |
| 143 | |
| 144 | initialized = |
| 145 | (initialized && (global_data.initialized == PSA_CRYPTO_SUBSYSTEM_ALL_INITIALISED)); |
| 146 | |
| 147 | #if defined(MBEDTLS_THREADING_C) |
| 148 | mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex); |
| 149 | #endif /* defined(MBEDTLS_THREADING_C) */ |
| 150 | |
| 151 | return initialized; |
| 152 | } |
| 153 | |
| 154 | static uint8_t psa_get_drivers_initialized(void) |
| 155 | { |
| 156 | uint8_t initialized; |
| 157 | |
| 158 | #if defined(MBEDTLS_THREADING_C) |
| 159 | mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex); |
| 160 | #endif /* defined(MBEDTLS_THREADING_C) */ |
| 161 | |
| 162 | initialized = (global_data.initialized & PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED) != 0; |
| 163 | |
| 164 | #if defined(MBEDTLS_THREADING_C) |
| 165 | mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex); |
| 166 | #endif /* defined(MBEDTLS_THREADING_C) */ |
| 167 | |
| 168 | return initialized; |
| 169 | } |
| 170 | |
| 171 | #define GUARD_MODULE_INITIALIZED \ |
| 172 | if (psa_get_initialized() == 0) \ |
| 173 | return PSA_ERROR_BAD_STATE; |
| 174 | |
| 175 | #if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) |
| 176 | |
| 177 | /* Declare a local copy of an input buffer and a variable that will be used |
| 178 | * to store a pointer to the start of the buffer. |
| 179 | * |
| 180 | * Note: This macro must be called before any operations which may jump to |
| 181 | * the exit label, so that the local input copy object is safe to be freed. |
| 182 | * |
| 183 | * Assumptions: |
| 184 | * - input is the name of a pointer to the buffer to be copied |
| 185 | * - The name LOCAL_INPUT_COPY_OF_input is unused in the current scope |
| 186 | * - input_copy_name is a name that is unused in the current scope |
| 187 | */ |
| 188 | #define LOCAL_INPUT_DECLARE(input, input_copy_name) \ |
| 189 | psa_crypto_local_input_t LOCAL_INPUT_COPY_OF_##input = PSA_CRYPTO_LOCAL_INPUT_INIT; \ |
| 190 | const uint8_t *input_copy_name = NULL; |
| 191 | |
| 192 | /* Allocate a copy of the buffer input and set the pointer input_copy to |
| 193 | * point to the start of the copy. |
| 194 | * |
| 195 | * Assumptions: |
| 196 | * - psa_status_t status exists |
| 197 | * - An exit label is declared |
| 198 | * - input is the name of a pointer to the buffer to be copied |
| 199 | * - LOCAL_INPUT_DECLARE(input, input_copy) has previously been called |
| 200 | */ |
| 201 | #define LOCAL_INPUT_ALLOC(input, length, input_copy) \ |
| 202 | status = psa_crypto_local_input_alloc(input, length, \ |
| 203 | &LOCAL_INPUT_COPY_OF_##input); \ |
| 204 | if (status != PSA_SUCCESS) { \ |
| 205 | goto exit; \ |
| 206 | } \ |
| 207 | input_copy = LOCAL_INPUT_COPY_OF_##input.buffer; |
| 208 | |
| 209 | /* Free the local input copy allocated previously by LOCAL_INPUT_ALLOC() |
| 210 | * |
| 211 | * Assumptions: |
| 212 | * - input_copy is the name of the input copy pointer set by LOCAL_INPUT_ALLOC() |
| 213 | * - input is the name of the original buffer that was copied |
| 214 | */ |
| 215 | #define LOCAL_INPUT_FREE(input, input_copy) \ |
| 216 | input_copy = NULL; \ |
| 217 | psa_crypto_local_input_free(&LOCAL_INPUT_COPY_OF_##input); |
| 218 | |
| 219 | /* Declare a local copy of an output buffer and a variable that will be used |
| 220 | * to store a pointer to the start of the buffer. |
| 221 | * |
| 222 | * Note: This macro must be called before any operations which may jump to |
| 223 | * the exit label, so that the local output copy object is safe to be freed. |
| 224 | * |
| 225 | * Assumptions: |
| 226 | * - output is the name of a pointer to the buffer to be copied |
| 227 | * - The name LOCAL_OUTPUT_COPY_OF_output is unused in the current scope |
| 228 | * - output_copy_name is a name that is unused in the current scope |
| 229 | */ |
| 230 | #define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \ |
| 231 | psa_crypto_local_output_t LOCAL_OUTPUT_COPY_OF_##output = PSA_CRYPTO_LOCAL_OUTPUT_INIT; \ |
| 232 | uint8_t *output_copy_name = NULL; |
| 233 | |
| 234 | /* Allocate a copy of the buffer output and set the pointer output_copy to |
| 235 | * point to the start of the copy. |
| 236 | * |
| 237 | * Assumptions: |
| 238 | * - psa_status_t status exists |
| 239 | * - An exit label is declared |
| 240 | * - output is the name of a pointer to the buffer to be copied |
| 241 | * - LOCAL_OUTPUT_DECLARE(output, output_copy) has previously been called |
| 242 | */ |
| 243 | #define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \ |
| 244 | status = psa_crypto_local_output_alloc(output, length, \ |
| 245 | &LOCAL_OUTPUT_COPY_OF_##output); \ |
| 246 | if (status != PSA_SUCCESS) { \ |
| 247 | goto exit; \ |
| 248 | } \ |
| 249 | output_copy = LOCAL_OUTPUT_COPY_OF_##output.buffer; |
| 250 | |
| 251 | /* Free the local output copy allocated previously by LOCAL_OUTPUT_ALLOC() |
| 252 | * after first copying back its contents to the original buffer. |
| 253 | * |
| 254 | * Assumptions: |
| 255 | * - psa_status_t status exists |
| 256 | * - output_copy is the name of the output copy pointer set by LOCAL_OUTPUT_ALLOC() |
| 257 | * - output is the name of the original buffer that was copied |
| 258 | */ |
| 259 | #define LOCAL_OUTPUT_FREE(output, output_copy) \ |
| 260 | output_copy = NULL; \ |
| 261 | do { \ |
| 262 | psa_status_t local_output_status; \ |
| 263 | local_output_status = psa_crypto_local_output_free(&LOCAL_OUTPUT_COPY_OF_##output); \ |
| 264 | if (local_output_status != PSA_SUCCESS) { \ |
| 265 | /* Since this error case is an internal error, it's more serious than \ |
| 266 | * any existing error code and so it's fine to overwrite the existing \ |
| 267 | * status. */ \ |
| 268 | status = local_output_status; \ |
| 269 | } \ |
| 270 | } while (0) |
| 271 | #else /* !MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS */ |
| 272 | #define LOCAL_INPUT_DECLARE(input, input_copy_name) \ |
| 273 | const uint8_t *input_copy_name = NULL; |
| 274 | #define LOCAL_INPUT_ALLOC(input, length, input_copy) \ |
| 275 | input_copy = input; |
| 276 | #define LOCAL_INPUT_FREE(input, input_copy) \ |
| 277 | input_copy = NULL; |
| 278 | #define LOCAL_OUTPUT_DECLARE(output, output_copy_name) \ |
| 279 | uint8_t *output_copy_name = NULL; |
| 280 | #define LOCAL_OUTPUT_ALLOC(output, length, output_copy) \ |
| 281 | output_copy = output; |
| 282 | #define LOCAL_OUTPUT_FREE(output, output_copy) \ |
| 283 | output_copy = NULL; |
| 284 | #endif /* !MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS */ |
| 285 | |
| 286 | |
| 287 | int psa_can_do_hash(psa_algorithm_t hash_alg) |
| 288 | { |
| 289 | (void) hash_alg; |
| 290 | return psa_get_drivers_initialized(); |
| 291 | } |
| 292 | |
| 293 | int psa_can_do_cipher(psa_key_type_t key_type, psa_algorithm_t cipher_alg) |
| 294 | { |
| 295 | (void) key_type; |
| 296 | (void) cipher_alg; |
| 297 | return psa_get_drivers_initialized(); |
| 298 | } |
| 299 | |
| 300 | |
| 301 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \ |
| 302 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \ |
| 303 | defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) |
| 304 | static int psa_is_dh_key_size_valid(size_t bits) |
| 305 | { |
| 306 | switch (bits) { |
| 307 | #if defined(PSA_WANT_DH_RFC7919_2048) |
| 308 | case 2048: |
| 309 | return 1; |
| 310 | #endif /* PSA_WANT_DH_RFC7919_2048 */ |
| 311 | #if defined(PSA_WANT_DH_RFC7919_3072) |
| 312 | case 3072: |
| 313 | return 1; |
| 314 | #endif /* PSA_WANT_DH_RFC7919_3072 */ |
| 315 | #if defined(PSA_WANT_DH_RFC7919_4096) |
| 316 | case 4096: |
| 317 | return 1; |
| 318 | #endif /* PSA_WANT_DH_RFC7919_4096 */ |
| 319 | #if defined(PSA_WANT_DH_RFC7919_6144) |
| 320 | case 6144: |
| 321 | return 1; |
| 322 | #endif /* PSA_WANT_DH_RFC7919_6144 */ |
| 323 | #if defined(PSA_WANT_DH_RFC7919_8192) |
| 324 | case 8192: |
| 325 | return 1; |
| 326 | #endif /* PSA_WANT_DH_RFC7919_8192 */ |
| 327 | default: |
| 328 | return 0; |
| 329 | } |
| 330 | } |
| 331 | #endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT || |
| 332 | MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY || |
| 333 | PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */ |
| 334 | |
| 335 | psa_status_t mbedtls_to_psa_error(int ret) |
| 336 | { |
| 337 | /* Mbed TLS error codes can combine a high-level error code and a |
| 338 | * low-level error code. The low-level error usually reflects the |
| 339 | * root cause better, so dispatch on that preferably. */ |
| 340 | int low_level_ret = -(-ret & 0x007f); |
| 341 | switch (low_level_ret != 0 ? low_level_ret : ret) { |
| 342 | case 0: |
| 343 | return PSA_SUCCESS; |
| 344 | |
| 345 | #if defined(MBEDTLS_AES_C) |
| 346 | case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH: |
| 347 | case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH: |
| 348 | return PSA_ERROR_NOT_SUPPORTED; |
| 349 | case MBEDTLS_ERR_AES_BAD_INPUT_DATA: |
| 350 | return PSA_ERROR_INVALID_ARGUMENT; |
| 351 | #endif |
| 352 | |
| 353 | #if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C) |
| 354 | case MBEDTLS_ERR_ASN1_OUT_OF_DATA: |
| 355 | case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG: |
| 356 | case MBEDTLS_ERR_ASN1_INVALID_LENGTH: |
| 357 | case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH: |
| 358 | case MBEDTLS_ERR_ASN1_INVALID_DATA: |
| 359 | return PSA_ERROR_INVALID_ARGUMENT; |
| 360 | case MBEDTLS_ERR_ASN1_ALLOC_FAILED: |
| 361 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 362 | case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL: |
| 363 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 364 | #endif |
| 365 | |
| 366 | #if defined(MBEDTLS_CAMELLIA_C) |
| 367 | case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA: |
| 368 | case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH: |
| 369 | return PSA_ERROR_NOT_SUPPORTED; |
| 370 | #endif |
| 371 | |
| 372 | #if defined(MBEDTLS_CCM_C) |
| 373 | case MBEDTLS_ERR_CCM_BAD_INPUT: |
| 374 | return PSA_ERROR_INVALID_ARGUMENT; |
| 375 | case MBEDTLS_ERR_CCM_AUTH_FAILED: |
| 376 | return PSA_ERROR_INVALID_SIGNATURE; |
| 377 | #endif |
| 378 | |
| 379 | #if defined(MBEDTLS_CHACHA20_C) |
| 380 | case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA: |
| 381 | return PSA_ERROR_INVALID_ARGUMENT; |
| 382 | #endif |
| 383 | |
| 384 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 385 | case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE: |
| 386 | return PSA_ERROR_BAD_STATE; |
| 387 | case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED: |
| 388 | return PSA_ERROR_INVALID_SIGNATURE; |
| 389 | #endif |
| 390 | |
| 391 | #if defined(MBEDTLS_CIPHER_C) |
| 392 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 393 | return PSA_ERROR_NOT_SUPPORTED; |
| 394 | case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA: |
| 395 | return PSA_ERROR_INVALID_ARGUMENT; |
| 396 | case MBEDTLS_ERR_CIPHER_ALLOC_FAILED: |
| 397 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 398 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: |
| 399 | return PSA_ERROR_INVALID_PADDING; |
| 400 | case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED: |
| 401 | return PSA_ERROR_INVALID_ARGUMENT; |
| 402 | case MBEDTLS_ERR_CIPHER_AUTH_FAILED: |
| 403 | return PSA_ERROR_INVALID_SIGNATURE; |
| 404 | case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT: |
| 405 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 406 | #endif |
| 407 | |
| 408 | #if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \ |
| 409 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)) |
| 410 | /* Only check CTR_DRBG error codes if underlying mbedtls_xxx |
| 411 | * functions are passed a CTR_DRBG instance. */ |
| 412 | case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED: |
| 413 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
| 414 | case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG: |
| 415 | case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG: |
| 416 | return PSA_ERROR_NOT_SUPPORTED; |
| 417 | case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR: |
| 418 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
| 419 | #endif |
| 420 | |
| 421 | #if defined(MBEDTLS_DES_C) |
| 422 | case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH: |
| 423 | return PSA_ERROR_NOT_SUPPORTED; |
| 424 | #endif |
| 425 | |
| 426 | case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED: |
| 427 | case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE: |
| 428 | case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED: |
| 429 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
| 430 | |
| 431 | #if defined(MBEDTLS_GCM_C) |
| 432 | case MBEDTLS_ERR_GCM_AUTH_FAILED: |
| 433 | return PSA_ERROR_INVALID_SIGNATURE; |
| 434 | case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL: |
| 435 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 436 | case MBEDTLS_ERR_GCM_BAD_INPUT: |
| 437 | return PSA_ERROR_INVALID_ARGUMENT; |
| 438 | #endif |
| 439 | |
| 440 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \ |
| 441 | defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) |
| 442 | /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx |
| 443 | * functions are passed a HMAC_DRBG instance. */ |
| 444 | case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED: |
| 445 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
| 446 | case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG: |
| 447 | case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG: |
| 448 | return PSA_ERROR_NOT_SUPPORTED; |
| 449 | case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR: |
| 450 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
| 451 | #endif |
| 452 | |
| 453 | #if defined(MBEDTLS_MD_LIGHT) |
| 454 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 455 | return PSA_ERROR_NOT_SUPPORTED; |
| 456 | case MBEDTLS_ERR_MD_BAD_INPUT_DATA: |
| 457 | return PSA_ERROR_INVALID_ARGUMENT; |
| 458 | case MBEDTLS_ERR_MD_ALLOC_FAILED: |
| 459 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 460 | #if defined(MBEDTLS_FS_IO) |
| 461 | case MBEDTLS_ERR_MD_FILE_IO_ERROR: |
| 462 | return PSA_ERROR_STORAGE_FAILURE; |
| 463 | #endif |
| 464 | #endif |
| 465 | |
| 466 | #if defined(MBEDTLS_BIGNUM_C) |
| 467 | #if defined(MBEDTLS_FS_IO) |
| 468 | case MBEDTLS_ERR_MPI_FILE_IO_ERROR: |
| 469 | return PSA_ERROR_STORAGE_FAILURE; |
| 470 | #endif |
| 471 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 472 | return PSA_ERROR_INVALID_ARGUMENT; |
| 473 | case MBEDTLS_ERR_MPI_INVALID_CHARACTER: |
| 474 | return PSA_ERROR_INVALID_ARGUMENT; |
| 475 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 476 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 477 | case MBEDTLS_ERR_MPI_NEGATIVE_VALUE: |
| 478 | return PSA_ERROR_INVALID_ARGUMENT; |
| 479 | case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO: |
| 480 | return PSA_ERROR_INVALID_ARGUMENT; |
| 481 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
| 482 | return PSA_ERROR_INVALID_ARGUMENT; |
| 483 | case MBEDTLS_ERR_MPI_ALLOC_FAILED: |
| 484 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 485 | #endif |
| 486 | |
| 487 | #if defined(MBEDTLS_PK_C) |
| 488 | case MBEDTLS_ERR_PK_ALLOC_FAILED: |
| 489 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 490 | case MBEDTLS_ERR_PK_TYPE_MISMATCH: |
| 491 | case MBEDTLS_ERR_PK_BAD_INPUT_DATA: |
| 492 | return PSA_ERROR_INVALID_ARGUMENT; |
| 493 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \ |
| 494 | defined(MBEDTLS_PSA_ITS_FILE_C) |
| 495 | case MBEDTLS_ERR_PK_FILE_IO_ERROR: |
| 496 | return PSA_ERROR_STORAGE_FAILURE; |
| 497 | #endif |
| 498 | case MBEDTLS_ERR_PK_KEY_INVALID_VERSION: |
| 499 | case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT: |
| 500 | return PSA_ERROR_INVALID_ARGUMENT; |
| 501 | case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG: |
| 502 | return PSA_ERROR_NOT_SUPPORTED; |
| 503 | case MBEDTLS_ERR_PK_PASSWORD_REQUIRED: |
| 504 | case MBEDTLS_ERR_PK_PASSWORD_MISMATCH: |
| 505 | return PSA_ERROR_NOT_PERMITTED; |
| 506 | case MBEDTLS_ERR_PK_INVALID_PUBKEY: |
| 507 | return PSA_ERROR_INVALID_ARGUMENT; |
| 508 | case MBEDTLS_ERR_PK_INVALID_ALG: |
| 509 | case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE: |
| 510 | case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE: |
| 511 | return PSA_ERROR_NOT_SUPPORTED; |
| 512 | case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH: |
| 513 | return PSA_ERROR_INVALID_SIGNATURE; |
| 514 | case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL: |
| 515 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 516 | #endif |
| 517 | |
| 518 | case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED: |
| 519 | return PSA_ERROR_HARDWARE_FAILURE; |
| 520 | case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED: |
| 521 | return PSA_ERROR_NOT_SUPPORTED; |
| 522 | |
| 523 | #if defined(MBEDTLS_RSA_C) |
| 524 | case MBEDTLS_ERR_RSA_BAD_INPUT_DATA: |
| 525 | return PSA_ERROR_INVALID_ARGUMENT; |
| 526 | case MBEDTLS_ERR_RSA_INVALID_PADDING: |
| 527 | return PSA_ERROR_INVALID_PADDING; |
| 528 | case MBEDTLS_ERR_RSA_KEY_GEN_FAILED: |
| 529 | return PSA_ERROR_HARDWARE_FAILURE; |
| 530 | case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED: |
| 531 | return PSA_ERROR_INVALID_ARGUMENT; |
| 532 | case MBEDTLS_ERR_RSA_PUBLIC_FAILED: |
| 533 | case MBEDTLS_ERR_RSA_PRIVATE_FAILED: |
| 534 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 535 | case MBEDTLS_ERR_RSA_VERIFY_FAILED: |
| 536 | return PSA_ERROR_INVALID_SIGNATURE; |
| 537 | case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE: |
| 538 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 539 | case MBEDTLS_ERR_RSA_RNG_FAILED: |
| 540 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
| 541 | #endif |
| 542 | |
| 543 | #if defined(MBEDTLS_ECP_LIGHT) |
| 544 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
| 545 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
| 546 | return PSA_ERROR_INVALID_ARGUMENT; |
| 547 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 548 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 549 | case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE: |
| 550 | return PSA_ERROR_NOT_SUPPORTED; |
| 551 | case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH: |
| 552 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 553 | return PSA_ERROR_INVALID_SIGNATURE; |
| 554 | case MBEDTLS_ERR_ECP_ALLOC_FAILED: |
| 555 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 556 | case MBEDTLS_ERR_ECP_RANDOM_FAILED: |
| 557 | return PSA_ERROR_INSUFFICIENT_ENTROPY; |
| 558 | |
| 559 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 560 | case MBEDTLS_ERR_ECP_IN_PROGRESS: |
| 561 | return PSA_OPERATION_INCOMPLETE; |
| 562 | #endif |
| 563 | #endif |
| 564 | |
| 565 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
| 566 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 567 | |
| 568 | default: |
| 569 | return PSA_ERROR_GENERIC_ERROR; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * \brief For output buffers which contain "tags" |
| 575 | * (outputs that may be checked for validity like |
| 576 | * hashes, MACs and signatures), fill the unused |
| 577 | * part of the output buffer (the whole buffer on |
| 578 | * error, the trailing part on success) with |
| 579 | * something that isn't a valid tag (barring an |
| 580 | * attack on the tag and deliberately-crafted |
| 581 | * input), in case the caller doesn't check the |
| 582 | * return status properly. |
| 583 | * |
| 584 | * \param output_buffer Pointer to buffer to wipe. May not be NULL |
| 585 | * unless \p output_buffer_size is zero. |
| 586 | * \param status Status of function called to generate |
| 587 | * output_buffer originally |
| 588 | * \param output_buffer_size Size of output buffer. If zero, \p output_buffer |
| 589 | * could be NULL. |
| 590 | * \param output_buffer_length Length of data written to output_buffer, must be |
| 591 | * less than \p output_buffer_size |
| 592 | */ |
| 593 | static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status, |
| 594 | size_t output_buffer_size, size_t output_buffer_length) |
| 595 | { |
| 596 | size_t offset = 0; |
| 597 | |
| 598 | if (output_buffer_size == 0) { |
| 599 | /* If output_buffer_size is 0 then we have nothing to do. We must not |
| 600 | call memset because output_buffer may be NULL in this case */ |
| 601 | return; |
| 602 | } |
| 603 | |
| 604 | if (status == PSA_SUCCESS) { |
| 605 | offset = output_buffer_length; |
| 606 | } |
| 607 | |
| 608 | memset(output_buffer + offset, '!', output_buffer_size - offset); |
| 609 | } |
| 610 | |
| 611 | |
| 612 | psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type, |
| 613 | size_t bits) |
| 614 | { |
| 615 | /* Check that the bit size is acceptable for the key type */ |
| 616 | switch (type) { |
| 617 | case PSA_KEY_TYPE_RAW_DATA: |
| 618 | case PSA_KEY_TYPE_HMAC: |
| 619 | case PSA_KEY_TYPE_DERIVE: |
| 620 | case PSA_KEY_TYPE_PASSWORD: |
| 621 | case PSA_KEY_TYPE_PASSWORD_HASH: |
| 622 | break; |
| 623 | #if defined(PSA_WANT_KEY_TYPE_AES) |
| 624 | case PSA_KEY_TYPE_AES: |
| 625 | if (bits != 128 && bits != 192 && bits != 256) { |
| 626 | return PSA_ERROR_INVALID_ARGUMENT; |
| 627 | } |
| 628 | break; |
| 629 | #endif |
| 630 | #if defined(PSA_WANT_KEY_TYPE_ARIA) |
| 631 | case PSA_KEY_TYPE_ARIA: |
| 632 | if (bits != 128 && bits != 192 && bits != 256) { |
| 633 | return PSA_ERROR_INVALID_ARGUMENT; |
| 634 | } |
| 635 | break; |
| 636 | #endif |
| 637 | #if defined(PSA_WANT_KEY_TYPE_CAMELLIA) |
| 638 | case PSA_KEY_TYPE_CAMELLIA: |
| 639 | if (bits != 128 && bits != 192 && bits != 256) { |
| 640 | return PSA_ERROR_INVALID_ARGUMENT; |
| 641 | } |
| 642 | break; |
| 643 | #endif |
| 644 | #if defined(PSA_WANT_KEY_TYPE_DES) |
| 645 | case PSA_KEY_TYPE_DES: |
| 646 | if (bits != 64 && bits != 128 && bits != 192) { |
| 647 | return PSA_ERROR_INVALID_ARGUMENT; |
| 648 | } |
| 649 | break; |
| 650 | #endif |
| 651 | #if defined(PSA_WANT_KEY_TYPE_CHACHA20) |
| 652 | case PSA_KEY_TYPE_CHACHA20: |
| 653 | if (bits != 256) { |
| 654 | return PSA_ERROR_INVALID_ARGUMENT; |
| 655 | } |
| 656 | break; |
| 657 | #endif |
| 658 | default: |
| 659 | return PSA_ERROR_NOT_SUPPORTED; |
| 660 | } |
| 661 | if (bits % 8 != 0) { |
| 662 | return PSA_ERROR_INVALID_ARGUMENT; |
| 663 | } |
| 664 | |
| 665 | return PSA_SUCCESS; |
| 666 | } |
| 667 | |
| 668 | /** Check whether a given key type is valid for use with a given MAC algorithm |
| 669 | * |
| 670 | * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH |
| 671 | * when called with the validated \p algorithm and \p key_type is well-defined. |
| 672 | * |
| 673 | * \param[in] algorithm The specific MAC algorithm (can be wildcard). |
| 674 | * \param[in] key_type The key type of the key to be used with the |
| 675 | * \p algorithm. |
| 676 | * |
| 677 | * \retval #PSA_SUCCESS |
| 678 | * The \p key_type is valid for use with the \p algorithm |
| 679 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 680 | * The \p key_type is not valid for use with the \p algorithm |
| 681 | */ |
| 682 | MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do( |
| 683 | psa_algorithm_t algorithm, |
| 684 | psa_key_type_t key_type) |
| 685 | { |
| 686 | if (PSA_ALG_IS_HMAC(algorithm)) { |
| 687 | if (key_type == PSA_KEY_TYPE_HMAC) { |
| 688 | return PSA_SUCCESS; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) { |
| 693 | /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher |
| 694 | * key. */ |
| 695 | if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) == |
| 696 | PSA_KEY_TYPE_CATEGORY_SYMMETRIC) { |
| 697 | /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and |
| 698 | * the block length (larger than 1) for block ciphers. */ |
| 699 | if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) { |
| 700 | return PSA_SUCCESS; |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | return PSA_ERROR_INVALID_ARGUMENT; |
| 706 | } |
| 707 | |
| 708 | psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot, |
| 709 | size_t buffer_length) |
| 710 | { |
| 711 | #if defined(MBEDTLS_PSA_STATIC_KEY_SLOTS) |
| 712 | if (buffer_length > ((size_t) MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE)) { |
| 713 | return PSA_ERROR_NOT_SUPPORTED; |
| 714 | } |
| 715 | #else |
| 716 | if (slot->key.data != NULL) { |
| 717 | return PSA_ERROR_ALREADY_EXISTS; |
| 718 | } |
| 719 | |
| 720 | slot->key.data = mbedtls_calloc(1, buffer_length); |
| 721 | if (slot->key.data == NULL) { |
| 722 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 723 | } |
| 724 | #endif |
| 725 | |
| 726 | slot->key.bytes = buffer_length; |
| 727 | return PSA_SUCCESS; |
| 728 | } |
| 729 | |
| 730 | psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot, |
| 731 | const uint8_t *data, |
| 732 | size_t data_length) |
| 733 | { |
| 734 | psa_status_t status = psa_allocate_buffer_to_slot(slot, |
| 735 | data_length); |
| 736 | if (status != PSA_SUCCESS) { |
| 737 | return status; |
| 738 | } |
| 739 | |
| 740 | memcpy(slot->key.data, data, data_length); |
| 741 | return PSA_SUCCESS; |
| 742 | } |
| 743 | |
| 744 | psa_status_t psa_import_key_into_slot( |
| 745 | const psa_key_attributes_t *attributes, |
| 746 | const uint8_t *data, size_t data_length, |
| 747 | uint8_t *key_buffer, size_t key_buffer_size, |
| 748 | size_t *key_buffer_length, size_t *bits) |
| 749 | { |
| 750 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 751 | psa_key_type_t type = attributes->type; |
| 752 | |
| 753 | /* zero-length keys are never supported. */ |
| 754 | if (data_length == 0) { |
| 755 | return PSA_ERROR_NOT_SUPPORTED; |
| 756 | } |
| 757 | |
| 758 | if (key_type_is_raw_bytes(type)) { |
| 759 | *bits = PSA_BYTES_TO_BITS(data_length); |
| 760 | |
| 761 | status = psa_validate_unstructured_key_bit_size(attributes->type, |
| 762 | *bits); |
| 763 | if (status != PSA_SUCCESS) { |
| 764 | return status; |
| 765 | } |
| 766 | |
| 767 | /* Copy the key material. */ |
| 768 | memcpy(key_buffer, data, data_length); |
| 769 | *key_buffer_length = data_length; |
| 770 | (void) key_buffer_size; |
| 771 | |
| 772 | return PSA_SUCCESS; |
| 773 | } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) { |
| 774 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \ |
| 775 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) |
| 776 | if (PSA_KEY_TYPE_IS_DH(type)) { |
| 777 | if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) { |
| 778 | return PSA_ERROR_NOT_SUPPORTED; |
| 779 | } |
| 780 | return mbedtls_psa_ffdh_import_key(attributes, |
| 781 | data, data_length, |
| 782 | key_buffer, key_buffer_size, |
| 783 | key_buffer_length, |
| 784 | bits); |
| 785 | } |
| 786 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || |
| 787 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */ |
| 788 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \ |
| 789 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
| 790 | if (PSA_KEY_TYPE_IS_ECC(type)) { |
| 791 | return mbedtls_psa_ecp_import_key(attributes, |
| 792 | data, data_length, |
| 793 | key_buffer, key_buffer_size, |
| 794 | key_buffer_length, |
| 795 | bits); |
| 796 | } |
| 797 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || |
| 798 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
| 799 | #if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \ |
| 800 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \ |
| 801 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 802 | if (PSA_KEY_TYPE_IS_RSA(type)) { |
| 803 | return mbedtls_psa_rsa_import_key(attributes, |
| 804 | data, data_length, |
| 805 | key_buffer, key_buffer_size, |
| 806 | key_buffer_length, |
| 807 | bits); |
| 808 | } |
| 809 | #endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && |
| 810 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || |
| 811 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
| 812 | } |
| 813 | |
| 814 | return PSA_ERROR_NOT_SUPPORTED; |
| 815 | } |
| 816 | |
| 817 | /** Calculate the intersection of two algorithm usage policies. |
| 818 | * |
| 819 | * Return 0 (which allows no operation) on incompatibility. |
| 820 | */ |
| 821 | static psa_algorithm_t psa_key_policy_algorithm_intersection( |
| 822 | psa_key_type_t key_type, |
| 823 | psa_algorithm_t alg1, |
| 824 | psa_algorithm_t alg2) |
| 825 | { |
| 826 | /* Common case: both sides actually specify the same policy. */ |
| 827 | if (alg1 == alg2) { |
| 828 | return alg1; |
| 829 | } |
| 830 | /* If the policies are from the same hash-and-sign family, check |
| 831 | * if one is a wildcard. If so the other has the specific algorithm. */ |
| 832 | if (PSA_ALG_IS_SIGN_HASH(alg1) && |
| 833 | PSA_ALG_IS_SIGN_HASH(alg2) && |
| 834 | (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) { |
| 835 | if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) { |
| 836 | return alg2; |
| 837 | } |
| 838 | if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) { |
| 839 | return alg1; |
| 840 | } |
| 841 | } |
| 842 | /* If the policies are from the same AEAD family, check whether |
| 843 | * one of them is a minimum-tag-length wildcard. Calculate the most |
| 844 | * restrictive tag length. */ |
| 845 | if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) && |
| 846 | (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) == |
| 847 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) { |
| 848 | size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1); |
| 849 | size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2); |
| 850 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
| 851 | |
| 852 | /* If both are wildcards, return most restrictive wildcard */ |
| 853 | if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 854 | ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 855 | return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( |
| 856 | alg1, restricted_len); |
| 857 | } |
| 858 | /* If only one is a wildcard, return specific algorithm if compatible. */ |
| 859 | if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 860 | (alg1_len <= alg2_len)) { |
| 861 | return alg2; |
| 862 | } |
| 863 | if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 864 | (alg2_len <= alg1_len)) { |
| 865 | return alg1; |
| 866 | } |
| 867 | } |
| 868 | /* If the policies are from the same MAC family, check whether one |
| 869 | * of them is a minimum-MAC-length policy. Calculate the most |
| 870 | * restrictive tag length. */ |
| 871 | if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) && |
| 872 | (PSA_ALG_FULL_LENGTH_MAC(alg1) == |
| 873 | PSA_ALG_FULL_LENGTH_MAC(alg2))) { |
| 874 | /* Validate the combination of key type and algorithm. Since the base |
| 875 | * algorithm of alg1 and alg2 are the same, we only need this once. */ |
| 876 | if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) { |
| 877 | return 0; |
| 878 | } |
| 879 | |
| 880 | /* Get the (exact or at-least) output lengths for both sides of the |
| 881 | * requested intersection. None of the currently supported algorithms |
| 882 | * have an output length dependent on the actual key size, so setting it |
| 883 | * to a bogus value of 0 is currently OK. |
| 884 | * |
| 885 | * Note that for at-least-this-length wildcard algorithms, the output |
| 886 | * length is set to the shortest allowed length, which allows us to |
| 887 | * calculate the most restrictive tag length for the intersection. */ |
| 888 | size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1); |
| 889 | size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2); |
| 890 | size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len; |
| 891 | |
| 892 | /* If both are wildcards, return most restrictive wildcard */ |
| 893 | if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) && |
| 894 | ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 895 | return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len); |
| 896 | } |
| 897 | |
| 898 | /* If only one is an at-least-this-length policy, the intersection would |
| 899 | * be the other (fixed-length) policy as long as said fixed length is |
| 900 | * equal to or larger than the shortest allowed length. */ |
| 901 | if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 902 | return (alg1_len <= alg2_len) ? alg2 : 0; |
| 903 | } |
| 904 | if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 905 | return (alg2_len <= alg1_len) ? alg1 : 0; |
| 906 | } |
| 907 | |
| 908 | /* If none of them are wildcards, check whether they define the same tag |
| 909 | * length. This is still possible here when one is default-length and |
| 910 | * the other specific-length. Ensure to always return the |
| 911 | * specific-length version for the intersection. */ |
| 912 | if (alg1_len == alg2_len) { |
| 913 | return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len); |
| 914 | } |
| 915 | } |
| 916 | /* If the policies are incompatible, allow nothing. */ |
| 917 | return 0; |
| 918 | } |
| 919 | |
| 920 | static int psa_key_algorithm_permits(psa_key_type_t key_type, |
| 921 | psa_algorithm_t policy_alg, |
| 922 | psa_algorithm_t requested_alg) |
| 923 | { |
| 924 | /* Common case: the policy only allows requested_alg. */ |
| 925 | if (requested_alg == policy_alg) { |
| 926 | return 1; |
| 927 | } |
| 928 | /* If policy_alg is a hash-and-sign with a wildcard for the hash, |
| 929 | * and requested_alg is the same hash-and-sign family with any hash, |
| 930 | * then requested_alg is compliant with policy_alg. */ |
| 931 | if (PSA_ALG_IS_SIGN_HASH(requested_alg) && |
| 932 | PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) { |
| 933 | return (policy_alg & ~PSA_ALG_HASH_MASK) == |
| 934 | (requested_alg & ~PSA_ALG_HASH_MASK); |
| 935 | } |
| 936 | /* If policy_alg is a wildcard AEAD algorithm of the same base as |
| 937 | * the requested algorithm, check the requested tag length to be |
| 938 | * equal-length or longer than the wildcard-specified length. */ |
| 939 | if (PSA_ALG_IS_AEAD(policy_alg) && |
| 940 | PSA_ALG_IS_AEAD(requested_alg) && |
| 941 | (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) == |
| 942 | PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) && |
| 943 | ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) { |
| 944 | return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <= |
| 945 | PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg); |
| 946 | } |
| 947 | /* If policy_alg is a MAC algorithm of the same base as the requested |
| 948 | * algorithm, check whether their MAC lengths are compatible. */ |
| 949 | if (PSA_ALG_IS_MAC(policy_alg) && |
| 950 | PSA_ALG_IS_MAC(requested_alg) && |
| 951 | (PSA_ALG_FULL_LENGTH_MAC(policy_alg) == |
| 952 | PSA_ALG_FULL_LENGTH_MAC(requested_alg))) { |
| 953 | /* Validate the combination of key type and algorithm. Since the policy |
| 954 | * and requested algorithms are the same, we only need this once. */ |
| 955 | if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) { |
| 956 | return 0; |
| 957 | } |
| 958 | |
| 959 | /* Get both the requested output length for the algorithm which is to be |
| 960 | * verified, and the default output length for the base algorithm. |
| 961 | * Note that none of the currently supported algorithms have an output |
| 962 | * length dependent on actual key size, so setting it to a bogus value |
| 963 | * of 0 is currently OK. */ |
| 964 | size_t requested_output_length = PSA_MAC_LENGTH( |
| 965 | key_type, 0, requested_alg); |
| 966 | size_t default_output_length = PSA_MAC_LENGTH( |
| 967 | key_type, 0, |
| 968 | PSA_ALG_FULL_LENGTH_MAC(requested_alg)); |
| 969 | |
| 970 | /* If the policy is default-length, only allow an algorithm with |
| 971 | * a declared exact-length matching the default. */ |
| 972 | if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) { |
| 973 | return requested_output_length == default_output_length; |
| 974 | } |
| 975 | |
| 976 | /* If the requested algorithm is default-length, allow it if the policy |
| 977 | * length exactly matches the default length. */ |
| 978 | if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 && |
| 979 | PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) { |
| 980 | return 1; |
| 981 | } |
| 982 | |
| 983 | /* If policy_alg is an at-least-this-length wildcard MAC algorithm, |
| 984 | * check for the requested MAC length to be equal to or longer than the |
| 985 | * minimum allowed length. */ |
| 986 | if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) { |
| 987 | return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <= |
| 988 | requested_output_length; |
| 989 | } |
| 990 | } |
| 991 | /* If policy_alg is a generic key agreement operation, then using it for |
| 992 | * a key derivation with that key agreement should also be allowed. This |
| 993 | * behaviour is expected to be defined in a future specification version. */ |
| 994 | if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) && |
| 995 | PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) { |
| 996 | return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) == |
| 997 | policy_alg; |
| 998 | } |
| 999 | /* If it isn't explicitly permitted, it's forbidden. */ |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
| 1003 | /** Test whether a policy permits an algorithm. |
| 1004 | * |
| 1005 | * The caller must test usage flags separately. |
| 1006 | * |
| 1007 | * \note This function requires providing the key type for which the policy is |
| 1008 | * being validated, since some algorithm policy definitions (e.g. MAC) |
| 1009 | * have different properties depending on what kind of cipher it is |
| 1010 | * combined with. |
| 1011 | * |
| 1012 | * \retval PSA_SUCCESS When \p alg is a specific algorithm |
| 1013 | * allowed by the \p policy. |
| 1014 | * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm |
| 1015 | * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but |
| 1016 | * the \p policy does not allow it. |
| 1017 | */ |
| 1018 | static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy, |
| 1019 | psa_key_type_t key_type, |
| 1020 | psa_algorithm_t alg) |
| 1021 | { |
| 1022 | /* '0' is not a valid algorithm */ |
| 1023 | if (alg == 0) { |
| 1024 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1025 | } |
| 1026 | |
| 1027 | /* A requested algorithm cannot be a wildcard. */ |
| 1028 | if (PSA_ALG_IS_WILDCARD(alg)) { |
| 1029 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1030 | } |
| 1031 | |
| 1032 | if (psa_key_algorithm_permits(key_type, policy->alg, alg) || |
| 1033 | psa_key_algorithm_permits(key_type, policy->alg2, alg)) { |
| 1034 | return PSA_SUCCESS; |
| 1035 | } else { |
| 1036 | return PSA_ERROR_NOT_PERMITTED; |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | /** Restrict a key policy based on a constraint. |
| 1041 | * |
| 1042 | * \note This function requires providing the key type for which the policy is |
| 1043 | * being restricted, since some algorithm policy definitions (e.g. MAC) |
| 1044 | * have different properties depending on what kind of cipher it is |
| 1045 | * combined with. |
| 1046 | * |
| 1047 | * \param[in] key_type The key type for which to restrict the policy |
| 1048 | * \param[in,out] policy The policy to restrict. |
| 1049 | * \param[in] constraint The policy constraint to apply. |
| 1050 | * |
| 1051 | * \retval #PSA_SUCCESS |
| 1052 | * \c *policy contains the intersection of the original value of |
| 1053 | * \c *policy and \c *constraint. |
| 1054 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 1055 | * \c key_type, \c *policy and \c *constraint are incompatible. |
| 1056 | * \c *policy is unchanged. |
| 1057 | */ |
| 1058 | static psa_status_t psa_restrict_key_policy( |
| 1059 | psa_key_type_t key_type, |
| 1060 | psa_key_policy_t *policy, |
| 1061 | const psa_key_policy_t *constraint) |
| 1062 | { |
| 1063 | psa_algorithm_t intersection_alg = |
| 1064 | psa_key_policy_algorithm_intersection(key_type, policy->alg, |
| 1065 | constraint->alg); |
| 1066 | psa_algorithm_t intersection_alg2 = |
| 1067 | psa_key_policy_algorithm_intersection(key_type, policy->alg2, |
| 1068 | constraint->alg2); |
| 1069 | if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) { |
| 1070 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1071 | } |
| 1072 | if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) { |
| 1073 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1074 | } |
| 1075 | policy->usage &= constraint->usage; |
| 1076 | policy->alg = intersection_alg; |
| 1077 | policy->alg2 = intersection_alg2; |
| 1078 | return PSA_SUCCESS; |
| 1079 | } |
| 1080 | |
| 1081 | /** Get the description of a key given its identifier and policy constraints |
| 1082 | * and lock it. |
| 1083 | * |
| 1084 | * The key must have allow all the usage flags set in \p usage. If \p alg is |
| 1085 | * nonzero, the key must allow operations with this algorithm. If \p alg is |
| 1086 | * zero, the algorithm is not checked. |
| 1087 | * |
| 1088 | * In case of a persistent key, the function loads the description of the key |
| 1089 | * into a key slot if not already done. |
| 1090 | * |
| 1091 | * On success, the returned key slot has been registered for reading. |
| 1092 | * It is the responsibility of the caller to then unregister |
| 1093 | * once they have finished reading the contents of the slot. |
| 1094 | * The caller unregisters by calling psa_unregister_read() or |
| 1095 | * psa_unregister_read_under_mutex(). psa_unregister_read() must be called |
| 1096 | * if and only if the caller already holds the global key slot mutex |
| 1097 | * (when mutexes are enabled). psa_unregister_read_under_mutex() encapsulates |
| 1098 | * the unregister with mutex lock and unlock operations. |
| 1099 | */ |
| 1100 | static psa_status_t psa_get_and_lock_key_slot_with_policy( |
| 1101 | mbedtls_svc_key_id_t key, |
| 1102 | psa_key_slot_t **p_slot, |
| 1103 | psa_key_usage_t usage, |
| 1104 | psa_algorithm_t alg) |
| 1105 | { |
| 1106 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1107 | psa_key_slot_t *slot = NULL; |
| 1108 | |
| 1109 | status = psa_get_and_lock_key_slot(key, p_slot); |
| 1110 | if (status != PSA_SUCCESS) { |
| 1111 | return status; |
| 1112 | } |
| 1113 | slot = *p_slot; |
| 1114 | |
| 1115 | /* Enforce that usage policy for the key slot contains all the flags |
| 1116 | * required by the usage parameter. There is one exception: public |
| 1117 | * keys can always be exported, so we treat public key objects as |
| 1118 | * if they had the export flag. */ |
| 1119 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) { |
| 1120 | usage &= ~PSA_KEY_USAGE_EXPORT; |
| 1121 | } |
| 1122 | |
| 1123 | if ((slot->attr.policy.usage & usage) != usage) { |
| 1124 | status = PSA_ERROR_NOT_PERMITTED; |
| 1125 | goto error; |
| 1126 | } |
| 1127 | |
| 1128 | /* Enforce that the usage policy permits the requested algorithm. */ |
| 1129 | if (alg != 0) { |
| 1130 | status = psa_key_policy_permits(&slot->attr.policy, |
| 1131 | slot->attr.type, |
| 1132 | alg); |
| 1133 | if (status != PSA_SUCCESS) { |
| 1134 | goto error; |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | return PSA_SUCCESS; |
| 1139 | |
| 1140 | error: |
| 1141 | *p_slot = NULL; |
| 1142 | psa_unregister_read_under_mutex(slot); |
| 1143 | |
| 1144 | return status; |
| 1145 | } |
| 1146 | |
| 1147 | /** Get a key slot containing a transparent key and lock it. |
| 1148 | * |
| 1149 | * A transparent key is a key for which the key material is directly |
| 1150 | * available, as opposed to a key in a secure element and/or to be used |
| 1151 | * by a secure element. |
| 1152 | * |
| 1153 | * This is a temporary function that may be used instead of |
| 1154 | * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support |
| 1155 | * for a cryptographic operation. |
| 1156 | * |
| 1157 | * On success, the returned key slot has been registered for reading. |
| 1158 | * It is the responsibility of the caller to then unregister |
| 1159 | * once they have finished reading the contents of the slot. |
| 1160 | * The caller unregisters by calling psa_unregister_read() or |
| 1161 | * psa_unregister_read_under_mutex(). psa_unregister_read() must be called |
| 1162 | * if and only if the caller already holds the global key slot mutex |
| 1163 | * (when mutexes are enabled). psa_unregister_read_under_mutex() encapsulates |
| 1164 | * psa_unregister_read() with mutex lock and unlock operations. |
| 1165 | */ |
| 1166 | static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy( |
| 1167 | mbedtls_svc_key_id_t key, |
| 1168 | psa_key_slot_t **p_slot, |
| 1169 | psa_key_usage_t usage, |
| 1170 | psa_algorithm_t alg) |
| 1171 | { |
| 1172 | psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot, |
| 1173 | usage, alg); |
| 1174 | if (status != PSA_SUCCESS) { |
| 1175 | return status; |
| 1176 | } |
| 1177 | |
| 1178 | if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) { |
| 1179 | psa_unregister_read_under_mutex(*p_slot); |
| 1180 | *p_slot = NULL; |
| 1181 | return PSA_ERROR_NOT_SUPPORTED; |
| 1182 | } |
| 1183 | |
| 1184 | return PSA_SUCCESS; |
| 1185 | } |
| 1186 | |
| 1187 | psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot) |
| 1188 | { |
| 1189 | #if defined(MBEDTLS_PSA_STATIC_KEY_SLOTS) |
| 1190 | if (slot->key.bytes > 0) { |
| 1191 | mbedtls_platform_zeroize(slot->key.data, MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE); |
| 1192 | } |
| 1193 | #else |
| 1194 | if (slot->key.data != NULL) { |
| 1195 | mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes); |
| 1196 | } |
| 1197 | |
| 1198 | slot->key.data = NULL; |
| 1199 | #endif /* MBEDTLS_PSA_STATIC_KEY_SLOTS */ |
| 1200 | |
| 1201 | slot->key.bytes = 0; |
| 1202 | |
| 1203 | return PSA_SUCCESS; |
| 1204 | } |
| 1205 | |
| 1206 | /** Completely wipe a slot in memory, including its policy. |
| 1207 | * Persistent storage is not affected. */ |
| 1208 | psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot) |
| 1209 | { |
| 1210 | psa_status_t status = psa_remove_key_data_from_memory(slot); |
| 1211 | |
| 1212 | /* |
| 1213 | * As the return error code may not be handled in case of multiple errors, |
| 1214 | * do our best to report an unexpected amount of registered readers or |
| 1215 | * an unexpected state. |
| 1216 | * Assert with MBEDTLS_TEST_HOOK_TEST_ASSERT that the slot is valid for |
| 1217 | * wiping. |
| 1218 | * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the |
| 1219 | * function is called as part of the execution of a test suite, the |
| 1220 | * execution of the test suite is stopped in error if the assertion fails. |
| 1221 | */ |
| 1222 | switch (slot->state) { |
| 1223 | case PSA_SLOT_FULL: |
| 1224 | /* In this state psa_wipe_key_slot() must only be called if the |
| 1225 | * caller is the last reader. */ |
| 1226 | case PSA_SLOT_PENDING_DELETION: |
| 1227 | /* In this state psa_wipe_key_slot() must only be called if the |
| 1228 | * caller is the last reader. */ |
| 1229 | if (slot->var.occupied.registered_readers != 1) { |
| 1230 | MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->var.occupied.registered_readers == 1); |
| 1231 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1232 | } |
| 1233 | break; |
| 1234 | case PSA_SLOT_FILLING: |
| 1235 | /* In this state registered_readers must be 0. */ |
| 1236 | if (slot->var.occupied.registered_readers != 0) { |
| 1237 | MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->var.occupied.registered_readers == 0); |
| 1238 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1239 | } |
| 1240 | break; |
| 1241 | case PSA_SLOT_EMPTY: |
| 1242 | /* The slot is already empty, it cannot be wiped. */ |
| 1243 | MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->state != PSA_SLOT_EMPTY); |
| 1244 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1245 | break; |
| 1246 | default: |
| 1247 | /* The slot's state is invalid. */ |
| 1248 | status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1249 | } |
| 1250 | |
| 1251 | #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC) |
| 1252 | size_t slice_index = slot->slice_index; |
| 1253 | #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */ |
| 1254 | |
| 1255 | |
| 1256 | /* Multipart operations may still be using the key. This is safe |
| 1257 | * because all multipart operation objects are independent from |
| 1258 | * the key slot: if they need to access the key after the setup |
| 1259 | * phase, they have a copy of the key. Note that this means that |
| 1260 | * key material can linger until all operations are completed. */ |
| 1261 | /* At this point, key material and other type-specific content has |
| 1262 | * been wiped. Clear remaining metadata. We can call memset and not |
| 1263 | * zeroize because the metadata is not particularly sensitive. |
| 1264 | * This memset also sets the slot's state to PSA_SLOT_EMPTY. */ |
| 1265 | memset(slot, 0, sizeof(*slot)); |
| 1266 | |
| 1267 | #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC) |
| 1268 | /* If the slot is already corrupted, something went deeply wrong, |
| 1269 | * like a thread still using the slot or a stray pointer leading |
| 1270 | * to the slot's memory being used for another object. Let the slot |
| 1271 | * leak rather than make the corruption worse. */ |
| 1272 | if (status == PSA_SUCCESS) { |
| 1273 | status = psa_free_key_slot(slice_index, slot); |
| 1274 | } |
| 1275 | #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */ |
| 1276 | |
| 1277 | return status; |
| 1278 | } |
| 1279 | |
| 1280 | psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key) |
| 1281 | { |
| 1282 | psa_key_slot_t *slot; |
| 1283 | psa_status_t status; /* status of the last operation */ |
| 1284 | psa_status_t overall_status = PSA_SUCCESS; |
| 1285 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1286 | psa_se_drv_table_entry_t *driver; |
| 1287 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1288 | |
| 1289 | if (mbedtls_svc_key_id_is_null(key)) { |
| 1290 | return PSA_SUCCESS; |
| 1291 | } |
| 1292 | |
| 1293 | /* |
| 1294 | * Get the description of the key in a key slot, and register to read it. |
| 1295 | * In the case of a persistent key, this will load the key description |
| 1296 | * from persistent memory if not done yet. |
| 1297 | * We cannot avoid this loading as without it we don't know if |
| 1298 | * the key is operated by an SE or not and this information is needed by |
| 1299 | * the current implementation. */ |
| 1300 | status = psa_get_and_lock_key_slot(key, &slot); |
| 1301 | if (status != PSA_SUCCESS) { |
| 1302 | return status; |
| 1303 | } |
| 1304 | |
| 1305 | #if defined(MBEDTLS_THREADING_C) |
| 1306 | /* We cannot unlock between setting the state to PENDING_DELETION |
| 1307 | * and destroying the key in storage, as otherwise another thread |
| 1308 | * could load the key into a new slot and the key will not be |
| 1309 | * fully destroyed. */ |
| 1310 | PSA_THREADING_CHK_GOTO_EXIT(mbedtls_mutex_lock( |
| 1311 | &mbedtls_threading_key_slot_mutex)); |
| 1312 | |
| 1313 | if (slot->state == PSA_SLOT_PENDING_DELETION) { |
| 1314 | /* Another thread has destroyed the key between us locking the slot |
| 1315 | * and us gaining the mutex. Unregister from the slot, |
| 1316 | * and report that the key does not exist. */ |
| 1317 | status = psa_unregister_read(slot); |
| 1318 | |
| 1319 | PSA_THREADING_CHK_RET(mbedtls_mutex_unlock( |
| 1320 | &mbedtls_threading_key_slot_mutex)); |
| 1321 | return (status == PSA_SUCCESS) ? PSA_ERROR_INVALID_HANDLE : status; |
| 1322 | } |
| 1323 | #endif |
| 1324 | /* Set the key slot containing the key description's state to |
| 1325 | * PENDING_DELETION. This stops new operations from registering |
| 1326 | * to read the slot. Current readers can safely continue to access |
| 1327 | * the key within the slot; the last registered reader will |
| 1328 | * automatically wipe the slot when they call psa_unregister_read(). |
| 1329 | * If the key is persistent, we can now delete the copy of the key |
| 1330 | * from memory. If the key is opaque, we require the driver to |
| 1331 | * deal with the deletion. */ |
| 1332 | overall_status = psa_key_slot_state_transition(slot, PSA_SLOT_FULL, |
| 1333 | PSA_SLOT_PENDING_DELETION); |
| 1334 | |
| 1335 | if (overall_status != PSA_SUCCESS) { |
| 1336 | goto exit; |
| 1337 | } |
| 1338 | |
| 1339 | if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) { |
| 1340 | /* Refuse the destruction of a read-only key (which may or may not work |
| 1341 | * if we attempt it, depending on whether the key is merely read-only |
| 1342 | * by policy or actually physically read-only). |
| 1343 | * Just do the best we can, which is to wipe the copy in memory |
| 1344 | * (done in this function's cleanup code). */ |
| 1345 | overall_status = PSA_ERROR_NOT_PERMITTED; |
| 1346 | goto exit; |
| 1347 | } |
| 1348 | |
| 1349 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1350 | driver = psa_get_se_driver_entry(slot->attr.lifetime); |
| 1351 | if (driver != NULL) { |
| 1352 | /* For a key in a secure element, we need to do three things: |
| 1353 | * remove the key file in internal storage, destroy the |
| 1354 | * key inside the secure element, and update the driver's |
| 1355 | * persistent data. Start a transaction that will encompass these |
| 1356 | * three actions. */ |
| 1357 | psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY); |
| 1358 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
| 1359 | psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot); |
| 1360 | psa_crypto_transaction.key.id = slot->attr.id; |
| 1361 | status = psa_crypto_save_transaction(); |
| 1362 | if (status != PSA_SUCCESS) { |
| 1363 | (void) psa_crypto_stop_transaction(); |
| 1364 | /* We should still try to destroy the key in the secure |
| 1365 | * element and the key metadata in storage. This is especially |
| 1366 | * important if the error is that the storage is full. |
| 1367 | * But how to do it exactly without risking an inconsistent |
| 1368 | * state after a reset? |
| 1369 | * https://github.com/ARMmbed/mbed-crypto/issues/215 |
| 1370 | */ |
| 1371 | overall_status = status; |
| 1372 | goto exit; |
| 1373 | } |
| 1374 | |
| 1375 | status = psa_destroy_se_key(driver, |
| 1376 | psa_key_slot_get_slot_number(slot)); |
| 1377 | if (overall_status == PSA_SUCCESS) { |
| 1378 | overall_status = status; |
| 1379 | } |
| 1380 | } |
| 1381 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1382 | |
| 1383 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
| 1384 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
| 1385 | /* Destroy the copy of the persistent key from storage. |
| 1386 | * The slot will still hold a copy of the key until the last reader |
| 1387 | * unregisters. */ |
| 1388 | status = psa_destroy_persistent_key(slot->attr.id); |
| 1389 | if (overall_status == PSA_SUCCESS) { |
| 1390 | overall_status = status; |
| 1391 | } |
| 1392 | } |
| 1393 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1394 | |
| 1395 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1396 | if (driver != NULL) { |
| 1397 | status = psa_save_se_persistent_data(driver); |
| 1398 | if (overall_status == PSA_SUCCESS) { |
| 1399 | overall_status = status; |
| 1400 | } |
| 1401 | status = psa_crypto_stop_transaction(); |
| 1402 | if (overall_status == PSA_SUCCESS) { |
| 1403 | overall_status = status; |
| 1404 | } |
| 1405 | } |
| 1406 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1407 | |
| 1408 | exit: |
| 1409 | /* Unregister from reading the slot. If we are the last active reader |
| 1410 | * then this will wipe the slot. */ |
| 1411 | status = psa_unregister_read(slot); |
| 1412 | /* Prioritize CORRUPTION_DETECTED from unregistering over |
| 1413 | * a storage error. */ |
| 1414 | if (status != PSA_SUCCESS) { |
| 1415 | overall_status = status; |
| 1416 | } |
| 1417 | |
| 1418 | #if defined(MBEDTLS_THREADING_C) |
| 1419 | /* Don't overwrite existing errors if the unlock fails. */ |
| 1420 | status = overall_status; |
| 1421 | PSA_THREADING_CHK_RET(mbedtls_mutex_unlock( |
| 1422 | &mbedtls_threading_key_slot_mutex)); |
| 1423 | #endif |
| 1424 | |
| 1425 | return overall_status; |
| 1426 | } |
| 1427 | |
| 1428 | /** Retrieve all the publicly-accessible attributes of a key. |
| 1429 | */ |
| 1430 | psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key, |
| 1431 | psa_key_attributes_t *attributes) |
| 1432 | { |
| 1433 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1434 | psa_key_slot_t *slot; |
| 1435 | |
| 1436 | psa_reset_key_attributes(attributes); |
| 1437 | |
| 1438 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0); |
| 1439 | if (status != PSA_SUCCESS) { |
| 1440 | return status; |
| 1441 | } |
| 1442 | |
| 1443 | *attributes = slot->attr; |
| 1444 | |
| 1445 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1446 | if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) { |
| 1447 | psa_set_key_slot_number(attributes, |
| 1448 | psa_key_slot_get_slot_number(slot)); |
| 1449 | } |
| 1450 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1451 | |
| 1452 | return psa_unregister_read_under_mutex(slot); |
| 1453 | } |
| 1454 | |
| 1455 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1456 | psa_status_t psa_get_key_slot_number( |
| 1457 | const psa_key_attributes_t *attributes, |
| 1458 | psa_key_slot_number_t *slot_number) |
| 1459 | { |
| 1460 | if (attributes->has_slot_number) { |
| 1461 | *slot_number = attributes->slot_number; |
| 1462 | return PSA_SUCCESS; |
| 1463 | } else { |
| 1464 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1465 | } |
| 1466 | } |
| 1467 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1468 | |
| 1469 | static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer, |
| 1470 | size_t key_buffer_size, |
| 1471 | uint8_t *data, |
| 1472 | size_t data_size, |
| 1473 | size_t *data_length) |
| 1474 | { |
| 1475 | if (key_buffer_size > data_size) { |
| 1476 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1477 | } |
| 1478 | memcpy(data, key_buffer, key_buffer_size); |
| 1479 | memset(data + key_buffer_size, 0, |
| 1480 | data_size - key_buffer_size); |
| 1481 | *data_length = key_buffer_size; |
| 1482 | return PSA_SUCCESS; |
| 1483 | } |
| 1484 | |
| 1485 | psa_status_t psa_export_key_internal( |
| 1486 | const psa_key_attributes_t *attributes, |
| 1487 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 1488 | uint8_t *data, size_t data_size, size_t *data_length) |
| 1489 | { |
| 1490 | psa_key_type_t type = attributes->type; |
| 1491 | |
| 1492 | if (key_type_is_raw_bytes(type) || |
| 1493 | PSA_KEY_TYPE_IS_RSA(type) || |
| 1494 | PSA_KEY_TYPE_IS_ECC(type) || |
| 1495 | PSA_KEY_TYPE_IS_DH(type)) { |
| 1496 | return psa_export_key_buffer_internal( |
| 1497 | key_buffer, key_buffer_size, |
| 1498 | data, data_size, data_length); |
| 1499 | } else { |
| 1500 | /* This shouldn't happen in the built-in implementation, but |
| 1501 | it is valid for a special-purpose drivers to omit |
| 1502 | support for exporting certain key types. */ |
| 1503 | return PSA_ERROR_NOT_SUPPORTED; |
| 1504 | } |
| 1505 | } |
| 1506 | |
| 1507 | psa_status_t psa_export_key(mbedtls_svc_key_id_t key, |
| 1508 | uint8_t *data_external, |
| 1509 | size_t data_size, |
| 1510 | size_t *data_length) |
| 1511 | { |
| 1512 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1513 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1514 | psa_key_slot_t *slot; |
| 1515 | LOCAL_OUTPUT_DECLARE(data_external, data); |
| 1516 | |
| 1517 | /* Reject a zero-length output buffer now, since this can never be a |
| 1518 | * valid key representation. This way we know that data must be a valid |
| 1519 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1520 | if (data_size == 0) { |
| 1521 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1522 | } |
| 1523 | |
| 1524 | /* Set the key to empty now, so that even when there are errors, we always |
| 1525 | * set data_length to a value between 0 and data_size. On error, setting |
| 1526 | * the key to empty is a good choice because an empty key representation is |
| 1527 | * unlikely to be accepted anywhere. */ |
| 1528 | *data_length = 0; |
| 1529 | |
| 1530 | /* Export requires the EXPORT flag. There is an exception for public keys, |
| 1531 | * which don't require any flag, but |
| 1532 | * psa_get_and_lock_key_slot_with_policy() takes care of this. |
| 1533 | */ |
| 1534 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 1535 | PSA_KEY_USAGE_EXPORT, 0); |
| 1536 | if (status != PSA_SUCCESS) { |
| 1537 | return status; |
| 1538 | } |
| 1539 | |
| 1540 | LOCAL_OUTPUT_ALLOC(data_external, data_size, data); |
| 1541 | |
| 1542 | status = psa_driver_wrapper_export_key(&slot->attr, |
| 1543 | slot->key.data, slot->key.bytes, |
| 1544 | data, data_size, data_length); |
| 1545 | |
| 1546 | #if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) |
| 1547 | exit: |
| 1548 | #endif |
| 1549 | unlock_status = psa_unregister_read_under_mutex(slot); |
| 1550 | |
| 1551 | LOCAL_OUTPUT_FREE(data_external, data); |
| 1552 | return (status == PSA_SUCCESS) ? unlock_status : status; |
| 1553 | } |
| 1554 | |
| 1555 | psa_status_t psa_export_public_key_internal( |
| 1556 | const psa_key_attributes_t *attributes, |
| 1557 | const uint8_t *key_buffer, |
| 1558 | size_t key_buffer_size, |
| 1559 | uint8_t *data, |
| 1560 | size_t data_size, |
| 1561 | size_t *data_length) |
| 1562 | { |
| 1563 | psa_key_type_t type = attributes->type; |
| 1564 | |
| 1565 | if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) && |
| 1566 | (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) || |
| 1567 | PSA_KEY_TYPE_IS_DH(type))) { |
| 1568 | /* Exporting public -> public */ |
| 1569 | return psa_export_key_buffer_internal( |
| 1570 | key_buffer, key_buffer_size, |
| 1571 | data, data_size, data_length); |
| 1572 | } else if (PSA_KEY_TYPE_IS_RSA(type)) { |
| 1573 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \ |
| 1574 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) |
| 1575 | return mbedtls_psa_rsa_export_public_key(attributes, |
| 1576 | key_buffer, |
| 1577 | key_buffer_size, |
| 1578 | data, |
| 1579 | data_size, |
| 1580 | data_length); |
| 1581 | #else |
| 1582 | /* We don't know how to convert a private RSA key to public. */ |
| 1583 | return PSA_ERROR_NOT_SUPPORTED; |
| 1584 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || |
| 1585 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ |
| 1586 | } else if (PSA_KEY_TYPE_IS_ECC(type)) { |
| 1587 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \ |
| 1588 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) |
| 1589 | return mbedtls_psa_ecp_export_public_key(attributes, |
| 1590 | key_buffer, |
| 1591 | key_buffer_size, |
| 1592 | data, |
| 1593 | data_size, |
| 1594 | data_length); |
| 1595 | #else |
| 1596 | /* We don't know how to convert a private ECC key to public */ |
| 1597 | return PSA_ERROR_NOT_SUPPORTED; |
| 1598 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || |
| 1599 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */ |
| 1600 | } else if (PSA_KEY_TYPE_IS_DH(type)) { |
| 1601 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \ |
| 1602 | defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) |
| 1603 | return mbedtls_psa_ffdh_export_public_key(attributes, |
| 1604 | key_buffer, |
| 1605 | key_buffer_size, |
| 1606 | data, data_size, |
| 1607 | data_length); |
| 1608 | #else |
| 1609 | return PSA_ERROR_NOT_SUPPORTED; |
| 1610 | #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || |
| 1611 | * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */ |
| 1612 | } else { |
| 1613 | (void) key_buffer; |
| 1614 | (void) key_buffer_size; |
| 1615 | (void) data; |
| 1616 | (void) data_size; |
| 1617 | (void) data_length; |
| 1618 | return PSA_ERROR_NOT_SUPPORTED; |
| 1619 | } |
| 1620 | } |
| 1621 | |
| 1622 | psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key, |
| 1623 | uint8_t *data_external, |
| 1624 | size_t data_size, |
| 1625 | size_t *data_length) |
| 1626 | { |
| 1627 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1628 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1629 | psa_key_slot_t *slot; |
| 1630 | |
| 1631 | LOCAL_OUTPUT_DECLARE(data_external, data); |
| 1632 | |
| 1633 | /* Reject a zero-length output buffer now, since this can never be a |
| 1634 | * valid key representation. This way we know that data must be a valid |
| 1635 | * pointer and we can do things like memset(data, ..., data_size). */ |
| 1636 | if (data_size == 0) { |
| 1637 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 1638 | } |
| 1639 | |
| 1640 | /* Set the key to empty now, so that even when there are errors, we always |
| 1641 | * set data_length to a value between 0 and data_size. On error, setting |
| 1642 | * the key to empty is a good choice because an empty key representation is |
| 1643 | * unlikely to be accepted anywhere. */ |
| 1644 | *data_length = 0; |
| 1645 | |
| 1646 | /* Exporting a public key doesn't require a usage flag. */ |
| 1647 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0); |
| 1648 | if (status != PSA_SUCCESS) { |
| 1649 | return status; |
| 1650 | } |
| 1651 | |
| 1652 | LOCAL_OUTPUT_ALLOC(data_external, data_size, data); |
| 1653 | |
| 1654 | if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) { |
| 1655 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 1656 | goto exit; |
| 1657 | } |
| 1658 | |
| 1659 | status = psa_driver_wrapper_export_public_key( |
| 1660 | &slot->attr, slot->key.data, slot->key.bytes, |
| 1661 | data, data_size, data_length); |
| 1662 | |
| 1663 | exit: |
| 1664 | unlock_status = psa_unregister_read_under_mutex(slot); |
| 1665 | |
| 1666 | LOCAL_OUTPUT_FREE(data_external, data); |
| 1667 | return (status == PSA_SUCCESS) ? unlock_status : status; |
| 1668 | } |
| 1669 | |
| 1670 | /** Validate that a key policy is internally well-formed. |
| 1671 | * |
| 1672 | * This function only rejects invalid policies. It does not validate the |
| 1673 | * consistency of the policy with respect to other attributes of the key |
| 1674 | * such as the key type. |
| 1675 | */ |
| 1676 | static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy) |
| 1677 | { |
| 1678 | if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT | |
| 1679 | PSA_KEY_USAGE_COPY | |
| 1680 | PSA_KEY_USAGE_ENCRYPT | |
| 1681 | PSA_KEY_USAGE_DECRYPT | |
| 1682 | PSA_KEY_USAGE_SIGN_MESSAGE | |
| 1683 | PSA_KEY_USAGE_VERIFY_MESSAGE | |
| 1684 | PSA_KEY_USAGE_SIGN_HASH | |
| 1685 | PSA_KEY_USAGE_VERIFY_HASH | |
| 1686 | PSA_KEY_USAGE_VERIFY_DERIVATION | |
| 1687 | PSA_KEY_USAGE_DERIVE)) != 0) { |
| 1688 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1689 | } |
| 1690 | |
| 1691 | return PSA_SUCCESS; |
| 1692 | } |
| 1693 | |
| 1694 | /** Validate the internal consistency of key attributes. |
| 1695 | * |
| 1696 | * This function only rejects invalid attribute values. If does not |
| 1697 | * validate the consistency of the attributes with any key data that may |
| 1698 | * be involved in the creation of the key. |
| 1699 | * |
| 1700 | * Call this function early in the key creation process. |
| 1701 | * |
| 1702 | * \param[in] attributes Key attributes for the new key. |
| 1703 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1704 | * NULL for a transparent key. |
| 1705 | * |
| 1706 | */ |
| 1707 | static psa_status_t psa_validate_key_attributes( |
| 1708 | const psa_key_attributes_t *attributes, |
| 1709 | psa_se_drv_table_entry_t **p_drv) |
| 1710 | { |
| 1711 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 1712 | psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes); |
| 1713 | mbedtls_svc_key_id_t key = psa_get_key_id(attributes); |
| 1714 | |
| 1715 | status = psa_validate_key_location(lifetime, p_drv); |
| 1716 | if (status != PSA_SUCCESS) { |
| 1717 | return status; |
| 1718 | } |
| 1719 | |
| 1720 | status = psa_validate_key_persistence(lifetime); |
| 1721 | if (status != PSA_SUCCESS) { |
| 1722 | return status; |
| 1723 | } |
| 1724 | |
| 1725 | if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) { |
| 1726 | if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) { |
| 1727 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1728 | } |
| 1729 | } else { |
| 1730 | if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) { |
| 1731 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1732 | } |
| 1733 | } |
| 1734 | |
| 1735 | status = psa_validate_key_policy(&attributes->policy); |
| 1736 | if (status != PSA_SUCCESS) { |
| 1737 | return status; |
| 1738 | } |
| 1739 | |
| 1740 | /* Refuse to create overly large keys. |
| 1741 | * Note that this doesn't trigger on import if the attributes don't |
| 1742 | * explicitly specify a size (so psa_get_key_bits returns 0), so |
| 1743 | * psa_import_key() needs its own checks. */ |
| 1744 | if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) { |
| 1745 | return PSA_ERROR_NOT_SUPPORTED; |
| 1746 | } |
| 1747 | |
| 1748 | return PSA_SUCCESS; |
| 1749 | } |
| 1750 | |
| 1751 | /** Prepare a key slot to receive key material. |
| 1752 | * |
| 1753 | * This function allocates a key slot and sets its metadata. |
| 1754 | * |
| 1755 | * If this function fails, call psa_fail_key_creation(). |
| 1756 | * |
| 1757 | * This function is intended to be used as follows: |
| 1758 | * -# Call psa_start_key_creation() to allocate a key slot, prepare |
| 1759 | * it with the specified attributes, and in case of a volatile key assign it |
| 1760 | * a volatile key identifier. |
| 1761 | * -# Populate the slot with the key material. |
| 1762 | * -# Call psa_finish_key_creation() to finalize the creation of the slot. |
| 1763 | * In case of failure at any step, stop the sequence and call |
| 1764 | * psa_fail_key_creation(). |
| 1765 | * |
| 1766 | * On success, the key slot's state is PSA_SLOT_FILLING. |
| 1767 | * It is the responsibility of the caller to change the slot's state to |
| 1768 | * PSA_SLOT_EMPTY/FULL once key creation has finished. |
| 1769 | * |
| 1770 | * \param method An identification of the calling function. |
| 1771 | * \param[in] attributes Key attributes for the new key. |
| 1772 | * \param[out] p_slot On success, a pointer to the prepared slot. |
| 1773 | * \param[out] p_drv On any return, the driver for the key, if any. |
| 1774 | * NULL for a transparent key. |
| 1775 | * |
| 1776 | * \retval #PSA_SUCCESS |
| 1777 | * The key slot is ready to receive key material. |
| 1778 | * \return If this function fails, the key slot is an invalid state. |
| 1779 | * You must call psa_fail_key_creation() to wipe and free the slot. |
| 1780 | */ |
| 1781 | static psa_status_t psa_start_key_creation( |
| 1782 | psa_key_creation_method_t method, |
| 1783 | const psa_key_attributes_t *attributes, |
| 1784 | psa_key_slot_t **p_slot, |
| 1785 | psa_se_drv_table_entry_t **p_drv) |
| 1786 | { |
| 1787 | psa_status_t status; |
| 1788 | |
| 1789 | (void) method; |
| 1790 | *p_drv = NULL; |
| 1791 | |
| 1792 | status = psa_validate_key_attributes(attributes, p_drv); |
| 1793 | if (status != PSA_SUCCESS) { |
| 1794 | return status; |
| 1795 | } |
| 1796 | |
| 1797 | int key_is_volatile = PSA_KEY_LIFETIME_IS_VOLATILE(attributes->lifetime); |
| 1798 | psa_key_id_t volatile_key_id; |
| 1799 | |
| 1800 | #if defined(MBEDTLS_THREADING_C) |
| 1801 | PSA_THREADING_CHK_RET(mbedtls_mutex_lock( |
| 1802 | &mbedtls_threading_key_slot_mutex)); |
| 1803 | #endif |
| 1804 | status = psa_reserve_free_key_slot( |
| 1805 | key_is_volatile ? &volatile_key_id : NULL, |
| 1806 | p_slot); |
| 1807 | #if defined(MBEDTLS_THREADING_C) |
| 1808 | PSA_THREADING_CHK_RET(mbedtls_mutex_unlock( |
| 1809 | &mbedtls_threading_key_slot_mutex)); |
| 1810 | #endif |
| 1811 | if (status != PSA_SUCCESS) { |
| 1812 | return status; |
| 1813 | } |
| 1814 | psa_key_slot_t *slot = *p_slot; |
| 1815 | |
| 1816 | /* We're storing the declared bit-size of the key. It's up to each |
| 1817 | * creation mechanism to verify that this information is correct. |
| 1818 | * It's automatically correct for mechanisms that use the bit-size as |
| 1819 | * an input (generate, device) but not for those where the bit-size |
| 1820 | * is optional (import, copy). In case of a volatile key, assign it the |
| 1821 | * volatile key identifier associated to the slot returned to contain its |
| 1822 | * definition. */ |
| 1823 | |
| 1824 | slot->attr = *attributes; |
| 1825 | if (key_is_volatile) { |
| 1826 | #if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 1827 | slot->attr.id = volatile_key_id; |
| 1828 | #else |
| 1829 | slot->attr.id.key_id = volatile_key_id; |
| 1830 | #endif |
| 1831 | } |
| 1832 | |
| 1833 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1834 | /* For a key in a secure element, we need to do three things |
| 1835 | * when creating or registering a persistent key: |
| 1836 | * create the key file in internal storage, create the |
| 1837 | * key inside the secure element, and update the driver's |
| 1838 | * persistent data. This is done by starting a transaction that will |
| 1839 | * encompass these three actions. |
| 1840 | * For registering a volatile key, we just need to find an appropriate |
| 1841 | * slot number inside the SE. Since the key is designated volatile, creating |
| 1842 | * a transaction is not required. */ |
| 1843 | /* The first thing to do is to find a slot number for the new key. |
| 1844 | * We save the slot number in persistent storage as part of the |
| 1845 | * transaction data. It will be needed to recover if the power |
| 1846 | * fails during the key creation process, to clean up on the secure |
| 1847 | * element side after restarting. Obtaining a slot number from the |
| 1848 | * secure element driver updates its persistent state, but we do not yet |
| 1849 | * save the driver's persistent state, so that if the power fails, |
| 1850 | * we can roll back to a state where the key doesn't exist. */ |
| 1851 | if (*p_drv != NULL) { |
| 1852 | psa_key_slot_number_t slot_number; |
| 1853 | status = psa_find_se_slot_for_key(attributes, method, *p_drv, |
| 1854 | &slot_number); |
| 1855 | if (status != PSA_SUCCESS) { |
| 1856 | return status; |
| 1857 | } |
| 1858 | |
| 1859 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->lifetime)) { |
| 1860 | psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY); |
| 1861 | psa_crypto_transaction.key.lifetime = slot->attr.lifetime; |
| 1862 | psa_crypto_transaction.key.slot = slot_number; |
| 1863 | psa_crypto_transaction.key.id = slot->attr.id; |
| 1864 | status = psa_crypto_save_transaction(); |
| 1865 | if (status != PSA_SUCCESS) { |
| 1866 | (void) psa_crypto_stop_transaction(); |
| 1867 | return status; |
| 1868 | } |
| 1869 | } |
| 1870 | |
| 1871 | status = psa_copy_key_material_into_slot( |
| 1872 | slot, (uint8_t *) (&slot_number), sizeof(slot_number)); |
| 1873 | if (status != PSA_SUCCESS) { |
| 1874 | return status; |
| 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) { |
| 1879 | /* Key registration only makes sense with a secure element. */ |
| 1880 | return PSA_ERROR_INVALID_ARGUMENT; |
| 1881 | } |
| 1882 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1883 | |
| 1884 | return PSA_SUCCESS; |
| 1885 | } |
| 1886 | |
| 1887 | /** Finalize the creation of a key once its key material has been set. |
| 1888 | * |
| 1889 | * This entails writing the key to persistent storage. |
| 1890 | * |
| 1891 | * If this function fails, call psa_fail_key_creation(). |
| 1892 | * See the documentation of psa_start_key_creation() for the intended use |
| 1893 | * of this function. |
| 1894 | * |
| 1895 | * If the finalization succeeds, the function sets the key slot's state to |
| 1896 | * PSA_SLOT_FULL, and the key slot can no longer be accessed as part of the |
| 1897 | * key creation process. |
| 1898 | * |
| 1899 | * \param[in,out] slot Pointer to the slot with key material. |
| 1900 | * \param[in] driver The secure element driver for the key, |
| 1901 | * or NULL for a transparent key. |
| 1902 | * \param[out] key On success, identifier of the key. Note that the |
| 1903 | * key identifier is also stored in the key slot. |
| 1904 | * |
| 1905 | * \retval #PSA_SUCCESS |
| 1906 | * The key was successfully created. |
| 1907 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription |
| 1908 | * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription |
| 1909 | * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription |
| 1910 | * \retval #PSA_ERROR_DATA_INVALID \emptydescription |
| 1911 | * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription |
| 1912 | * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription |
| 1913 | * |
| 1914 | * \return If this function fails, the key slot is an invalid state. |
| 1915 | * You must call psa_fail_key_creation() to wipe and free the slot. |
| 1916 | */ |
| 1917 | static psa_status_t psa_finish_key_creation( |
| 1918 | psa_key_slot_t *slot, |
| 1919 | psa_se_drv_table_entry_t *driver, |
| 1920 | mbedtls_svc_key_id_t *key) |
| 1921 | { |
| 1922 | psa_status_t status = PSA_SUCCESS; |
| 1923 | (void) slot; |
| 1924 | (void) driver; |
| 1925 | |
| 1926 | #if defined(MBEDTLS_THREADING_C) |
| 1927 | PSA_THREADING_CHK_RET(mbedtls_mutex_lock( |
| 1928 | &mbedtls_threading_key_slot_mutex)); |
| 1929 | #endif |
| 1930 | |
| 1931 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
| 1932 | if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) { |
| 1933 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1934 | if (driver != NULL) { |
| 1935 | psa_se_key_data_storage_t data; |
| 1936 | psa_key_slot_number_t slot_number = |
| 1937 | psa_key_slot_get_slot_number(slot); |
| 1938 | |
| 1939 | MBEDTLS_STATIC_ASSERT(sizeof(slot_number) == |
| 1940 | sizeof(data.slot_number), |
| 1941 | "Slot number size does not match psa_se_key_data_storage_t"); |
| 1942 | |
| 1943 | memcpy(&data.slot_number, &slot_number, sizeof(slot_number)); |
| 1944 | status = psa_save_persistent_key(&slot->attr, |
| 1945 | (uint8_t *) &data, |
| 1946 | sizeof(data)); |
| 1947 | } else |
| 1948 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1949 | { |
| 1950 | /* Key material is saved in export representation in the slot, so |
| 1951 | * just pass the slot buffer for storage. */ |
| 1952 | status = psa_save_persistent_key(&slot->attr, |
| 1953 | slot->key.data, |
| 1954 | slot->key.bytes); |
| 1955 | } |
| 1956 | } |
| 1957 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 1958 | |
| 1959 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 1960 | /* Finish the transaction for a key creation. This does not |
| 1961 | * happen when registering an existing key. Detect this case |
| 1962 | * by checking whether a transaction is in progress (actual |
| 1963 | * creation of a persistent key in a secure element requires a transaction, |
| 1964 | * but registration or volatile key creation doesn't use one). */ |
| 1965 | if (driver != NULL && |
| 1966 | psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) { |
| 1967 | status = psa_save_se_persistent_data(driver); |
| 1968 | if (status != PSA_SUCCESS) { |
| 1969 | psa_destroy_persistent_key(slot->attr.id); |
| 1970 | |
| 1971 | #if defined(MBEDTLS_THREADING_C) |
| 1972 | PSA_THREADING_CHK_RET(mbedtls_mutex_unlock( |
| 1973 | &mbedtls_threading_key_slot_mutex)); |
| 1974 | #endif |
| 1975 | return status; |
| 1976 | } |
| 1977 | status = psa_crypto_stop_transaction(); |
| 1978 | } |
| 1979 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 1980 | |
| 1981 | if (status == PSA_SUCCESS) { |
| 1982 | *key = slot->attr.id; |
| 1983 | status = psa_key_slot_state_transition(slot, PSA_SLOT_FILLING, |
| 1984 | PSA_SLOT_FULL); |
| 1985 | if (status != PSA_SUCCESS) { |
| 1986 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1987 | } |
| 1988 | } |
| 1989 | |
| 1990 | #if defined(MBEDTLS_THREADING_C) |
| 1991 | PSA_THREADING_CHK_RET(mbedtls_mutex_unlock( |
| 1992 | &mbedtls_threading_key_slot_mutex)); |
| 1993 | #endif |
| 1994 | return status; |
| 1995 | } |
| 1996 | |
| 1997 | /** Abort the creation of a key. |
| 1998 | * |
| 1999 | * You may call this function after calling psa_start_key_creation(), |
| 2000 | * or after psa_finish_key_creation() fails. In other circumstances, this |
| 2001 | * function may not clean up persistent storage. |
| 2002 | * See the documentation of psa_start_key_creation() for the intended use |
| 2003 | * of this function. Sets the slot's state to PSA_SLOT_EMPTY. |
| 2004 | * |
| 2005 | * \param[in,out] slot Pointer to the slot with key material. |
| 2006 | * \param[in] driver The secure element driver for the key, |
| 2007 | * or NULL for a transparent key. |
| 2008 | */ |
| 2009 | static void psa_fail_key_creation(psa_key_slot_t *slot, |
| 2010 | psa_se_drv_table_entry_t *driver) |
| 2011 | { |
| 2012 | (void) driver; |
| 2013 | |
| 2014 | if (slot == NULL) { |
| 2015 | return; |
| 2016 | } |
| 2017 | |
| 2018 | #if defined(MBEDTLS_THREADING_C) |
| 2019 | /* If the lock operation fails we still wipe the slot. |
| 2020 | * Operations will no longer work after a failed lock, |
| 2021 | * but we still need to wipe the slot of confidential data. */ |
| 2022 | mbedtls_mutex_lock(&mbedtls_threading_key_slot_mutex); |
| 2023 | #endif |
| 2024 | |
| 2025 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2026 | /* TODO: If the key has already been created in the secure |
| 2027 | * element, and the failure happened later (when saving metadata |
| 2028 | * to internal storage), we need to destroy the key in the secure |
| 2029 | * element. |
| 2030 | * https://github.com/ARMmbed/mbed-crypto/issues/217 |
| 2031 | */ |
| 2032 | |
| 2033 | /* Abort the ongoing transaction if any (there may not be one if |
| 2034 | * the creation process failed before starting one, or if the |
| 2035 | * key creation is a registration of a key in a secure element). |
| 2036 | * Earlier functions must already have done what it takes to undo any |
| 2037 | * partial creation. All that's left is to update the transaction data |
| 2038 | * itself. */ |
| 2039 | (void) psa_crypto_stop_transaction(); |
| 2040 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2041 | |
| 2042 | psa_wipe_key_slot(slot); |
| 2043 | |
| 2044 | #if defined(MBEDTLS_THREADING_C) |
| 2045 | mbedtls_mutex_unlock(&mbedtls_threading_key_slot_mutex); |
| 2046 | #endif |
| 2047 | } |
| 2048 | |
| 2049 | /** Validate optional attributes during key creation. |
| 2050 | * |
| 2051 | * Some key attributes are optional during key creation. If they are |
| 2052 | * specified in the attributes structure, check that they are consistent |
| 2053 | * with the data in the slot. |
| 2054 | * |
| 2055 | * This function should be called near the end of key creation, after |
| 2056 | * the slot in memory is fully populated but before saving persistent data. |
| 2057 | */ |
| 2058 | static psa_status_t psa_validate_optional_attributes( |
| 2059 | const psa_key_slot_t *slot, |
| 2060 | const psa_key_attributes_t *attributes) |
| 2061 | { |
| 2062 | if (attributes->type != 0) { |
| 2063 | if (attributes->type != slot->attr.type) { |
| 2064 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2065 | } |
| 2066 | } |
| 2067 | |
| 2068 | if (attributes->bits != 0) { |
| 2069 | if (attributes->bits != slot->attr.bits) { |
| 2070 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2071 | } |
| 2072 | } |
| 2073 | |
| 2074 | return PSA_SUCCESS; |
| 2075 | } |
| 2076 | |
| 2077 | psa_status_t psa_import_key(const psa_key_attributes_t *attributes, |
| 2078 | const uint8_t *data_external, |
| 2079 | size_t data_length, |
| 2080 | mbedtls_svc_key_id_t *key) |
| 2081 | { |
| 2082 | psa_status_t status; |
| 2083 | LOCAL_INPUT_DECLARE(data_external, data); |
| 2084 | psa_key_slot_t *slot = NULL; |
| 2085 | psa_se_drv_table_entry_t *driver = NULL; |
| 2086 | size_t bits; |
| 2087 | size_t storage_size = data_length; |
| 2088 | |
| 2089 | *key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2090 | |
| 2091 | /* Reject zero-length symmetric keys (including raw data key objects). |
| 2092 | * This also rejects any key which might be encoded as an empty string, |
| 2093 | * which is never valid. */ |
| 2094 | if (data_length == 0) { |
| 2095 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2096 | } |
| 2097 | |
| 2098 | /* Ensure that the bytes-to-bits conversion cannot overflow. */ |
| 2099 | if (data_length > SIZE_MAX / 8) { |
| 2100 | return PSA_ERROR_NOT_SUPPORTED; |
| 2101 | } |
| 2102 | |
| 2103 | LOCAL_INPUT_ALLOC(data_external, data_length, data); |
| 2104 | |
| 2105 | status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes, |
| 2106 | &slot, &driver); |
| 2107 | if (status != PSA_SUCCESS) { |
| 2108 | goto exit; |
| 2109 | } |
| 2110 | |
| 2111 | /* In the case of a transparent key or an opaque key stored in local |
| 2112 | * storage ( thus not in the case of importing a key in a secure element |
| 2113 | * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a |
| 2114 | * buffer to hold the imported key material. */ |
| 2115 | if (slot->key.bytes == 0) { |
| 2116 | if (psa_key_lifetime_is_external(attributes->lifetime)) { |
| 2117 | status = psa_driver_wrapper_get_key_buffer_size_from_key_data( |
| 2118 | attributes, data, data_length, &storage_size); |
| 2119 | if (status != PSA_SUCCESS) { |
| 2120 | goto exit; |
| 2121 | } |
| 2122 | } |
| 2123 | status = psa_allocate_buffer_to_slot(slot, storage_size); |
| 2124 | if (status != PSA_SUCCESS) { |
| 2125 | goto exit; |
| 2126 | } |
| 2127 | } |
| 2128 | |
| 2129 | bits = slot->attr.bits; |
| 2130 | status = psa_driver_wrapper_import_key(attributes, |
| 2131 | data, data_length, |
| 2132 | slot->key.data, |
| 2133 | slot->key.bytes, |
| 2134 | &slot->key.bytes, &bits); |
| 2135 | if (status != PSA_SUCCESS) { |
| 2136 | goto exit; |
| 2137 | } |
| 2138 | |
| 2139 | if (slot->attr.bits == 0) { |
| 2140 | slot->attr.bits = (psa_key_bits_t) bits; |
| 2141 | } else if (bits != slot->attr.bits) { |
| 2142 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2143 | goto exit; |
| 2144 | } |
| 2145 | |
| 2146 | /* Enforce a size limit, and in particular ensure that the bit |
| 2147 | * size fits in its representation type.*/ |
| 2148 | if (bits > PSA_MAX_KEY_BITS) { |
| 2149 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2150 | goto exit; |
| 2151 | } |
| 2152 | status = psa_validate_optional_attributes(slot, attributes); |
| 2153 | if (status != PSA_SUCCESS) { |
| 2154 | goto exit; |
| 2155 | } |
| 2156 | |
| 2157 | status = psa_finish_key_creation(slot, driver, key); |
| 2158 | exit: |
| 2159 | LOCAL_INPUT_FREE(data_external, data); |
| 2160 | if (status != PSA_SUCCESS) { |
| 2161 | psa_fail_key_creation(slot, driver); |
| 2162 | } |
| 2163 | |
| 2164 | return status; |
| 2165 | } |
| 2166 | |
| 2167 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 2168 | psa_status_t mbedtls_psa_register_se_key( |
| 2169 | const psa_key_attributes_t *attributes) |
| 2170 | { |
| 2171 | psa_status_t status; |
| 2172 | psa_key_slot_t *slot = NULL; |
| 2173 | psa_se_drv_table_entry_t *driver = NULL; |
| 2174 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2175 | |
| 2176 | /* Leaving attributes unspecified is not currently supported. |
| 2177 | * It could make sense to query the key type and size from the |
| 2178 | * secure element, but not all secure elements support this |
| 2179 | * and the driver HAL doesn't currently support it. */ |
| 2180 | if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) { |
| 2181 | return PSA_ERROR_NOT_SUPPORTED; |
| 2182 | } |
| 2183 | if (psa_get_key_bits(attributes) == 0) { |
| 2184 | return PSA_ERROR_NOT_SUPPORTED; |
| 2185 | } |
| 2186 | |
| 2187 | /* Not usable with volatile keys, even with an appropriate location, |
| 2188 | * due to the API design. |
| 2189 | * https://github.com/Mbed-TLS/mbedtls/issues/9253 |
| 2190 | */ |
| 2191 | if (PSA_KEY_LIFETIME_IS_VOLATILE(psa_get_key_lifetime(attributes))) { |
| 2192 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2193 | } |
| 2194 | |
| 2195 | status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes, |
| 2196 | &slot, &driver); |
| 2197 | if (status != PSA_SUCCESS) { |
| 2198 | goto exit; |
| 2199 | } |
| 2200 | |
| 2201 | status = psa_finish_key_creation(slot, driver, &key); |
| 2202 | |
| 2203 | exit: |
| 2204 | if (status != PSA_SUCCESS) { |
| 2205 | psa_fail_key_creation(slot, driver); |
| 2206 | } |
| 2207 | |
| 2208 | /* Registration doesn't keep the key in RAM. */ |
| 2209 | psa_close_key(key); |
| 2210 | return status; |
| 2211 | } |
| 2212 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 2213 | |
| 2214 | psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key, |
| 2215 | const psa_key_attributes_t *specified_attributes, |
| 2216 | mbedtls_svc_key_id_t *target_key) |
| 2217 | { |
| 2218 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2219 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2220 | psa_key_slot_t *source_slot = NULL; |
| 2221 | psa_key_slot_t *target_slot = NULL; |
| 2222 | psa_key_attributes_t actual_attributes = *specified_attributes; |
| 2223 | psa_se_drv_table_entry_t *driver = NULL; |
| 2224 | size_t storage_size = 0; |
| 2225 | |
| 2226 | *target_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2227 | |
| 2228 | status = psa_get_and_lock_key_slot_with_policy( |
| 2229 | source_key, &source_slot, PSA_KEY_USAGE_COPY, 0); |
| 2230 | if (status != PSA_SUCCESS) { |
| 2231 | goto exit; |
| 2232 | } |
| 2233 | |
| 2234 | status = psa_validate_optional_attributes(source_slot, |
| 2235 | specified_attributes); |
| 2236 | if (status != PSA_SUCCESS) { |
| 2237 | goto exit; |
| 2238 | } |
| 2239 | |
| 2240 | /* The target key type and number of bits have been validated by |
| 2241 | * psa_validate_optional_attributes() to be either equal to zero or |
| 2242 | * equal to the ones of the source key. So it is safe to inherit |
| 2243 | * them from the source key now." |
| 2244 | * */ |
| 2245 | actual_attributes.bits = source_slot->attr.bits; |
| 2246 | actual_attributes.type = source_slot->attr.type; |
| 2247 | |
| 2248 | |
| 2249 | status = psa_restrict_key_policy(source_slot->attr.type, |
| 2250 | &actual_attributes.policy, |
| 2251 | &source_slot->attr.policy); |
| 2252 | if (status != PSA_SUCCESS) { |
| 2253 | goto exit; |
| 2254 | } |
| 2255 | |
| 2256 | status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes, |
| 2257 | &target_slot, &driver); |
| 2258 | if (status != PSA_SUCCESS) { |
| 2259 | goto exit; |
| 2260 | } |
| 2261 | if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) != |
| 2262 | PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) { |
| 2263 | /* |
| 2264 | * If the source and target keys are stored in different locations, |
| 2265 | * the source key would need to be exported as plaintext and re-imported |
| 2266 | * in the other location. This has security implications which have not |
| 2267 | * been fully mapped. For now, this can be achieved through |
| 2268 | * appropriate API invocations from the application, if needed. |
| 2269 | * */ |
| 2270 | status = PSA_ERROR_NOT_SUPPORTED; |
| 2271 | goto exit; |
| 2272 | } |
| 2273 | /* |
| 2274 | * When the source and target keys are within the same location, |
| 2275 | * - For transparent keys it is a blind copy without any driver invocation, |
| 2276 | * - For opaque keys this translates to an invocation of the drivers' |
| 2277 | * copy_key entry point through the dispatch layer. |
| 2278 | * */ |
| 2279 | if (psa_key_lifetime_is_external(actual_attributes.lifetime)) { |
| 2280 | status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes, |
| 2281 | &storage_size); |
| 2282 | if (status != PSA_SUCCESS) { |
| 2283 | goto exit; |
| 2284 | } |
| 2285 | |
| 2286 | status = psa_allocate_buffer_to_slot(target_slot, storage_size); |
| 2287 | if (status != PSA_SUCCESS) { |
| 2288 | goto exit; |
| 2289 | } |
| 2290 | |
| 2291 | status = psa_driver_wrapper_copy_key(&actual_attributes, |
| 2292 | source_slot->key.data, |
| 2293 | source_slot->key.bytes, |
| 2294 | target_slot->key.data, |
| 2295 | target_slot->key.bytes, |
| 2296 | &target_slot->key.bytes); |
| 2297 | if (status != PSA_SUCCESS) { |
| 2298 | goto exit; |
| 2299 | } |
| 2300 | } else { |
| 2301 | status = psa_copy_key_material_into_slot(target_slot, |
| 2302 | source_slot->key.data, |
| 2303 | source_slot->key.bytes); |
| 2304 | if (status != PSA_SUCCESS) { |
| 2305 | goto exit; |
| 2306 | } |
| 2307 | } |
| 2308 | status = psa_finish_key_creation(target_slot, driver, target_key); |
| 2309 | exit: |
| 2310 | if (status != PSA_SUCCESS) { |
| 2311 | psa_fail_key_creation(target_slot, driver); |
| 2312 | } |
| 2313 | |
| 2314 | unlock_status = psa_unregister_read_under_mutex(source_slot); |
| 2315 | |
| 2316 | return (status == PSA_SUCCESS) ? unlock_status : status; |
| 2317 | } |
| 2318 | |
| 2319 | |
| 2320 | |
| 2321 | /****************************************************************/ |
| 2322 | /* Message digests */ |
| 2323 | /****************************************************************/ |
| 2324 | |
| 2325 | static int is_hash_supported(psa_algorithm_t alg) |
| 2326 | { |
| 2327 | switch (alg) { |
| 2328 | #if defined(PSA_WANT_ALG_MD5) |
| 2329 | case PSA_ALG_MD5: |
| 2330 | return 1; |
| 2331 | #endif |
| 2332 | #if defined(PSA_WANT_ALG_RIPEMD160) |
| 2333 | case PSA_ALG_RIPEMD160: |
| 2334 | return 1; |
| 2335 | #endif |
| 2336 | #if defined(PSA_WANT_ALG_SHA_1) |
| 2337 | case PSA_ALG_SHA_1: |
| 2338 | return 1; |
| 2339 | #endif |
| 2340 | #if defined(PSA_WANT_ALG_SHA_224) |
| 2341 | case PSA_ALG_SHA_224: |
| 2342 | return 1; |
| 2343 | #endif |
| 2344 | #if defined(PSA_WANT_ALG_SHA_256) |
| 2345 | case PSA_ALG_SHA_256: |
| 2346 | return 1; |
| 2347 | #endif |
| 2348 | #if defined(PSA_WANT_ALG_SHA_384) |
| 2349 | case PSA_ALG_SHA_384: |
| 2350 | return 1; |
| 2351 | #endif |
| 2352 | #if defined(PSA_WANT_ALG_SHA_512) |
| 2353 | case PSA_ALG_SHA_512: |
| 2354 | return 1; |
| 2355 | #endif |
| 2356 | #if defined(PSA_WANT_ALG_SHA3_224) |
| 2357 | case PSA_ALG_SHA3_224: |
| 2358 | return 1; |
| 2359 | #endif |
| 2360 | #if defined(PSA_WANT_ALG_SHA3_256) |
| 2361 | case PSA_ALG_SHA3_256: |
| 2362 | return 1; |
| 2363 | #endif |
| 2364 | #if defined(PSA_WANT_ALG_SHA3_384) |
| 2365 | case PSA_ALG_SHA3_384: |
| 2366 | return 1; |
| 2367 | #endif |
| 2368 | #if defined(PSA_WANT_ALG_SHA3_512) |
| 2369 | case PSA_ALG_SHA3_512: |
| 2370 | return 1; |
| 2371 | #endif |
| 2372 | default: |
| 2373 | return 0; |
| 2374 | } |
| 2375 | } |
| 2376 | |
| 2377 | psa_status_t psa_hash_abort(psa_hash_operation_t *operation) |
| 2378 | { |
| 2379 | /* Aborting a non-active operation is allowed */ |
| 2380 | if (operation->id == 0) { |
| 2381 | return PSA_SUCCESS; |
| 2382 | } |
| 2383 | |
| 2384 | psa_status_t status = psa_driver_wrapper_hash_abort(operation); |
| 2385 | operation->id = 0; |
| 2386 | |
| 2387 | return status; |
| 2388 | } |
| 2389 | |
| 2390 | psa_status_t psa_hash_setup(psa_hash_operation_t *operation, |
| 2391 | psa_algorithm_t alg) |
| 2392 | { |
| 2393 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2394 | |
| 2395 | /* A context must be freshly initialized before it can be set up. */ |
| 2396 | if (operation->id != 0) { |
| 2397 | status = PSA_ERROR_BAD_STATE; |
| 2398 | goto exit; |
| 2399 | } |
| 2400 | |
| 2401 | if (!PSA_ALG_IS_HASH(alg)) { |
| 2402 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2403 | goto exit; |
| 2404 | } |
| 2405 | |
| 2406 | /* Make sure the driver-dependent part of the operation is zeroed. |
| 2407 | * This is a guarantee we make to drivers. Initializing the operation |
| 2408 | * does not necessarily take care of it, since the context is a |
| 2409 | * union and initializing a union does not necessarily initialize |
| 2410 | * all of its members. */ |
| 2411 | memset(&operation->ctx, 0, sizeof(operation->ctx)); |
| 2412 | |
| 2413 | status = psa_driver_wrapper_hash_setup(operation, alg); |
| 2414 | |
| 2415 | exit: |
| 2416 | if (status != PSA_SUCCESS) { |
| 2417 | psa_hash_abort(operation); |
| 2418 | } |
| 2419 | |
| 2420 | return status; |
| 2421 | } |
| 2422 | |
| 2423 | psa_status_t psa_hash_update(psa_hash_operation_t *operation, |
| 2424 | const uint8_t *input_external, |
| 2425 | size_t input_length) |
| 2426 | { |
| 2427 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2428 | LOCAL_INPUT_DECLARE(input_external, input); |
| 2429 | |
| 2430 | if (operation->id == 0) { |
| 2431 | status = PSA_ERROR_BAD_STATE; |
| 2432 | goto exit; |
| 2433 | } |
| 2434 | |
| 2435 | /* Don't require hash implementations to behave correctly on a |
| 2436 | * zero-length input, which may have an invalid pointer. */ |
| 2437 | if (input_length == 0) { |
| 2438 | return PSA_SUCCESS; |
| 2439 | } |
| 2440 | |
| 2441 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 2442 | status = psa_driver_wrapper_hash_update(operation, input, input_length); |
| 2443 | |
| 2444 | exit: |
| 2445 | if (status != PSA_SUCCESS) { |
| 2446 | psa_hash_abort(operation); |
| 2447 | } |
| 2448 | |
| 2449 | LOCAL_INPUT_FREE(input_external, input); |
| 2450 | return status; |
| 2451 | } |
| 2452 | |
| 2453 | static psa_status_t psa_hash_finish_internal(psa_hash_operation_t *operation, |
| 2454 | uint8_t *hash, |
| 2455 | size_t hash_size, |
| 2456 | size_t *hash_length) |
| 2457 | { |
| 2458 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2459 | |
| 2460 | *hash_length = 0; |
| 2461 | if (operation->id == 0) { |
| 2462 | return PSA_ERROR_BAD_STATE; |
| 2463 | } |
| 2464 | |
| 2465 | status = psa_driver_wrapper_hash_finish( |
| 2466 | operation, hash, hash_size, hash_length); |
| 2467 | psa_hash_abort(operation); |
| 2468 | |
| 2469 | return status; |
| 2470 | } |
| 2471 | |
| 2472 | psa_status_t psa_hash_finish(psa_hash_operation_t *operation, |
| 2473 | uint8_t *hash_external, |
| 2474 | size_t hash_size, |
| 2475 | size_t *hash_length) |
| 2476 | { |
| 2477 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2478 | LOCAL_OUTPUT_DECLARE(hash_external, hash); |
| 2479 | |
| 2480 | LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash); |
| 2481 | status = psa_hash_finish_internal(operation, hash, hash_size, hash_length); |
| 2482 | |
| 2483 | #if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) |
| 2484 | exit: |
| 2485 | #endif |
| 2486 | LOCAL_OUTPUT_FREE(hash_external, hash); |
| 2487 | return status; |
| 2488 | } |
| 2489 | |
| 2490 | psa_status_t psa_hash_verify(psa_hash_operation_t *operation, |
| 2491 | const uint8_t *hash_external, |
| 2492 | size_t hash_length) |
| 2493 | { |
| 2494 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
| 2495 | size_t actual_hash_length; |
| 2496 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2497 | LOCAL_INPUT_DECLARE(hash_external, hash); |
| 2498 | |
| 2499 | status = psa_hash_finish_internal( |
| 2500 | operation, |
| 2501 | actual_hash, sizeof(actual_hash), |
| 2502 | &actual_hash_length); |
| 2503 | |
| 2504 | if (status != PSA_SUCCESS) { |
| 2505 | goto exit; |
| 2506 | } |
| 2507 | |
| 2508 | if (actual_hash_length != hash_length) { |
| 2509 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2510 | goto exit; |
| 2511 | } |
| 2512 | |
| 2513 | LOCAL_INPUT_ALLOC(hash_external, hash_length, hash); |
| 2514 | if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) { |
| 2515 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2516 | } |
| 2517 | |
| 2518 | exit: |
| 2519 | mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash)); |
| 2520 | if (status != PSA_SUCCESS) { |
| 2521 | psa_hash_abort(operation); |
| 2522 | } |
| 2523 | LOCAL_INPUT_FREE(hash_external, hash); |
| 2524 | return status; |
| 2525 | } |
| 2526 | |
| 2527 | psa_status_t psa_hash_compute(psa_algorithm_t alg, |
| 2528 | const uint8_t *input_external, size_t input_length, |
| 2529 | uint8_t *hash_external, size_t hash_size, |
| 2530 | size_t *hash_length) |
| 2531 | { |
| 2532 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2533 | LOCAL_INPUT_DECLARE(input_external, input); |
| 2534 | LOCAL_OUTPUT_DECLARE(hash_external, hash); |
| 2535 | |
| 2536 | *hash_length = 0; |
| 2537 | if (!PSA_ALG_IS_HASH(alg)) { |
| 2538 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2539 | } |
| 2540 | |
| 2541 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 2542 | LOCAL_OUTPUT_ALLOC(hash_external, hash_size, hash); |
| 2543 | status = psa_driver_wrapper_hash_compute(alg, input, input_length, |
| 2544 | hash, hash_size, hash_length); |
| 2545 | |
| 2546 | #if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) |
| 2547 | exit: |
| 2548 | #endif |
| 2549 | LOCAL_INPUT_FREE(input_external, input); |
| 2550 | LOCAL_OUTPUT_FREE(hash_external, hash); |
| 2551 | return status; |
| 2552 | } |
| 2553 | |
| 2554 | psa_status_t psa_hash_compare(psa_algorithm_t alg, |
| 2555 | const uint8_t *input_external, size_t input_length, |
| 2556 | const uint8_t *hash_external, size_t hash_length) |
| 2557 | { |
| 2558 | uint8_t actual_hash[PSA_HASH_MAX_SIZE]; |
| 2559 | size_t actual_hash_length; |
| 2560 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2561 | |
| 2562 | LOCAL_INPUT_DECLARE(input_external, input); |
| 2563 | LOCAL_INPUT_DECLARE(hash_external, hash); |
| 2564 | |
| 2565 | if (!PSA_ALG_IS_HASH(alg)) { |
| 2566 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 2567 | return status; |
| 2568 | } |
| 2569 | |
| 2570 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 2571 | status = psa_driver_wrapper_hash_compute( |
| 2572 | alg, input, input_length, |
| 2573 | actual_hash, sizeof(actual_hash), |
| 2574 | &actual_hash_length); |
| 2575 | if (status != PSA_SUCCESS) { |
| 2576 | goto exit; |
| 2577 | } |
| 2578 | if (actual_hash_length != hash_length) { |
| 2579 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2580 | goto exit; |
| 2581 | } |
| 2582 | |
| 2583 | LOCAL_INPUT_ALLOC(hash_external, hash_length, hash); |
| 2584 | if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) { |
| 2585 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2586 | } |
| 2587 | |
| 2588 | exit: |
| 2589 | mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash)); |
| 2590 | |
| 2591 | LOCAL_INPUT_FREE(input_external, input); |
| 2592 | LOCAL_INPUT_FREE(hash_external, hash); |
| 2593 | |
| 2594 | return status; |
| 2595 | } |
| 2596 | |
| 2597 | psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation, |
| 2598 | psa_hash_operation_t *target_operation) |
| 2599 | { |
| 2600 | if (source_operation->id == 0 || |
| 2601 | target_operation->id != 0) { |
| 2602 | return PSA_ERROR_BAD_STATE; |
| 2603 | } |
| 2604 | |
| 2605 | /* Make sure the driver-dependent part of the operation is zeroed. |
| 2606 | * This is a guarantee we make to drivers. Initializing the operation |
| 2607 | * does not necessarily take care of it, since the context is a |
| 2608 | * union and initializing a union does not necessarily initialize |
| 2609 | * all of its members. */ |
| 2610 | memset(&target_operation->ctx, 0, sizeof(target_operation->ctx)); |
| 2611 | |
| 2612 | psa_status_t status = psa_driver_wrapper_hash_clone(source_operation, |
| 2613 | target_operation); |
| 2614 | if (status != PSA_SUCCESS) { |
| 2615 | psa_hash_abort(target_operation); |
| 2616 | } |
| 2617 | |
| 2618 | return status; |
| 2619 | } |
| 2620 | |
| 2621 | |
| 2622 | /****************************************************************/ |
| 2623 | /* MAC */ |
| 2624 | /****************************************************************/ |
| 2625 | |
| 2626 | psa_status_t psa_mac_abort(psa_mac_operation_t *operation) |
| 2627 | { |
| 2628 | /* Aborting a non-active operation is allowed */ |
| 2629 | if (operation->id == 0) { |
| 2630 | return PSA_SUCCESS; |
| 2631 | } |
| 2632 | |
| 2633 | psa_status_t status = psa_driver_wrapper_mac_abort(operation); |
| 2634 | operation->mac_size = 0; |
| 2635 | operation->is_sign = 0; |
| 2636 | operation->id = 0; |
| 2637 | |
| 2638 | return status; |
| 2639 | } |
| 2640 | |
| 2641 | static psa_status_t psa_mac_finalize_alg_and_key_validation( |
| 2642 | psa_algorithm_t alg, |
| 2643 | const psa_key_attributes_t *attributes, |
| 2644 | uint8_t *mac_size) |
| 2645 | { |
| 2646 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2647 | psa_key_type_t key_type = psa_get_key_type(attributes); |
| 2648 | size_t key_bits = psa_get_key_bits(attributes); |
| 2649 | |
| 2650 | if (!PSA_ALG_IS_MAC(alg)) { |
| 2651 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2652 | } |
| 2653 | |
| 2654 | /* Validate the combination of key type and algorithm */ |
| 2655 | status = psa_mac_key_can_do(alg, key_type); |
| 2656 | if (status != PSA_SUCCESS) { |
| 2657 | return status; |
| 2658 | } |
| 2659 | |
| 2660 | /* Get the output length for the algorithm and key combination */ |
| 2661 | *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg); |
| 2662 | |
| 2663 | if (*mac_size < 4) { |
| 2664 | /* A very short MAC is too short for security since it can be |
| 2665 | * brute-forced. Ancient protocols with 32-bit MACs do exist, |
| 2666 | * so we make this our minimum, even though 32 bits is still |
| 2667 | * too small for security. */ |
| 2668 | return PSA_ERROR_NOT_SUPPORTED; |
| 2669 | } |
| 2670 | |
| 2671 | if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits, |
| 2672 | PSA_ALG_FULL_LENGTH_MAC(alg))) { |
| 2673 | /* It's impossible to "truncate" to a larger length than the full length |
| 2674 | * of the algorithm. */ |
| 2675 | return PSA_ERROR_INVALID_ARGUMENT; |
| 2676 | } |
| 2677 | |
| 2678 | if (*mac_size > PSA_MAC_MAX_SIZE) { |
| 2679 | /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm |
| 2680 | * that is disabled in the compile-time configuration. The result can |
| 2681 | * therefore be larger than PSA_MAC_MAX_SIZE, which does take the |
| 2682 | * configuration into account. In this case, force a return of |
| 2683 | * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or |
| 2684 | * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return |
| 2685 | * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size |
| 2686 | * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks |
| 2687 | * systematically generated tests. */ |
| 2688 | return PSA_ERROR_NOT_SUPPORTED; |
| 2689 | } |
| 2690 | |
| 2691 | return PSA_SUCCESS; |
| 2692 | } |
| 2693 | |
| 2694 | static psa_status_t psa_mac_setup(psa_mac_operation_t *operation, |
| 2695 | mbedtls_svc_key_id_t key, |
| 2696 | psa_algorithm_t alg, |
| 2697 | int is_sign) |
| 2698 | { |
| 2699 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2700 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2701 | psa_key_slot_t *slot = NULL; |
| 2702 | |
| 2703 | /* A context must be freshly initialized before it can be set up. */ |
| 2704 | if (operation->id != 0) { |
| 2705 | status = PSA_ERROR_BAD_STATE; |
| 2706 | goto exit; |
| 2707 | } |
| 2708 | |
| 2709 | /* Make sure the driver-dependent part of the operation is zeroed. |
| 2710 | * This is a guarantee we make to drivers. Initializing the operation |
| 2711 | * does not necessarily take care of it, since the context is a |
| 2712 | * union and initializing a union does not necessarily initialize |
| 2713 | * all of its members. */ |
| 2714 | memset(&operation->ctx, 0, sizeof(operation->ctx)); |
| 2715 | |
| 2716 | status = psa_get_and_lock_key_slot_with_policy( |
| 2717 | key, |
| 2718 | &slot, |
| 2719 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
| 2720 | alg); |
| 2721 | if (status != PSA_SUCCESS) { |
| 2722 | goto exit; |
| 2723 | } |
| 2724 | |
| 2725 | status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr, |
| 2726 | &operation->mac_size); |
| 2727 | if (status != PSA_SUCCESS) { |
| 2728 | goto exit; |
| 2729 | } |
| 2730 | |
| 2731 | operation->is_sign = is_sign; |
| 2732 | /* Dispatch the MAC setup call with validated input */ |
| 2733 | if (is_sign) { |
| 2734 | status = psa_driver_wrapper_mac_sign_setup(operation, |
| 2735 | &slot->attr, |
| 2736 | slot->key.data, |
| 2737 | slot->key.bytes, |
| 2738 | alg); |
| 2739 | } else { |
| 2740 | status = psa_driver_wrapper_mac_verify_setup(operation, |
| 2741 | &slot->attr, |
| 2742 | slot->key.data, |
| 2743 | slot->key.bytes, |
| 2744 | alg); |
| 2745 | } |
| 2746 | |
| 2747 | exit: |
| 2748 | if (status != PSA_SUCCESS) { |
| 2749 | psa_mac_abort(operation); |
| 2750 | } |
| 2751 | |
| 2752 | unlock_status = psa_unregister_read_under_mutex(slot); |
| 2753 | |
| 2754 | return (status == PSA_SUCCESS) ? unlock_status : status; |
| 2755 | } |
| 2756 | |
| 2757 | psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation, |
| 2758 | mbedtls_svc_key_id_t key, |
| 2759 | psa_algorithm_t alg) |
| 2760 | { |
| 2761 | return psa_mac_setup(operation, key, alg, 1); |
| 2762 | } |
| 2763 | |
| 2764 | psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation, |
| 2765 | mbedtls_svc_key_id_t key, |
| 2766 | psa_algorithm_t alg) |
| 2767 | { |
| 2768 | return psa_mac_setup(operation, key, alg, 0); |
| 2769 | } |
| 2770 | |
| 2771 | psa_status_t psa_mac_update(psa_mac_operation_t *operation, |
| 2772 | const uint8_t *input_external, |
| 2773 | size_t input_length) |
| 2774 | { |
| 2775 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2776 | LOCAL_INPUT_DECLARE(input_external, input); |
| 2777 | |
| 2778 | if (operation->id == 0) { |
| 2779 | status = PSA_ERROR_BAD_STATE; |
| 2780 | return status; |
| 2781 | } |
| 2782 | |
| 2783 | /* Don't require hash implementations to behave correctly on a |
| 2784 | * zero-length input, which may have an invalid pointer. */ |
| 2785 | if (input_length == 0) { |
| 2786 | status = PSA_SUCCESS; |
| 2787 | return status; |
| 2788 | } |
| 2789 | |
| 2790 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 2791 | status = psa_driver_wrapper_mac_update(operation, input, input_length); |
| 2792 | |
| 2793 | if (status != PSA_SUCCESS) { |
| 2794 | psa_mac_abort(operation); |
| 2795 | } |
| 2796 | |
| 2797 | #if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) |
| 2798 | exit: |
| 2799 | #endif |
| 2800 | LOCAL_INPUT_FREE(input_external, input); |
| 2801 | |
| 2802 | return status; |
| 2803 | } |
| 2804 | |
| 2805 | psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation, |
| 2806 | uint8_t *mac_external, |
| 2807 | size_t mac_size, |
| 2808 | size_t *mac_length) |
| 2809 | { |
| 2810 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2811 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2812 | LOCAL_OUTPUT_DECLARE(mac_external, mac); |
| 2813 | LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac); |
| 2814 | |
| 2815 | if (operation->id == 0) { |
| 2816 | status = PSA_ERROR_BAD_STATE; |
| 2817 | goto exit; |
| 2818 | } |
| 2819 | |
| 2820 | if (!operation->is_sign) { |
| 2821 | status = PSA_ERROR_BAD_STATE; |
| 2822 | goto exit; |
| 2823 | } |
| 2824 | |
| 2825 | /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL) |
| 2826 | * once all the error checks are done. */ |
| 2827 | if (operation->mac_size == 0) { |
| 2828 | status = PSA_ERROR_BAD_STATE; |
| 2829 | goto exit; |
| 2830 | } |
| 2831 | |
| 2832 | if (mac_size < operation->mac_size) { |
| 2833 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2834 | goto exit; |
| 2835 | } |
| 2836 | |
| 2837 | |
| 2838 | status = psa_driver_wrapper_mac_sign_finish(operation, |
| 2839 | mac, operation->mac_size, |
| 2840 | mac_length); |
| 2841 | |
| 2842 | exit: |
| 2843 | /* In case of success, set the potential excess room in the output buffer |
| 2844 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2845 | * In case of error, set the output length and content to a safe default, |
| 2846 | * such that in case the caller misses an error check, the output would be |
| 2847 | * an unachievable MAC. |
| 2848 | */ |
| 2849 | if (status != PSA_SUCCESS) { |
| 2850 | *mac_length = mac_size; |
| 2851 | operation->mac_size = 0; |
| 2852 | } |
| 2853 | |
| 2854 | if (mac != NULL) { |
| 2855 | psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length); |
| 2856 | } |
| 2857 | |
| 2858 | abort_status = psa_mac_abort(operation); |
| 2859 | LOCAL_OUTPUT_FREE(mac_external, mac); |
| 2860 | |
| 2861 | return status == PSA_SUCCESS ? abort_status : status; |
| 2862 | } |
| 2863 | |
| 2864 | psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation, |
| 2865 | const uint8_t *mac_external, |
| 2866 | size_t mac_length) |
| 2867 | { |
| 2868 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2869 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2870 | LOCAL_INPUT_DECLARE(mac_external, mac); |
| 2871 | |
| 2872 | if (operation->id == 0) { |
| 2873 | status = PSA_ERROR_BAD_STATE; |
| 2874 | goto exit; |
| 2875 | } |
| 2876 | |
| 2877 | if (operation->is_sign) { |
| 2878 | status = PSA_ERROR_BAD_STATE; |
| 2879 | goto exit; |
| 2880 | } |
| 2881 | |
| 2882 | if (operation->mac_size != mac_length) { |
| 2883 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 2884 | goto exit; |
| 2885 | } |
| 2886 | |
| 2887 | LOCAL_INPUT_ALLOC(mac_external, mac_length, mac); |
| 2888 | status = psa_driver_wrapper_mac_verify_finish(operation, |
| 2889 | mac, mac_length); |
| 2890 | |
| 2891 | exit: |
| 2892 | abort_status = psa_mac_abort(operation); |
| 2893 | LOCAL_INPUT_FREE(mac_external, mac); |
| 2894 | |
| 2895 | return status == PSA_SUCCESS ? abort_status : status; |
| 2896 | } |
| 2897 | |
| 2898 | static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key, |
| 2899 | psa_algorithm_t alg, |
| 2900 | const uint8_t *input, |
| 2901 | size_t input_length, |
| 2902 | uint8_t *mac, |
| 2903 | size_t mac_size, |
| 2904 | size_t *mac_length, |
| 2905 | int is_sign) |
| 2906 | { |
| 2907 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2908 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2909 | psa_key_slot_t *slot; |
| 2910 | uint8_t operation_mac_size = 0; |
| 2911 | |
| 2912 | status = psa_get_and_lock_key_slot_with_policy( |
| 2913 | key, |
| 2914 | &slot, |
| 2915 | is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, |
| 2916 | alg); |
| 2917 | if (status != PSA_SUCCESS) { |
| 2918 | goto exit; |
| 2919 | } |
| 2920 | |
| 2921 | status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr, |
| 2922 | &operation_mac_size); |
| 2923 | if (status != PSA_SUCCESS) { |
| 2924 | goto exit; |
| 2925 | } |
| 2926 | |
| 2927 | if (mac_size < operation_mac_size) { |
| 2928 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 2929 | goto exit; |
| 2930 | } |
| 2931 | |
| 2932 | status = psa_driver_wrapper_mac_compute( |
| 2933 | &slot->attr, |
| 2934 | slot->key.data, slot->key.bytes, |
| 2935 | alg, |
| 2936 | input, input_length, |
| 2937 | mac, operation_mac_size, mac_length); |
| 2938 | |
| 2939 | exit: |
| 2940 | /* In case of success, set the potential excess room in the output buffer |
| 2941 | * to an invalid value, to avoid potentially leaking a longer MAC. |
| 2942 | * In case of error, set the output length and content to a safe default, |
| 2943 | * such that in case the caller misses an error check, the output would be |
| 2944 | * an unachievable MAC. |
| 2945 | */ |
| 2946 | if (status != PSA_SUCCESS) { |
| 2947 | *mac_length = mac_size; |
| 2948 | operation_mac_size = 0; |
| 2949 | } |
| 2950 | |
| 2951 | psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length); |
| 2952 | |
| 2953 | unlock_status = psa_unregister_read_under_mutex(slot); |
| 2954 | |
| 2955 | return (status == PSA_SUCCESS) ? unlock_status : status; |
| 2956 | } |
| 2957 | |
| 2958 | psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key, |
| 2959 | psa_algorithm_t alg, |
| 2960 | const uint8_t *input_external, |
| 2961 | size_t input_length, |
| 2962 | uint8_t *mac_external, |
| 2963 | size_t mac_size, |
| 2964 | size_t *mac_length) |
| 2965 | { |
| 2966 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2967 | LOCAL_INPUT_DECLARE(input_external, input); |
| 2968 | LOCAL_OUTPUT_DECLARE(mac_external, mac); |
| 2969 | |
| 2970 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 2971 | LOCAL_OUTPUT_ALLOC(mac_external, mac_size, mac); |
| 2972 | status = psa_mac_compute_internal(key, alg, |
| 2973 | input, input_length, |
| 2974 | mac, mac_size, mac_length, 1); |
| 2975 | |
| 2976 | #if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) |
| 2977 | exit: |
| 2978 | #endif |
| 2979 | LOCAL_INPUT_FREE(input_external, input); |
| 2980 | LOCAL_OUTPUT_FREE(mac_external, mac); |
| 2981 | |
| 2982 | return status; |
| 2983 | } |
| 2984 | |
| 2985 | psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key, |
| 2986 | psa_algorithm_t alg, |
| 2987 | const uint8_t *input_external, |
| 2988 | size_t input_length, |
| 2989 | const uint8_t *mac_external, |
| 2990 | size_t mac_length) |
| 2991 | { |
| 2992 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 2993 | uint8_t actual_mac[PSA_MAC_MAX_SIZE]; |
| 2994 | size_t actual_mac_length; |
| 2995 | LOCAL_INPUT_DECLARE(input_external, input); |
| 2996 | LOCAL_INPUT_DECLARE(mac_external, mac); |
| 2997 | |
| 2998 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 2999 | status = psa_mac_compute_internal(key, alg, |
| 3000 | input, input_length, |
| 3001 | actual_mac, sizeof(actual_mac), |
| 3002 | &actual_mac_length, 0); |
| 3003 | if (status != PSA_SUCCESS) { |
| 3004 | goto exit; |
| 3005 | } |
| 3006 | |
| 3007 | if (mac_length != actual_mac_length) { |
| 3008 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3009 | goto exit; |
| 3010 | } |
| 3011 | |
| 3012 | LOCAL_INPUT_ALLOC(mac_external, mac_length, mac); |
| 3013 | if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) { |
| 3014 | status = PSA_ERROR_INVALID_SIGNATURE; |
| 3015 | goto exit; |
| 3016 | } |
| 3017 | |
| 3018 | exit: |
| 3019 | mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac)); |
| 3020 | LOCAL_INPUT_FREE(input_external, input); |
| 3021 | LOCAL_INPUT_FREE(mac_external, mac); |
| 3022 | |
| 3023 | return status; |
| 3024 | } |
| 3025 | |
| 3026 | /****************************************************************/ |
| 3027 | /* Asymmetric cryptography */ |
| 3028 | /****************************************************************/ |
| 3029 | |
| 3030 | static psa_status_t psa_sign_verify_check_alg(int input_is_message, |
| 3031 | psa_algorithm_t alg) |
| 3032 | { |
| 3033 | if (input_is_message) { |
| 3034 | if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) { |
| 3035 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3036 | } |
| 3037 | } |
| 3038 | |
| 3039 | psa_algorithm_t hash_alg = 0; |
| 3040 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
| 3041 | hash_alg = PSA_ALG_SIGN_GET_HASH(alg); |
| 3042 | } |
| 3043 | |
| 3044 | /* Now hash_alg==0 if alg by itself doesn't need a hash. |
| 3045 | * This is good enough for sign-hash, but a guaranteed failure for |
| 3046 | * sign-message which needs to hash first for all algorithms |
| 3047 | * supported at the moment. */ |
| 3048 | |
| 3049 | if (hash_alg == 0 && input_is_message) { |
| 3050 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3051 | } |
| 3052 | if (hash_alg == PSA_ALG_ANY_HASH) { |
| 3053 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3054 | } |
| 3055 | /* Give up immediately if the hash is not supported. This has |
| 3056 | * several advantages: |
| 3057 | * - For mechanisms that don't use the hash at all (e.g. |
| 3058 | * ECDSA verification, randomized ECDSA signature), without |
| 3059 | * this check, the operation would succeed even though it has |
| 3060 | * been given an invalid argument. This would not be insecure |
| 3061 | * since the hash was not necessary, but it would be weird. |
| 3062 | * - For mechanisms that do use the hash, we avoid an error |
| 3063 | * deep inside the execution. In principle this doesn't matter, |
| 3064 | * but there is a little more risk of a bug in error handling |
| 3065 | * deep inside than in this preliminary check. |
| 3066 | * - When calling a driver, the driver might be capable of using |
| 3067 | * a hash that the core doesn't support. This could potentially |
| 3068 | * result in a buffer overflow if the hash is larger than the |
| 3069 | * maximum hash size assumed by the core. |
| 3070 | * - Returning a consistent error makes it possible to test |
| 3071 | * not-supported hashes in a consistent way. |
| 3072 | */ |
| 3073 | if (hash_alg != 0 && !is_hash_supported(hash_alg)) { |
| 3074 | return PSA_ERROR_NOT_SUPPORTED; |
| 3075 | } |
| 3076 | |
| 3077 | return PSA_SUCCESS; |
| 3078 | } |
| 3079 | |
| 3080 | static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key, |
| 3081 | int input_is_message, |
| 3082 | psa_algorithm_t alg, |
| 3083 | const uint8_t *input, |
| 3084 | size_t input_length, |
| 3085 | uint8_t *signature, |
| 3086 | size_t signature_size, |
| 3087 | size_t *signature_length) |
| 3088 | { |
| 3089 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3090 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3091 | psa_key_slot_t *slot; |
| 3092 | |
| 3093 | *signature_length = 0; |
| 3094 | |
| 3095 | status = psa_sign_verify_check_alg(input_is_message, alg); |
| 3096 | if (status != PSA_SUCCESS) { |
| 3097 | return status; |
| 3098 | } |
| 3099 | |
| 3100 | /* Immediately reject a zero-length signature buffer. This guarantees |
| 3101 | * that signature must be a valid pointer. (On the other hand, the input |
| 3102 | * buffer can in principle be empty since it doesn't actually have |
| 3103 | * to be a hash.) */ |
| 3104 | if (signature_size == 0) { |
| 3105 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 3106 | } |
| 3107 | |
| 3108 | status = psa_get_and_lock_key_slot_with_policy( |
| 3109 | key, &slot, |
| 3110 | input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE : |
| 3111 | PSA_KEY_USAGE_SIGN_HASH, |
| 3112 | alg); |
| 3113 | |
| 3114 | if (status != PSA_SUCCESS) { |
| 3115 | goto exit; |
| 3116 | } |
| 3117 | |
| 3118 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
| 3119 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3120 | goto exit; |
| 3121 | } |
| 3122 | |
| 3123 | if (input_is_message) { |
| 3124 | status = psa_driver_wrapper_sign_message( |
| 3125 | &slot->attr, slot->key.data, slot->key.bytes, |
| 3126 | alg, input, input_length, |
| 3127 | signature, signature_size, signature_length); |
| 3128 | } else { |
| 3129 | |
| 3130 | status = psa_driver_wrapper_sign_hash( |
| 3131 | &slot->attr, slot->key.data, slot->key.bytes, |
| 3132 | alg, input, input_length, |
| 3133 | signature, signature_size, signature_length); |
| 3134 | } |
| 3135 | |
| 3136 | |
| 3137 | exit: |
| 3138 | psa_wipe_tag_output_buffer(signature, status, signature_size, |
| 3139 | *signature_length); |
| 3140 | |
| 3141 | unlock_status = psa_unregister_read_under_mutex(slot); |
| 3142 | |
| 3143 | return (status == PSA_SUCCESS) ? unlock_status : status; |
| 3144 | } |
| 3145 | |
| 3146 | static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key, |
| 3147 | int input_is_message, |
| 3148 | psa_algorithm_t alg, |
| 3149 | const uint8_t *input, |
| 3150 | size_t input_length, |
| 3151 | const uint8_t *signature, |
| 3152 | size_t signature_length) |
| 3153 | { |
| 3154 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3155 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3156 | psa_key_slot_t *slot; |
| 3157 | |
| 3158 | status = psa_sign_verify_check_alg(input_is_message, alg); |
| 3159 | if (status != PSA_SUCCESS) { |
| 3160 | return status; |
| 3161 | } |
| 3162 | |
| 3163 | status = psa_get_and_lock_key_slot_with_policy( |
| 3164 | key, &slot, |
| 3165 | input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE : |
| 3166 | PSA_KEY_USAGE_VERIFY_HASH, |
| 3167 | alg); |
| 3168 | |
| 3169 | if (status != PSA_SUCCESS) { |
| 3170 | return status; |
| 3171 | } |
| 3172 | |
| 3173 | if (input_is_message) { |
| 3174 | status = psa_driver_wrapper_verify_message( |
| 3175 | &slot->attr, slot->key.data, slot->key.bytes, |
| 3176 | alg, input, input_length, |
| 3177 | signature, signature_length); |
| 3178 | } else { |
| 3179 | status = psa_driver_wrapper_verify_hash( |
| 3180 | &slot->attr, slot->key.data, slot->key.bytes, |
| 3181 | alg, input, input_length, |
| 3182 | signature, signature_length); |
| 3183 | } |
| 3184 | |
| 3185 | unlock_status = psa_unregister_read_under_mutex(slot); |
| 3186 | |
| 3187 | return (status == PSA_SUCCESS) ? unlock_status : status; |
| 3188 | |
| 3189 | } |
| 3190 | |
| 3191 | psa_status_t psa_sign_message_builtin( |
| 3192 | const psa_key_attributes_t *attributes, |
| 3193 | const uint8_t *key_buffer, |
| 3194 | size_t key_buffer_size, |
| 3195 | psa_algorithm_t alg, |
| 3196 | const uint8_t *input, |
| 3197 | size_t input_length, |
| 3198 | uint8_t *signature, |
| 3199 | size_t signature_size, |
| 3200 | size_t *signature_length) |
| 3201 | { |
| 3202 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3203 | |
| 3204 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
| 3205 | size_t hash_length; |
| 3206 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 3207 | |
| 3208 | status = psa_driver_wrapper_hash_compute( |
| 3209 | PSA_ALG_SIGN_GET_HASH(alg), |
| 3210 | input, input_length, |
| 3211 | hash, sizeof(hash), &hash_length); |
| 3212 | |
| 3213 | if (status != PSA_SUCCESS) { |
| 3214 | return status; |
| 3215 | } |
| 3216 | |
| 3217 | return psa_driver_wrapper_sign_hash( |
| 3218 | attributes, key_buffer, key_buffer_size, |
| 3219 | alg, hash, hash_length, |
| 3220 | signature, signature_size, signature_length); |
| 3221 | } |
| 3222 | |
| 3223 | return PSA_ERROR_NOT_SUPPORTED; |
| 3224 | } |
| 3225 | |
| 3226 | psa_status_t psa_sign_message(mbedtls_svc_key_id_t key, |
| 3227 | psa_algorithm_t alg, |
| 3228 | const uint8_t *input_external, |
| 3229 | size_t input_length, |
| 3230 | uint8_t *signature_external, |
| 3231 | size_t signature_size, |
| 3232 | size_t *signature_length) |
| 3233 | { |
| 3234 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3235 | LOCAL_INPUT_DECLARE(input_external, input); |
| 3236 | LOCAL_OUTPUT_DECLARE(signature_external, signature); |
| 3237 | |
| 3238 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 3239 | LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature); |
| 3240 | status = psa_sign_internal(key, 1, alg, input, input_length, signature, |
| 3241 | signature_size, signature_length); |
| 3242 | |
| 3243 | #if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) |
| 3244 | exit: |
| 3245 | #endif |
| 3246 | LOCAL_INPUT_FREE(input_external, input); |
| 3247 | LOCAL_OUTPUT_FREE(signature_external, signature); |
| 3248 | return status; |
| 3249 | } |
| 3250 | |
| 3251 | psa_status_t psa_verify_message_builtin( |
| 3252 | const psa_key_attributes_t *attributes, |
| 3253 | const uint8_t *key_buffer, |
| 3254 | size_t key_buffer_size, |
| 3255 | psa_algorithm_t alg, |
| 3256 | const uint8_t *input, |
| 3257 | size_t input_length, |
| 3258 | const uint8_t *signature, |
| 3259 | size_t signature_length) |
| 3260 | { |
| 3261 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3262 | |
| 3263 | if (PSA_ALG_IS_SIGN_HASH(alg)) { |
| 3264 | size_t hash_length; |
| 3265 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 3266 | |
| 3267 | status = psa_driver_wrapper_hash_compute( |
| 3268 | PSA_ALG_SIGN_GET_HASH(alg), |
| 3269 | input, input_length, |
| 3270 | hash, sizeof(hash), &hash_length); |
| 3271 | |
| 3272 | if (status != PSA_SUCCESS) { |
| 3273 | return status; |
| 3274 | } |
| 3275 | |
| 3276 | return psa_driver_wrapper_verify_hash( |
| 3277 | attributes, key_buffer, key_buffer_size, |
| 3278 | alg, hash, hash_length, |
| 3279 | signature, signature_length); |
| 3280 | } |
| 3281 | |
| 3282 | return PSA_ERROR_NOT_SUPPORTED; |
| 3283 | } |
| 3284 | |
| 3285 | psa_status_t psa_verify_message(mbedtls_svc_key_id_t key, |
| 3286 | psa_algorithm_t alg, |
| 3287 | const uint8_t *input_external, |
| 3288 | size_t input_length, |
| 3289 | const uint8_t *signature_external, |
| 3290 | size_t signature_length) |
| 3291 | { |
| 3292 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3293 | LOCAL_INPUT_DECLARE(input_external, input); |
| 3294 | LOCAL_INPUT_DECLARE(signature_external, signature); |
| 3295 | |
| 3296 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 3297 | LOCAL_INPUT_ALLOC(signature_external, signature_length, signature); |
| 3298 | status = psa_verify_internal(key, 1, alg, input, input_length, signature, |
| 3299 | signature_length); |
| 3300 | |
| 3301 | #if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) |
| 3302 | exit: |
| 3303 | #endif |
| 3304 | LOCAL_INPUT_FREE(input_external, input); |
| 3305 | LOCAL_INPUT_FREE(signature_external, signature); |
| 3306 | |
| 3307 | return status; |
| 3308 | } |
| 3309 | |
| 3310 | psa_status_t psa_sign_hash_builtin( |
| 3311 | const psa_key_attributes_t *attributes, |
| 3312 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 3313 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 3314 | uint8_t *signature, size_t signature_size, size_t *signature_length) |
| 3315 | { |
| 3316 | if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) { |
| 3317 | if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || |
| 3318 | PSA_ALG_IS_RSA_PSS(alg)) { |
| 3319 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 3320 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 3321 | return mbedtls_psa_rsa_sign_hash( |
| 3322 | attributes, |
| 3323 | key_buffer, key_buffer_size, |
| 3324 | alg, hash, hash_length, |
| 3325 | signature, signature_size, signature_length); |
| 3326 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3327 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
| 3328 | } else { |
| 3329 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3330 | } |
| 3331 | } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) { |
| 3332 | if (PSA_ALG_IS_ECDSA(alg)) { |
| 3333 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3334 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3335 | return mbedtls_psa_ecdsa_sign_hash( |
| 3336 | attributes, |
| 3337 | key_buffer, key_buffer_size, |
| 3338 | alg, hash, hash_length, |
| 3339 | signature, signature_size, signature_length); |
| 3340 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3341 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 3342 | } else { |
| 3343 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3344 | } |
| 3345 | } |
| 3346 | |
| 3347 | (void) key_buffer; |
| 3348 | (void) key_buffer_size; |
| 3349 | (void) hash; |
| 3350 | (void) hash_length; |
| 3351 | (void) signature; |
| 3352 | (void) signature_size; |
| 3353 | (void) signature_length; |
| 3354 | |
| 3355 | return PSA_ERROR_NOT_SUPPORTED; |
| 3356 | } |
| 3357 | |
| 3358 | psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key, |
| 3359 | psa_algorithm_t alg, |
| 3360 | const uint8_t *hash_external, |
| 3361 | size_t hash_length, |
| 3362 | uint8_t *signature_external, |
| 3363 | size_t signature_size, |
| 3364 | size_t *signature_length) |
| 3365 | { |
| 3366 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3367 | LOCAL_INPUT_DECLARE(hash_external, hash); |
| 3368 | LOCAL_OUTPUT_DECLARE(signature_external, signature); |
| 3369 | |
| 3370 | LOCAL_INPUT_ALLOC(hash_external, hash_length, hash); |
| 3371 | LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature); |
| 3372 | status = psa_sign_internal(key, 0, alg, hash, hash_length, signature, |
| 3373 | signature_size, signature_length); |
| 3374 | |
| 3375 | #if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) |
| 3376 | exit: |
| 3377 | #endif |
| 3378 | LOCAL_INPUT_FREE(hash_external, hash); |
| 3379 | LOCAL_OUTPUT_FREE(signature_external, signature); |
| 3380 | |
| 3381 | return status; |
| 3382 | } |
| 3383 | |
| 3384 | psa_status_t psa_verify_hash_builtin( |
| 3385 | const psa_key_attributes_t *attributes, |
| 3386 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 3387 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 3388 | const uint8_t *signature, size_t signature_length) |
| 3389 | { |
| 3390 | if (PSA_KEY_TYPE_IS_RSA(attributes->type)) { |
| 3391 | if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || |
| 3392 | PSA_ALG_IS_RSA_PSS(alg)) { |
| 3393 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 3394 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) |
| 3395 | return mbedtls_psa_rsa_verify_hash( |
| 3396 | attributes, |
| 3397 | key_buffer, key_buffer_size, |
| 3398 | alg, hash, hash_length, |
| 3399 | signature, signature_length); |
| 3400 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 3401 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */ |
| 3402 | } else { |
| 3403 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3404 | } |
| 3405 | } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) { |
| 3406 | if (PSA_ALG_IS_ECDSA(alg)) { |
| 3407 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ |
| 3408 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 3409 | return mbedtls_psa_ecdsa_verify_hash( |
| 3410 | attributes, |
| 3411 | key_buffer, key_buffer_size, |
| 3412 | alg, hash, hash_length, |
| 3413 | signature, signature_length); |
| 3414 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || |
| 3415 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 3416 | } else { |
| 3417 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3418 | } |
| 3419 | } |
| 3420 | |
| 3421 | (void) key_buffer; |
| 3422 | (void) key_buffer_size; |
| 3423 | (void) hash; |
| 3424 | (void) hash_length; |
| 3425 | (void) signature; |
| 3426 | (void) signature_length; |
| 3427 | |
| 3428 | return PSA_ERROR_NOT_SUPPORTED; |
| 3429 | } |
| 3430 | |
| 3431 | psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key, |
| 3432 | psa_algorithm_t alg, |
| 3433 | const uint8_t *hash_external, |
| 3434 | size_t hash_length, |
| 3435 | const uint8_t *signature_external, |
| 3436 | size_t signature_length) |
| 3437 | { |
| 3438 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3439 | LOCAL_INPUT_DECLARE(hash_external, hash); |
| 3440 | LOCAL_INPUT_DECLARE(signature_external, signature); |
| 3441 | |
| 3442 | LOCAL_INPUT_ALLOC(hash_external, hash_length, hash); |
| 3443 | LOCAL_INPUT_ALLOC(signature_external, signature_length, signature); |
| 3444 | status = psa_verify_internal(key, 0, alg, hash, hash_length, signature, |
| 3445 | signature_length); |
| 3446 | |
| 3447 | #if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) |
| 3448 | exit: |
| 3449 | #endif |
| 3450 | LOCAL_INPUT_FREE(hash_external, hash); |
| 3451 | LOCAL_INPUT_FREE(signature_external, signature); |
| 3452 | |
| 3453 | return status; |
| 3454 | } |
| 3455 | |
| 3456 | psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key, |
| 3457 | psa_algorithm_t alg, |
| 3458 | const uint8_t *input_external, |
| 3459 | size_t input_length, |
| 3460 | const uint8_t *salt_external, |
| 3461 | size_t salt_length, |
| 3462 | uint8_t *output_external, |
| 3463 | size_t output_size, |
| 3464 | size_t *output_length) |
| 3465 | { |
| 3466 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3467 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3468 | psa_key_slot_t *slot; |
| 3469 | |
| 3470 | LOCAL_INPUT_DECLARE(input_external, input); |
| 3471 | LOCAL_INPUT_DECLARE(salt_external, salt); |
| 3472 | LOCAL_OUTPUT_DECLARE(output_external, output); |
| 3473 | |
| 3474 | (void) input; |
| 3475 | (void) input_length; |
| 3476 | (void) salt; |
| 3477 | (void) output; |
| 3478 | (void) output_size; |
| 3479 | |
| 3480 | *output_length = 0; |
| 3481 | |
| 3482 | if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) { |
| 3483 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3484 | } |
| 3485 | |
| 3486 | status = psa_get_and_lock_key_slot_with_policy( |
| 3487 | key, &slot, PSA_KEY_USAGE_ENCRYPT, alg); |
| 3488 | if (status != PSA_SUCCESS) { |
| 3489 | return status; |
| 3490 | } |
| 3491 | if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) || |
| 3492 | PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) { |
| 3493 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3494 | goto exit; |
| 3495 | } |
| 3496 | |
| 3497 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 3498 | LOCAL_INPUT_ALLOC(salt_external, salt_length, salt); |
| 3499 | LOCAL_OUTPUT_ALLOC(output_external, output_size, output); |
| 3500 | |
| 3501 | status = psa_driver_wrapper_asymmetric_encrypt( |
| 3502 | &slot->attr, slot->key.data, slot->key.bytes, |
| 3503 | alg, input, input_length, salt, salt_length, |
| 3504 | output, output_size, output_length); |
| 3505 | exit: |
| 3506 | unlock_status = psa_unregister_read_under_mutex(slot); |
| 3507 | |
| 3508 | LOCAL_INPUT_FREE(input_external, input); |
| 3509 | LOCAL_INPUT_FREE(salt_external, salt); |
| 3510 | LOCAL_OUTPUT_FREE(output_external, output); |
| 3511 | |
| 3512 | return (status == PSA_SUCCESS) ? unlock_status : status; |
| 3513 | } |
| 3514 | |
| 3515 | psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key, |
| 3516 | psa_algorithm_t alg, |
| 3517 | const uint8_t *input_external, |
| 3518 | size_t input_length, |
| 3519 | const uint8_t *salt_external, |
| 3520 | size_t salt_length, |
| 3521 | uint8_t *output_external, |
| 3522 | size_t output_size, |
| 3523 | size_t *output_length) |
| 3524 | { |
| 3525 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3526 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3527 | psa_key_slot_t *slot; |
| 3528 | |
| 3529 | LOCAL_INPUT_DECLARE(input_external, input); |
| 3530 | LOCAL_INPUT_DECLARE(salt_external, salt); |
| 3531 | LOCAL_OUTPUT_DECLARE(output_external, output); |
| 3532 | |
| 3533 | (void) input; |
| 3534 | (void) input_length; |
| 3535 | (void) salt; |
| 3536 | (void) output; |
| 3537 | (void) output_size; |
| 3538 | |
| 3539 | *output_length = 0; |
| 3540 | |
| 3541 | if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) { |
| 3542 | return PSA_ERROR_INVALID_ARGUMENT; |
| 3543 | } |
| 3544 | |
| 3545 | status = psa_get_and_lock_key_slot_with_policy( |
| 3546 | key, &slot, PSA_KEY_USAGE_DECRYPT, alg); |
| 3547 | if (status != PSA_SUCCESS) { |
| 3548 | return status; |
| 3549 | } |
| 3550 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
| 3551 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3552 | goto exit; |
| 3553 | } |
| 3554 | |
| 3555 | LOCAL_INPUT_ALLOC(input_external, input_length, input); |
| 3556 | LOCAL_INPUT_ALLOC(salt_external, salt_length, salt); |
| 3557 | LOCAL_OUTPUT_ALLOC(output_external, output_size, output); |
| 3558 | |
| 3559 | status = psa_driver_wrapper_asymmetric_decrypt( |
| 3560 | &slot->attr, slot->key.data, slot->key.bytes, |
| 3561 | alg, input, input_length, salt, salt_length, |
| 3562 | output, output_size, output_length); |
| 3563 | |
| 3564 | exit: |
| 3565 | unlock_status = psa_unregister_read_under_mutex(slot); |
| 3566 | |
| 3567 | LOCAL_INPUT_FREE(input_external, input); |
| 3568 | LOCAL_INPUT_FREE(salt_external, salt); |
| 3569 | LOCAL_OUTPUT_FREE(output_external, output); |
| 3570 | |
| 3571 | return (status == PSA_SUCCESS) ? unlock_status : status; |
| 3572 | } |
| 3573 | |
| 3574 | /****************************************************************/ |
| 3575 | /* Asymmetric interruptible cryptography */ |
| 3576 | /****************************************************************/ |
| 3577 | |
| 3578 | static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED; |
| 3579 | |
| 3580 | void psa_interruptible_set_max_ops(uint32_t max_ops) |
| 3581 | { |
| 3582 | psa_interruptible_max_ops = max_ops; |
| 3583 | } |
| 3584 | |
| 3585 | uint32_t psa_interruptible_get_max_ops(void) |
| 3586 | { |
| 3587 | return psa_interruptible_max_ops; |
| 3588 | } |
| 3589 | |
| 3590 | uint32_t psa_sign_hash_get_num_ops( |
| 3591 | const psa_sign_hash_interruptible_operation_t *operation) |
| 3592 | { |
| 3593 | return operation->num_ops; |
| 3594 | } |
| 3595 | |
| 3596 | uint32_t psa_verify_hash_get_num_ops( |
| 3597 | const psa_verify_hash_interruptible_operation_t *operation) |
| 3598 | { |
| 3599 | return operation->num_ops; |
| 3600 | } |
| 3601 | |
| 3602 | static psa_status_t psa_sign_hash_abort_internal( |
| 3603 | psa_sign_hash_interruptible_operation_t *operation) |
| 3604 | { |
| 3605 | if (operation->id == 0) { |
| 3606 | /* The object has (apparently) been initialized but it is not (yet) |
| 3607 | * in use. It's ok to call abort on such an object, and there's |
| 3608 | * nothing to do. */ |
| 3609 | return PSA_SUCCESS; |
| 3610 | } |
| 3611 | |
| 3612 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3613 | |
| 3614 | status = psa_driver_wrapper_sign_hash_abort(operation); |
| 3615 | |
| 3616 | operation->id = 0; |
| 3617 | |
| 3618 | /* Do not clear either the error_occurred or num_ops elements here as they |
| 3619 | * only want to be cleared by the application calling abort, not by abort |
| 3620 | * being called at completion of an operation. */ |
| 3621 | |
| 3622 | return status; |
| 3623 | } |
| 3624 | |
| 3625 | psa_status_t psa_sign_hash_start( |
| 3626 | psa_sign_hash_interruptible_operation_t *operation, |
| 3627 | mbedtls_svc_key_id_t key, psa_algorithm_t alg, |
| 3628 | const uint8_t *hash_external, size_t hash_length) |
| 3629 | { |
| 3630 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3631 | psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3632 | psa_key_slot_t *slot; |
| 3633 | |
| 3634 | LOCAL_INPUT_DECLARE(hash_external, hash); |
| 3635 | |
| 3636 | /* Check that start has not been previously called, or operation has not |
| 3637 | * previously errored. */ |
| 3638 | if (operation->id != 0 || operation->error_occurred) { |
| 3639 | return PSA_ERROR_BAD_STATE; |
| 3640 | } |
| 3641 | |
| 3642 | /* Make sure the driver-dependent part of the operation is zeroed. |
| 3643 | * This is a guarantee we make to drivers. Initializing the operation |
| 3644 | * does not necessarily take care of it, since the context is a |
| 3645 | * union and initializing a union does not necessarily initialize |
| 3646 | * all of its members. */ |
| 3647 | memset(&operation->ctx, 0, sizeof(operation->ctx)); |
| 3648 | |
| 3649 | status = psa_sign_verify_check_alg(0, alg); |
| 3650 | if (status != PSA_SUCCESS) { |
| 3651 | operation->error_occurred = 1; |
| 3652 | return status; |
| 3653 | } |
| 3654 | |
| 3655 | status = psa_get_and_lock_key_slot_with_policy(key, &slot, |
| 3656 | PSA_KEY_USAGE_SIGN_HASH, |
| 3657 | alg); |
| 3658 | |
| 3659 | if (status != PSA_SUCCESS) { |
| 3660 | goto exit; |
| 3661 | } |
| 3662 | |
| 3663 | if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) { |
| 3664 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 3665 | goto exit; |
| 3666 | } |
| 3667 | |
| 3668 | LOCAL_INPUT_ALLOC(hash_external, hash_length, hash); |
| 3669 | |
| 3670 | /* Ensure ops count gets reset, in case of operation re-use. */ |
| 3671 | operation->num_ops = 0; |
| 3672 | |
| 3673 | status = psa_driver_wrapper_sign_hash_start(operation, &slot->attr, |
| 3674 | slot->key.data, |
| 3675 | slot->key.bytes, alg, |
| 3676 | hash, hash_length); |
| 3677 | exit: |
| 3678 | |
| 3679 | if (status != PSA_SUCCESS) { |
| 3680 | operation->error_occurred = 1; |
| 3681 | psa_sign_hash_abort_internal(operation); |
| 3682 | } |
| 3683 | |
| 3684 | unlock_status = psa_unregister_read_under_mutex(slot); |
| 3685 | |
| 3686 | if (unlock_status != PSA_SUCCESS) { |
| 3687 | operation->error_occurred = 1; |
| 3688 | } |
| 3689 | |
| 3690 | LOCAL_INPUT_FREE(hash_external, hash); |
| 3691 | |
| 3692 | return (status == PSA_SUCCESS) ? unlock_status : status; |
| 3693 | } |
| 3694 | |
| 3695 | |
| 3696 | psa_status_t psa_sign_hash_complete( |
| 3697 | psa_sign_hash_interruptible_operation_t *operation, |
| 3698 | uint8_t *signature_external, size_t signature_size, |
| 3699 | size_t *signature_length) |
| 3700 | { |
| 3701 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 3702 | |
| 3703 | LOCAL_OUTPUT_DECLARE(signature_external, signature); |
| 3704 | |
| 3705 | *signature_length = 0; |
| 3706 | |
| 3707 | /* Check that start has been called first, and that operation has not |
| 3708 | * previously errored. */ |
| 3709 | if (operation->id == 0 || operation->error_occurred) { |
| 3710 | status = PSA_ERROR_BAD_STATE; |
| 3711 | goto exit; |
| 3712 | } |
| 3713 | |
| 3714 | /* Immediately reject a zero-length signature buffer. This guarantees that |
| 3715 | * signature must be a valid pointer. */ |
| 3716 | if (signature_size == 0) { |
| 3717 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 3718 | goto exit; |
| 3719 | } |
| 3720 | |
| 3721 | LOCAL_OUTPUT_ALLOC(signature_external, signature_size, signature); |
| 3722 | |
| 3723 | status = psa_driver_wrapper_sign_hash_complete(operation, signature, |
| 3724 | signature_size, |
| 3725 | signature_length); |
| 3726 | |
| 3727 | /* Update ops count with work done. */ |
| 3728 | operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation); |
| 3729 | |
| 3730 | exit: |
| 3731 | |
| 3732 | if (signature != NULL) { |
| 3733 | psa_wipe_tag_output_buffer(signature, status, signature_size, |
| 3734 | *signature_length); |
| 3735 | } |
| 3736 | |
| 3737 | if (status != PSA_OPERATION_INCOMPLETE) { |
| 3738 | if (status != PSA_SUCCESS) { |
| 3739 | operation->error_occurred = 1; |
| 3740 | } |
| 3741 | |
| 3742 |