1 /* 2 * Context structure declaration of the Mbed TLS software-based PSA drivers 3 * called through the PSA Crypto driver dispatch layer. 4 * This file contains the context structures of those algorithms which need to 5 * rely on other algorithms, i.e. are 'composite' algorithms. 6 * 7 * \note This file may not be included directly. Applications must 8 * include psa/crypto.h. 9 * 10 * \note This header and its content are not part of the Mbed TLS API and 11 * applications must not depend on it. Its main purpose is to define the 12 * multi-part state objects of the Mbed TLS software-based PSA drivers. The 13 * definitions of these objects are then used by crypto_struct.h to define the 14 * implementation-defined types of PSA multi-part state objects. 15 */ 16 /* 17 * Copyright The Mbed TLS Contributors 18 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 19 */ 20 21 #ifndef PSA_CRYPTO_BUILTIN_COMPOSITES_H 22 #define PSA_CRYPTO_BUILTIN_COMPOSITES_H 23 #include "mbedtls/private_access.h" 24 25 #include <psa/crypto_driver_common.h> 26 27 #include "mbedtls/cmac.h" 28 #include "mbedtls/gcm.h" 29 #include "mbedtls/ccm.h" 30 #include "mbedtls/chachapoly.h" 31 32 /* 33 * MAC multi-part operation definitions. 34 */ 35 #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) || \ 36 defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) 37 #define MBEDTLS_PSA_BUILTIN_MAC 38 #endif 39 40 #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || defined(PSA_CRYPTO_DRIVER_TEST) 41 typedef struct { 42 /** The HMAC algorithm in use */ 43 psa_algorithm_t MBEDTLS_PRIVATE(alg); 44 /** The hash context. */ 45 struct psa_hash_operation_s hash_ctx; 46 /** The HMAC part of the context. */ 47 uint8_t MBEDTLS_PRIVATE(opad)[PSA_HMAC_MAX_HASH_BLOCK_SIZE]; 48 } mbedtls_psa_hmac_operation_t; 49 50 #define MBEDTLS_PSA_HMAC_OPERATION_INIT { 0, PSA_HASH_OPERATION_INIT, { 0 } } 51 #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ 52 53 typedef struct { 54 psa_algorithm_t MBEDTLS_PRIVATE(alg); 55 union { 56 unsigned MBEDTLS_PRIVATE(dummy); /* Make the union non-empty even with no supported algorithms. */ 57 #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || defined(PSA_CRYPTO_DRIVER_TEST) 58 mbedtls_psa_hmac_operation_t MBEDTLS_PRIVATE(hmac); 59 #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */ 60 #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) || defined(PSA_CRYPTO_DRIVER_TEST) 61 mbedtls_cipher_context_t MBEDTLS_PRIVATE(cmac); 62 #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */ 63 } MBEDTLS_PRIVATE(ctx); 64 } mbedtls_psa_mac_operation_t; 65 66 #define MBEDTLS_PSA_MAC_OPERATION_INIT { 0, { 0 } } 67 68 #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) || \ 69 defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) || \ 70 defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) 71 #define MBEDTLS_PSA_BUILTIN_AEAD 1 72 #endif 73 74 /* Context structure for the Mbed TLS AEAD implementation. */ 75 typedef struct { 76 psa_algorithm_t MBEDTLS_PRIVATE(alg); 77 psa_key_type_t MBEDTLS_PRIVATE(key_type); 78 79 unsigned int MBEDTLS_PRIVATE(is_encrypt) : 1; 80 81 uint8_t MBEDTLS_PRIVATE(tag_length); 82 83 union { 84 unsigned dummy; /* Enable easier initializing of the union. */ 85 #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) 86 mbedtls_ccm_context MBEDTLS_PRIVATE(ccm); 87 #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ 88 #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) 89 mbedtls_gcm_context MBEDTLS_PRIVATE(gcm); 90 #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ 91 #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) 92 mbedtls_chachapoly_context MBEDTLS_PRIVATE(chachapoly); 93 #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ 94 95 } ctx; 96 97 } mbedtls_psa_aead_operation_t; 98 99 #define MBEDTLS_PSA_AEAD_OPERATION_INIT { 0, 0, 0, 0, { 0 } } 100 101 #include "mbedtls/ecdsa.h" 102 103 /* Context structure for the Mbed TLS interruptible sign hash implementation. */ 104 typedef struct { 105 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ 106 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ 107 defined(MBEDTLS_ECP_RESTARTABLE) 108 mbedtls_ecdsa_context *MBEDTLS_PRIVATE(ctx); 109 mbedtls_ecdsa_restart_ctx MBEDTLS_PRIVATE(restart_ctx); 110 111 uint32_t MBEDTLS_PRIVATE(num_ops); 112 113 size_t MBEDTLS_PRIVATE(coordinate_bytes); 114 psa_algorithm_t MBEDTLS_PRIVATE(alg); 115 mbedtls_md_type_t MBEDTLS_PRIVATE(md_alg); 116 uint8_t MBEDTLS_PRIVATE(hash)[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)]; 117 size_t MBEDTLS_PRIVATE(hash_length); 118 119 #else 120 /* Make the struct non-empty if algs not supported. */ 121 unsigned MBEDTLS_PRIVATE(dummy); 122 123 #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || 124 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && 125 * defined( MBEDTLS_ECP_RESTARTABLE ) */ 126 } mbedtls_psa_sign_hash_interruptible_operation_t; 127 128 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ 129 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ 130 defined(MBEDTLS_ECP_RESTARTABLE) 131 #define MBEDTLS_PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { { 0 }, { 0 }, 0, 0, 0, 0, 0, 0 } 132 #else 133 #define MBEDTLS_PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 } 134 #endif 135 136 /* Context structure for the Mbed TLS interruptible verify hash 137 * implementation.*/ 138 typedef struct { 139 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ 140 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ 141 defined(MBEDTLS_ECP_RESTARTABLE) 142 143 mbedtls_ecdsa_context *MBEDTLS_PRIVATE(ctx); 144 mbedtls_ecdsa_restart_ctx MBEDTLS_PRIVATE(restart_ctx); 145 146 uint32_t MBEDTLS_PRIVATE(num_ops); 147 148 uint8_t MBEDTLS_PRIVATE(hash)[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)]; 149 size_t MBEDTLS_PRIVATE(hash_length); 150 151 mbedtls_mpi MBEDTLS_PRIVATE(r); 152 mbedtls_mpi MBEDTLS_PRIVATE(s); 153 154 #else 155 /* Make the struct non-empty if algs not supported. */ 156 unsigned MBEDTLS_PRIVATE(dummy); 157 158 #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || 159 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) && 160 * defined( MBEDTLS_ECP_RESTARTABLE ) */ 161 162 } mbedtls_psa_verify_hash_interruptible_operation_t; 163 164 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ 165 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ 166 defined(MBEDTLS_ECP_RESTARTABLE) 167 #define MBEDTLS_VERIFY_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { { 0 }, { 0 }, 0, 0, 0, 0, { 0 }, \ 168 { 0 } } 169 #else 170 #define MBEDTLS_VERIFY_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 } 171 #endif 172 173 174 /* EC-JPAKE operation definitions */ 175 176 #include "mbedtls/ecjpake.h" 177 178 #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) 179 #define MBEDTLS_PSA_BUILTIN_PAKE 1 180 #endif 181 182 /* Note: the format for mbedtls_ecjpake_read/write function has an extra 183 * length byte for each step, plus an extra 3 bytes for ECParameters in the 184 * server's 2nd round. */ 185 #define MBEDTLS_PSA_JPAKE_BUFFER_SIZE ((3 + 1 + 65 + 1 + 65 + 1 + 32) * 2) 186 187 typedef struct { 188 psa_algorithm_t MBEDTLS_PRIVATE(alg); 189 190 uint8_t *MBEDTLS_PRIVATE(password); 191 size_t MBEDTLS_PRIVATE(password_len); 192 #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) 193 mbedtls_ecjpake_role MBEDTLS_PRIVATE(role); 194 uint8_t MBEDTLS_PRIVATE(buffer[MBEDTLS_PSA_JPAKE_BUFFER_SIZE]); 195 size_t MBEDTLS_PRIVATE(buffer_length); 196 size_t MBEDTLS_PRIVATE(buffer_offset); 197 #endif 198 /* Context structure for the Mbed TLS EC-JPAKE implementation. */ 199 union { 200 unsigned int MBEDTLS_PRIVATE(dummy); 201 #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) 202 mbedtls_ecjpake_context MBEDTLS_PRIVATE(jpake); 203 #endif 204 } MBEDTLS_PRIVATE(ctx); 205 206 } mbedtls_psa_pake_operation_t; 207 208 #define MBEDTLS_PSA_PAKE_OPERATION_INIT { { 0 } } 209 210 #endif /* PSA_CRYPTO_BUILTIN_COMPOSITES_H */ 211