1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2016-2017 Intel Corporation 3 */ 4 5 #ifndef _OPENSSL_PMD_PRIVATE_H_ 6 #define _OPENSSL_PMD_PRIVATE_H_ 7 8 #include <openssl/evp.h> 9 #include <openssl/cmac.h> 10 #include <openssl/hmac.h> 11 #include <openssl/des.h> 12 #include <openssl/rsa.h> 13 #include <openssl/dh.h> 14 #include <openssl/dsa.h> 15 #include <openssl/ec.h> 16 #if (OPENSSL_VERSION_NUMBER >= 0x30000000L) 17 #include <openssl/provider.h> 18 #include <openssl/core_names.h> 19 #endif 20 21 #define CRYPTODEV_NAME_OPENSSL_PMD crypto_openssl 22 /**< Open SSL Crypto PMD device name */ 23 24 /** OPENSSL PMD LOGTYPE DRIVER */ 25 extern int openssl_logtype_driver; 26 #define RTE_LOGTYPE_OPENSSL_DRIVER openssl_logtype_driver 27 #define OPENSSL_LOG(level, ...) \ 28 RTE_LOG_LINE_PREFIX(level, OPENSSL_DRIVER, "%s() line %u: ", \ 29 __func__ RTE_LOG_COMMA __LINE__, __VA_ARGS__) 30 31 /* Maximum length for digest (SHA-512 needs 64 bytes) */ 32 #define DIGEST_LENGTH_MAX 64 33 34 /** OPENSSL operation order mode enumerator */ 35 enum openssl_chain_order { 36 OPENSSL_CHAIN_ONLY_CIPHER, 37 OPENSSL_CHAIN_ONLY_AUTH, 38 OPENSSL_CHAIN_CIPHER_BPI, 39 OPENSSL_CHAIN_CIPHER_AUTH, 40 OPENSSL_CHAIN_AUTH_CIPHER, 41 OPENSSL_CHAIN_COMBINED, 42 OPENSSL_CHAIN_NOT_SUPPORTED 43 }; 44 45 /** OPENSSL cipher mode enumerator */ 46 enum openssl_cipher_mode { 47 OPENSSL_CIPHER_LIB, 48 OPENSSL_CIPHER_DES3CTR, 49 }; 50 51 /** OPENSSL auth mode enumerator */ 52 enum openssl_auth_mode { 53 OPENSSL_AUTH_AS_AUTH, 54 OPENSSL_AUTH_AS_HMAC, 55 OPENSSL_AUTH_AS_CMAC, 56 }; 57 58 /** private data structure for each OPENSSL crypto device */ 59 struct openssl_private { 60 unsigned int max_nb_qpairs; 61 /**< Max number of queue pairs */ 62 }; 63 64 /** OPENSSL crypto queue pair */ 65 struct __rte_cache_aligned openssl_qp { 66 uint16_t id; 67 /**< Queue Pair Identifier */ 68 char name[RTE_CRYPTODEV_NAME_MAX_LEN]; 69 /**< Unique Queue Pair Name */ 70 struct rte_ring *processed_ops; 71 /**< Ring for placing process packets */ 72 struct rte_mempool *sess_mp; 73 /**< Session Mempool */ 74 struct rte_cryptodev_stats stats; 75 /**< Queue pair statistics */ 76 uint8_t temp_digest[DIGEST_LENGTH_MAX]; 77 /**< Buffer used to store the digest generated 78 * by the driver when verifying a digest provided 79 * by the user (using authentication verify operation) 80 */ 81 }; 82 83 struct evp_ctx_pair { 84 EVP_CIPHER_CTX *cipher; 85 union { 86 EVP_MD_CTX *auth; 87 #if OPENSSL_VERSION_NUMBER >= 0x30000000L 88 EVP_MAC_CTX *hmac; 89 EVP_MAC_CTX *cmac; 90 #else 91 HMAC_CTX *hmac; 92 CMAC_CTX *cmac; 93 #endif 94 }; 95 }; 96 97 /** OPENSSL crypto private session structure */ 98 struct __rte_cache_aligned openssl_session { 99 enum openssl_chain_order chain_order; 100 /**< chain order mode */ 101 102 struct { 103 uint16_t length; 104 uint16_t offset; 105 } iv; 106 /**< IV parameters */ 107 108 enum rte_crypto_aead_algorithm aead_algo; 109 /**< AEAD algorithm */ 110 111 /** Cipher Parameters */ 112 struct { 113 enum rte_crypto_cipher_operation direction; 114 /**< cipher operation direction */ 115 enum openssl_cipher_mode mode; 116 /**< cipher operation mode */ 117 enum rte_crypto_cipher_algorithm algo; 118 /**< cipher algorithm */ 119 120 struct { 121 uint8_t data[32]; 122 /**< key data */ 123 size_t length; 124 /**< key length in bytes */ 125 } key; 126 127 const EVP_CIPHER *evp_algo; 128 /**< pointer to EVP algorithm function */ 129 EVP_CIPHER_CTX *ctx; 130 /**< pointer to EVP context structure */ 131 EVP_CIPHER_CTX *bpi_ctx; 132 } cipher; 133 134 /** Authentication Parameters */ 135 struct { 136 enum rte_crypto_auth_operation operation; 137 /**< auth operation generate or verify */ 138 enum openssl_auth_mode mode; 139 /**< auth operation mode */ 140 enum rte_crypto_auth_algorithm algo; 141 /**< cipher algorithm */ 142 143 union { 144 struct { 145 const EVP_MD *evp_algo; 146 /**< pointer to EVP algorithm function */ 147 EVP_MD_CTX *ctx; 148 /**< pointer to EVP context structure */ 149 } auth; 150 151 struct { 152 EVP_PKEY *pkey; 153 /**< pointer to EVP key */ 154 const EVP_MD *evp_algo; 155 /**< pointer to EVP algorithm function */ 156 # if OPENSSL_VERSION_NUMBER >= 0x30000000L 157 EVP_MAC_CTX * ctx; 158 # else 159 HMAC_CTX *ctx; 160 # endif 161 /**< pointer to EVP context structure */ 162 } hmac; 163 164 struct { 165 # if OPENSSL_VERSION_NUMBER >= 0x30000000L 166 EVP_MAC_CTX * ctx; 167 /**< pointer to EVP context structure */ 168 # else 169 const EVP_CIPHER * evp_algo; 170 /**< pointer to EVP algorithm function */ 171 CMAC_CTX *ctx; 172 /**< pointer to EVP context structure */ 173 # endif 174 } cmac; 175 }; 176 177 uint16_t aad_length; 178 /**< AAD length */ 179 uint16_t digest_length; 180 /**< digest length */ 181 } auth; 182 183 uint16_t ctx_copies_len; 184 /* < number of entries in ctx_copies */ 185 struct evp_ctx_pair qp_ctx[]; 186 /**< Flexible array member of per-queue-pair structures, each containing 187 * pointers to copies of the cipher and auth EVP contexts. Cipher 188 * contexts are not safe to use from multiple cores simultaneously, so 189 * maintaining these copies allows avoiding per-buffer copying into a 190 * temporary context. 191 */ 192 }; 193 194 /** OPENSSL crypto private asymmetric session structure */ 195 struct __rte_cache_aligned openssl_asym_session { 196 enum rte_crypto_asym_xform_type xfrm_type; 197 union { 198 struct rsa { 199 RSA *rsa; 200 #if (OPENSSL_VERSION_NUMBER >= 0x30000000L) 201 EVP_PKEY_CTX * ctx; 202 #endif 203 } r; 204 struct exp { 205 BIGNUM *exp; 206 BIGNUM *mod; 207 BN_CTX *ctx; 208 } e; 209 struct mod { 210 BIGNUM *modulus; 211 BN_CTX *ctx; 212 } m; 213 struct dh { 214 DH *dh_key; 215 uint32_t key_op; 216 BIGNUM *p; 217 BIGNUM *g; 218 #if (OPENSSL_VERSION_NUMBER >= 0x30000000L) 219 OSSL_PARAM_BLD * param_bld; 220 OSSL_PARAM_BLD *param_bld_peer; 221 #endif 222 } dh; 223 struct { 224 DSA *dsa; 225 #if (OPENSSL_VERSION_NUMBER >= 0x30000000L) 226 OSSL_PARAM_BLD * param_bld; 227 BIGNUM *p; 228 BIGNUM *g; 229 BIGNUM *q; 230 BIGNUM *priv_key; 231 #endif 232 } s; 233 struct { 234 uint8_t curve_id; 235 #if (OPENSSL_VERSION_NUMBER >= 0x30000000L) 236 EC_GROUP * group; 237 BIGNUM *priv_key; 238 #endif 239 } ec; 240 struct { 241 #if (OPENSSL_VERSION_NUMBER >= 0x30000000L) 242 OSSL_PARAM * params; 243 #endif 244 } sm2; 245 struct { 246 uint8_t curve_id; 247 #if (OPENSSL_VERSION_NUMBER >= 0x30000000L) 248 OSSL_PARAM * params; 249 #endif 250 } eddsa; 251 } u; 252 }; 253 /** Set and validate OPENSSL crypto session parameters */ 254 extern int 255 openssl_set_session_parameters(struct openssl_session *sess, 256 const struct rte_crypto_sym_xform *xform, 257 uint16_t nb_queue_pairs); 258 259 /** Reset OPENSSL crypto session parameters */ 260 extern void 261 openssl_reset_session(struct openssl_session *sess); 262 263 /** device specific operations function pointer structure */ 264 extern struct rte_cryptodev_ops *rte_openssl_pmd_ops; 265 266 #endif /* _OPENSSL_PMD_PRIVATE_H_ */ 267