4 * Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of Intel Corporation nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <rte_common.h>
34 #include <rte_config.h>
35 #include <rte_hexdump.h>
36 #include <rte_cryptodev.h>
37 #include <rte_cryptodev_pmd.h>
38 #include <rte_cryptodev_vdev.h>
40 #include <rte_malloc.h>
41 #include <rte_cpuflags.h>
43 #include "rte_zuc_pmd_private.h"
45 #define ZUC_MAX_BURST 8
48 static uint8_t cryptodev_driver_id;
50 /** Get xform chain order. */
51 static enum zuc_operation
52 zuc_get_mode(const struct rte_crypto_sym_xform *xform)
55 return ZUC_OP_NOT_SUPPORTED;
58 if (xform->next->next != NULL)
59 return ZUC_OP_NOT_SUPPORTED;
61 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
62 if (xform->next == NULL)
63 return ZUC_OP_ONLY_AUTH;
64 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
65 return ZUC_OP_AUTH_CIPHER;
67 return ZUC_OP_NOT_SUPPORTED;
70 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
71 if (xform->next == NULL)
72 return ZUC_OP_ONLY_CIPHER;
73 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
74 return ZUC_OP_CIPHER_AUTH;
76 return ZUC_OP_NOT_SUPPORTED;
79 return ZUC_OP_NOT_SUPPORTED;
83 /** Parse crypto xform chain and set private session parameters. */
85 zuc_set_session_parameters(struct zuc_session *sess,
86 const struct rte_crypto_sym_xform *xform)
88 const struct rte_crypto_sym_xform *auth_xform = NULL;
89 const struct rte_crypto_sym_xform *cipher_xform = NULL;
90 enum zuc_operation mode;
92 /* Select Crypto operation - hash then cipher / cipher then hash */
93 mode = zuc_get_mode(xform);
96 case ZUC_OP_CIPHER_AUTH:
97 auth_xform = xform->next;
100 case ZUC_OP_ONLY_CIPHER:
101 cipher_xform = xform;
103 case ZUC_OP_AUTH_CIPHER:
104 cipher_xform = xform->next;
106 case ZUC_OP_ONLY_AUTH:
109 case ZUC_OP_NOT_SUPPORTED:
111 ZUC_LOG_ERR("Unsupported operation chain order parameter");
116 /* Only ZUC EEA3 supported */
117 if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_ZUC_EEA3)
120 if (cipher_xform->cipher.iv.length != ZUC_IV_KEY_LENGTH) {
121 ZUC_LOG_ERR("Wrong IV length");
124 sess->cipher_iv_offset = cipher_xform->cipher.iv.offset;
127 memcpy(sess->pKey_cipher, cipher_xform->cipher.key.data,
132 /* Only ZUC EIA3 supported */
133 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_ZUC_EIA3)
136 if (auth_xform->auth.digest_length != ZUC_DIGEST_LENGTH) {
137 ZUC_LOG_ERR("Wrong digest length");
141 sess->auth_op = auth_xform->auth.op;
143 if (auth_xform->auth.iv.length != ZUC_IV_KEY_LENGTH) {
144 ZUC_LOG_ERR("Wrong IV length");
147 sess->auth_iv_offset = auth_xform->auth.iv.offset;
150 memcpy(sess->pKey_hash, auth_xform->auth.key.data,
160 /** Get ZUC session. */
161 static struct zuc_session *
162 zuc_get_session(struct zuc_qp *qp, struct rte_crypto_op *op)
164 struct zuc_session *sess = NULL;
166 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
167 if (likely(op->sym->session != NULL))
168 sess = (struct zuc_session *)get_session_private_data(
170 cryptodev_driver_id);
173 void *_sess_private_data = NULL;
175 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
178 if (rte_mempool_get(qp->sess_mp, (void **)&_sess_private_data))
181 sess = (struct zuc_session *)_sess_private_data;
183 if (unlikely(zuc_set_session_parameters(sess,
184 op->sym->xform) != 0)) {
185 rte_mempool_put(qp->sess_mp, _sess);
186 rte_mempool_put(qp->sess_mp, _sess_private_data);
189 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
190 set_session_private_data(op->sym->session, cryptodev_driver_id,
194 if (unlikely(sess == NULL))
195 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
201 /** Encrypt/decrypt mbufs with same cipher key. */
203 process_zuc_cipher_op(struct rte_crypto_op **ops,
204 struct zuc_session *session,
208 uint8_t processed_ops = 0;
209 uint8_t *src[ZUC_MAX_BURST], *dst[ZUC_MAX_BURST];
210 uint8_t *iv[ZUC_MAX_BURST];
211 uint32_t num_bytes[ZUC_MAX_BURST];
212 uint8_t *cipher_keys[ZUC_MAX_BURST];
214 for (i = 0; i < num_ops; i++) {
215 if (((ops[i]->sym->cipher.data.length % BYTE_LEN) != 0)
216 || ((ops[i]->sym->cipher.data.offset
218 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
219 ZUC_LOG_ERR("Data Length or offset");
223 #ifdef RTE_LIBRTE_PMD_ZUC_DEBUG
224 if (!rte_pktmbuf_is_contiguous(ops[i]->sym->m_src) ||
225 (ops[i]->sym->m_dst != NULL &&
226 !rte_pktmbuf_is_contiguous(
227 ops[i]->sym->m_dst))) {
228 ZUC_LOG_ERR("PMD supports only contiguous mbufs, "
229 "op (%p) provides noncontiguous mbuf as "
230 "source/destination buffer.\n", ops[i]);
231 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
236 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
237 (ops[i]->sym->cipher.data.offset >> 3);
238 dst[i] = ops[i]->sym->m_dst ?
239 rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
240 (ops[i]->sym->cipher.data.offset >> 3) :
241 rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
242 (ops[i]->sym->cipher.data.offset >> 3);
243 iv[i] = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
244 session->cipher_iv_offset);
245 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
247 cipher_keys[i] = session->pKey_cipher;
252 sso_zuc_eea3_n_buffer(cipher_keys, iv, src, dst,
253 num_bytes, processed_ops);
255 return processed_ops;
258 /** Generate/verify hash from mbufs with same hash key. */
260 process_zuc_hash_op(struct zuc_qp *qp, struct rte_crypto_op **ops,
261 struct zuc_session *session,
265 uint8_t processed_ops = 0;
268 uint32_t length_in_bits;
271 for (i = 0; i < num_ops; i++) {
272 /* Data must be byte aligned */
273 if ((ops[i]->sym->auth.data.offset % BYTE_LEN) != 0) {
274 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
275 ZUC_LOG_ERR("Offset");
279 length_in_bits = ops[i]->sym->auth.data.length;
281 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
282 (ops[i]->sym->auth.data.offset >> 3);
283 iv = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
284 session->auth_iv_offset);
286 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
287 dst = (uint32_t *)qp->temp_digest;
289 sso_zuc_eia3_1_buffer(session->pKey_hash,
291 length_in_bits, dst);
293 if (memcmp(dst, ops[i]->sym->auth.digest.data,
294 ZUC_DIGEST_LENGTH) != 0)
295 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
297 dst = (uint32_t *)ops[i]->sym->auth.digest.data;
299 sso_zuc_eia3_1_buffer(session->pKey_hash,
301 length_in_bits, dst);
306 return processed_ops;
309 /** Process a batch of crypto ops which shares the same session. */
311 process_ops(struct rte_crypto_op **ops, struct zuc_session *session,
312 struct zuc_qp *qp, uint8_t num_ops,
313 uint16_t *accumulated_enqueued_ops)
316 unsigned enqueued_ops, processed_ops;
318 switch (session->op) {
319 case ZUC_OP_ONLY_CIPHER:
320 processed_ops = process_zuc_cipher_op(ops,
323 case ZUC_OP_ONLY_AUTH:
324 processed_ops = process_zuc_hash_op(qp, ops, session,
327 case ZUC_OP_CIPHER_AUTH:
328 processed_ops = process_zuc_cipher_op(ops, session,
330 process_zuc_hash_op(qp, ops, session, processed_ops);
332 case ZUC_OP_AUTH_CIPHER:
333 processed_ops = process_zuc_hash_op(qp, ops, session,
335 process_zuc_cipher_op(ops, session, processed_ops);
338 /* Operation not supported. */
342 for (i = 0; i < num_ops; i++) {
344 * If there was no error/authentication failure,
345 * change status to successful.
347 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
348 ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
349 /* Free session if a session-less crypto op. */
350 if (ops[i]->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
351 memset(session, 0, sizeof(struct zuc_session));
352 memset(ops[i]->sym->session, 0,
353 rte_cryptodev_get_header_session_size());
354 rte_mempool_put(qp->sess_mp, session);
355 rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
356 ops[i]->sym->session = NULL;
360 enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
361 (void **)ops, processed_ops, NULL);
362 qp->qp_stats.enqueued_count += enqueued_ops;
363 *accumulated_enqueued_ops += enqueued_ops;
369 zuc_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
372 struct rte_crypto_op *c_ops[ZUC_MAX_BURST];
373 struct rte_crypto_op *curr_c_op;
375 struct zuc_session *prev_sess = NULL, *curr_sess = NULL;
376 struct zuc_qp *qp = queue_pair;
378 uint8_t burst_size = 0;
379 uint16_t enqueued_ops = 0;
380 uint8_t processed_ops;
382 for (i = 0; i < nb_ops; i++) {
385 /* Set status as enqueued (not processed yet) by default. */
386 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
388 curr_sess = zuc_get_session(qp, curr_c_op);
389 if (unlikely(curr_sess == NULL ||
390 curr_sess->op == ZUC_OP_NOT_SUPPORTED)) {
392 RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
396 /* Batch ops that share the same session. */
397 if (prev_sess == NULL) {
398 prev_sess = curr_sess;
399 c_ops[burst_size++] = curr_c_op;
400 } else if (curr_sess == prev_sess) {
401 c_ops[burst_size++] = curr_c_op;
403 * When there are enough ops to process in a batch,
404 * process them, and start a new batch.
406 if (burst_size == ZUC_MAX_BURST) {
407 processed_ops = process_ops(c_ops, prev_sess,
408 qp, burst_size, &enqueued_ops);
409 if (processed_ops < burst_size) {
419 * Different session, process the ops
420 * of the previous session.
422 processed_ops = process_ops(c_ops, prev_sess,
423 qp, burst_size, &enqueued_ops);
424 if (processed_ops < burst_size) {
430 prev_sess = curr_sess;
432 c_ops[burst_size++] = curr_c_op;
436 if (burst_size != 0) {
437 /* Process the crypto ops of the last session. */
438 processed_ops = process_ops(c_ops, prev_sess,
439 qp, burst_size, &enqueued_ops);
442 qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
447 zuc_pmd_dequeue_burst(void *queue_pair,
448 struct rte_crypto_op **c_ops, uint16_t nb_ops)
450 struct zuc_qp *qp = queue_pair;
452 unsigned nb_dequeued;
454 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
455 (void **)c_ops, nb_ops, NULL);
456 qp->qp_stats.dequeued_count += nb_dequeued;
461 static int cryptodev_zuc_remove(struct rte_vdev_device *vdev);
464 cryptodev_zuc_create(const char *name,
465 struct rte_vdev_device *vdev,
466 struct rte_crypto_vdev_init_params *init_params)
468 struct rte_cryptodev *dev;
469 struct zuc_private *internals;
470 uint64_t cpu_flags = RTE_CRYPTODEV_FF_CPU_SSE;
472 if (init_params->name[0] == '\0')
473 snprintf(init_params->name, sizeof(init_params->name),
476 dev = rte_cryptodev_vdev_pmd_init(init_params->name,
477 sizeof(struct zuc_private), init_params->socket_id,
480 ZUC_LOG_ERR("failed to create cryptodev vdev");
484 dev->driver_id = cryptodev_driver_id;
485 dev->dev_ops = rte_zuc_pmd_ops;
487 /* Register RX/TX burst functions for data path. */
488 dev->dequeue_burst = zuc_pmd_dequeue_burst;
489 dev->enqueue_burst = zuc_pmd_enqueue_burst;
491 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
492 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
495 internals = dev->data->dev_private;
497 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
498 internals->max_nb_sessions = init_params->max_nb_sessions;
502 ZUC_LOG_ERR("driver %s: cryptodev_zuc_create failed",
505 cryptodev_zuc_remove(vdev);
510 cryptodev_zuc_probe(struct rte_vdev_device *vdev)
512 struct rte_crypto_vdev_init_params init_params = {
513 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
514 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
519 const char *input_args;
521 name = rte_vdev_device_name(vdev);
524 input_args = rte_vdev_device_args(vdev);
526 rte_cryptodev_vdev_parse_init_params(&init_params, input_args);
528 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
529 init_params.socket_id);
530 if (init_params.name[0] != '\0')
531 RTE_LOG(INFO, PMD, " User defined name = %s\n",
533 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
534 init_params.max_nb_queue_pairs);
535 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
536 init_params.max_nb_sessions);
538 return cryptodev_zuc_create(name, vdev, &init_params);
542 cryptodev_zuc_remove(struct rte_vdev_device *vdev)
546 name = rte_vdev_device_name(vdev);
550 RTE_LOG(INFO, PMD, "Closing ZUC crypto device %s"
551 " on numa socket %u\n",
552 name, rte_socket_id());
557 static struct rte_vdev_driver cryptodev_zuc_pmd_drv = {
558 .probe = cryptodev_zuc_probe,
559 .remove = cryptodev_zuc_remove
562 static struct cryptodev_driver zuc_crypto_drv;
564 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_ZUC_PMD, cryptodev_zuc_pmd_drv);
565 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_ZUC_PMD,
566 "max_nb_queue_pairs=<int> "
567 "max_nb_sessions=<int> "
569 RTE_PMD_REGISTER_CRYPTO_DRIVER(zuc_crypto_drv, cryptodev_zuc_pmd_drv,
570 cryptodev_driver_id);