#include "common.h"
#include "psa_crypto_core_common.h"
#if defined(MBEDTLS_PSA_CRYPTO_C)
#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
#include "check_crypto_config.h"
#endif
#include "psa/crypto.h"
#include "psa/crypto_values.h"
#include "psa_crypto_cipher.h"
#include "psa_crypto_core.h"
#include "psa_crypto_invasive.h"
#include "psa_crypto_driver_wrappers.h"
#include "psa_crypto_driver_wrappers_no_static.h"
#include "psa_crypto_ecp.h"
#include "psa_crypto_ffdh.h"
#include "psa_crypto_hash.h"
#include "psa_crypto_mac.h"
#include "psa_crypto_rsa.h"
#include "psa_crypto_ecp.h"
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
#include "psa_crypto_se.h"
#endif
#include "psa_crypto_slot_management.h"
#include "psa_crypto_storage.h"
#include "psa_crypto_random_impl.h"
#include <stdlib.h>
#include <string.h>
#include "mbedtls/platform.h"
#include "mbedtls/aes.h"
#include "mbedtls/asn1.h"
#include "mbedtls/asn1write.h"
#include "mbedtls/bignum.h"
#include "mbedtls/camellia.h"
#include "mbedtls/chacha20.h"
#include "mbedtls/chachapoly.h"
#include "mbedtls/cipher.h"
#include "mbedtls/ccm.h"
#include "mbedtls/cmac.h"
#include "mbedtls/constant_time.h"
#include "mbedtls/des.h"
#include "mbedtls/ecdh.h"
#include "mbedtls/ecp.h"
#include "mbedtls/entropy.h"
#include "mbedtls/error.h"
#include "mbedtls/gcm.h"
#include "mbedtls/md5.h"
#include "mbedtls/pk.h"
#include "pk_wrap.h"
#include "mbedtls/platform_util.h"
#include "mbedtls/error.h"
#include "mbedtls/ripemd160.h"
#include "mbedtls/rsa.h"
#include "mbedtls/sha1.h"
#include "mbedtls/sha256.h"
#include "mbedtls/sha512.h"
#include "mbedtls/psa_util.h"
#include "mbedtls/threading.h"
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
#define BUILTIN_ALG_ANY_HKDF …
#endif
static int key_type_is_raw_bytes(psa_key_type_t type)
{ … }
#define RNG_NOT_INITIALIZED …
#define RNG_INITIALIZED …
#define RNG_SEEDED …
mbedtls_psa_crypto_subsystem;
#define PSA_CRYPTO_SUBSYSTEM_DRIVER_WRAPPERS_INITIALIZED …
#define PSA_CRYPTO_SUBSYSTEM_KEY_SLOTS_INITIALIZED …
#define PSA_CRYPTO_SUBSYSTEM_TRANSACTION_INITIALIZED …
#define PSA_CRYPTO_SUBSYSTEM_ALL_INITIALISED …
psa_global_data_t;
static psa_global_data_t global_data;
static uint8_t psa_get_initialized(void)
{ … }
static uint8_t psa_get_drivers_initialized(void)
{ … }
#define GUARD_MODULE_INITIALIZED …
#if !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS)
#define LOCAL_INPUT_DECLARE(input, input_copy_name) …
#define LOCAL_INPUT_ALLOC(input, length, input_copy) …
#define LOCAL_INPUT_FREE(input, input_copy) …
#define LOCAL_OUTPUT_DECLARE(output, output_copy_name) …
#define LOCAL_OUTPUT_ALLOC(output, length, output_copy) …
#define LOCAL_OUTPUT_FREE(output, output_copy) …
#else
#define LOCAL_INPUT_DECLARE …
#define LOCAL_INPUT_ALLOC …
#define LOCAL_INPUT_FREE …
#define LOCAL_OUTPUT_DECLARE …
#define LOCAL_OUTPUT_ALLOC …
#define LOCAL_OUTPUT_FREE …
#endif
int psa_can_do_hash(psa_algorithm_t hash_alg)
{ … }
int psa_can_do_cipher(psa_key_type_t key_type, psa_algorithm_t cipher_alg)
{ … }
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
static int psa_is_dh_key_size_valid(size_t bits)
{
switch (bits) {
#if defined(PSA_WANT_DH_RFC7919_2048)
case 2048:
return 1;
#endif
#if defined(PSA_WANT_DH_RFC7919_3072)
case 3072:
return 1;
#endif
#if defined(PSA_WANT_DH_RFC7919_4096)
case 4096:
return 1;
#endif
#if defined(PSA_WANT_DH_RFC7919_6144)
case 6144:
return 1;
#endif
#if defined(PSA_WANT_DH_RFC7919_8192)
case 8192:
return 1;
#endif
default:
return 0;
}
}
#endif
psa_status_t mbedtls_to_psa_error(int ret)
{ … }
static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
size_t output_buffer_size, size_t output_buffer_length)
{ … }
psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
size_t bits)
{ … }
MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
psa_algorithm_t algorithm,
psa_key_type_t key_type)
{ … }
psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
size_t buffer_length)
{ … }
psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
const uint8_t *data,
size_t data_length)
{ … }
psa_status_t psa_import_key_into_slot(
const psa_key_attributes_t *attributes,
const uint8_t *data, size_t data_length,
uint8_t *key_buffer, size_t key_buffer_size,
size_t *key_buffer_length, size_t *bits)
{ … }
static psa_algorithm_t psa_key_policy_algorithm_intersection(
psa_key_type_t key_type,
psa_algorithm_t alg1,
psa_algorithm_t alg2)
{ … }
static int psa_key_algorithm_permits(psa_key_type_t key_type,
psa_algorithm_t policy_alg,
psa_algorithm_t requested_alg)
{ … }
static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
psa_key_type_t key_type,
psa_algorithm_t alg)
{ … }
static psa_status_t psa_restrict_key_policy(
psa_key_type_t key_type,
psa_key_policy_t *policy,
const psa_key_policy_t *constraint)
{ … }
static psa_status_t psa_get_and_lock_key_slot_with_policy(
mbedtls_svc_key_id_t key,
psa_key_slot_t **p_slot,
psa_key_usage_t usage,
psa_algorithm_t alg)
{ … }
static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
mbedtls_svc_key_id_t key,
psa_key_slot_t **p_slot,
psa_key_usage_t usage,
psa_algorithm_t alg)
{ … }
psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
{ … }
psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
{ … }
psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
{ … }
psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
psa_key_attributes_t *attributes)
{ … }
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
psa_status_t psa_get_key_slot_number(
const psa_key_attributes_t *attributes,
psa_key_slot_number_t *slot_number)
{
if (attributes->has_slot_number) {
*slot_number = attributes->slot_number;
return PSA_SUCCESS;
} else {
return PSA_ERROR_INVALID_ARGUMENT;
}
}
#endif
static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
size_t key_buffer_size,
uint8_t *data,
size_t data_size,
size_t *data_length)
{ … }
psa_status_t psa_export_key_internal(
const psa_key_attributes_t *attributes,
const uint8_t *key_buffer, size_t key_buffer_size,
uint8_t *data, size_t data_size, size_t *data_length)
{ … }
psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
uint8_t *data_external,
size_t data_size,
size_t *data_length)
{ … }
psa_status_t psa_export_public_key_internal(
const psa_key_attributes_t *attributes,
const uint8_t *key_buffer,
size_t key_buffer_size,
uint8_t *data,
size_t data_size,
size_t *data_length)
{ … }
psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
uint8_t *data_external,
size_t data_size,
size_t *data_length)
{ … }
static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
{ … }
static psa_status_t psa_validate_key_attributes(
const psa_key_attributes_t *attributes,
psa_se_drv_table_entry_t **p_drv)
{ … }
static psa_status_t psa_start_key_creation(
psa_key_creation_method_t method,
const psa_key_attributes_t *attributes,
psa_key_slot_t **p_slot,
psa_se_drv_table_entry_t **p_drv)
{ … }
static psa_status_t psa_finish_key_creation(
psa_key_slot_t *slot,
psa_se_drv_table_entry_t *driver,
mbedtls_svc_key_id_t *key)
{ … }
static void psa_fail_key_creation(psa_key_slot_t *slot,
psa_se_drv_table_entry_t *driver)
{ … }
static psa_status_t psa_validate_optional_attributes(
const psa_key_slot_t *slot,
const psa_key_attributes_t *attributes)
{ … }
psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
const uint8_t *data_external,
size_t data_length,
mbedtls_svc_key_id_t *key)
{ … }
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
psa_status_t mbedtls_psa_register_se_key(
const psa_key_attributes_t *attributes)
{
psa_status_t status;
psa_key_slot_t *slot = NULL;
psa_se_drv_table_entry_t *driver = NULL;
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
return PSA_ERROR_NOT_SUPPORTED;
}
if (psa_get_key_bits(attributes) == 0) {
return PSA_ERROR_NOT_SUPPORTED;
}
if (PSA_KEY_LIFETIME_IS_VOLATILE(psa_get_key_lifetime(attributes))) {
return PSA_ERROR_INVALID_ARGUMENT;
}
status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
&slot, &driver);
if (status != PSA_SUCCESS) {
goto exit;
}
status = psa_finish_key_creation(slot, driver, &key);
exit:
if (status != PSA_SUCCESS) {
psa_fail_key_creation(slot, driver);
}
psa_close_key(key);
return status;
}
#endif
psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
const psa_key_attributes_t *specified_attributes,
mbedtls_svc_key_id_t *target_key)
{ … }
psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
{ … }
psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
psa_algorithm_t alg)
{ … }
psa_status_t psa_hash_update(psa_hash_operation_t *operation,
const uint8_t *input_external,
size_t input_length)
{ … }
static psa_status_t psa_hash_finish_internal(psa_hash_operation_t *operation,
uint8_t *hash,
size_t hash_size,
size_t *hash_length)
{ … }
psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
uint8_t *hash_external,
size_t hash_size,
size_t *hash_length)
{ … }
psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
const uint8_t *hash_external,
size_t hash_length)
{ … }
psa_status_t psa_hash_compute(psa_algorithm_t alg,
const uint8_t *input_external, size_t input_length,
uint8_t *hash_external, size_t hash_size,
size_t *hash_length)
{ … }
psa_status_t psa_hash_compare(psa_algorithm_t alg,
const uint8_t *input_external, size_t input_length,
const uint8_t *hash_external, size_t hash_length)
{ … }
psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
psa_hash_operation_t *target_operation)
{ … }
psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
{ … }
static psa_status_t psa_mac_finalize_alg_and_key_validation(
psa_algorithm_t alg,
const psa_key_attributes_t *attributes,
uint8_t *mac_size)
{ … }
static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
mbedtls_svc_key_id_t key,
psa_algorithm_t alg,
int is_sign)
{ … }
psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
mbedtls_svc_key_id_t key,
psa_algorithm_t alg)
{ … }
psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
mbedtls_svc_key_id_t key,
psa_algorithm_t alg)
{ … }
psa_status_t psa_mac_update(psa_mac_operation_t *operation,
const uint8_t *input_external,
size_t input_length)
{ … }
psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
uint8_t *mac_external,
size_t mac_size,
size_t *mac_length)
{ … }
psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
const uint8_t *mac_external,
size_t mac_length)
{ … }
static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
psa_algorithm_t alg,
const uint8_t *input,
size_t input_length,
uint8_t *mac,
size_t mac_size,
size_t *mac_length,
int is_sign)
{ … }
psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
psa_algorithm_t alg,
const uint8_t *input_external,
size_t input_length,
uint8_t *mac_external,
size_t mac_size,
size_t *mac_length)
{ … }
psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
psa_algorithm_t alg,
const uint8_t *input_external,
size_t input_length,
const uint8_t *mac_external,
size_t mac_length)
{ … }
static psa_status_t psa_sign_verify_check_alg(int input_is_message,
psa_algorithm_t alg)
{ … }
static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
int input_is_message,
psa_algorithm_t alg,
const uint8_t *input,
size_t input_length,
uint8_t *signature,
size_t signature_size,
size_t *signature_length)
{ … }
static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
int input_is_message,
psa_algorithm_t alg,
const uint8_t *input,
size_t input_length,
const uint8_t *signature,
size_t signature_length)
{ … }
psa_status_t psa_sign_message_builtin(
const psa_key_attributes_t *attributes,
const uint8_t *key_buffer,
size_t key_buffer_size,
psa_algorithm_t alg,
const uint8_t *input,
size_t input_length,
uint8_t *signature,
size_t signature_size,
size_t *signature_length)
{ … }
psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
psa_algorithm_t alg,
const uint8_t *input_external,
size_t input_length,
uint8_t *signature_external,
size_t signature_size,
size_t *signature_length)
{ … }
psa_status_t psa_verify_message_builtin(
const psa_key_attributes_t *attributes,
const uint8_t *key_buffer,
size_t key_buffer_size,
psa_algorithm_t alg,
const uint8_t *input,
size_t input_length,
const uint8_t *signature,
size_t signature_length)
{ … }
psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
psa_algorithm_t alg,
const uint8_t *input_external,
size_t input_length,
const uint8_t *signature_external,
size_t signature_length)
{ … }
psa_status_t psa_sign_hash_builtin(
const psa_key_attributes_t *attributes,
const uint8_t *key_buffer, size_t key_buffer_size,
psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
uint8_t *signature, size_t signature_size, size_t *signature_length)
{ … }
psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
psa_algorithm_t alg,
const uint8_t *hash_external,
size_t hash_length,
uint8_t *signature_external,
size_t signature_size,
size_t *signature_length)
{ … }
psa_status_t psa_verify_hash_builtin(
const psa_key_attributes_t *attributes,
const uint8_t *key_buffer, size_t key_buffer_size,
psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
const uint8_t *signature, size_t signature_length)
{ … }
psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
psa_algorithm_t alg,
const uint8_t *hash_external,
size_t hash_length,
const uint8_t *signature_external,
size_t signature_length)
{ … }
psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
psa_algorithm_t alg,
const uint8_t *input_external,
size_t input_length,
const uint8_t *salt_external,
size_t salt_length,
uint8_t *output_external,
size_t output_size,
size_t *output_length)
{ … }
psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
psa_algorithm_t alg,
const uint8_t *input_external,
size_t input_length,
const uint8_t *salt_external,
size_t salt_length,
uint8_t *output_external,
size_t output_size,
size_t *output_length)
{ … }
static uint32_t psa_interruptible_max_ops = …;
void psa_interruptible_set_max_ops(uint32_t max_ops)
{ … }
uint32_t psa_interruptible_get_max_ops(void)
{ … }
uint32_t psa_sign_hash_get_num_ops(
const psa_sign_hash_interruptible_operation_t *operation)
{ … }
uint32_t psa_verify_hash_get_num_ops(
const psa_verify_hash_interruptible_operation_t *operation)
{ … }
static psa_status_t psa_sign_hash_abort_internal(
psa_sign_hash_interruptible_operation_t *operation)
{ … }
psa_status_t psa_sign_hash_start(
psa_sign_hash_interruptible_operation_t *operation,
mbedtls_svc_key_id_t key, psa_algorithm_t alg,
const uint8_t *hash_external, size_t hash_length)
{ … }
psa_status_t psa_sign_hash_complete(
psa_sign_hash_interruptible_operation_t *operation,
uint8_t *signature_external, size_t signature_size,
size_t *signature_length)
{ … }
psa_status_t psa_sign_hash_abort(
psa_sign_hash_interruptible_operation_t *operation)
{ … }
static psa_status_t psa_verify_hash_abort_internal(
psa_verify_hash_interruptible_operation_t *operation)
{ … }
psa_status_t psa_verify_hash_start(
psa_verify_hash_interruptible_operation_t *operation,
mbedtls_svc_key_id_t key, psa_algorithm_t alg,
const uint8_t *hash_external, size_t hash_length,
const uint8_t *signature_external, size_t signature_length)
{ … }
psa_status_t psa_verify_hash_complete(
psa_verify_hash_interruptible_operation_t *operation)
{ … }
psa_status_t psa_verify_hash_abort(
psa_verify_hash_interruptible_operation_t *operation)
{ … }
void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
{ … }
uint32_t mbedtls_psa_sign_hash_get_num_ops(
const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
{ … }
uint32_t mbedtls_psa_verify_hash_get_num_ops(
const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
{ … }
psa_status_t mbedtls_psa_sign_hash_start(
mbedtls_psa_sign_hash_interruptible_operation_t *operation,
const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
size_t key_buffer_size, psa_algorithm_t alg,
const uint8_t *hash, size_t hash_length)
{ … }
psa_status_t mbedtls_psa_sign_hash_complete(
mbedtls_psa_sign_hash_interruptible_operation_t *operation,
uint8_t *signature, size_t signature_size,
size_t *signature_length)
{ … }
psa_status_t mbedtls_psa_sign_hash_abort(
mbedtls_psa_sign_hash_interruptible_operation_t *operation)
{ … }
psa_status_t mbedtls_psa_verify_hash_start(
mbedtls_psa_verify_hash_interruptible_operation_t *operation,
const psa_key_attributes_t *attributes,
const uint8_t *key_buffer, size_t key_buffer_size,
psa_algorithm_t alg,
const uint8_t *hash, size_t hash_length,
const uint8_t *signature, size_t signature_length)
{ … }
psa_status_t mbedtls_psa_verify_hash_complete(
mbedtls_psa_verify_hash_interruptible_operation_t *operation)
{ … }
psa_status_t mbedtls_psa_verify_hash_abort(
mbedtls_psa_verify_hash_interruptible_operation_t *operation)
{ … }
static psa_status_t psa_generate_random_internal(uint8_t *output,
size_t output_size)
{ … }
static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
mbedtls_svc_key_id_t key,
psa_algorithm_t alg,
mbedtls_operation_t cipher_operation)
{ … }
psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
mbedtls_svc_key_id_t key,
psa_algorithm_t alg)
{ … }
psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
mbedtls_svc_key_id_t key,
psa_algorithm_t alg)
{ … }
psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
uint8_t *iv_external,
size_t iv_size,
size_t *iv_length)
{ … }
psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
const uint8_t *iv_external,
size_t iv_length)
{ … }
psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
const uint8_t *input_external,
size_t input_length,
uint8_t *output_external,
size_t output_size,
size_t *output_length)
{ … }
psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
uint8_t *output_external,
size_t output_size,
size_t *output_length)
{ … }
psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
{ … }
psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
psa_algorithm_t alg,
const uint8_t *input_external,
size_t input_length,
uint8_t *output_external,
size_t output_size,
size_t *output_length)
{ … }
psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
psa_algorithm_t alg,
const uint8_t *input_external,
size_t input_length,
uint8_t *output_external,
size_t output_size,
size_t *output_length)
{ … }
static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
{ … }
static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
size_t nonce_length)
{ … }
static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
{ … }
psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
psa_algorithm_t alg,
const uint8_t *nonce_external,
size_t nonce_length,
const uint8_t *additional_data_external,
size_t additional_data_length,
const uint8_t *plaintext_external,
size_t plaintext_length,
uint8_t *ciphertext_external,
size_t ciphertext_size,
size_t *ciphertext_length)
{ … }
psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
psa_algorithm_t alg,
const uint8_t *nonce_external,
size_t nonce_length,
const uint8_t *additional_data_external,
size_t additional_data_length,
const uint8_t *ciphertext_external,
size_t ciphertext_length,
uint8_t *plaintext_external,
size_t plaintext_size,
size_t *plaintext_length)
{ … }
static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
{ … }
static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
int is_encrypt,
mbedtls_svc_key_id_t key,
psa_algorithm_t alg)
{ … }
psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
mbedtls_svc_key_id_t key,
psa_algorithm_t alg)
{ … }
psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
mbedtls_svc_key_id_t key,
psa_algorithm_t alg)
{ … }
static psa_status_t psa_aead_set_nonce_internal(psa_aead_operation_t *operation,
const uint8_t *nonce,
size_t nonce_length)
{ … }
psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
uint8_t *nonce_external,
size_t nonce_size,
size_t *nonce_length)
{ … }
psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
const uint8_t *nonce_external,
size_t nonce_length)
{ … }
psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
size_t ad_length,
size_t plaintext_length)
{ … }
psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
const uint8_t *input_external,
size_t input_length)
{ … }
psa_status_t psa_aead_update(psa_aead_operation_t *operation,
const uint8_t *input_external,
size_t input_length,
uint8_t *output_external,
size_t output_size,
size_t *output_length)
{ … }
static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
{ … }
psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
uint8_t *ciphertext_external,
size_t ciphertext_size,
size_t *ciphertext_length,
uint8_t *tag_external,
size_t tag_size,
size_t *tag_length)
{ … }
psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
uint8_t *plaintext_external,
size_t plaintext_size,
size_t *plaintext_length,
const uint8_t *tag_external,
size_t tag_length)
{ … }
psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
{ … }
#if defined(BUILTIN_ALG_ANY_HKDF) || \
defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
defined(PSA_HAVE_SOFT_PBKDF2)
#define AT_LEAST_ONE_BUILTIN_KDF
#endif
#if defined(BUILTIN_ALG_ANY_HKDF) || \
defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
static psa_status_t psa_key_derivation_start_hmac(
psa_mac_operation_t *operation,
psa_algorithm_t hash_alg,
const uint8_t *hmac_key,
size_t hmac_key_length)
{ … }
#endif
#define HKDF_STATE_INIT …
#define HKDF_STATE_STARTED …
#define HKDF_STATE_KEYED …
#define HKDF_STATE_OUTPUT …
static psa_algorithm_t psa_key_derivation_get_kdf_alg(
const psa_key_derivation_operation_t *operation)
{ … }
psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
{ … }
psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
size_t *capacity)
{ … }
psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
size_t capacity)
{ … }
#if defined(BUILTIN_ALG_ANY_HKDF)
static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
psa_algorithm_t kdf_alg,
uint8_t *output,
size_t output_length)
{ … }
#endif
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
psa_tls12_prf_key_derivation_t *tls12_prf,
psa_algorithm_t alg)
{ … }
static psa_status_t psa_key_derivation_tls12_prf_read(
psa_tls12_prf_key_derivation_t *tls12_prf,
psa_algorithm_t alg,
uint8_t *output,
size_t output_length)
{ … }
#endif
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
psa_tls12_ecjpake_to_pms_t *ecjpake,
uint8_t *output,
size_t output_length)
{ … }
#endif
#if defined(PSA_HAVE_SOFT_PBKDF2)
static psa_status_t psa_key_derivation_pbkdf2_generate_block(
psa_pbkdf2_key_derivation_t *pbkdf2,
psa_algorithm_t prf_alg,
uint8_t prf_output_length,
psa_key_attributes_t *attributes)
{
psa_status_t status;
psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
size_t mac_output_length;
uint8_t U_i[PSA_MAC_MAX_SIZE];
uint8_t *U_accumulator = pbkdf2->output_block;
uint64_t i;
uint8_t block_counter[4];
mac_operation.is_sign = 1;
mac_operation.mac_size = prf_output_length;
MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
attributes,
pbkdf2->password,
pbkdf2->password_length,
prf_alg);
if (status != PSA_SUCCESS) {
goto cleanup;
}
status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
if (status != PSA_SUCCESS) {
goto cleanup;
}
status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
if (status != PSA_SUCCESS) {
goto cleanup;
}
status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
&mac_output_length);
if (status != PSA_SUCCESS) {
goto cleanup;
}
if (mac_output_length != prf_output_length) {
status = PSA_ERROR_CORRUPTION_DETECTED;
goto cleanup;
}
memcpy(U_accumulator, U_i, prf_output_length);
for (i = 1; i < pbkdf2->input_cost; i++) {
status = psa_driver_wrapper_mac_compute(attributes,
pbkdf2->password,
pbkdf2->password_length,
prf_alg, U_i, prf_output_length,
U_i, prf_output_length,
&mac_output_length);
if (status != PSA_SUCCESS) {
goto cleanup;
}
mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
}
cleanup:
mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
return status;
}
static psa_status_t psa_key_derivation_pbkdf2_read(
psa_pbkdf2_key_derivation_t *pbkdf2,
psa_algorithm_t kdf_alg,
uint8_t *output,
size_t output_length)
{
psa_status_t status;
psa_algorithm_t prf_alg;
uint8_t prf_output_length;
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
prf_output_length = PSA_HASH_LENGTH(prf_alg);
psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
} else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
prf_alg = PSA_ALG_CMAC;
prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
} else {
return PSA_ERROR_INVALID_ARGUMENT;
}
switch (pbkdf2->state) {
case PSA_PBKDF2_STATE_PASSWORD_SET:
pbkdf2->bytes_used = prf_output_length;
pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
break;
case PSA_PBKDF2_STATE_OUTPUT:
break;
default:
return PSA_ERROR_BAD_STATE;
}
while (output_length != 0) {
uint8_t n = prf_output_length - pbkdf2->bytes_used;
if (n > output_length) {
n = (uint8_t) output_length;
}
memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
output += n;
output_length -= n;
pbkdf2->bytes_used += n;
if (output_length == 0) {
break;
}
pbkdf2->bytes_used = 0;
pbkdf2->block_number++;
status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
prf_output_length,
&attributes);
if (status != PSA_SUCCESS) {
return status;
}
}
return PSA_SUCCESS;
}
#endif
psa_status_t psa_key_derivation_output_bytes(
psa_key_derivation_operation_t *operation,
uint8_t *output_external,
size_t output_length)
{ … }
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
{
if (data_size >= 8) {
mbedtls_des_key_set_parity(data);
}
if (data_size >= 16) {
mbedtls_des_key_set_parity(data + 8);
}
if (data_size >= 24) {
mbedtls_des_key_set_parity(data + 16);
}
}
#endif
#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
psa_key_slot_t *slot,
size_t bits,
psa_key_derivation_operation_t *operation,
uint8_t **data
)
{ … }
static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
size_t bits,
psa_key_derivation_operation_t *operation,
uint8_t **data
)
{ … }
#else
static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
psa_key_slot_t *slot, size_t bits,
psa_key_derivation_operation_t *operation, uint8_t **data)
{
(void) slot;
(void) bits;
(void) operation;
(void) data;
return PSA_ERROR_NOT_SUPPORTED;
}
static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
{
(void) bits;
(void) operation;
(void) data;
return PSA_ERROR_NOT_SUPPORTED;
}
#endif
#endif
static psa_status_t psa_generate_derived_key_internal(
psa_key_slot_t *slot,
size_t bits,
psa_key_derivation_operation_t *operation)
{ … }
static const psa_custom_key_parameters_t default_custom_production = …;
int psa_custom_key_parameters_are_default(
const psa_custom_key_parameters_t *custom,
size_t custom_data_length)
{ … }
psa_status_t psa_key_derivation_output_key_custom(
const psa_key_attributes_t *attributes,
psa_key_derivation_operation_t *operation,
const psa_custom_key_parameters_t *custom,
const uint8_t *custom_data,
size_t custom_data_length,
mbedtls_svc_key_id_t *key)
{ … }
psa_status_t psa_key_derivation_output_key_ext(
const psa_key_attributes_t *attributes,
psa_key_derivation_operation_t *operation,
const psa_key_production_parameters_t *params,
size_t params_data_length,
mbedtls_svc_key_id_t *key)
{ … }
psa_status_t psa_key_derivation_output_key(
const psa_key_attributes_t *attributes,
psa_key_derivation_operation_t *operation,
mbedtls_svc_key_id_t *key)
{ … }
#if defined(AT_LEAST_ONE_BUILTIN_KDF)
static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
{ … }
static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
{ … }
static psa_status_t psa_key_derivation_set_maximum_capacity(
psa_key_derivation_operation_t *operation,
psa_algorithm_t kdf_alg)
{ … }
static psa_status_t psa_key_derivation_setup_kdf(
psa_key_derivation_operation_t *operation,
psa_algorithm_t kdf_alg)
{ … }
static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
{ … }
static int psa_key_derivation_allows_free_form_secret_input(
psa_algorithm_t kdf_alg)
{ … }
#endif
psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
psa_algorithm_t alg)
{ … }
#if defined(BUILTIN_ALG_ANY_HKDF)
static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
psa_algorithm_t kdf_alg,
psa_key_derivation_step_t step,
const uint8_t *data,
size_t data_length)
{ … }
#endif
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
const uint8_t *data,
size_t data_length)
{ … }
static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
const uint8_t *data,
size_t data_length)
{ … }
static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
const uint8_t *data,
size_t data_length)
{ … }
static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
psa_key_derivation_step_t step,
const uint8_t *data,
size_t data_length)
{ … }
#endif
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
psa_tls12_prf_key_derivation_t *prf,
const uint8_t *data,
size_t data_length)
{ … }
static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
psa_tls12_prf_key_derivation_t *prf,
const uint8_t *data,
size_t data_length)
{ … }
static psa_status_t psa_tls12_prf_psk_to_ms_input(
psa_tls12_prf_key_derivation_t *prf,
psa_key_derivation_step_t step,
const uint8_t *data,
size_t data_length)
{ … }
#endif
#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
static psa_status_t psa_tls12_ecjpake_to_pms_input(
psa_tls12_ecjpake_to_pms_t *ecjpake,
psa_key_derivation_step_t step,
const uint8_t *data,
size_t data_length)
{ … }
#endif
#if defined(PSA_HAVE_SOFT_PBKDF2)
static psa_status_t psa_pbkdf2_set_input_cost(
psa_pbkdf2_key_derivation_t *pbkdf2,
psa_key_derivation_step_t step,
uint64_t data)
{
if (step != PSA_KEY_DERIVATION_INPUT_COST) {
return PSA_ERROR_INVALID_ARGUMENT;
}
if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
return PSA_ERROR_BAD_STATE;
}
if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
return PSA_ERROR_NOT_SUPPORTED;
}
if (data == 0) {
return PSA_ERROR_INVALID_ARGUMENT;
}
pbkdf2->input_cost = data;
pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
return PSA_SUCCESS;
}
static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
const uint8_t *data,
size_t data_length)
{
if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
} else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
} else {
return PSA_ERROR_BAD_STATE;
}
if (data_length == 0) {
} else {
uint8_t *next_salt;
next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
if (next_salt == NULL) {
return PSA_ERROR_INSUFFICIENT_MEMORY;
}
if (pbkdf2->salt_length != 0) {
memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
}
memcpy(next_salt + pbkdf2->salt_length, data, data_length);
pbkdf2->salt_length += data_length;
mbedtls_free(pbkdf2->salt);
pbkdf2->salt = next_salt;
}
return PSA_SUCCESS;
}
#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
const uint8_t *input,
size_t input_len,
uint8_t *output,
size_t *output_len)
{
psa_status_t status = PSA_SUCCESS;
if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
return psa_hash_compute(hash_alg, input, input_len, output,
PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
} else if (input_len > 0) {
memcpy(output, input, input_len);
}
*output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
return status;
}
#endif
#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
size_t input_len,
uint8_t *output,
size_t *output_len)
{
psa_status_t status = PSA_SUCCESS;
if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
uint8_t zeros[16] = { 0 };
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
status = psa_driver_wrapper_mac_compute(&attributes,
zeros, sizeof(zeros),
PSA_ALG_CMAC, input, input_len,
output,
PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
128U,
PSA_ALG_CMAC),
output_len);
} else {
memcpy(output, input, input_len);
*output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
}
return status;
}
#endif
static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
psa_algorithm_t kdf_alg,
const uint8_t *data,
size_t data_length)
{
psa_status_t status = PSA_SUCCESS;
if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
return PSA_ERROR_BAD_STATE;
}
#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
pbkdf2->password,
&pbkdf2->password_length);
} else
#endif
#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
status = psa_pbkdf2_cmac_set_password(data, data_length,
pbkdf2->password,
&pbkdf2->password_length);
} else
#endif
{
return PSA_ERROR_INVALID_ARGUMENT;
}
pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
return status;
}
static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
psa_algorithm_t kdf_alg,
psa_key_derivation_step_t step,
const uint8_t *data,
size_t data_length)
{
switch (step) {
case PSA_KEY_DERIVATION_INPUT_SALT:
return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
case PSA_KEY_DERIVATION_INPUT_PASSWORD:
return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
default:
return PSA_ERROR_INVALID_ARGUMENT;
}
}
#endif
static int psa_key_derivation_check_input_type(
psa_key_derivation_step_t step,
psa_key_type_t key_type)
{ … }
static psa_status_t psa_key_derivation_input_internal(
psa_key_derivation_operation_t *operation,
psa_key_derivation_step_t step,
psa_key_type_t key_type,
const uint8_t *data,
size_t data_length)
{ … }
static psa_status_t psa_key_derivation_input_integer_internal(
psa_key_derivation_operation_t *operation,
psa_key_derivation_step_t step,
uint64_t value)
{ … }
psa_status_t psa_key_derivation_input_bytes(
psa_key_derivation_operation_t *operation,
psa_key_derivation_step_t step,
const uint8_t *data_external,
size_t data_length)
{ … }
psa_status_t psa_key_derivation_input_integer(
psa_key_derivation_operation_t *operation,
psa_key_derivation_step_t step,
uint64_t value)
{ … }
psa_status_t psa_key_derivation_input_key(
psa_key_derivation_operation_t *operation,
psa_key_derivation_step_t step,
mbedtls_svc_key_id_t key)
{ … }
psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
const uint8_t *key_buffer,
size_t key_buffer_size,
psa_algorithm_t alg,
const uint8_t *peer_key,
size_t peer_key_length,
uint8_t *shared_secret,
size_t shared_secret_size,
size_t *shared_secret_length)
{ … }
static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
psa_key_slot_t *private_key,
const uint8_t *peer_key,
size_t peer_key_length,
uint8_t *shared_secret,
size_t shared_secret_size,
size_t *shared_secret_length)
{ … }
static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
psa_key_derivation_step_t step,
psa_key_slot_t *private_key,
const uint8_t *peer_key,
size_t peer_key_length)
{ … }
psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
psa_key_derivation_step_t step,
mbedtls_svc_key_id_t private_key,
const uint8_t *peer_key_external,
size_t peer_key_length)
{ … }
psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
mbedtls_svc_key_id_t private_key,
const uint8_t *peer_key_external,
size_t peer_key_length,
uint8_t *output_external,
size_t output_size,
size_t *output_length)
{ … }
#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
#include "entropy_poll.h"
#endif
static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
{ … }
static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
{ … }
static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
{ … }
psa_status_t psa_generate_random(uint8_t *output_external,
size_t output_size)
{ … }
#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
size_t seed_size)
{
if (psa_get_initialized()) {
return PSA_ERROR_NOT_PERMITTED;
}
if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
(seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
(seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
return PSA_ERROR_INVALID_ARGUMENT;
}
return mbedtls_psa_storage_inject_entropy(seed, seed_size);
}
#endif
static psa_status_t psa_validate_key_type_and_size_for_key_generation(
psa_key_type_t type, size_t bits)
{ … }
psa_status_t psa_generate_key_internal(
const psa_key_attributes_t *attributes,
const psa_custom_key_parameters_t *custom,
const uint8_t *custom_data,
size_t custom_data_length,
uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
{ … }
psa_status_t psa_generate_key_custom(const psa_key_attributes_t *attributes,
const psa_custom_key_parameters_t *custom,
const uint8_t *custom_data,
size_t custom_data_length,
mbedtls_svc_key_id_t *key)
{ … }
psa_status_t psa_generate_key_ext(const psa_key_attributes_t *attributes,
const psa_key_production_parameters_t *params,
size_t params_data_length,
mbedtls_svc_key_id_t *key)
{ … }
psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
mbedtls_svc_key_id_t *key)
{ … }
#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
void (* entropy_init)(mbedtls_entropy_context *ctx),
void (* entropy_free)(mbedtls_entropy_context *ctx))
{ … }
#endif
void mbedtls_psa_crypto_free(void)
{ … }
#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
static psa_status_t psa_crypto_recover_transaction(
const psa_crypto_transaction_t *transaction)
{
switch (transaction->unknown.type) {
case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
default:
return PSA_ERROR_DATA_INVALID;
}
}
#endif
static psa_status_t mbedtls_psa_crypto_init_subsystem(mbedtls_psa_crypto_subsystem subsystem)
{ … }
psa_status_t psa_crypto_init(void)
{ … }
#if defined(PSA_WANT_ALG_SOME_PAKE)
psa_status_t psa_crypto_driver_pake_get_password_len(
const psa_crypto_driver_pake_inputs_t *inputs,
size_t *password_len)
{ … }
psa_status_t psa_crypto_driver_pake_get_password(
const psa_crypto_driver_pake_inputs_t *inputs,
uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
{ … }
psa_status_t psa_crypto_driver_pake_get_user_len(
const psa_crypto_driver_pake_inputs_t *inputs,
size_t *user_len)
{ … }
psa_status_t psa_crypto_driver_pake_get_user(
const psa_crypto_driver_pake_inputs_t *inputs,
uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
{ … }
psa_status_t psa_crypto_driver_pake_get_peer_len(
const psa_crypto_driver_pake_inputs_t *inputs,
size_t *peer_len)
{ … }
psa_status_t psa_crypto_driver_pake_get_peer(
const psa_crypto_driver_pake_inputs_t *inputs,
uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
{ … }
psa_status_t psa_crypto_driver_pake_get_cipher_suite(
const psa_crypto_driver_pake_inputs_t *inputs,
psa_pake_cipher_suite_t *cipher_suite)
{ … }
psa_status_t psa_pake_setup(
psa_pake_operation_t *operation,
const psa_pake_cipher_suite_t *cipher_suite)
{ … }
psa_status_t psa_pake_set_password_key(
psa_pake_operation_t *operation,
mbedtls_svc_key_id_t password)
{ … }
psa_status_t psa_pake_set_user(
psa_pake_operation_t *operation,
const uint8_t *user_id_external,
size_t user_id_len)
{ … }
psa_status_t psa_pake_set_peer(
psa_pake_operation_t *operation,
const uint8_t *peer_id_external,
size_t peer_id_len)
{ … }
psa_status_t psa_pake_set_role(
psa_pake_operation_t *operation,
psa_pake_role_t role)
{ … }
#if defined(PSA_WANT_ALG_JPAKE)
static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
psa_jpake_computation_stage_t *stage)
{ … }
#endif
static psa_status_t psa_pake_complete_inputs(
psa_pake_operation_t *operation)
{ … }
#if defined(PSA_WANT_ALG_JPAKE)
static psa_status_t psa_jpake_prologue(
psa_pake_operation_t *operation,
psa_pake_step_t step,
psa_jpake_io_mode_t io_mode)
{ … }
static psa_status_t psa_jpake_epilogue(
psa_pake_operation_t *operation,
psa_jpake_io_mode_t io_mode)
{ … }
#endif
psa_status_t psa_pake_output(
psa_pake_operation_t *operation,
psa_pake_step_t step,
uint8_t *output_external,
size_t output_size,
size_t *output_length)
{ … }
psa_status_t psa_pake_input(
psa_pake_operation_t *operation,
psa_pake_step_t step,
const uint8_t *input_external,
size_t input_length)
{ … }
psa_status_t psa_pake_get_implicit_key(
psa_pake_operation_t *operation,
psa_key_derivation_operation_t *output)
{ … }
psa_status_t psa_pake_abort(
psa_pake_operation_t *operation)
{ … }
#endif
#if defined(MBEDTLS_TEST_HOOKS)
void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
#endif
MBEDTLS_STATIC_TESTABLE
psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
uint8_t *input_copy, size_t input_copy_len)
{ … }
MBEDTLS_STATIC_TESTABLE
psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
uint8_t *output, size_t output_len)
{ … }
psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
psa_crypto_local_input_t *local_input)
{ … }
void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
{ … }
psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
psa_crypto_local_output_t *local_output)
{ … }
psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
{ … }
#endif