xref: /dpdk/lib/cryptodev/rte_crypto.h (revision 719834a6849e1daf4a70ff7742bbcc3ae7e25607)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2016-2017 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson #ifndef _RTE_CRYPTO_H_
699a2dd95SBruce Richardson #define _RTE_CRYPTO_H_
799a2dd95SBruce Richardson 
899a2dd95SBruce Richardson /**
999a2dd95SBruce Richardson  * @file rte_crypto.h
1099a2dd95SBruce Richardson  *
1199a2dd95SBruce Richardson  * RTE Cryptography Common Definitions
1299a2dd95SBruce Richardson  */
1399a2dd95SBruce Richardson 
1499a2dd95SBruce Richardson 
1599a2dd95SBruce Richardson #include <rte_mbuf.h>
1699a2dd95SBruce Richardson #include <rte_memory.h>
1799a2dd95SBruce Richardson #include <rte_mempool.h>
1899a2dd95SBruce Richardson #include <rte_common.h>
1999a2dd95SBruce Richardson 
2099a2dd95SBruce Richardson #include "rte_crypto_sym.h"
2199a2dd95SBruce Richardson #include "rte_crypto_asym.h"
2299a2dd95SBruce Richardson 
23*719834a6SMattias Rönnblom #ifdef __cplusplus
24*719834a6SMattias Rönnblom extern "C" {
25*719834a6SMattias Rönnblom #endif
26*719834a6SMattias Rönnblom 
2799a2dd95SBruce Richardson /** Crypto operation types */
2899a2dd95SBruce Richardson enum rte_crypto_op_type {
2999a2dd95SBruce Richardson 	RTE_CRYPTO_OP_TYPE_UNDEFINED,
3099a2dd95SBruce Richardson 	/**< Undefined operation type */
3199a2dd95SBruce Richardson 	RTE_CRYPTO_OP_TYPE_SYMMETRIC,
3299a2dd95SBruce Richardson 	/**< Symmetric operation */
3399a2dd95SBruce Richardson 	RTE_CRYPTO_OP_TYPE_ASYMMETRIC
3499a2dd95SBruce Richardson 	/**< Asymmetric operation */
3599a2dd95SBruce Richardson };
3699a2dd95SBruce Richardson 
3799a2dd95SBruce Richardson /** Status of crypto operation */
3899a2dd95SBruce Richardson enum rte_crypto_op_status {
3999a2dd95SBruce Richardson 	RTE_CRYPTO_OP_STATUS_SUCCESS,
4099a2dd95SBruce Richardson 	/**< Operation completed successfully */
4199a2dd95SBruce Richardson 	RTE_CRYPTO_OP_STATUS_NOT_PROCESSED,
4299a2dd95SBruce Richardson 	/**< Operation has not yet been processed by a crypto device */
4399a2dd95SBruce Richardson 	RTE_CRYPTO_OP_STATUS_AUTH_FAILED,
4499a2dd95SBruce Richardson 	/**< Authentication verification failed */
4599a2dd95SBruce Richardson 	RTE_CRYPTO_OP_STATUS_INVALID_SESSION,
4699a2dd95SBruce Richardson 	/**<
4799a2dd95SBruce Richardson 	 * Symmetric operation failed due to invalid session arguments, or if
4899a2dd95SBruce Richardson 	 * in session-less mode, failed to allocate private operation material.
4999a2dd95SBruce Richardson 	 */
5099a2dd95SBruce Richardson 	RTE_CRYPTO_OP_STATUS_INVALID_ARGS,
5199a2dd95SBruce Richardson 	/**< Operation failed due to invalid arguments in request */
5299a2dd95SBruce Richardson 	RTE_CRYPTO_OP_STATUS_ERROR,
5399a2dd95SBruce Richardson 	/**< Error handling operation */
5499a2dd95SBruce Richardson };
5599a2dd95SBruce Richardson 
5699a2dd95SBruce Richardson /**
5799a2dd95SBruce Richardson  * Crypto operation session type. This is used to specify whether a crypto
5899a2dd95SBruce Richardson  * operation has session structure attached for immutable parameters or if all
5999a2dd95SBruce Richardson  * operation information is included in the operation data structure.
6099a2dd95SBruce Richardson  */
6199a2dd95SBruce Richardson enum rte_crypto_op_sess_type {
6299a2dd95SBruce Richardson 	RTE_CRYPTO_OP_WITH_SESSION,	/**< Session based crypto operation */
6399a2dd95SBruce Richardson 	RTE_CRYPTO_OP_SESSIONLESS,	/**< Session-less crypto operation */
6499a2dd95SBruce Richardson 	RTE_CRYPTO_OP_SECURITY_SESSION	/**< Security session crypto operation */
6599a2dd95SBruce Richardson };
6699a2dd95SBruce Richardson 
671447ec84SVidya Sagar Velumuri /* Auxiliary flags related to crypto operation */
681447ec84SVidya Sagar Velumuri #define RTE_CRYPTO_OP_AUX_FLAGS_SESS_SOFT_EXPIRY (1 << 0)
691447ec84SVidya Sagar Velumuri /**< Session soft expiry limit has been reached.
701447ec84SVidya Sagar Velumuri  * Applicable for any session that has a soft lifetime feature supported.
711447ec84SVidya Sagar Velumuri  *
721447ec84SVidya Sagar Velumuri  * @see rte_security_ipsec_lifetime
731447ec84SVidya Sagar Velumuri  * @see rte_security_tls_record_lifetime
741447ec84SVidya Sagar Velumuri  */
751447ec84SVidya Sagar Velumuri 
76ad7515a3SAnoob Joseph /* Auxiliary flags related to IPsec offload with RTE_SECURITY */
77ad7515a3SAnoob Joseph 
781447ec84SVidya Sagar Velumuri #define RTE_CRYPTO_OP_AUX_FLAGS_IPSEC_SOFT_EXPIRY RTE_CRYPTO_OP_AUX_FLAGS_SESS_SOFT_EXPIRY
79ad7515a3SAnoob Joseph /**< SA soft expiry limit has been reached */
80ad7515a3SAnoob Joseph 
8199a2dd95SBruce Richardson /**
8299a2dd95SBruce Richardson  * Cryptographic Operation.
8399a2dd95SBruce Richardson  *
8499a2dd95SBruce Richardson  * This structure contains data relating to performing cryptographic
8599a2dd95SBruce Richardson  * operations. This operation structure is used to contain any operation which
8699a2dd95SBruce Richardson  * is supported by the cryptodev API, PMDs should check the type parameter to
8799a2dd95SBruce Richardson  * verify that the operation is a support function of the device. Crypto
8899a2dd95SBruce Richardson  * operations are enqueued and dequeued in crypto PMDs using the
8999a2dd95SBruce Richardson  * rte_cryptodev_enqueue_burst() / rte_cryptodev_dequeue_burst() .
9099a2dd95SBruce Richardson  */
9199a2dd95SBruce Richardson struct rte_crypto_op {
9299a2dd95SBruce Richardson 	__extension__
9399a2dd95SBruce Richardson 	union {
9499a2dd95SBruce Richardson 		uint64_t raw;
9599a2dd95SBruce Richardson 		__extension__
9699a2dd95SBruce Richardson 		struct {
9799a2dd95SBruce Richardson 			uint8_t type;
9899a2dd95SBruce Richardson 			/**< operation type */
9999a2dd95SBruce Richardson 			uint8_t status;
10099a2dd95SBruce Richardson 			/**<
10199a2dd95SBruce Richardson 			 * operation status - this is reset to
10299a2dd95SBruce Richardson 			 * RTE_CRYPTO_OP_STATUS_NOT_PROCESSED on allocation
10399a2dd95SBruce Richardson 			 * from mempool and will be set to
10499a2dd95SBruce Richardson 			 * RTE_CRYPTO_OP_STATUS_SUCCESS after crypto operation
10599a2dd95SBruce Richardson 			 * is successfully processed by a crypto PMD
10699a2dd95SBruce Richardson 			 */
10799a2dd95SBruce Richardson 			uint8_t sess_type;
10899a2dd95SBruce Richardson 			/**< operation session type */
109ad7515a3SAnoob Joseph 			uint8_t aux_flags;
110ad7515a3SAnoob Joseph 			/**< Operation specific auxiliary/additional flags.
111f495824bSAnoob Joseph 			 * These flags carry additional information from/to the
112ad7515a3SAnoob Joseph 			 * operation. Processing of the same is optional.
113f495824bSAnoob Joseph 			 *
114f495824bSAnoob Joseph 			 * The flags are defined as RTE_CRYPTO_OP_AUX_FLAGS_* and
115f495824bSAnoob Joseph 			 * would be set by PMD for application consumption when
116f495824bSAnoob Joseph 			 * the status is RTE_CRYPTO_OP_STATUS_SUCCESS.
117f495824bSAnoob Joseph 			 * In case of errors, the value of this field is undefined.
118f495824bSAnoob Joseph 			 *
119f495824bSAnoob Joseph 			 * With TLS record offload (RTE_SECURITY_PROTOCOL_TLS_RECORD),
120f495824bSAnoob Joseph 			 * application may provide the extra padding required for the plaintext
121f495824bSAnoob Joseph 			 * provided. This field can be used for passing the same in units of 8B.
122f495824bSAnoob Joseph 			 * The value would be set by application for PMD consumption.
123f495824bSAnoob Joseph 			 *
124f495824bSAnoob Joseph 			 * @see struct rte_security_tls_record_sess_options
125ad7515a3SAnoob Joseph 			 */
126f306fabfSVidya Sagar Velumuri 			union {
127f306fabfSVidya Sagar Velumuri 				struct {
128f306fabfSVidya Sagar Velumuri 					uint8_t content_type;
129f306fabfSVidya Sagar Velumuri 					/**< Content type. The field can act both as input
130f306fabfSVidya Sagar Velumuri 					 * and output.
131f306fabfSVidya Sagar Velumuri 					 *
132f306fabfSVidya Sagar Velumuri 					 * As input, for passing message type in case of record
133f306fabfSVidya Sagar Velumuri 					 * write (encrypt) operation. Applicable for,
134f306fabfSVidya Sagar Velumuri 					 * 1. TLS 1.2
135f306fabfSVidya Sagar Velumuri 					 * 2. TLS 1.3
136f306fabfSVidya Sagar Velumuri 					 * 3. DTLS 1.2
137f306fabfSVidya Sagar Velumuri 					 *
138f306fabfSVidya Sagar Velumuri 					 * As output, for returning message type in case of record
139f306fabfSVidya Sagar Velumuri 					 * read (decrypt) operation. Applicable for,
140f306fabfSVidya Sagar Velumuri 					 * 1. TLS 1.3
141f306fabfSVidya Sagar Velumuri 					 *
142f306fabfSVidya Sagar Velumuri 					 * Message types are listed as RTE_TLS_TYPE_* and
143f306fabfSVidya Sagar Velumuri 					 * RTE_DTLS_TYPE_*.
144f306fabfSVidya Sagar Velumuri 					 */
145f306fabfSVidya Sagar Velumuri 				} tls_record;
146f306fabfSVidya Sagar Velumuri 				/**< TLS record */
147f306fabfSVidya Sagar Velumuri 			} param1;
148f306fabfSVidya Sagar Velumuri 			/**< Additional per operation parameter 1. */
149f306fabfSVidya Sagar Velumuri 			uint8_t reserved[1];
15099a2dd95SBruce Richardson 			/**< Reserved bytes to fill 64 bits for
15199a2dd95SBruce Richardson 			 * future additions
15299a2dd95SBruce Richardson 			 */
15399a2dd95SBruce Richardson 			uint16_t private_data_offset;
15499a2dd95SBruce Richardson 			/**< Offset to indicate start of private data (if any).
15599a2dd95SBruce Richardson 			 * The offset is counted from the start of the
15699a2dd95SBruce Richardson 			 * rte_crypto_op including IV.
15799a2dd95SBruce Richardson 			 * The private data may be used by the application
15899a2dd95SBruce Richardson 			 * to store information which should remain untouched
15999a2dd95SBruce Richardson 			 * in the library/driver
16099a2dd95SBruce Richardson 			 */
16199a2dd95SBruce Richardson 		};
16299a2dd95SBruce Richardson 	};
16399a2dd95SBruce Richardson 	struct rte_mempool *mempool;
16499a2dd95SBruce Richardson 	/**< crypto operation mempool which operation is allocated from */
16599a2dd95SBruce Richardson 
16699a2dd95SBruce Richardson 	rte_iova_t phys_addr;
16799a2dd95SBruce Richardson 	/**< physical address of crypto operation */
16899a2dd95SBruce Richardson 
1691763c91bSBruce Richardson /* empty structures do not have zero size in C++ leading to compilation errors
1701763c91bSBruce Richardson  * with clang about structure/union having different sizes in C and C++.
1711763c91bSBruce Richardson  * While things are clearer with an explicit union, since each field is
1721763c91bSBruce Richardson  * zero-sized it's not actually needed, so omit it for C++
1731763c91bSBruce Richardson  */
1741763c91bSBruce Richardson #ifndef __cplusplus
17599a2dd95SBruce Richardson 	__extension__
17699a2dd95SBruce Richardson 	union {
1771763c91bSBruce Richardson #endif
17899a2dd95SBruce Richardson 		struct rte_crypto_sym_op sym[0];
17999a2dd95SBruce Richardson 		/**< Symmetric operation parameters */
18099a2dd95SBruce Richardson 
18199a2dd95SBruce Richardson 		struct rte_crypto_asym_op asym[0];
18299a2dd95SBruce Richardson 		/**< Asymmetric operation parameters */
18399a2dd95SBruce Richardson 
1841763c91bSBruce Richardson #ifndef __cplusplus
18599a2dd95SBruce Richardson 	}; /**< operation specific parameters */
1861763c91bSBruce Richardson #endif
18799a2dd95SBruce Richardson };
18899a2dd95SBruce Richardson 
18999a2dd95SBruce Richardson /**
19099a2dd95SBruce Richardson  * Reset the fields of a crypto operation to their default values.
19199a2dd95SBruce Richardson  *
19299a2dd95SBruce Richardson  * @param	op	The crypto operation to be reset.
19399a2dd95SBruce Richardson  * @param	type	The crypto operation type.
19499a2dd95SBruce Richardson  */
19599a2dd95SBruce Richardson static inline void
19699a2dd95SBruce Richardson __rte_crypto_op_reset(struct rte_crypto_op *op, enum rte_crypto_op_type type)
19799a2dd95SBruce Richardson {
19899a2dd95SBruce Richardson 	op->type = type;
19999a2dd95SBruce Richardson 	op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
20099a2dd95SBruce Richardson 	op->sess_type = RTE_CRYPTO_OP_SESSIONLESS;
20199a2dd95SBruce Richardson 
20299a2dd95SBruce Richardson 	switch (type) {
20399a2dd95SBruce Richardson 	case RTE_CRYPTO_OP_TYPE_SYMMETRIC:
20499a2dd95SBruce Richardson 		__rte_crypto_sym_op_reset(op->sym);
20599a2dd95SBruce Richardson 		break;
20699a2dd95SBruce Richardson 	case RTE_CRYPTO_OP_TYPE_ASYMMETRIC:
20799a2dd95SBruce Richardson 		memset(op->asym, 0, sizeof(struct rte_crypto_asym_op));
20899a2dd95SBruce Richardson 	break;
20999a2dd95SBruce Richardson 	case RTE_CRYPTO_OP_TYPE_UNDEFINED:
21099a2dd95SBruce Richardson 	default:
21199a2dd95SBruce Richardson 		break;
21299a2dd95SBruce Richardson 	}
21399a2dd95SBruce Richardson }
21499a2dd95SBruce Richardson 
21599a2dd95SBruce Richardson /**
21699a2dd95SBruce Richardson  * Private data structure belonging to a crypto symmetric operation pool.
21799a2dd95SBruce Richardson  */
21899a2dd95SBruce Richardson struct rte_crypto_op_pool_private {
21999a2dd95SBruce Richardson 	enum rte_crypto_op_type type;
22099a2dd95SBruce Richardson 	/**< Crypto op pool type operation. */
22199a2dd95SBruce Richardson 	uint16_t priv_size;
22299a2dd95SBruce Richardson 	/**< Size of private area in each crypto operation. */
22399a2dd95SBruce Richardson };
22499a2dd95SBruce Richardson 
22599a2dd95SBruce Richardson 
22699a2dd95SBruce Richardson /**
22799a2dd95SBruce Richardson  * Returns the size of private data allocated with each rte_crypto_op object by
22899a2dd95SBruce Richardson  * the mempool
22999a2dd95SBruce Richardson  *
23099a2dd95SBruce Richardson  * @param	mempool	rte_crypto_op mempool
23199a2dd95SBruce Richardson  *
23299a2dd95SBruce Richardson  * @return	private data size
23399a2dd95SBruce Richardson  */
23499a2dd95SBruce Richardson static inline uint16_t
23599a2dd95SBruce Richardson __rte_crypto_op_get_priv_data_size(struct rte_mempool *mempool)
23699a2dd95SBruce Richardson {
23799a2dd95SBruce Richardson 	struct rte_crypto_op_pool_private *priv =
23899a2dd95SBruce Richardson 		(struct rte_crypto_op_pool_private *) rte_mempool_get_priv(mempool);
23999a2dd95SBruce Richardson 
24099a2dd95SBruce Richardson 	return priv->priv_size;
24199a2dd95SBruce Richardson }
24299a2dd95SBruce Richardson 
24399a2dd95SBruce Richardson 
24499a2dd95SBruce Richardson /**
24599a2dd95SBruce Richardson  * Creates a crypto operation pool
24699a2dd95SBruce Richardson  *
24799a2dd95SBruce Richardson  * @param	name		pool name
24899a2dd95SBruce Richardson  * @param	type		crypto operation type, use
24999a2dd95SBruce Richardson  *				RTE_CRYPTO_OP_TYPE_UNDEFINED for a pool which
25099a2dd95SBruce Richardson  *				supports all operation types
25199a2dd95SBruce Richardson  * @param	nb_elts		number of elements in pool
25299a2dd95SBruce Richardson  * @param	cache_size	Number of elements to cache on lcore, see
25399a2dd95SBruce Richardson  *				*rte_mempool_create* for further details about
25499a2dd95SBruce Richardson  *				cache size
25599a2dd95SBruce Richardson  * @param	priv_size	Size of private data to allocate with each
25699a2dd95SBruce Richardson  *				operation
25799a2dd95SBruce Richardson  * @param	socket_id	Socket to allocate memory on
25899a2dd95SBruce Richardson  *
25999a2dd95SBruce Richardson  * @return
26099a2dd95SBruce Richardson  *  - On success pointer to mempool
26199a2dd95SBruce Richardson  *  - On failure NULL
26299a2dd95SBruce Richardson  */
26316bd1c62SThomas Monjalon struct rte_mempool *
26499a2dd95SBruce Richardson rte_crypto_op_pool_create(const char *name, enum rte_crypto_op_type type,
26599a2dd95SBruce Richardson 		unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
26699a2dd95SBruce Richardson 		int socket_id);
26799a2dd95SBruce Richardson 
26899a2dd95SBruce Richardson /**
26999a2dd95SBruce Richardson  * Bulk allocate raw element from mempool and return as crypto operations
27099a2dd95SBruce Richardson  *
27199a2dd95SBruce Richardson  * @param	mempool		crypto operation mempool.
27299a2dd95SBruce Richardson  * @param	type		crypto operation type.
27399a2dd95SBruce Richardson  * @param	ops		Array to place allocated crypto operations
27499a2dd95SBruce Richardson  * @param	nb_ops		Number of crypto operations to allocate
27599a2dd95SBruce Richardson  *
27699a2dd95SBruce Richardson  * @returns
27799a2dd95SBruce Richardson  * - On success returns  number of ops allocated
27899a2dd95SBruce Richardson  */
27999a2dd95SBruce Richardson static inline int
28099a2dd95SBruce Richardson __rte_crypto_op_raw_bulk_alloc(struct rte_mempool *mempool,
28199a2dd95SBruce Richardson 		enum rte_crypto_op_type type,
28299a2dd95SBruce Richardson 		struct rte_crypto_op **ops, uint16_t nb_ops)
28399a2dd95SBruce Richardson {
28499a2dd95SBruce Richardson 	struct rte_crypto_op_pool_private *priv;
28599a2dd95SBruce Richardson 
28699a2dd95SBruce Richardson 	priv = (struct rte_crypto_op_pool_private *) rte_mempool_get_priv(mempool);
28799a2dd95SBruce Richardson 	if (unlikely(priv->type != type &&
28899a2dd95SBruce Richardson 			priv->type != RTE_CRYPTO_OP_TYPE_UNDEFINED))
28999a2dd95SBruce Richardson 		return -EINVAL;
29099a2dd95SBruce Richardson 
29199a2dd95SBruce Richardson 	if (rte_mempool_get_bulk(mempool, (void **)ops, nb_ops) == 0)
29299a2dd95SBruce Richardson 		return nb_ops;
29399a2dd95SBruce Richardson 
29499a2dd95SBruce Richardson 	return 0;
29599a2dd95SBruce Richardson }
29699a2dd95SBruce Richardson 
29799a2dd95SBruce Richardson /**
29899a2dd95SBruce Richardson  * Allocate a crypto operation from a mempool with default parameters set
29999a2dd95SBruce Richardson  *
30099a2dd95SBruce Richardson  * @param	mempool	crypto operation mempool
30199a2dd95SBruce Richardson  * @param	type	operation type to allocate
30299a2dd95SBruce Richardson  *
30399a2dd95SBruce Richardson  * @returns
30499a2dd95SBruce Richardson  * - On success returns a valid rte_crypto_op structure
30599a2dd95SBruce Richardson  * - On failure returns NULL
30699a2dd95SBruce Richardson  */
30799a2dd95SBruce Richardson static inline struct rte_crypto_op *
30899a2dd95SBruce Richardson rte_crypto_op_alloc(struct rte_mempool *mempool, enum rte_crypto_op_type type)
30999a2dd95SBruce Richardson {
31099a2dd95SBruce Richardson 	struct rte_crypto_op *op = NULL;
31199a2dd95SBruce Richardson 	int retval;
31299a2dd95SBruce Richardson 
31399a2dd95SBruce Richardson 	retval = __rte_crypto_op_raw_bulk_alloc(mempool, type, &op, 1);
31499a2dd95SBruce Richardson 	if (unlikely(retval != 1))
31599a2dd95SBruce Richardson 		return NULL;
31699a2dd95SBruce Richardson 
31799a2dd95SBruce Richardson 	__rte_crypto_op_reset(op, type);
31899a2dd95SBruce Richardson 
31999a2dd95SBruce Richardson 	return op;
32099a2dd95SBruce Richardson }
32199a2dd95SBruce Richardson 
32299a2dd95SBruce Richardson 
32399a2dd95SBruce Richardson /**
32499a2dd95SBruce Richardson  * Bulk allocate crypto operations from a mempool with default parameters set
32599a2dd95SBruce Richardson  *
32699a2dd95SBruce Richardson  * @param	mempool	crypto operation mempool
32799a2dd95SBruce Richardson  * @param	type	operation type to allocate
32899a2dd95SBruce Richardson  * @param	ops	Array to place allocated crypto operations
32999a2dd95SBruce Richardson  * @param	nb_ops	Number of crypto operations to allocate
33099a2dd95SBruce Richardson  *
33199a2dd95SBruce Richardson  * @returns
33299a2dd95SBruce Richardson  * - nb_ops if the number of operations requested were allocated.
33399a2dd95SBruce Richardson  * - 0 if the requested number of ops are not available.
33499a2dd95SBruce Richardson  *   None are allocated in this case.
33599a2dd95SBruce Richardson  */
33699a2dd95SBruce Richardson 
33799a2dd95SBruce Richardson static inline unsigned
33899a2dd95SBruce Richardson rte_crypto_op_bulk_alloc(struct rte_mempool *mempool,
33999a2dd95SBruce Richardson 		enum rte_crypto_op_type type,
34099a2dd95SBruce Richardson 		struct rte_crypto_op **ops, uint16_t nb_ops)
34199a2dd95SBruce Richardson {
34299a2dd95SBruce Richardson 	int i;
34399a2dd95SBruce Richardson 
34499a2dd95SBruce Richardson 	if (unlikely(__rte_crypto_op_raw_bulk_alloc(mempool, type, ops, nb_ops)
34599a2dd95SBruce Richardson 			!= nb_ops))
34699a2dd95SBruce Richardson 		return 0;
34799a2dd95SBruce Richardson 
34899a2dd95SBruce Richardson 	for (i = 0; i < nb_ops; i++)
34999a2dd95SBruce Richardson 		__rte_crypto_op_reset(ops[i], type);
35099a2dd95SBruce Richardson 
35199a2dd95SBruce Richardson 	return nb_ops;
35299a2dd95SBruce Richardson }
35399a2dd95SBruce Richardson 
35499a2dd95SBruce Richardson 
35599a2dd95SBruce Richardson 
35699a2dd95SBruce Richardson /**
35799a2dd95SBruce Richardson  * Returns a pointer to the private data of a crypto operation if
35899a2dd95SBruce Richardson  * that operation has enough capacity for requested size.
35999a2dd95SBruce Richardson  *
36099a2dd95SBruce Richardson  * @param	op	crypto operation.
36199a2dd95SBruce Richardson  * @param	size	size of space requested in private data.
36299a2dd95SBruce Richardson  *
36399a2dd95SBruce Richardson  * @returns
36499a2dd95SBruce Richardson  * - if sufficient space available returns pointer to start of private data
36599a2dd95SBruce Richardson  * - if insufficient space returns NULL
36699a2dd95SBruce Richardson  */
36799a2dd95SBruce Richardson static inline void *
36899a2dd95SBruce Richardson __rte_crypto_op_get_priv_data(struct rte_crypto_op *op, uint32_t size)
36999a2dd95SBruce Richardson {
37099a2dd95SBruce Richardson 	uint32_t priv_size;
37199a2dd95SBruce Richardson 
37299a2dd95SBruce Richardson 	if (likely(op->mempool != NULL)) {
37399a2dd95SBruce Richardson 		priv_size = __rte_crypto_op_get_priv_data_size(op->mempool);
37499a2dd95SBruce Richardson 
37599a2dd95SBruce Richardson 		if (likely(priv_size >= size)) {
37699a2dd95SBruce Richardson 			if (op->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC)
37799a2dd95SBruce Richardson 				return (void *)((uint8_t *)(op + 1) +
37899a2dd95SBruce Richardson 					sizeof(struct rte_crypto_sym_op));
37999a2dd95SBruce Richardson 			if (op->type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC)
38099a2dd95SBruce Richardson 				return (void *)((uint8_t *)(op + 1) +
38199a2dd95SBruce Richardson 					sizeof(struct rte_crypto_asym_op));
38299a2dd95SBruce Richardson 		}
38399a2dd95SBruce Richardson 	}
38499a2dd95SBruce Richardson 
38599a2dd95SBruce Richardson 	return NULL;
38699a2dd95SBruce Richardson }
38799a2dd95SBruce Richardson 
38899a2dd95SBruce Richardson /**
38999a2dd95SBruce Richardson  * free crypto operation structure
39099a2dd95SBruce Richardson  * If operation has been allocate from a rte_mempool, then the operation will
39199a2dd95SBruce Richardson  * be returned to the mempool.
39299a2dd95SBruce Richardson  *
393448e01f1SStephen Hemminger  * @param op
394448e01f1SStephen Hemminger  *   Pointer to symmetric crypto operation allocated with rte_crypto_op_alloc()
395448e01f1SStephen Hemminger  *   If op is NULL, no operation is performed.
39699a2dd95SBruce Richardson  */
39799a2dd95SBruce Richardson static inline void
39899a2dd95SBruce Richardson rte_crypto_op_free(struct rte_crypto_op *op)
39999a2dd95SBruce Richardson {
40099a2dd95SBruce Richardson 	if (op != NULL && op->mempool != NULL)
40199a2dd95SBruce Richardson 		rte_mempool_put(op->mempool, op);
40299a2dd95SBruce Richardson }
40399a2dd95SBruce Richardson 
40499a2dd95SBruce Richardson /**
40599a2dd95SBruce Richardson  * Allocate a symmetric crypto operation in the private data of an mbuf.
40699a2dd95SBruce Richardson  *
40799a2dd95SBruce Richardson  * @param	m	mbuf which is associated with the crypto operation, the
40899a2dd95SBruce Richardson  *			operation will be allocated in the private data of that
40999a2dd95SBruce Richardson  *			mbuf.
41099a2dd95SBruce Richardson  *
41199a2dd95SBruce Richardson  * @returns
41299a2dd95SBruce Richardson  * - On success returns a pointer to the crypto operation.
41399a2dd95SBruce Richardson  * - On failure returns NULL.
41499a2dd95SBruce Richardson  */
41599a2dd95SBruce Richardson static inline struct rte_crypto_op *
41699a2dd95SBruce Richardson rte_crypto_sym_op_alloc_from_mbuf_priv_data(struct rte_mbuf *m)
41799a2dd95SBruce Richardson {
41899a2dd95SBruce Richardson 	if (unlikely(m == NULL))
41999a2dd95SBruce Richardson 		return NULL;
42099a2dd95SBruce Richardson 
42199a2dd95SBruce Richardson 	/*
42299a2dd95SBruce Richardson 	 * check that the mbuf's private data size is sufficient to contain a
42399a2dd95SBruce Richardson 	 * crypto operation
42499a2dd95SBruce Richardson 	 */
42599a2dd95SBruce Richardson 	if (unlikely(m->priv_size < (sizeof(struct rte_crypto_op) +
42699a2dd95SBruce Richardson 			sizeof(struct rte_crypto_sym_op))))
42799a2dd95SBruce Richardson 		return NULL;
42899a2dd95SBruce Richardson 
42999a2dd95SBruce Richardson 	/* private data starts immediately after the mbuf header in the mbuf. */
43099a2dd95SBruce Richardson 	struct rte_crypto_op *op = (struct rte_crypto_op *)(m + 1);
43199a2dd95SBruce Richardson 
43299a2dd95SBruce Richardson 	__rte_crypto_op_reset(op, RTE_CRYPTO_OP_TYPE_SYMMETRIC);
43399a2dd95SBruce Richardson 
43499a2dd95SBruce Richardson 	op->mempool = NULL;
43599a2dd95SBruce Richardson 	op->sym->m_src = m;
43699a2dd95SBruce Richardson 
43799a2dd95SBruce Richardson 	return op;
43899a2dd95SBruce Richardson }
43999a2dd95SBruce Richardson 
44099a2dd95SBruce Richardson /**
44199a2dd95SBruce Richardson  * Allocate space for symmetric crypto xforms in the private data space of the
44299a2dd95SBruce Richardson  * crypto operation. This also defaults the crypto xform type and configures
44399a2dd95SBruce Richardson  * the chaining of the xforms in the crypto operation
44499a2dd95SBruce Richardson  *
44599a2dd95SBruce Richardson  * @return
44699a2dd95SBruce Richardson  * - On success returns pointer to first crypto xform in crypto operations chain
44799a2dd95SBruce Richardson  * - On failure returns NULL
44899a2dd95SBruce Richardson  */
44999a2dd95SBruce Richardson static inline struct rte_crypto_sym_xform *
45099a2dd95SBruce Richardson rte_crypto_op_sym_xforms_alloc(struct rte_crypto_op *op, uint8_t nb_xforms)
45199a2dd95SBruce Richardson {
45299a2dd95SBruce Richardson 	void *priv_data;
45399a2dd95SBruce Richardson 	uint32_t size;
45499a2dd95SBruce Richardson 
45599a2dd95SBruce Richardson 	if (unlikely(op->type != RTE_CRYPTO_OP_TYPE_SYMMETRIC))
45699a2dd95SBruce Richardson 		return NULL;
45799a2dd95SBruce Richardson 
45899a2dd95SBruce Richardson 	size = sizeof(struct rte_crypto_sym_xform) * nb_xforms;
45999a2dd95SBruce Richardson 
46099a2dd95SBruce Richardson 	priv_data = __rte_crypto_op_get_priv_data(op, size);
46199a2dd95SBruce Richardson 	if (priv_data == NULL)
46299a2dd95SBruce Richardson 		return NULL;
46399a2dd95SBruce Richardson 
46499a2dd95SBruce Richardson 	return __rte_crypto_sym_op_sym_xforms_alloc(op->sym, priv_data,
46599a2dd95SBruce Richardson 			nb_xforms);
46699a2dd95SBruce Richardson }
46799a2dd95SBruce Richardson 
46899a2dd95SBruce Richardson 
46999a2dd95SBruce Richardson /**
47099a2dd95SBruce Richardson  * Attach a session to a crypto operation
47199a2dd95SBruce Richardson  *
47299a2dd95SBruce Richardson  * @param	op	crypto operation, must be of type symmetric
47399a2dd95SBruce Richardson  * @param	sess	cryptodev session
47499a2dd95SBruce Richardson  */
47599a2dd95SBruce Richardson static inline int
4762a440d6aSAkhil Goyal rte_crypto_op_attach_sym_session(struct rte_crypto_op *op, void *sess)
47799a2dd95SBruce Richardson {
47899a2dd95SBruce Richardson 	if (unlikely(op->type != RTE_CRYPTO_OP_TYPE_SYMMETRIC))
47999a2dd95SBruce Richardson 		return -1;
48099a2dd95SBruce Richardson 
48199a2dd95SBruce Richardson 	op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
48299a2dd95SBruce Richardson 
48399a2dd95SBruce Richardson 	return __rte_crypto_sym_op_attach_sym_session(op->sym, sess);
48499a2dd95SBruce Richardson }
48599a2dd95SBruce Richardson 
48699a2dd95SBruce Richardson /**
48799a2dd95SBruce Richardson  * Attach a asymmetric session to a crypto operation
48899a2dd95SBruce Richardson  *
48999a2dd95SBruce Richardson  * @param	op	crypto operation, must be of type asymmetric
49099a2dd95SBruce Richardson  * @param	sess	cryptodev session
49199a2dd95SBruce Richardson  */
49299a2dd95SBruce Richardson static inline int
49399a2dd95SBruce Richardson rte_crypto_op_attach_asym_session(struct rte_crypto_op *op,
49499a2dd95SBruce Richardson 		struct rte_cryptodev_asym_session *sess)
49599a2dd95SBruce Richardson {
49699a2dd95SBruce Richardson 	if (unlikely(op->type != RTE_CRYPTO_OP_TYPE_ASYMMETRIC))
49799a2dd95SBruce Richardson 		return -1;
49899a2dd95SBruce Richardson 
49999a2dd95SBruce Richardson 	op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
50099a2dd95SBruce Richardson 	op->asym->session = sess;
50199a2dd95SBruce Richardson 	return 0;
50299a2dd95SBruce Richardson }
50399a2dd95SBruce Richardson 
50499a2dd95SBruce Richardson #ifdef __cplusplus
50599a2dd95SBruce Richardson }
50699a2dd95SBruce Richardson #endif
50799a2dd95SBruce Richardson 
50899a2dd95SBruce Richardson #endif /* _RTE_CRYPTO_H_ */
509