4 * Copyright(c) 2016 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>
39 #include <rte_malloc.h>
40 #include <rte_cpuflags.h>
42 #include "rte_zuc_pmd_private.h"
44 #define ZUC_DIGEST_LENGTH 4
45 #define ZUC_MAX_BURST 8
48 /** Get xform chain order. */
49 static enum zuc_operation
50 zuc_get_mode(const struct rte_crypto_sym_xform *xform)
53 return ZUC_OP_NOT_SUPPORTED;
56 if (xform->next->next != NULL)
57 return ZUC_OP_NOT_SUPPORTED;
59 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
60 if (xform->next == NULL)
61 return ZUC_OP_ONLY_AUTH;
62 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
63 return ZUC_OP_AUTH_CIPHER;
65 return ZUC_OP_NOT_SUPPORTED;
68 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
69 if (xform->next == NULL)
70 return ZUC_OP_ONLY_CIPHER;
71 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
72 return ZUC_OP_CIPHER_AUTH;
74 return ZUC_OP_NOT_SUPPORTED;
77 return ZUC_OP_NOT_SUPPORTED;
81 /** Parse crypto xform chain and set private session parameters. */
83 zuc_set_session_parameters(struct zuc_session *sess,
84 const struct rte_crypto_sym_xform *xform)
86 const struct rte_crypto_sym_xform *auth_xform = NULL;
87 const struct rte_crypto_sym_xform *cipher_xform = NULL;
88 enum zuc_operation mode;
90 /* Select Crypto operation - hash then cipher / cipher then hash */
91 mode = zuc_get_mode(xform);
94 case ZUC_OP_CIPHER_AUTH:
95 auth_xform = xform->next;
98 case ZUC_OP_ONLY_CIPHER:
101 case ZUC_OP_AUTH_CIPHER:
102 cipher_xform = xform->next;
104 case ZUC_OP_ONLY_AUTH:
107 case ZUC_OP_NOT_SUPPORTED:
109 ZUC_LOG_ERR("Unsupported operation chain order parameter");
114 /* Only ZUC EEA3 supported */
115 if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_ZUC_EEA3)
118 memcpy(sess->pKey_cipher, cipher_xform->cipher.key.data,
123 /* Only ZUC EIA3 supported */
124 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_ZUC_EIA3)
126 sess->auth_op = auth_xform->auth.op;
128 memcpy(sess->pKey_hash, auth_xform->auth.key.data,
138 /** Get ZUC session. */
139 static struct zuc_session *
140 zuc_get_session(struct zuc_qp *qp, struct rte_crypto_op *op)
142 struct zuc_session *sess;
144 if (op->sym->sess_type == RTE_CRYPTO_SYM_OP_WITH_SESSION) {
145 if (unlikely(op->sym->session->dev_type !=
146 RTE_CRYPTODEV_ZUC_PMD))
149 sess = (struct zuc_session *)op->sym->session->_private;
151 struct rte_cryptodev_session *c_sess = NULL;
153 if (rte_mempool_get(qp->sess_mp, (void **)&c_sess))
156 sess = (struct zuc_session *)c_sess->_private;
158 if (unlikely(zuc_set_session_parameters(sess,
159 op->sym->xform) != 0))
166 /** Encrypt/decrypt mbufs with same cipher key. */
168 process_zuc_cipher_op(struct rte_crypto_op **ops,
169 struct zuc_session *session,
173 uint8_t processed_ops = 0;
174 uint8_t *src[ZUC_MAX_BURST], *dst[ZUC_MAX_BURST];
175 uint8_t *IV[ZUC_MAX_BURST];
176 uint32_t num_bytes[ZUC_MAX_BURST];
177 uint8_t *cipher_keys[ZUC_MAX_BURST];
179 for (i = 0; i < num_ops; i++) {
181 if (unlikely(ops[i]->sym->cipher.iv.length != ZUC_IV_KEY_LENGTH)) {
182 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
187 if (((ops[i]->sym->cipher.data.length % BYTE_LEN) != 0)
188 || ((ops[i]->sym->cipher.data.offset
190 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
191 ZUC_LOG_ERR("Data Length or offset");
195 #ifdef RTE_LIBRTE_PMD_ZUC_DEBUG
196 if (!rte_pktmbuf_is_contiguous(ops[i]->sym->m_src) ||
197 (ops[i]->sym->m_dst != NULL &&
198 !rte_pktmbuf_is_contiguous(
199 ops[i]->sym->m_dst))) {
200 ZUC_LOG_ERR("PMD supports only contiguous mbufs, "
201 "op (%p) provides noncontiguous mbuf as "
202 "source/destination buffer.\n", ops[i]);
203 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
208 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
209 (ops[i]->sym->cipher.data.offset >> 3);
210 dst[i] = ops[i]->sym->m_dst ?
211 rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
212 (ops[i]->sym->cipher.data.offset >> 3) :
213 rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
214 (ops[i]->sym->cipher.data.offset >> 3);
215 IV[i] = ops[i]->sym->cipher.iv.data;
216 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
218 cipher_keys[i] = session->pKey_cipher;
223 sso_zuc_eea3_n_buffer(cipher_keys, IV, src, dst,
224 num_bytes, processed_ops);
226 return processed_ops;
229 /** Generate/verify hash from mbufs with same hash key. */
231 process_zuc_hash_op(struct rte_crypto_op **ops,
232 struct zuc_session *session,
236 uint8_t processed_ops = 0;
239 uint32_t length_in_bits;
241 for (i = 0; i < num_ops; i++) {
242 if (unlikely(ops[i]->sym->auth.aad.length != ZUC_IV_KEY_LENGTH)) {
243 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
248 if (unlikely(ops[i]->sym->auth.digest.length != ZUC_DIGEST_LENGTH)) {
249 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
250 ZUC_LOG_ERR("digest");
254 /* Data must be byte aligned */
255 if ((ops[i]->sym->auth.data.offset % BYTE_LEN) != 0) {
256 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
257 ZUC_LOG_ERR("Offset");
261 length_in_bits = ops[i]->sym->auth.data.length;
263 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
264 (ops[i]->sym->auth.data.offset >> 3);
266 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
267 dst = (uint32_t *)rte_pktmbuf_append(ops[i]->sym->m_src,
268 ops[i]->sym->auth.digest.length);
270 sso_zuc_eia3_1_buffer(session->pKey_hash,
271 ops[i]->sym->auth.aad.data, src,
272 length_in_bits, dst);
274 if (memcmp(dst, ops[i]->sym->auth.digest.data,
275 ops[i]->sym->auth.digest.length) != 0)
276 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
278 /* Trim area used for digest from mbuf. */
279 rte_pktmbuf_trim(ops[i]->sym->m_src,
280 ops[i]->sym->auth.digest.length);
282 dst = (uint32_t *)ops[i]->sym->auth.digest.data;
284 sso_zuc_eia3_1_buffer(session->pKey_hash,
285 ops[i]->sym->auth.aad.data, src,
286 length_in_bits, dst);
291 return processed_ops;
294 /** Process a batch of crypto ops which shares the same session. */
296 process_ops(struct rte_crypto_op **ops, struct zuc_session *session,
297 struct zuc_qp *qp, uint8_t num_ops,
298 uint16_t *accumulated_enqueued_ops)
301 unsigned enqueued_ops, processed_ops;
303 switch (session->op) {
304 case ZUC_OP_ONLY_CIPHER:
305 processed_ops = process_zuc_cipher_op(ops,
308 case ZUC_OP_ONLY_AUTH:
309 processed_ops = process_zuc_hash_op(ops, session,
312 case ZUC_OP_CIPHER_AUTH:
313 processed_ops = process_zuc_cipher_op(ops, session,
315 process_zuc_hash_op(ops, session, processed_ops);
317 case ZUC_OP_AUTH_CIPHER:
318 processed_ops = process_zuc_hash_op(ops, session,
320 process_zuc_cipher_op(ops, session, processed_ops);
323 /* Operation not supported. */
327 for (i = 0; i < num_ops; i++) {
329 * If there was no error/authentication failure,
330 * change status to successful.
332 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
333 ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
334 /* Free session if a session-less crypto op. */
335 if (ops[i]->sym->sess_type == RTE_CRYPTO_SYM_OP_SESSIONLESS) {
336 rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
337 ops[i]->sym->session = NULL;
341 enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
342 (void **)ops, processed_ops, NULL);
343 qp->qp_stats.enqueued_count += enqueued_ops;
344 *accumulated_enqueued_ops += enqueued_ops;
350 zuc_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
353 struct rte_crypto_op *c_ops[ZUC_MAX_BURST];
354 struct rte_crypto_op *curr_c_op;
356 struct zuc_session *prev_sess = NULL, *curr_sess = NULL;
357 struct zuc_qp *qp = queue_pair;
359 uint8_t burst_size = 0;
360 uint16_t enqueued_ops = 0;
361 uint8_t processed_ops;
363 for (i = 0; i < nb_ops; i++) {
366 /* Set status as enqueued (not processed yet) by default. */
367 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
369 curr_sess = zuc_get_session(qp, curr_c_op);
370 if (unlikely(curr_sess == NULL ||
371 curr_sess->op == ZUC_OP_NOT_SUPPORTED)) {
373 RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
377 /* Batch ops that share the same session. */
378 if (prev_sess == NULL) {
379 prev_sess = curr_sess;
380 c_ops[burst_size++] = curr_c_op;
381 } else if (curr_sess == prev_sess) {
382 c_ops[burst_size++] = curr_c_op;
384 * When there are enough ops to process in a batch,
385 * process them, and start a new batch.
387 if (burst_size == ZUC_MAX_BURST) {
388 processed_ops = process_ops(c_ops, prev_sess,
389 qp, burst_size, &enqueued_ops);
390 if (processed_ops < burst_size) {
400 * Different session, process the ops
401 * of the previous session.
403 processed_ops = process_ops(c_ops, prev_sess,
404 qp, burst_size, &enqueued_ops);
405 if (processed_ops < burst_size) {
411 prev_sess = curr_sess;
413 c_ops[burst_size++] = curr_c_op;
417 if (burst_size != 0) {
418 /* Process the crypto ops of the last session. */
419 processed_ops = process_ops(c_ops, prev_sess,
420 qp, burst_size, &enqueued_ops);
423 qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
428 zuc_pmd_dequeue_burst(void *queue_pair,
429 struct rte_crypto_op **c_ops, uint16_t nb_ops)
431 struct zuc_qp *qp = queue_pair;
433 unsigned nb_dequeued;
435 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
436 (void **)c_ops, nb_ops, NULL);
437 qp->qp_stats.dequeued_count += nb_dequeued;
442 static int cryptodev_zuc_remove(struct rte_vdev_device *vdev);
445 cryptodev_zuc_create(const char *name,
446 struct rte_vdev_device *vdev,
447 struct rte_crypto_vdev_init_params *init_params)
449 struct rte_cryptodev *dev;
450 struct zuc_private *internals;
451 uint64_t cpu_flags = 0;
453 if (init_params->name[0] == '\0')
454 snprintf(init_params->name, sizeof(init_params->name),
457 /* Check CPU for supported vector instruction set */
458 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_1))
459 cpu_flags |= RTE_CRYPTODEV_FF_CPU_SSE;
461 ZUC_LOG_ERR("Vector instructions are not supported by CPU");
465 dev = rte_cryptodev_pmd_virtual_dev_init(init_params->name,
466 sizeof(struct zuc_private), init_params->socket_id);
468 ZUC_LOG_ERR("failed to create cryptodev vdev");
472 dev->dev_type = RTE_CRYPTODEV_ZUC_PMD;
473 dev->dev_ops = rte_zuc_pmd_ops;
475 /* Register RX/TX burst functions for data path. */
476 dev->dequeue_burst = zuc_pmd_dequeue_burst;
477 dev->enqueue_burst = zuc_pmd_enqueue_burst;
479 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
480 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
483 internals = dev->data->dev_private;
485 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
486 internals->max_nb_sessions = init_params->max_nb_sessions;
490 ZUC_LOG_ERR("driver %s: cryptodev_zuc_create failed",
493 cryptodev_zuc_remove(vdev);
498 cryptodev_zuc_probe(struct rte_vdev_device *vdev)
500 struct rte_crypto_vdev_init_params init_params = {
501 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
502 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
507 const char *input_args;
509 name = rte_vdev_device_name(vdev);
512 input_args = rte_vdev_device_args(vdev);
514 rte_cryptodev_parse_vdev_init_params(&init_params, input_args);
516 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
517 init_params.socket_id);
518 if (init_params.name[0] != '\0')
519 RTE_LOG(INFO, PMD, " User defined name = %s\n",
521 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
522 init_params.max_nb_queue_pairs);
523 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
524 init_params.max_nb_sessions);
526 return cryptodev_zuc_create(name, vdev, &init_params);
530 cryptodev_zuc_remove(struct rte_vdev_device *vdev)
534 name = rte_vdev_device_name(vdev);
538 RTE_LOG(INFO, PMD, "Closing ZUC crypto device %s"
539 " on numa socket %u\n",
540 name, rte_socket_id());
545 static struct rte_vdev_driver cryptodev_zuc_pmd_drv = {
546 .probe = cryptodev_zuc_probe,
547 .remove = cryptodev_zuc_remove
550 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_ZUC_PMD, cryptodev_zuc_pmd_drv);
551 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_ZUC_PMD,
552 "max_nb_queue_pairs=<int> "
553 "max_nb_sessions=<int> "