Loading...
Searching...
No Matches
crypto_extra.h
Go to the documentation of this file.
/*
* Copyright The Mbed TLS Contributors
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
*/
#ifndef PSA_CRYPTO_EXTRA_H
#define PSA_CRYPTO_EXTRA_H
#include "mbedtls/platform_util.h"
#include "crypto_types.h"
#include "crypto_compat.h"
#ifdef __cplusplus
extern "C" {
#endif
/* UID for secure storage seed */
#define PSA_CRYPTO_ITS_RANDOM_SEED_UID 0xFFFFFF52
/* See config.h for definition */
#if !defined(MBEDTLS_PSA_KEY_SLOT_COUNT)
#define MBEDTLS_PSA_KEY_SLOT_COUNT 32
#endif
static inline void psa_set_key_enrollment_algorithm(
psa_key_attributes_t *attributes,
psa_algorithm_t alg2)
{
attributes->core.policy.alg2 = alg2;
}
static inline psa_algorithm_t psa_get_key_enrollment_algorithm(
const psa_key_attributes_t *attributes)
{
return attributes->core.policy.alg2;
}
#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);
static inline void psa_set_key_slot_number(
psa_key_attributes_t *attributes,
psa_key_slot_number_t slot_number)
{
attributes->core.flags |= MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER;
attributes->slot_number = slot_number;
}
static inline void psa_clear_key_slot_number(
psa_key_attributes_t *attributes)
{
attributes->core.flags &= ~MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER;
}
psa_status_t mbedtls_psa_register_se_key(
const psa_key_attributes_t *attributes);
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
void mbedtls_psa_crypto_free(void);
typedef struct mbedtls_psa_stats_s {
size_t volatile_slots;
size_t persistent_slots;
size_t external_slots;
size_t half_filled_slots;
size_t cache_slots;
size_t empty_slots;
size_t locked_slots;
psa_key_id_t max_open_internal_key_id;
psa_key_id_t max_open_external_key_id;
} mbedtls_psa_stats_t;
void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats);
psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
size_t seed_size);
#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t) 0x4002)
#define PSA_KEY_TYPE_DSA_KEY_PAIR ((psa_key_type_t) 0x7002)
#define PSA_KEY_TYPE_IS_DSA(type) \
(PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY)
#define PSA_ALG_DSA_BASE ((psa_algorithm_t) 0x06000400)
#define PSA_ALG_DSA(hash_alg) \
(PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t) 0x06000500)
#define PSA_ALG_DSA_DETERMINISTIC_FLAG PSA_ALG_ECDSA_DETERMINISTIC_FLAG
#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
(PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
#define PSA_ALG_IS_DSA(alg) \
(((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
PSA_ALG_DSA_BASE)
#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
(((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
(PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
(PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
/* We need to expand the sample definition of this macro from
* the API definition. */
#undef PSA_ALG_IS_VENDOR_HASH_AND_SIGN
#define PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg) \
PSA_ALG_IS_DSA(alg)
#define PSA_DH_FAMILY_CUSTOM ((psa_dh_family_t) 0x7e)
psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
psa_key_type_t type,
const uint8_t *data,
size_t data_length);
psa_status_t psa_get_key_domain_parameters(
const psa_key_attributes_t *attributes,
uint8_t *data,
size_t data_size,
size_t *data_length);
#define PSA_KEY_DOMAIN_PARAMETERS_SIZE(key_type, key_bits) \
(PSA_KEY_TYPE_IS_RSA(key_type) ? sizeof(int) : \
PSA_KEY_TYPE_IS_DH(key_type) ? PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
PSA_KEY_TYPE_IS_DSA(key_type) ? PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
0)
#define PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
(4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 3 /*without optional parts*/)
#define PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
(4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 2 /*p, g*/ + 34 /*q*/)
#if defined(MBEDTLS_ECP_C)
#include <mbedtls/ecp.h>
static inline psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
size_t *bits)
{
switch (grpid) {
case MBEDTLS_ECP_DP_SECP192R1:
*bits = 192;
return PSA_ECC_FAMILY_SECP_R1;
case MBEDTLS_ECP_DP_SECP224R1:
*bits = 224;
return PSA_ECC_FAMILY_SECP_R1;
case MBEDTLS_ECP_DP_SECP256R1:
*bits = 256;
return PSA_ECC_FAMILY_SECP_R1;
case MBEDTLS_ECP_DP_SECP384R1:
*bits = 384;
return PSA_ECC_FAMILY_SECP_R1;
case MBEDTLS_ECP_DP_SECP521R1:
*bits = 521;
return PSA_ECC_FAMILY_SECP_R1;
case MBEDTLS_ECP_DP_BP256R1:
*bits = 256;
return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
case MBEDTLS_ECP_DP_BP384R1:
*bits = 384;
return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
case MBEDTLS_ECP_DP_BP512R1:
*bits = 512;
return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
case MBEDTLS_ECP_DP_CURVE25519:
*bits = 255;
return PSA_ECC_FAMILY_MONTGOMERY;
case MBEDTLS_ECP_DP_SECP192K1:
*bits = 192;
return PSA_ECC_FAMILY_SECP_K1;
case MBEDTLS_ECP_DP_SECP224K1:
*bits = 224;
return PSA_ECC_FAMILY_SECP_K1;
case MBEDTLS_ECP_DP_SECP256K1:
*bits = 256;
return PSA_ECC_FAMILY_SECP_K1;
case MBEDTLS_ECP_DP_CURVE448:
*bits = 448;
return PSA_ECC_FAMILY_MONTGOMERY;
default:
*bits = 0;
return 0;
}
}
mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
size_t bits,
int bits_is_sloppy);
#endif /* MBEDTLS_ECP_C */
#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
psa_status_t mbedtls_psa_external_get_random(
mbedtls_psa_external_random_context_t *context,
uint8_t *output, size_t output_size, size_t *output_length);
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
#define MBEDTLS_PSA_KEY_ID_BUILTIN_MIN ((psa_key_id_t) 0x7fff0000)
#define MBEDTLS_PSA_KEY_ID_BUILTIN_MAX ((psa_key_id_t) 0x7fffefff)
typedef uint64_t psa_drv_slot_number_t;
#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
static inline int psa_key_id_is_builtin(psa_key_id_t key_id)
{
return (key_id >= MBEDTLS_PSA_KEY_ID_BUILTIN_MIN) &&
(key_id <= MBEDTLS_PSA_KEY_ID_BUILTIN_MAX);
}
psa_status_t mbedtls_psa_platform_get_builtin_key(
mbedtls_svc_key_id_t key_id,
psa_key_lifetime_t *lifetime,
psa_drv_slot_number_t *slot_number);
#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
#ifdef __cplusplus
}
#endif
#endif /* PSA_CRYPTO_EXTRA_H */
crypto_compat.h
PSA cryptography module: Backward compatibility aliases.
mbedtls_psa_crypto_free
void mbedtls_psa_crypto_free(void)
Library deinitialization.
mbedtls_psa_get_stats
void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats)
Get statistics about resource consumption related to the PSA keystore.
mbedtls_psa_stats_t
struct mbedtls_psa_stats_s mbedtls_psa_stats_t
Statistics about resource consumption related to the PSA keystore.
mbedtls_psa_inject_entropy
psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed, size_t seed_size)
Inject an initial entropy seed for the random generator into secure storage.
MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER
#define MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER
Definition crypto_struct.h:273
crypto_types.h
PSA cryptography module: type aliases.
ecp.h
This file provides an API for Elliptic Curves over GF(P) (ECP).
mbedtls_ecp_group_id
mbedtls_ecp_group_id
Definition ecp.h:113
MBEDTLS_ECP_DP_SECP192K1
@ MBEDTLS_ECP_DP_SECP192K1
Definition ecp.h:124
MBEDTLS_ECP_DP_SECP384R1
@ MBEDTLS_ECP_DP_SECP384R1
Definition ecp.h:118
MBEDTLS_ECP_DP_CURVE448
@ MBEDTLS_ECP_DP_CURVE448
Definition ecp.h:127
MBEDTLS_ECP_DP_CURVE25519
@ MBEDTLS_ECP_DP_CURVE25519
Definition ecp.h:123
MBEDTLS_ECP_DP_SECP256K1
@ MBEDTLS_ECP_DP_SECP256K1
Definition ecp.h:126
MBEDTLS_ECP_DP_BP512R1
@ MBEDTLS_ECP_DP_BP512R1
Definition ecp.h:122
MBEDTLS_ECP_DP_SECP224R1
@ MBEDTLS_ECP_DP_SECP224R1
Definition ecp.h:116
MBEDTLS_ECP_DP_SECP521R1
@ MBEDTLS_ECP_DP_SECP521R1
Definition ecp.h:119
MBEDTLS_ECP_DP_BP384R1
@ MBEDTLS_ECP_DP_BP384R1
Definition ecp.h:121
MBEDTLS_ECP_DP_SECP224K1
@ MBEDTLS_ECP_DP_SECP224K1
Definition ecp.h:125
MBEDTLS_ECP_DP_BP256R1
@ MBEDTLS_ECP_DP_BP256R1
Definition ecp.h:120
MBEDTLS_ECP_DP_SECP192R1
@ MBEDTLS_ECP_DP_SECP192R1
Definition ecp.h:115
MBEDTLS_ECP_DP_SECP256R1
@ MBEDTLS_ECP_DP_SECP256R1
Definition ecp.h:117
psa_get_key_enrollment_algorithm
static psa_algorithm_t psa_get_key_enrollment_algorithm(const psa_key_attributes_t *attributes)
Definition crypto_extra.h:72
psa_set_key_domain_parameters
psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes, psa_key_type_t type, const uint8_t *data, size_t data_length)
Set domain parameters for a key.
psa_get_key_domain_parameters
psa_status_t psa_get_key_domain_parameters(const psa_key_attributes_t *attributes, uint8_t *data, size_t data_size, size_t *data_length)
Get domain parameters for a key.
psa_set_key_enrollment_algorithm
static void psa_set_key_enrollment_algorithm(psa_key_attributes_t *attributes, psa_algorithm_t alg2)
Declare the enrollment algorithm for a key.
Definition crypto_extra.h:59
PSA_ECC_FAMILY_MONTGOMERY
#define PSA_ECC_FAMILY_MONTGOMERY
Definition crypto_values.h:621
PSA_ECC_FAMILY_SECP_R1
#define PSA_ECC_FAMILY_SECP_R1
Definition crypto_values.h:559
PSA_ECC_FAMILY_SECP_K1
#define PSA_ECC_FAMILY_SECP_K1
Definition crypto_values.h:549
psa_key_type_t
uint16_t psa_key_type_t
Encoding of a key type.
Definition crypto_types.h:69
PSA_ECC_FAMILY_BRAINPOOL_P_R1
#define PSA_ECC_FAMILY_BRAINPOOL_P_R1
Definition crypto_values.h:609
psa_algorithm_t
uint32_t psa_algorithm_t
Encoding of a cryptographic algorithm.
Definition crypto_types.h:125
psa_ecc_family_t
uint8_t psa_ecc_family_t
Definition crypto_types.h:88
psa_status_t
int32_t psa_status_t
Function return status.
Definition crypto_types.h:50
psa_key_id_t
uint32_t psa_key_id_t
Definition crypto_types.h:266
psa_key_lifetime_t
uint32_t psa_key_lifetime_t
Definition crypto_types.h:174
mbedtls_svc_key_id_t
psa_key_id_t mbedtls_svc_key_id_t
Definition crypto_types.h:283
MBEDTLS_PSA_KEY_ID_BUILTIN_MAX
#define MBEDTLS_PSA_KEY_ID_BUILTIN_MAX
Definition crypto_extra.h:722
psa_drv_slot_number_t
uint64_t psa_drv_slot_number_t
Definition crypto_extra.h:728
MBEDTLS_PSA_KEY_ID_BUILTIN_MIN
#define MBEDTLS_PSA_KEY_ID_BUILTIN_MIN
Definition crypto_extra.h:715
mbedtls_ecc_group_to_psa
static psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid, size_t *bits)
Definition crypto_extra.h:574
mbedtls_ecc_group_of_psa
mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve, size_t bits, int bits_is_sloppy)
psa_key_slot_number_t
uint64_t psa_key_slot_number_t
Definition crypto_se_driver.h:136
platform_util.h
Common and shared functions used by multiple modules in the Mbed TLS library.
mbedtls_psa_stats_s
Statistics about resource consumption related to the PSA keystore.
Definition crypto_extra.h:212
mbedtls_psa_stats_s::cache_slots
size_t cache_slots
Definition crypto_extra.h:225
mbedtls_psa_stats_s::half_filled_slots
size_t half_filled_slots
Definition crypto_extra.h:223
mbedtls_psa_stats_s::max_open_internal_key_id
psa_key_id_t max_open_internal_key_id
Definition crypto_extra.h:231
mbedtls_psa_stats_s::persistent_slots
size_t persistent_slots
Definition crypto_extra.h:217
mbedtls_psa_stats_s::max_open_external_key_id
psa_key_id_t max_open_external_key_id
Definition crypto_extra.h:233
mbedtls_psa_stats_s::volatile_slots
size_t volatile_slots
Definition crypto_extra.h:214
mbedtls_psa_stats_s::locked_slots
size_t locked_slots
Definition crypto_extra.h:229
mbedtls_psa_stats_s::empty_slots
size_t empty_slots
Definition crypto_extra.h:227
mbedtls_psa_stats_s::external_slots
size_t external_slots
Definition crypto_extra.h:220
psa_core_key_attributes_t::policy
psa_key_policy_t policy
Definition crypto_struct.h:292
psa_core_key_attributes_t::flags
psa_key_attributes_flag_t flags
Definition crypto_struct.h:293
psa_key_attributes_s
Definition crypto_struct.h:299
psa_key_attributes_s::core
psa_core_key_attributes_t core
Definition crypto_struct.h:300
psa_key_policy_s::alg2
psa_algorithm_t alg2
Definition crypto_struct.h:240