Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-27 09:43:22

0001 /*
0002  *  Context structure declaration of the Mbed TLS software-based PSA drivers
0003  *  called through the PSA Crypto driver dispatch layer.
0004  *  This file contains the context structures of those algorithms which need to
0005  *  rely on other algorithms, i.e. are 'composite' algorithms.
0006  *
0007  * \note This file may not be included directly. Applications must
0008  * include psa/crypto.h.
0009  *
0010  * \note This header and its content are not part of the Mbed TLS API and
0011  * applications must not depend on it. Its main purpose is to define the
0012  * multi-part state objects of the Mbed TLS software-based PSA drivers. The
0013  * definitions of these objects are then used by crypto_struct.h to define the
0014  * implementation-defined types of PSA multi-part state objects.
0015  */
0016 /*
0017  *  Copyright The Mbed TLS Contributors
0018  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
0019  */
0020 
0021 #ifndef PSA_CRYPTO_BUILTIN_COMPOSITES_H
0022 #define PSA_CRYPTO_BUILTIN_COMPOSITES_H
0023 #include "mbedtls/private_access.h"
0024 
0025 #include <psa/crypto_driver_common.h>
0026 
0027 #include "mbedtls/cmac.h"
0028 #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
0029 #include "mbedtls/gcm.h"
0030 #endif
0031 #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
0032 #include "mbedtls/ccm.h"
0033 #endif
0034 #include "mbedtls/chachapoly.h"
0035 
0036 /*
0037  * MAC multi-part operation definitions.
0038  */
0039 #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) || \
0040     defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
0041 #define MBEDTLS_PSA_BUILTIN_MAC
0042 #endif
0043 
0044 #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || defined(PSA_CRYPTO_DRIVER_TEST)
0045 typedef struct {
0046     /** The HMAC algorithm in use */
0047     psa_algorithm_t MBEDTLS_PRIVATE(alg);
0048     /** The hash context. */
0049     struct psa_hash_operation_s hash_ctx;
0050     /** The HMAC part of the context. */
0051     uint8_t MBEDTLS_PRIVATE(opad)[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
0052 } mbedtls_psa_hmac_operation_t;
0053 
0054 #define MBEDTLS_PSA_HMAC_OPERATION_INIT { 0, PSA_HASH_OPERATION_INIT, { 0 } }
0055 #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
0056 
0057 typedef struct {
0058     psa_algorithm_t MBEDTLS_PRIVATE(alg);
0059     union {
0060         unsigned MBEDTLS_PRIVATE(dummy); /* Make the union non-empty even with no supported algorithms. */
0061 #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || defined(PSA_CRYPTO_DRIVER_TEST)
0062         mbedtls_psa_hmac_operation_t MBEDTLS_PRIVATE(hmac);
0063 #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
0064 #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) || defined(PSA_CRYPTO_DRIVER_TEST)
0065         mbedtls_cipher_context_t MBEDTLS_PRIVATE(cmac);
0066 #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
0067     } MBEDTLS_PRIVATE(ctx);
0068 } mbedtls_psa_mac_operation_t;
0069 
0070 #define MBEDTLS_PSA_MAC_OPERATION_INIT { 0, { 0 } }
0071 
0072 #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) || \
0073     defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) || \
0074     defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
0075 #define MBEDTLS_PSA_BUILTIN_AEAD  1
0076 #endif
0077 
0078 /* Context structure for the Mbed TLS AEAD implementation. */
0079 typedef struct {
0080     psa_algorithm_t MBEDTLS_PRIVATE(alg);
0081     psa_key_type_t MBEDTLS_PRIVATE(key_type);
0082 
0083     unsigned int MBEDTLS_PRIVATE(is_encrypt) : 1;
0084 
0085     uint8_t MBEDTLS_PRIVATE(tag_length);
0086 
0087     union {
0088         unsigned dummy; /* Enable easier initializing of the union. */
0089 #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
0090         mbedtls_ccm_context MBEDTLS_PRIVATE(ccm);
0091 #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
0092 #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
0093         mbedtls_gcm_context MBEDTLS_PRIVATE(gcm);
0094 #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
0095 #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
0096         mbedtls_chachapoly_context MBEDTLS_PRIVATE(chachapoly);
0097 #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
0098 
0099     } ctx;
0100 
0101 } mbedtls_psa_aead_operation_t;
0102 
0103 #define MBEDTLS_PSA_AEAD_OPERATION_INIT { 0, 0, 0, 0, { 0 } }
0104 
0105 #include "mbedtls/ecdsa.h"
0106 
0107 /* Context structure for the Mbed TLS interruptible sign hash implementation. */
0108 typedef struct {
0109 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
0110     defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
0111     defined(MBEDTLS_ECP_RESTARTABLE)
0112     mbedtls_ecdsa_context *MBEDTLS_PRIVATE(ctx);
0113     mbedtls_ecdsa_restart_ctx MBEDTLS_PRIVATE(restart_ctx);
0114 
0115     uint32_t MBEDTLS_PRIVATE(num_ops);
0116 
0117     size_t MBEDTLS_PRIVATE(coordinate_bytes);
0118     psa_algorithm_t MBEDTLS_PRIVATE(alg);
0119     mbedtls_md_type_t MBEDTLS_PRIVATE(md_alg);
0120     uint8_t MBEDTLS_PRIVATE(hash)[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)];
0121     size_t MBEDTLS_PRIVATE(hash_length);
0122 
0123 #else
0124     /* Make the struct non-empty if algs not supported. */
0125     unsigned MBEDTLS_PRIVATE(dummy);
0126 
0127 #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
0128         * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
0129         * defined( MBEDTLS_ECP_RESTARTABLE ) */
0130 } mbedtls_psa_sign_hash_interruptible_operation_t;
0131 
0132 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
0133     defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
0134     defined(MBEDTLS_ECP_RESTARTABLE)
0135 #define MBEDTLS_PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { { 0 }, { 0 }, 0, 0, 0, 0, 0, 0 }
0136 #else
0137 #define MBEDTLS_PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 }
0138 #endif
0139 
0140 /* Context structure for the Mbed TLS interruptible verify hash
0141  * implementation.*/
0142 typedef struct {
0143 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
0144     defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
0145     defined(MBEDTLS_ECP_RESTARTABLE)
0146 
0147     mbedtls_ecdsa_context *MBEDTLS_PRIVATE(ctx);
0148     mbedtls_ecdsa_restart_ctx MBEDTLS_PRIVATE(restart_ctx);
0149 
0150     uint32_t MBEDTLS_PRIVATE(num_ops);
0151 
0152     uint8_t MBEDTLS_PRIVATE(hash)[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)];
0153     size_t MBEDTLS_PRIVATE(hash_length);
0154 
0155     mbedtls_mpi MBEDTLS_PRIVATE(r);
0156     mbedtls_mpi MBEDTLS_PRIVATE(s);
0157 
0158 #else
0159     /* Make the struct non-empty if algs not supported. */
0160     unsigned MBEDTLS_PRIVATE(dummy);
0161 
0162 #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
0163         * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
0164         * defined( MBEDTLS_ECP_RESTARTABLE ) */
0165 
0166 } mbedtls_psa_verify_hash_interruptible_operation_t;
0167 
0168 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
0169     defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
0170     defined(MBEDTLS_ECP_RESTARTABLE)
0171 #define MBEDTLS_VERIFY_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { { 0 }, { 0 }, 0, 0, 0, 0, { 0 }, \
0172         { 0 } }
0173 #else
0174 #define MBEDTLS_VERIFY_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 }
0175 #endif
0176 
0177 
0178 /* EC-JPAKE operation definitions */
0179 
0180 #include "mbedtls/ecjpake.h"
0181 
0182 #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
0183 #define MBEDTLS_PSA_BUILTIN_PAKE  1
0184 #endif
0185 
0186 /* Note: the format for mbedtls_ecjpake_read/write function has an extra
0187  * length byte for each step, plus an extra 3 bytes for ECParameters in the
0188  * server's 2nd round. */
0189 #define MBEDTLS_PSA_JPAKE_BUFFER_SIZE ((3 + 1 + 65 + 1 + 65 + 1 + 32) * 2)
0190 
0191 typedef struct {
0192     psa_algorithm_t MBEDTLS_PRIVATE(alg);
0193 
0194     uint8_t *MBEDTLS_PRIVATE(password);
0195     size_t MBEDTLS_PRIVATE(password_len);
0196 #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
0197     mbedtls_ecjpake_role MBEDTLS_PRIVATE(role);
0198     uint8_t MBEDTLS_PRIVATE(buffer[MBEDTLS_PSA_JPAKE_BUFFER_SIZE]);
0199     size_t MBEDTLS_PRIVATE(buffer_length);
0200     size_t MBEDTLS_PRIVATE(buffer_offset);
0201 #endif
0202     /* Context structure for the Mbed TLS EC-JPAKE implementation. */
0203     union {
0204         unsigned int MBEDTLS_PRIVATE(dummy);
0205 #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
0206         mbedtls_ecjpake_context MBEDTLS_PRIVATE(jpake);
0207 #endif
0208     } MBEDTLS_PRIVATE(ctx);
0209 
0210 } mbedtls_psa_pake_operation_t;
0211 
0212 #define MBEDTLS_PSA_PAKE_OPERATION_INIT { { 0 } }
0213 
0214 #endif /* PSA_CRYPTO_BUILTIN_COMPOSITES_H */