xref: /dpdk/drivers/crypto/ipsec_mb/pmd_zuc.c (revision e931ffa76dce48840fa2ef728ac7b2cbe1bd6eca)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016-2021 Intel Corporation
3  */
4 
5 #include "pmd_zuc_priv.h"
6 
7 /** Parse crypto xform chain and set private session parameters. */
8 static int
zuc_session_configure(__rte_unused IMB_MGR * mgr,void * zuc_sess,const struct rte_crypto_sym_xform * xform)9 zuc_session_configure(__rte_unused IMB_MGR * mgr, void *zuc_sess,
10 		const struct rte_crypto_sym_xform *xform)
11 {
12 	struct zuc_session *sess = (struct zuc_session *) zuc_sess;
13 	const struct rte_crypto_sym_xform *auth_xform = NULL;
14 	const struct rte_crypto_sym_xform *cipher_xform = NULL;
15 	enum ipsec_mb_operation mode;
16 	/* Select Crypto operation - hash then cipher / cipher then hash */
17 	int ret = ipsec_mb_parse_xform(xform, &mode, &auth_xform,
18 				&cipher_xform, NULL);
19 
20 	if (ret)
21 		return ret;
22 
23 	if (cipher_xform) {
24 		/* Only ZUC EEA3 supported */
25 		if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_ZUC_EEA3)
26 			return -ENOTSUP;
27 
28 		if (cipher_xform->cipher.iv.length != ZUC_IV_KEY_LENGTH) {
29 			IPSEC_MB_LOG(ERR, "Wrong IV length");
30 			return -EINVAL;
31 		}
32 		sess->cipher_iv_offset = cipher_xform->cipher.iv.offset;
33 
34 		/* Copy the key */
35 		memcpy(sess->pKey_cipher, cipher_xform->cipher.key.data,
36 				ZUC_IV_KEY_LENGTH);
37 	}
38 
39 	if (auth_xform) {
40 		/* Only ZUC EIA3 supported */
41 		if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_ZUC_EIA3)
42 			return -ENOTSUP;
43 
44 		if (auth_xform->auth.digest_length != ZUC_DIGEST_LENGTH) {
45 			IPSEC_MB_LOG(ERR, "Wrong digest length");
46 			return -EINVAL;
47 		}
48 
49 		sess->auth_op = auth_xform->auth.op;
50 
51 		if (auth_xform->auth.iv.length != ZUC_IV_KEY_LENGTH) {
52 			IPSEC_MB_LOG(ERR, "Wrong IV length");
53 			return -EINVAL;
54 		}
55 		sess->auth_iv_offset = auth_xform->auth.iv.offset;
56 
57 		/* Copy the key */
58 		memcpy(sess->pKey_hash, auth_xform->auth.key.data,
59 				ZUC_IV_KEY_LENGTH);
60 	}
61 
62 	sess->op = mode;
63 	return 0;
64 }
65 
66 /** Encrypt/decrypt mbufs. */
67 static uint8_t
process_zuc_cipher_op(struct ipsec_mb_qp * qp,struct rte_crypto_op ** ops,struct zuc_session ** sessions,uint8_t num_ops)68 process_zuc_cipher_op(struct ipsec_mb_qp *qp, struct rte_crypto_op **ops,
69 		struct zuc_session **sessions,
70 		uint8_t num_ops)
71 {
72 	unsigned int i;
73 	uint8_t processed_ops = 0;
74 	const void *src[ZUC_MAX_BURST];
75 	void *dst[ZUC_MAX_BURST];
76 	const void *iv[ZUC_MAX_BURST];
77 	uint32_t num_bytes[ZUC_MAX_BURST];
78 	const void *cipher_keys[ZUC_MAX_BURST];
79 	struct zuc_session *sess;
80 
81 	for (i = 0; i < num_ops; i++) {
82 		if (((ops[i]->sym->cipher.data.length % BYTE_LEN) != 0)
83 				|| ((ops[i]->sym->cipher.data.offset
84 					% BYTE_LEN) != 0)) {
85 			ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
86 			IPSEC_MB_LOG(ERR, "Data Length or offset");
87 			break;
88 		}
89 
90 		sess = sessions[i];
91 
92 #ifdef RTE_LIBRTE_PMD_ZUC_DEBUG
93 		if (!rte_pktmbuf_is_contiguous(ops[i]->sym->m_src) ||
94 				(ops[i]->sym->m_dst != NULL &&
95 				!rte_pktmbuf_is_contiguous(
96 						ops[i]->sym->m_dst))) {
97 			IPSEC_MB_LOG(ERR, "PMD supports only "
98 				" contiguous mbufs, op (%p) "
99 				"provides noncontiguous mbuf "
100 				"as source/destination buffer.\n",
101 				"PMD supports only contiguous mbufs, "
102 				"op (%p) provides noncontiguous mbuf "
103 				"as source/destination buffer.\n",
104 				ops[i]);
105 			ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
106 			break;
107 		}
108 #endif
109 
110 		src[i] = rte_pktmbuf_mtod_offset(ops[i]->sym->m_src,
111 						 uint8_t *,
112 						 (ops[i]->sym->cipher.data.offset >> 3));
113 		dst[i] = ops[i]->sym->m_dst ?
114 			rte_pktmbuf_mtod_offset(ops[i]->sym->m_dst, uint8_t *,
115 						(ops[i]->sym->cipher.data.offset >> 3)) :
116 			rte_pktmbuf_mtod_offset(ops[i]->sym->m_src, uint8_t *,
117 						(ops[i]->sym->cipher.data.offset >> 3));
118 		iv[i] = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
119 				sess->cipher_iv_offset);
120 		num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
121 
122 		cipher_keys[i] = sess->pKey_cipher;
123 
124 		processed_ops++;
125 	}
126 
127 	IMB_ZUC_EEA3_N_BUFFER(qp->mb_mgr, (const void **)cipher_keys,
128 			(const void **)iv, (const void **)src, (void **)dst,
129 			num_bytes, processed_ops);
130 
131 	return processed_ops;
132 }
133 
134 /** Generate/verify hash from mbufs. */
135 static int
process_zuc_hash_op(struct ipsec_mb_qp * qp,struct rte_crypto_op ** ops,struct zuc_session ** sessions,uint8_t num_ops)136 process_zuc_hash_op(struct ipsec_mb_qp *qp, struct rte_crypto_op **ops,
137 		struct zuc_session **sessions,
138 		uint8_t num_ops)
139 {
140 	unsigned int i;
141 	uint8_t processed_ops = 0;
142 	uint8_t *src[ZUC_MAX_BURST] = { 0 };
143 	uint32_t *dst[ZUC_MAX_BURST];
144 	uint32_t length_in_bits[ZUC_MAX_BURST] = { 0 };
145 	uint8_t *iv[ZUC_MAX_BURST] = { 0 };
146 	const void *hash_keys[ZUC_MAX_BURST] = { 0 };
147 	struct zuc_session *sess;
148 	struct zuc_qp_data *qp_data = ipsec_mb_get_qp_private_data(qp);
149 
150 
151 	for (i = 0; i < num_ops; i++) {
152 		/* Data must be byte aligned */
153 		if ((ops[i]->sym->auth.data.offset % BYTE_LEN) != 0) {
154 			ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
155 			IPSEC_MB_LOG(ERR, "Offset");
156 			break;
157 		}
158 
159 		sess = sessions[i];
160 
161 		length_in_bits[i] = ops[i]->sym->auth.data.length;
162 
163 		src[i] = rte_pktmbuf_mtod_offset(ops[i]->sym->m_src,
164 						 uint8_t *,
165 						 (ops[i]->sym->auth.data.offset >> 3));
166 		iv[i] = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
167 				sess->auth_iv_offset);
168 
169 		hash_keys[i] = sess->pKey_hash;
170 		if (sess->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY)
171 			dst[i] = (uint32_t *)qp_data->temp_digest[i];
172 		else
173 			dst[i] = (uint32_t *)ops[i]->sym->auth.digest.data;
174 
175 		processed_ops++;
176 	}
177 
178 	IMB_ZUC_EIA3_N_BUFFER(qp->mb_mgr, (const void **)hash_keys,
179 			(const void * const *)iv, (const void * const *)src,
180 			length_in_bits, dst, processed_ops);
181 
182 	/*
183 	 * If tag needs to be verified, compare generated tag
184 	 * with attached tag
185 	 */
186 	for (i = 0; i < processed_ops; i++)
187 		if (sessions[i]->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY)
188 			if (memcmp(dst[i], ops[i]->sym->auth.digest.data,
189 					ZUC_DIGEST_LENGTH) != 0)
190 				ops[i]->status =
191 					RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
192 
193 	return processed_ops;
194 }
195 
196 /** Process a batch of crypto ops which shares the same operation type. */
197 static int
process_ops(struct rte_crypto_op ** ops,enum ipsec_mb_operation op_type,struct zuc_session ** sessions,struct ipsec_mb_qp * qp,uint8_t num_ops)198 process_ops(struct rte_crypto_op **ops, enum ipsec_mb_operation op_type,
199 		struct zuc_session **sessions,
200 		struct ipsec_mb_qp *qp, uint8_t num_ops)
201 {
202 	unsigned int i;
203 	unsigned int processed_ops = 0;
204 
205 	switch (op_type) {
206 	case IPSEC_MB_OP_ENCRYPT_ONLY:
207 	case IPSEC_MB_OP_DECRYPT_ONLY:
208 		processed_ops = process_zuc_cipher_op(qp, ops,
209 				sessions, num_ops);
210 		break;
211 	case IPSEC_MB_OP_HASH_GEN_ONLY:
212 	case IPSEC_MB_OP_HASH_VERIFY_ONLY:
213 		processed_ops = process_zuc_hash_op(qp, ops, sessions,
214 				num_ops);
215 		break;
216 	case IPSEC_MB_OP_ENCRYPT_THEN_HASH_GEN:
217 	case IPSEC_MB_OP_DECRYPT_THEN_HASH_VERIFY:
218 		processed_ops = process_zuc_cipher_op(qp, ops, sessions,
219 				num_ops);
220 		process_zuc_hash_op(qp, ops, sessions, processed_ops);
221 		break;
222 	case IPSEC_MB_OP_HASH_VERIFY_THEN_DECRYPT:
223 	case IPSEC_MB_OP_HASH_GEN_THEN_ENCRYPT:
224 		processed_ops = process_zuc_hash_op(qp, ops, sessions,
225 				num_ops);
226 		process_zuc_cipher_op(qp, ops, sessions, processed_ops);
227 		break;
228 	default:
229 		/* Operation not supported. */
230 		for (i = 0; i < num_ops; i++)
231 			ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
232 	}
233 
234 	for (i = 0; i < num_ops; i++) {
235 		/*
236 		 * If there was no error/authentication failure,
237 		 * change status to successful.
238 		 */
239 		if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
240 			ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
241 		/* Free session if a session-less crypto op. */
242 		if (ops[i]->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
243 			memset(sessions[i], 0, sizeof(struct zuc_session));
244 			rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
245 			ops[i]->sym->session = NULL;
246 		}
247 	}
248 	return processed_ops;
249 }
250 
251 static uint16_t
zuc_pmd_dequeue_burst(void * queue_pair,struct rte_crypto_op ** c_ops,uint16_t nb_ops)252 zuc_pmd_dequeue_burst(void *queue_pair,
253 		struct rte_crypto_op **c_ops, uint16_t nb_ops)
254 {
255 
256 	struct rte_crypto_op *curr_c_op;
257 
258 	struct zuc_session *curr_sess;
259 	struct zuc_session *sessions[ZUC_MAX_BURST];
260 	struct rte_crypto_op *int_c_ops[ZUC_MAX_BURST];
261 	enum ipsec_mb_operation prev_zuc_op = IPSEC_MB_OP_NOT_SUPPORTED;
262 	enum ipsec_mb_operation curr_zuc_op;
263 	struct ipsec_mb_qp *qp = queue_pair;
264 	unsigned int nb_dequeued;
265 	unsigned int i;
266 	uint8_t burst_size = 0;
267 	uint8_t processed_ops;
268 
269 	nb_dequeued = rte_ring_dequeue_burst(qp->ingress_queue,
270 			(void **)c_ops, nb_ops, NULL);
271 
272 
273 	for (i = 0; i < nb_dequeued; i++) {
274 		curr_c_op = c_ops[i];
275 
276 		curr_sess = (struct zuc_session *)
277 			ipsec_mb_get_session_private(qp, curr_c_op);
278 		if (unlikely(curr_sess == NULL)) {
279 			curr_c_op->status =
280 					RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
281 			break;
282 		}
283 
284 		curr_zuc_op = curr_sess->op;
285 
286 		/*
287 		 * Batch ops that share the same operation type
288 		 * (cipher only, auth only...).
289 		 */
290 		if (burst_size == 0) {
291 			prev_zuc_op = curr_zuc_op;
292 			int_c_ops[0] = curr_c_op;
293 			sessions[0] = curr_sess;
294 			burst_size++;
295 		} else if (curr_zuc_op == prev_zuc_op) {
296 			int_c_ops[burst_size] = curr_c_op;
297 			sessions[burst_size] = curr_sess;
298 			burst_size++;
299 			/*
300 			 * When there are enough ops to process in a batch,
301 			 * process them, and start a new batch.
302 			 */
303 			if (burst_size == ZUC_MAX_BURST) {
304 				processed_ops = process_ops(int_c_ops, curr_zuc_op,
305 						sessions, qp, burst_size);
306 				if (processed_ops < burst_size) {
307 					burst_size = 0;
308 					break;
309 				}
310 
311 				burst_size = 0;
312 			}
313 		} else {
314 			/*
315 			 * Different operation type, process the ops
316 			 * of the previous type.
317 			 */
318 			processed_ops = process_ops(int_c_ops, prev_zuc_op,
319 					sessions, qp, burst_size);
320 			if (processed_ops < burst_size) {
321 				burst_size = 0;
322 				break;
323 			}
324 
325 			burst_size = 0;
326 			prev_zuc_op = curr_zuc_op;
327 
328 			int_c_ops[0] = curr_c_op;
329 			sessions[0] = curr_sess;
330 			burst_size++;
331 		}
332 	}
333 
334 	if (burst_size != 0) {
335 		/* Process the crypto ops of the last operation type. */
336 		processed_ops = process_ops(int_c_ops, prev_zuc_op,
337 				sessions, qp, burst_size);
338 	}
339 
340 	qp->stats.dequeued_count += i;
341 	return i;
342 }
343 
344 struct rte_cryptodev_ops zuc_pmd_ops = {
345 	.dev_configure = ipsec_mb_config,
346 	.dev_start = ipsec_mb_start,
347 	.dev_stop = ipsec_mb_stop,
348 	.dev_close = ipsec_mb_close,
349 
350 	.stats_get = ipsec_mb_stats_get,
351 	.stats_reset = ipsec_mb_stats_reset,
352 
353 	.dev_infos_get = ipsec_mb_info_get,
354 
355 	.queue_pair_setup = ipsec_mb_qp_setup,
356 	.queue_pair_release = ipsec_mb_qp_release,
357 
358 	.sym_session_get_size = ipsec_mb_sym_session_get_size,
359 	.sym_session_configure = ipsec_mb_sym_session_configure,
360 	.sym_session_clear = ipsec_mb_sym_session_clear
361 };
362 
363 struct rte_cryptodev_ops *rte_zuc_pmd_ops = &zuc_pmd_ops;
364 
365 static int
zuc_probe(struct rte_vdev_device * vdev)366 zuc_probe(struct rte_vdev_device *vdev)
367 {
368 	return ipsec_mb_create(vdev, IPSEC_MB_PMD_TYPE_ZUC);
369 }
370 
371 static struct rte_vdev_driver cryptodev_zuc_pmd_drv = {
372 	.probe = zuc_probe,
373 	.remove = ipsec_mb_remove
374 
375 };
376 
377 static struct cryptodev_driver zuc_crypto_drv;
378 
379 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_ZUC_PMD, cryptodev_zuc_pmd_drv);
380 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_ZUC_PMD, cryptodev_zuc_pmd);
381 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_ZUC_PMD,
382 	"max_nb_queue_pairs=<int> socket_id=<int>");
383 RTE_PMD_REGISTER_CRYPTO_DRIVER(zuc_crypto_drv, cryptodev_zuc_pmd_drv.driver,
384 		pmd_driver_id_zuc);
385 
386 /* Constructor function to register zuc PMD */
RTE_INIT(ipsec_mb_register_zuc)387 RTE_INIT(ipsec_mb_register_zuc)
388 {
389 	struct ipsec_mb_internals *zuc_data
390 	    = &ipsec_mb_pmds[IPSEC_MB_PMD_TYPE_ZUC];
391 
392 	zuc_data->caps = zuc_capabilities;
393 	zuc_data->dequeue_burst = zuc_pmd_dequeue_burst;
394 	zuc_data->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO
395 			| RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING
396 			| RTE_CRYPTODEV_FF_NON_BYTE_ALIGNED_DATA
397 			| RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT
398 			| RTE_CRYPTODEV_FF_SYM_SESSIONLESS
399 			| RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
400 	zuc_data->internals_priv_size = 0;
401 	zuc_data->ops = &zuc_pmd_ops;
402 	zuc_data->qp_priv_size = sizeof(struct zuc_qp_data);
403 	zuc_data->session_configure = zuc_session_configure;
404 	zuc_data->session_priv_size = sizeof(struct zuc_session);
405 }
406