Path: blob/master/thirdparty/mbedtls/include/psa/crypto_struct.h
9904 views
/**1* \file psa/crypto_struct.h2*3* \brief PSA cryptography module: Mbed TLS structured type implementations4*5* \note This file may not be included directly. Applications must6* include psa/crypto.h.7*8* This file contains the definitions of some data structures with9* implementation-specific definitions.10*11* In implementations with isolation between the application and the12* cryptography module, it is expected that the front-end and the back-end13* would have different versions of this file.14*15* <h3>Design notes about multipart operation structures</h3>16*17* For multipart operations without driver delegation support, each multipart18* operation structure contains a `psa_algorithm_t alg` field which indicates19* which specific algorithm the structure is for. When the structure is not in20* use, `alg` is 0. Most of the structure consists of a union which is21* discriminated by `alg`.22*23* For multipart operations with driver delegation support, each multipart24* operation structure contains an `unsigned int id` field indicating which25* driver got assigned to do the operation. When the structure is not in use,26* 'id' is 0. The structure contains also a driver context which is the union27* of the contexts of all drivers able to handle the type of multipart28* operation.29*30* Note that when `alg` or `id` is 0, the content of other fields is undefined.31* In particular, it is not guaranteed that a freshly-initialized structure32* is all-zero: we initialize structures to something like `{0, 0}`, which33* is only guaranteed to initializes the first member of the union;34* GCC and Clang initialize the whole structure to 0 (at the time of writing),35* but MSVC and CompCert don't.36*37* In Mbed TLS, multipart operation structures live independently from38* the key. This allows Mbed TLS to free the key objects when destroying39* a key slot. If a multipart operation needs to remember the key after40* the setup function returns, the operation structure needs to contain a41* copy of the key.42*/43/*44* Copyright The Mbed TLS Contributors45* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later46*/4748#ifndef PSA_CRYPTO_STRUCT_H49#define PSA_CRYPTO_STRUCT_H50#include "mbedtls/private_access.h"5152#ifdef __cplusplus53extern "C" {54#endif5556/*57* Include the build-time configuration information header. Here, we do not58* include `"mbedtls/build_info.h"` directly but `"psa/build_info.h"`, which59* is basically just an alias to it. This is to ease the maintenance of the60* TF-PSA-Crypto repository which has a different build system and61* configuration.62*/63#include "psa/build_info.h"6465/* Include the context definition for the compiled-in drivers for the primitive66* algorithms. */67#include "psa/crypto_driver_contexts_primitives.h"6869struct psa_hash_operation_s {70#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)71mbedtls_psa_client_handle_t handle;72#else73/** Unique ID indicating which driver got assigned to do the74* operation. Since driver contexts are driver-specific, swapping75* drivers halfway through the operation is not supported.76* ID values are auto-generated in psa_driver_wrappers.h.77* ID value zero means the context is not valid or not assigned to78* any driver (i.e. the driver context is not active, in use). */79unsigned int MBEDTLS_PRIVATE(id);80psa_driver_hash_context_t MBEDTLS_PRIVATE(ctx);81#endif82};83#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)84#define PSA_HASH_OPERATION_INIT { 0 }85#else86#define PSA_HASH_OPERATION_INIT { 0, { 0 } }87#endif88static inline struct psa_hash_operation_s psa_hash_operation_init(void)89{90const struct psa_hash_operation_s v = PSA_HASH_OPERATION_INIT;91return v;92}9394struct psa_cipher_operation_s {95#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)96mbedtls_psa_client_handle_t handle;97#else98/** Unique ID indicating which driver got assigned to do the99* operation. Since driver contexts are driver-specific, swapping100* drivers halfway through the operation is not supported.101* ID values are auto-generated in psa_crypto_driver_wrappers.h102* ID value zero means the context is not valid or not assigned to103* any driver (i.e. none of the driver contexts are active). */104unsigned int MBEDTLS_PRIVATE(id);105106unsigned int MBEDTLS_PRIVATE(iv_required) : 1;107unsigned int MBEDTLS_PRIVATE(iv_set) : 1;108109uint8_t MBEDTLS_PRIVATE(default_iv_length);110111psa_driver_cipher_context_t MBEDTLS_PRIVATE(ctx);112#endif113};114115#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)116#define PSA_CIPHER_OPERATION_INIT { 0 }117#else118#define PSA_CIPHER_OPERATION_INIT { 0, 0, 0, 0, { 0 } }119#endif120static inline struct psa_cipher_operation_s psa_cipher_operation_init(void)121{122const struct psa_cipher_operation_s v = PSA_CIPHER_OPERATION_INIT;123return v;124}125126/* Include the context definition for the compiled-in drivers for the composite127* algorithms. */128#include "psa/crypto_driver_contexts_composites.h"129130struct psa_mac_operation_s {131#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)132mbedtls_psa_client_handle_t handle;133#else134/** Unique ID indicating which driver got assigned to do the135* operation. Since driver contexts are driver-specific, swapping136* drivers halfway through the operation is not supported.137* ID values are auto-generated in psa_driver_wrappers.h138* ID value zero means the context is not valid or not assigned to139* any driver (i.e. none of the driver contexts are active). */140unsigned int MBEDTLS_PRIVATE(id);141uint8_t MBEDTLS_PRIVATE(mac_size);142unsigned int MBEDTLS_PRIVATE(is_sign) : 1;143psa_driver_mac_context_t MBEDTLS_PRIVATE(ctx);144#endif145};146147#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)148#define PSA_MAC_OPERATION_INIT { 0 }149#else150#define PSA_MAC_OPERATION_INIT { 0, 0, 0, { 0 } }151#endif152static inline struct psa_mac_operation_s psa_mac_operation_init(void)153{154const struct psa_mac_operation_s v = PSA_MAC_OPERATION_INIT;155return v;156}157158struct psa_aead_operation_s {159#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)160mbedtls_psa_client_handle_t handle;161#else162/** Unique ID indicating which driver got assigned to do the163* operation. Since driver contexts are driver-specific, swapping164* drivers halfway through the operation is not supported.165* ID values are auto-generated in psa_crypto_driver_wrappers.h166* ID value zero means the context is not valid or not assigned to167* any driver (i.e. none of the driver contexts are active). */168unsigned int MBEDTLS_PRIVATE(id);169170psa_algorithm_t MBEDTLS_PRIVATE(alg);171psa_key_type_t MBEDTLS_PRIVATE(key_type);172173size_t MBEDTLS_PRIVATE(ad_remaining);174size_t MBEDTLS_PRIVATE(body_remaining);175176unsigned int MBEDTLS_PRIVATE(nonce_set) : 1;177unsigned int MBEDTLS_PRIVATE(lengths_set) : 1;178unsigned int MBEDTLS_PRIVATE(ad_started) : 1;179unsigned int MBEDTLS_PRIVATE(body_started) : 1;180unsigned int MBEDTLS_PRIVATE(is_encrypt) : 1;181182psa_driver_aead_context_t MBEDTLS_PRIVATE(ctx);183#endif184};185186#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)187#define PSA_AEAD_OPERATION_INIT { 0 }188#else189#define PSA_AEAD_OPERATION_INIT { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, { 0 } }190#endif191static inline struct psa_aead_operation_s psa_aead_operation_init(void)192{193const struct psa_aead_operation_s v = PSA_AEAD_OPERATION_INIT;194return v;195}196197/* Include the context definition for the compiled-in drivers for the key198* derivation algorithms. */199#include "psa/crypto_driver_contexts_key_derivation.h"200201struct psa_key_derivation_s {202#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)203mbedtls_psa_client_handle_t handle;204#else205psa_algorithm_t MBEDTLS_PRIVATE(alg);206unsigned int MBEDTLS_PRIVATE(can_output_key) : 1;207size_t MBEDTLS_PRIVATE(capacity);208psa_driver_key_derivation_context_t MBEDTLS_PRIVATE(ctx);209#endif210};211212#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)213#define PSA_KEY_DERIVATION_OPERATION_INIT { 0 }214#else215/* This only zeroes out the first byte in the union, the rest is unspecified. */216#define PSA_KEY_DERIVATION_OPERATION_INIT { 0, 0, 0, { 0 } }217#endif218static inline struct psa_key_derivation_s psa_key_derivation_operation_init(219void)220{221const struct psa_key_derivation_s v = PSA_KEY_DERIVATION_OPERATION_INIT;222return v;223}224225struct psa_custom_key_parameters_s {226/* Future versions may add other fields in this structure. */227uint32_t flags;228};229230/** The default production parameters for key generation or key derivation.231*232* Calling psa_generate_key_custom() or psa_key_derivation_output_key_custom()233* with `custom=PSA_CUSTOM_KEY_PARAMETERS_INIT` and `custom_data_length=0` is234* equivalent to calling psa_generate_key() or psa_key_derivation_output_key()235* respectively.236*/237#define PSA_CUSTOM_KEY_PARAMETERS_INIT { 0 }238239#ifndef __cplusplus240/* Omitted when compiling in C++, because one of the parameters is a241* pointer to a struct with a flexible array member, and that is not242* standard C++.243* https://github.com/Mbed-TLS/mbedtls/issues/9020244*/245/* This is a deprecated variant of `struct psa_custom_key_parameters_s`.246* It has exactly the same layout, plus an extra field which is a flexible247* array member. Thus a `const struct psa_key_production_parameters_s *`248* can be passed to any function that reads a249* `const struct psa_custom_key_parameters_s *`.250*/251struct psa_key_production_parameters_s {252uint32_t flags;253uint8_t data[];254};255256/** The default production parameters for key generation or key derivation.257*258* Calling psa_generate_key_ext() or psa_key_derivation_output_key_ext()259* with `params=PSA_KEY_PRODUCTION_PARAMETERS_INIT` and260* `params_data_length == 0` is equivalent to261* calling psa_generate_key() or psa_key_derivation_output_key()262* respectively.263*/264#define PSA_KEY_PRODUCTION_PARAMETERS_INIT { 0 }265#endif /* !__cplusplus */266267struct psa_key_policy_s {268psa_key_usage_t MBEDTLS_PRIVATE(usage);269psa_algorithm_t MBEDTLS_PRIVATE(alg);270psa_algorithm_t MBEDTLS_PRIVATE(alg2);271};272typedef struct psa_key_policy_s psa_key_policy_t;273274#define PSA_KEY_POLICY_INIT { 0, 0, 0 }275static inline struct psa_key_policy_s psa_key_policy_init(void)276{277const struct psa_key_policy_s v = PSA_KEY_POLICY_INIT;278return v;279}280281/* The type used internally for key sizes.282* Public interfaces use size_t, but internally we use a smaller type. */283typedef uint16_t psa_key_bits_t;284/* The maximum value of the type used to represent bit-sizes.285* This is used to mark an invalid key size. */286#define PSA_KEY_BITS_TOO_LARGE ((psa_key_bits_t) -1)287/* The maximum size of a key in bits.288* Currently defined as the maximum that can be represented, rounded down289* to a whole number of bytes.290* This is an uncast value so that it can be used in preprocessor291* conditionals. */292#define PSA_MAX_KEY_BITS 0xfff8293294struct psa_key_attributes_s {295#if defined(MBEDTLS_PSA_CRYPTO_SE_C)296psa_key_slot_number_t MBEDTLS_PRIVATE(slot_number);297int MBEDTLS_PRIVATE(has_slot_number);298#endif /* MBEDTLS_PSA_CRYPTO_SE_C */299psa_key_type_t MBEDTLS_PRIVATE(type);300psa_key_bits_t MBEDTLS_PRIVATE(bits);301psa_key_lifetime_t MBEDTLS_PRIVATE(lifetime);302psa_key_policy_t MBEDTLS_PRIVATE(policy);303/* This type has a different layout in the client view wrt the304* service view of the key id, i.e. in service view usually is305* expected to have MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined306* thus adding an owner field to the standard psa_key_id_t. For307* implementations with client/service separation, this means the308* object will be marshalled through a transport channel and309* interpreted differently at each side of the transport. Placing310* it at the end of structures allows to interpret the structure311* at the client without reorganizing the memory layout of the312* struct313*/314mbedtls_svc_key_id_t MBEDTLS_PRIVATE(id);315};316317#if defined(MBEDTLS_PSA_CRYPTO_SE_C)318#define PSA_KEY_ATTRIBUTES_MAYBE_SLOT_NUMBER 0, 0,319#else320#define PSA_KEY_ATTRIBUTES_MAYBE_SLOT_NUMBER321#endif322#define PSA_KEY_ATTRIBUTES_INIT { PSA_KEY_ATTRIBUTES_MAYBE_SLOT_NUMBER \323PSA_KEY_TYPE_NONE, 0, \324PSA_KEY_LIFETIME_VOLATILE, \325PSA_KEY_POLICY_INIT, \326MBEDTLS_SVC_KEY_ID_INIT }327328static inline struct psa_key_attributes_s psa_key_attributes_init(void)329{330const struct psa_key_attributes_s v = PSA_KEY_ATTRIBUTES_INIT;331return v;332}333334static inline void psa_set_key_id(psa_key_attributes_t *attributes,335mbedtls_svc_key_id_t key)336{337psa_key_lifetime_t lifetime = attributes->MBEDTLS_PRIVATE(lifetime);338339attributes->MBEDTLS_PRIVATE(id) = key;340341if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {342attributes->MBEDTLS_PRIVATE(lifetime) =343PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(344PSA_KEY_LIFETIME_PERSISTENT,345PSA_KEY_LIFETIME_GET_LOCATION(lifetime));346}347}348349static inline mbedtls_svc_key_id_t psa_get_key_id(350const psa_key_attributes_t *attributes)351{352return attributes->MBEDTLS_PRIVATE(id);353}354355#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER356static inline void mbedtls_set_key_owner_id(psa_key_attributes_t *attributes,357mbedtls_key_owner_id_t owner)358{359attributes->MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(owner) = owner;360}361#endif362363static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes,364psa_key_lifetime_t lifetime)365{366attributes->MBEDTLS_PRIVATE(lifetime) = lifetime;367if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {368#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER369attributes->MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(key_id) = 0;370#else371attributes->MBEDTLS_PRIVATE(id) = 0;372#endif373}374}375376static inline psa_key_lifetime_t psa_get_key_lifetime(377const psa_key_attributes_t *attributes)378{379return attributes->MBEDTLS_PRIVATE(lifetime);380}381382static inline void psa_extend_key_usage_flags(psa_key_usage_t *usage_flags)383{384if (*usage_flags & PSA_KEY_USAGE_SIGN_HASH) {385*usage_flags |= PSA_KEY_USAGE_SIGN_MESSAGE;386}387388if (*usage_flags & PSA_KEY_USAGE_VERIFY_HASH) {389*usage_flags |= PSA_KEY_USAGE_VERIFY_MESSAGE;390}391}392393static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes,394psa_key_usage_t usage_flags)395{396psa_extend_key_usage_flags(&usage_flags);397attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage) = usage_flags;398}399400static inline psa_key_usage_t psa_get_key_usage_flags(401const psa_key_attributes_t *attributes)402{403return attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage);404}405406static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes,407psa_algorithm_t alg)408{409attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg) = alg;410}411412static inline psa_algorithm_t psa_get_key_algorithm(413const psa_key_attributes_t *attributes)414{415return attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg);416}417418static inline void psa_set_key_type(psa_key_attributes_t *attributes,419psa_key_type_t type)420{421attributes->MBEDTLS_PRIVATE(type) = type;422}423424static inline psa_key_type_t psa_get_key_type(425const psa_key_attributes_t *attributes)426{427return attributes->MBEDTLS_PRIVATE(type);428}429430static inline void psa_set_key_bits(psa_key_attributes_t *attributes,431size_t bits)432{433if (bits > PSA_MAX_KEY_BITS) {434attributes->MBEDTLS_PRIVATE(bits) = PSA_KEY_BITS_TOO_LARGE;435} else {436attributes->MBEDTLS_PRIVATE(bits) = (psa_key_bits_t) bits;437}438}439440static inline size_t psa_get_key_bits(441const psa_key_attributes_t *attributes)442{443return attributes->MBEDTLS_PRIVATE(bits);444}445446/**447* \brief The context for PSA interruptible hash signing.448*/449struct psa_sign_hash_interruptible_operation_s {450#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)451mbedtls_psa_client_handle_t handle;452#else453/** Unique ID indicating which driver got assigned to do the454* operation. Since driver contexts are driver-specific, swapping455* drivers halfway through the operation is not supported.456* ID values are auto-generated in psa_crypto_driver_wrappers.h457* ID value zero means the context is not valid or not assigned to458* any driver (i.e. none of the driver contexts are active). */459unsigned int MBEDTLS_PRIVATE(id);460461psa_driver_sign_hash_interruptible_context_t MBEDTLS_PRIVATE(ctx);462463unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;464465uint32_t MBEDTLS_PRIVATE(num_ops);466#endif467};468469#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)470#define PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 }471#else472#define PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 }473#endif474475static inline struct psa_sign_hash_interruptible_operation_s476psa_sign_hash_interruptible_operation_init(void)477{478const struct psa_sign_hash_interruptible_operation_s v =479PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT;480481return v;482}483484/**485* \brief The context for PSA interruptible hash verification.486*/487struct psa_verify_hash_interruptible_operation_s {488#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)489mbedtls_psa_client_handle_t handle;490#else491/** Unique ID indicating which driver got assigned to do the492* operation. Since driver contexts are driver-specific, swapping493* drivers halfway through the operation is not supported.494* ID values are auto-generated in psa_crypto_driver_wrappers.h495* ID value zero means the context is not valid or not assigned to496* any driver (i.e. none of the driver contexts are active). */497unsigned int MBEDTLS_PRIVATE(id);498499psa_driver_verify_hash_interruptible_context_t MBEDTLS_PRIVATE(ctx);500501unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;502503uint32_t MBEDTLS_PRIVATE(num_ops);504#endif505};506507#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)508#define PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 }509#else510#define PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 }511#endif512513static inline struct psa_verify_hash_interruptible_operation_s514psa_verify_hash_interruptible_operation_init(void)515{516const struct psa_verify_hash_interruptible_operation_s v =517PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT;518519return v;520}521522#ifdef __cplusplus523}524#endif525526#endif /* PSA_CRYPTO_STRUCT_H */527528529