1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2017 Intel Corporation
5 #include <rte_common.h>
6 #include <rte_hexdump.h>
7 #include <rte_cryptodev.h>
8 #include <rte_cryptodev_pmd.h>
9 #include <rte_bus_vdev.h>
10 #include <rte_malloc.h>
11 #include <rte_cpuflags.h>
13 #include "rte_zuc_pmd_private.h"
15 #define ZUC_MAX_BURST 4
18 static uint8_t cryptodev_driver_id;
20 /** Get xform chain order. */
21 static enum zuc_operation
22 zuc_get_mode(const struct rte_crypto_sym_xform *xform)
25 return ZUC_OP_NOT_SUPPORTED;
28 if (xform->next->next != NULL)
29 return ZUC_OP_NOT_SUPPORTED;
31 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
32 if (xform->next == NULL)
33 return ZUC_OP_ONLY_AUTH;
34 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
35 return ZUC_OP_AUTH_CIPHER;
37 return ZUC_OP_NOT_SUPPORTED;
40 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
41 if (xform->next == NULL)
42 return ZUC_OP_ONLY_CIPHER;
43 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
44 return ZUC_OP_CIPHER_AUTH;
46 return ZUC_OP_NOT_SUPPORTED;
49 return ZUC_OP_NOT_SUPPORTED;
53 /** Parse crypto xform chain and set private session parameters. */
55 zuc_set_session_parameters(struct zuc_session *sess,
56 const struct rte_crypto_sym_xform *xform)
58 const struct rte_crypto_sym_xform *auth_xform = NULL;
59 const struct rte_crypto_sym_xform *cipher_xform = NULL;
60 enum zuc_operation mode;
62 /* Select Crypto operation - hash then cipher / cipher then hash */
63 mode = zuc_get_mode(xform);
66 case ZUC_OP_CIPHER_AUTH:
67 auth_xform = xform->next;
70 case ZUC_OP_ONLY_CIPHER:
73 case ZUC_OP_AUTH_CIPHER:
74 cipher_xform = xform->next;
76 case ZUC_OP_ONLY_AUTH:
79 case ZUC_OP_NOT_SUPPORTED:
81 ZUC_LOG_ERR("Unsupported operation chain order parameter");
86 /* Only ZUC EEA3 supported */
87 if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_ZUC_EEA3)
90 if (cipher_xform->cipher.iv.length != ZUC_IV_KEY_LENGTH) {
91 ZUC_LOG_ERR("Wrong IV length");
94 sess->cipher_iv_offset = cipher_xform->cipher.iv.offset;
97 memcpy(sess->pKey_cipher, cipher_xform->cipher.key.data,
102 /* Only ZUC EIA3 supported */
103 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_ZUC_EIA3)
106 if (auth_xform->auth.digest_length != ZUC_DIGEST_LENGTH) {
107 ZUC_LOG_ERR("Wrong digest length");
111 sess->auth_op = auth_xform->auth.op;
113 if (auth_xform->auth.iv.length != ZUC_IV_KEY_LENGTH) {
114 ZUC_LOG_ERR("Wrong IV length");
117 sess->auth_iv_offset = auth_xform->auth.iv.offset;
120 memcpy(sess->pKey_hash, auth_xform->auth.key.data,
130 /** Get ZUC session. */
131 static struct zuc_session *
132 zuc_get_session(struct zuc_qp *qp, struct rte_crypto_op *op)
134 struct zuc_session *sess = NULL;
136 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
137 if (likely(op->sym->session != NULL))
138 sess = (struct zuc_session *)get_session_private_data(
140 cryptodev_driver_id);
143 void *_sess_private_data = NULL;
145 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
148 if (rte_mempool_get(qp->sess_mp, (void **)&_sess_private_data))
151 sess = (struct zuc_session *)_sess_private_data;
153 if (unlikely(zuc_set_session_parameters(sess,
154 op->sym->xform) != 0)) {
155 rte_mempool_put(qp->sess_mp, _sess);
156 rte_mempool_put(qp->sess_mp, _sess_private_data);
159 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
160 set_session_private_data(op->sym->session, cryptodev_driver_id,
164 if (unlikely(sess == NULL))
165 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
171 /** Encrypt/decrypt mbufs. */
173 process_zuc_cipher_op(struct rte_crypto_op **ops,
174 struct zuc_session **sessions,
178 uint8_t processed_ops = 0;
179 uint8_t *src[ZUC_MAX_BURST], *dst[ZUC_MAX_BURST];
180 uint8_t *iv[ZUC_MAX_BURST];
181 uint32_t num_bytes[ZUC_MAX_BURST];
182 uint8_t *cipher_keys[ZUC_MAX_BURST];
183 struct zuc_session *sess;
185 for (i = 0; i < num_ops; i++) {
186 if (((ops[i]->sym->cipher.data.length % BYTE_LEN) != 0)
187 || ((ops[i]->sym->cipher.data.offset
189 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
190 ZUC_LOG_ERR("Data Length or offset");
196 #ifdef RTE_LIBRTE_PMD_ZUC_DEBUG
197 if (!rte_pktmbuf_is_contiguous(ops[i]->sym->m_src) ||
198 (ops[i]->sym->m_dst != NULL &&
199 !rte_pktmbuf_is_contiguous(
200 ops[i]->sym->m_dst))) {
201 ZUC_LOG_ERR("PMD supports only contiguous mbufs, "
202 "op (%p) provides noncontiguous mbuf as "
203 "source/destination buffer.\n", ops[i]);
204 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
209 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
210 (ops[i]->sym->cipher.data.offset >> 3);
211 dst[i] = ops[i]->sym->m_dst ?
212 rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
213 (ops[i]->sym->cipher.data.offset >> 3) :
214 rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
215 (ops[i]->sym->cipher.data.offset >> 3);
216 iv[i] = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
217 sess->cipher_iv_offset);
218 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
220 cipher_keys[i] = sess->pKey_cipher;
225 sso_zuc_eea3_n_buffer(cipher_keys, iv, src, dst,
226 num_bytes, processed_ops);
228 return processed_ops;
231 /** Generate/verify hash from mbufs. */
233 process_zuc_hash_op(struct zuc_qp *qp, struct rte_crypto_op **ops,
234 struct zuc_session **sessions,
238 uint8_t processed_ops = 0;
241 uint32_t length_in_bits;
243 struct zuc_session *sess;
245 for (i = 0; i < num_ops; i++) {
246 /* Data must be byte aligned */
247 if ((ops[i]->sym->auth.data.offset % BYTE_LEN) != 0) {
248 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
249 ZUC_LOG_ERR("Offset");
255 length_in_bits = ops[i]->sym->auth.data.length;
257 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
258 (ops[i]->sym->auth.data.offset >> 3);
259 iv = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
260 sess->auth_iv_offset);
262 if (sess->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
263 dst = (uint32_t *)qp->temp_digest;
265 sso_zuc_eia3_1_buffer(sess->pKey_hash,
267 length_in_bits, dst);
269 if (memcmp(dst, ops[i]->sym->auth.digest.data,
270 ZUC_DIGEST_LENGTH) != 0)
271 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
273 dst = (uint32_t *)ops[i]->sym->auth.digest.data;
275 sso_zuc_eia3_1_buffer(sess->pKey_hash,
277 length_in_bits, dst);
282 return processed_ops;
285 /** Process a batch of crypto ops which shares the same operation type. */
287 process_ops(struct rte_crypto_op **ops, enum zuc_operation op_type,
288 struct zuc_session **sessions,
289 struct zuc_qp *qp, uint8_t num_ops,
290 uint16_t *accumulated_enqueued_ops)
293 unsigned enqueued_ops, processed_ops;
296 case ZUC_OP_ONLY_CIPHER:
297 processed_ops = process_zuc_cipher_op(ops,
300 case ZUC_OP_ONLY_AUTH:
301 processed_ops = process_zuc_hash_op(qp, ops, sessions,
304 case ZUC_OP_CIPHER_AUTH:
305 processed_ops = process_zuc_cipher_op(ops, sessions,
307 process_zuc_hash_op(qp, ops, sessions, processed_ops);
309 case ZUC_OP_AUTH_CIPHER:
310 processed_ops = process_zuc_hash_op(qp, ops, sessions,
312 process_zuc_cipher_op(ops, sessions, processed_ops);
315 /* Operation not supported. */
319 for (i = 0; i < num_ops; i++) {
321 * If there was no error/authentication failure,
322 * change status to successful.
324 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
325 ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
326 /* Free session if a session-less crypto op. */
327 if (ops[i]->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
328 memset(sessions[i], 0, sizeof(struct zuc_session));
329 memset(ops[i]->sym->session, 0,
330 rte_cryptodev_get_header_session_size());
331 rte_mempool_put(qp->sess_mp, sessions[i]);
332 rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
333 ops[i]->sym->session = NULL;
337 enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
338 (void **)ops, processed_ops, NULL);
339 qp->qp_stats.enqueued_count += enqueued_ops;
340 *accumulated_enqueued_ops += enqueued_ops;
346 zuc_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
349 struct rte_crypto_op *c_ops[ZUC_MAX_BURST];
350 struct rte_crypto_op *curr_c_op;
352 struct zuc_session *curr_sess;
353 struct zuc_session *sessions[ZUC_MAX_BURST];
354 enum zuc_operation prev_zuc_op = ZUC_OP_NOT_SUPPORTED;
355 enum zuc_operation curr_zuc_op;
356 struct zuc_qp *qp = queue_pair;
358 uint8_t burst_size = 0;
359 uint16_t enqueued_ops = 0;
360 uint8_t processed_ops;
362 for (i = 0; i < nb_ops; i++) {
365 curr_sess = zuc_get_session(qp, curr_c_op);
366 if (unlikely(curr_sess == NULL)) {
368 RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
372 curr_zuc_op = curr_sess->op;
375 * Batch ops that share the same operation type
376 * (cipher only, auth only...).
378 if (burst_size == 0) {
379 prev_zuc_op = curr_zuc_op;
380 c_ops[0] = curr_c_op;
381 sessions[0] = curr_sess;
383 } else if (curr_zuc_op == prev_zuc_op) {
384 c_ops[burst_size] = curr_c_op;
385 sessions[burst_size] = curr_sess;
388 * When there are enough ops to process in a batch,
389 * process them, and start a new batch.
391 if (burst_size == ZUC_MAX_BURST) {
392 processed_ops = process_ops(c_ops, curr_zuc_op,
393 sessions, qp, burst_size,
395 if (processed_ops < burst_size) {
404 * Different operation type, process the ops
405 * of the previous type.
407 processed_ops = process_ops(c_ops, prev_zuc_op,
408 sessions, qp, burst_size,
410 if (processed_ops < burst_size) {
416 prev_zuc_op = curr_zuc_op;
418 c_ops[0] = curr_c_op;
419 sessions[0] = curr_sess;
424 if (burst_size != 0) {
425 /* Process the crypto ops of the last operation type. */
426 processed_ops = process_ops(c_ops, prev_zuc_op,
427 sessions, qp, burst_size,
431 qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
436 zuc_pmd_dequeue_burst(void *queue_pair,
437 struct rte_crypto_op **c_ops, uint16_t nb_ops)
439 struct zuc_qp *qp = queue_pair;
441 unsigned nb_dequeued;
443 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
444 (void **)c_ops, nb_ops, NULL);
445 qp->qp_stats.dequeued_count += nb_dequeued;
450 static int cryptodev_zuc_remove(struct rte_vdev_device *vdev);
453 cryptodev_zuc_create(const char *name,
454 struct rte_vdev_device *vdev,
455 struct rte_cryptodev_pmd_init_params *init_params)
457 struct rte_cryptodev *dev;
458 struct zuc_private *internals;
459 uint64_t cpu_flags = RTE_CRYPTODEV_FF_CPU_SSE;
462 dev = rte_cryptodev_pmd_create(name, &vdev->device, init_params);
464 ZUC_LOG_ERR("failed to create cryptodev vdev");
468 dev->driver_id = cryptodev_driver_id;
469 dev->dev_ops = rte_zuc_pmd_ops;
471 /* Register RX/TX burst functions for data path. */
472 dev->dequeue_burst = zuc_pmd_dequeue_burst;
473 dev->enqueue_burst = zuc_pmd_enqueue_burst;
475 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
476 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
479 internals = dev->data->dev_private;
481 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
482 internals->max_nb_sessions = init_params->max_nb_sessions;
486 ZUC_LOG_ERR("driver %s: cryptodev_zuc_create failed",
489 cryptodev_zuc_remove(vdev);
494 cryptodev_zuc_probe(struct rte_vdev_device *vdev)
496 struct rte_cryptodev_pmd_init_params init_params = {
498 sizeof(struct zuc_private),
500 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS,
501 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_SESSIONS
504 const char *input_args;
506 name = rte_vdev_device_name(vdev);
509 input_args = rte_vdev_device_args(vdev);
511 rte_cryptodev_pmd_parse_input_args(&init_params, input_args);
513 return cryptodev_zuc_create(name, vdev, &init_params);
517 cryptodev_zuc_remove(struct rte_vdev_device *vdev)
520 struct rte_cryptodev *cryptodev;
523 name = rte_vdev_device_name(vdev);
527 cryptodev = rte_cryptodev_pmd_get_named_dev(name);
528 if (cryptodev == NULL)
531 return rte_cryptodev_pmd_destroy(cryptodev);
534 static struct rte_vdev_driver cryptodev_zuc_pmd_drv = {
535 .probe = cryptodev_zuc_probe,
536 .remove = cryptodev_zuc_remove
539 static struct cryptodev_driver zuc_crypto_drv;
541 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_ZUC_PMD, cryptodev_zuc_pmd_drv);
542 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_ZUC_PMD,
543 "max_nb_queue_pairs=<int> "
544 "max_nb_sessions=<int> "
546 RTE_PMD_REGISTER_CRYPTO_DRIVER(zuc_crypto_drv, cryptodev_zuc_pmd_drv.driver,
547 cryptodev_driver_id);