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_DIGEST_LENGTH 4
46 #define ZUC_MAX_BURST 8
49 /** Get xform chain order. */
50 static enum zuc_operation
51 zuc_get_mode(const struct rte_crypto_sym_xform *xform)
54 return ZUC_OP_NOT_SUPPORTED;
57 if (xform->next->next != NULL)
58 return ZUC_OP_NOT_SUPPORTED;
60 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
61 if (xform->next == NULL)
62 return ZUC_OP_ONLY_AUTH;
63 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
64 return ZUC_OP_AUTH_CIPHER;
66 return ZUC_OP_NOT_SUPPORTED;
69 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
70 if (xform->next == NULL)
71 return ZUC_OP_ONLY_CIPHER;
72 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
73 return ZUC_OP_CIPHER_AUTH;
75 return ZUC_OP_NOT_SUPPORTED;
78 return ZUC_OP_NOT_SUPPORTED;
82 /** Parse crypto xform chain and set private session parameters. */
84 zuc_set_session_parameters(struct zuc_session *sess,
85 const struct rte_crypto_sym_xform *xform)
87 const struct rte_crypto_sym_xform *auth_xform = NULL;
88 const struct rte_crypto_sym_xform *cipher_xform = NULL;
89 enum zuc_operation mode;
91 /* Select Crypto operation - hash then cipher / cipher then hash */
92 mode = zuc_get_mode(xform);
95 case ZUC_OP_CIPHER_AUTH:
96 auth_xform = xform->next;
99 case ZUC_OP_ONLY_CIPHER:
100 cipher_xform = xform;
102 case ZUC_OP_AUTH_CIPHER:
103 cipher_xform = xform->next;
105 case ZUC_OP_ONLY_AUTH:
108 case ZUC_OP_NOT_SUPPORTED:
110 ZUC_LOG_ERR("Unsupported operation chain order parameter");
115 /* Only ZUC EEA3 supported */
116 if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_ZUC_EEA3)
119 if (cipher_xform->cipher.iv.length != ZUC_IV_KEY_LENGTH) {
120 ZUC_LOG_ERR("Wrong IV length");
123 sess->iv_offset = cipher_xform->cipher.iv.offset;
126 memcpy(sess->pKey_cipher, cipher_xform->cipher.key.data,
131 /* Only ZUC EIA3 supported */
132 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_ZUC_EIA3)
134 sess->auth_op = auth_xform->auth.op;
136 memcpy(sess->pKey_hash, auth_xform->auth.key.data,
146 /** Get ZUC session. */
147 static struct zuc_session *
148 zuc_get_session(struct zuc_qp *qp, struct rte_crypto_op *op)
150 struct zuc_session *sess;
152 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
153 if (unlikely(op->sym->session->dev_type !=
154 RTE_CRYPTODEV_ZUC_PMD))
157 sess = (struct zuc_session *)op->sym->session->_private;
159 struct rte_cryptodev_session *c_sess = NULL;
161 if (rte_mempool_get(qp->sess_mp, (void **)&c_sess))
164 sess = (struct zuc_session *)c_sess->_private;
166 if (unlikely(zuc_set_session_parameters(sess,
167 op->sym->xform) != 0))
174 /** Encrypt/decrypt mbufs with same cipher key. */
176 process_zuc_cipher_op(struct rte_crypto_op **ops,
177 struct zuc_session *session,
181 uint8_t processed_ops = 0;
182 uint8_t *src[ZUC_MAX_BURST], *dst[ZUC_MAX_BURST];
183 uint8_t *iv[ZUC_MAX_BURST];
184 uint32_t num_bytes[ZUC_MAX_BURST];
185 uint8_t *cipher_keys[ZUC_MAX_BURST];
187 for (i = 0; i < num_ops; i++) {
188 if (((ops[i]->sym->cipher.data.length % BYTE_LEN) != 0)
189 || ((ops[i]->sym->cipher.data.offset
191 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
192 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 *,
218 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
220 cipher_keys[i] = session->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 with same hash key. */
233 process_zuc_hash_op(struct rte_crypto_op **ops,
234 struct zuc_session *session,
238 uint8_t processed_ops = 0;
241 uint32_t length_in_bits;
243 for (i = 0; i < num_ops; i++) {
244 if (unlikely(ops[i]->sym->auth.aad.length != ZUC_IV_KEY_LENGTH)) {
245 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
250 if (unlikely(ops[i]->sym->auth.digest.length != ZUC_DIGEST_LENGTH)) {
251 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
252 ZUC_LOG_ERR("digest");
256 /* Data must be byte aligned */
257 if ((ops[i]->sym->auth.data.offset % BYTE_LEN) != 0) {
258 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
259 ZUC_LOG_ERR("Offset");
263 length_in_bits = ops[i]->sym->auth.data.length;
265 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
266 (ops[i]->sym->auth.data.offset >> 3);
268 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
269 dst = (uint32_t *)rte_pktmbuf_append(ops[i]->sym->m_src,
270 ops[i]->sym->auth.digest.length);
272 sso_zuc_eia3_1_buffer(session->pKey_hash,
273 ops[i]->sym->auth.aad.data, src,
274 length_in_bits, dst);
276 if (memcmp(dst, ops[i]->sym->auth.digest.data,
277 ops[i]->sym->auth.digest.length) != 0)
278 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
280 /* Trim area used for digest from mbuf. */
281 rte_pktmbuf_trim(ops[i]->sym->m_src,
282 ops[i]->sym->auth.digest.length);
284 dst = (uint32_t *)ops[i]->sym->auth.digest.data;
286 sso_zuc_eia3_1_buffer(session->pKey_hash,
287 ops[i]->sym->auth.aad.data, src,
288 length_in_bits, dst);
293 return processed_ops;
296 /** Process a batch of crypto ops which shares the same session. */
298 process_ops(struct rte_crypto_op **ops, struct zuc_session *session,
299 struct zuc_qp *qp, uint8_t num_ops,
300 uint16_t *accumulated_enqueued_ops)
303 unsigned enqueued_ops, processed_ops;
305 switch (session->op) {
306 case ZUC_OP_ONLY_CIPHER:
307 processed_ops = process_zuc_cipher_op(ops,
310 case ZUC_OP_ONLY_AUTH:
311 processed_ops = process_zuc_hash_op(ops, session,
314 case ZUC_OP_CIPHER_AUTH:
315 processed_ops = process_zuc_cipher_op(ops, session,
317 process_zuc_hash_op(ops, session, processed_ops);
319 case ZUC_OP_AUTH_CIPHER:
320 processed_ops = process_zuc_hash_op(ops, session,
322 process_zuc_cipher_op(ops, session, processed_ops);
325 /* Operation not supported. */
329 for (i = 0; i < num_ops; i++) {
331 * If there was no error/authentication failure,
332 * change status to successful.
334 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
335 ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
336 /* Free session if a session-less crypto op. */
337 if (ops[i]->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
338 rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
339 ops[i]->sym->session = NULL;
343 enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
344 (void **)ops, processed_ops, NULL);
345 qp->qp_stats.enqueued_count += enqueued_ops;
346 *accumulated_enqueued_ops += enqueued_ops;
352 zuc_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
355 struct rte_crypto_op *c_ops[ZUC_MAX_BURST];
356 struct rte_crypto_op *curr_c_op;
358 struct zuc_session *prev_sess = NULL, *curr_sess = NULL;
359 struct zuc_qp *qp = queue_pair;
361 uint8_t burst_size = 0;
362 uint16_t enqueued_ops = 0;
363 uint8_t processed_ops;
365 for (i = 0; i < nb_ops; i++) {
368 /* Set status as enqueued (not processed yet) by default. */
369 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
371 curr_sess = zuc_get_session(qp, curr_c_op);
372 if (unlikely(curr_sess == NULL ||
373 curr_sess->op == ZUC_OP_NOT_SUPPORTED)) {
375 RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
379 /* Batch ops that share the same session. */
380 if (prev_sess == NULL) {
381 prev_sess = curr_sess;
382 c_ops[burst_size++] = curr_c_op;
383 } else if (curr_sess == prev_sess) {
384 c_ops[burst_size++] = curr_c_op;
386 * When there are enough ops to process in a batch,
387 * process them, and start a new batch.
389 if (burst_size == ZUC_MAX_BURST) {
390 processed_ops = process_ops(c_ops, prev_sess,
391 qp, burst_size, &enqueued_ops);
392 if (processed_ops < burst_size) {
402 * Different session, process the ops
403 * of the previous session.
405 processed_ops = process_ops(c_ops, prev_sess,
406 qp, burst_size, &enqueued_ops);
407 if (processed_ops < burst_size) {
413 prev_sess = curr_sess;
415 c_ops[burst_size++] = curr_c_op;
419 if (burst_size != 0) {
420 /* Process the crypto ops of the last session. */
421 processed_ops = process_ops(c_ops, prev_sess,
422 qp, burst_size, &enqueued_ops);
425 qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
430 zuc_pmd_dequeue_burst(void *queue_pair,
431 struct rte_crypto_op **c_ops, uint16_t nb_ops)
433 struct zuc_qp *qp = queue_pair;
435 unsigned nb_dequeued;
437 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
438 (void **)c_ops, nb_ops, NULL);
439 qp->qp_stats.dequeued_count += nb_dequeued;
444 static int cryptodev_zuc_remove(struct rte_vdev_device *vdev);
447 cryptodev_zuc_create(const char *name,
448 struct rte_vdev_device *vdev,
449 struct rte_crypto_vdev_init_params *init_params)
451 struct rte_cryptodev *dev;
452 struct zuc_private *internals;
453 uint64_t cpu_flags = RTE_CRYPTODEV_FF_CPU_SSE;
455 if (init_params->name[0] == '\0')
456 snprintf(init_params->name, sizeof(init_params->name),
459 dev = rte_cryptodev_vdev_pmd_init(init_params->name,
460 sizeof(struct zuc_private), init_params->socket_id,
463 ZUC_LOG_ERR("failed to create cryptodev vdev");
467 dev->dev_type = RTE_CRYPTODEV_ZUC_PMD;
468 dev->dev_ops = rte_zuc_pmd_ops;
470 /* Register RX/TX burst functions for data path. */
471 dev->dequeue_burst = zuc_pmd_dequeue_burst;
472 dev->enqueue_burst = zuc_pmd_enqueue_burst;
474 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
475 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
478 internals = dev->data->dev_private;
480 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
481 internals->max_nb_sessions = init_params->max_nb_sessions;
485 ZUC_LOG_ERR("driver %s: cryptodev_zuc_create failed",
488 cryptodev_zuc_remove(vdev);
493 cryptodev_zuc_probe(struct rte_vdev_device *vdev)
495 struct rte_crypto_vdev_init_params init_params = {
496 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
497 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
502 const char *input_args;
504 name = rte_vdev_device_name(vdev);
507 input_args = rte_vdev_device_args(vdev);
509 rte_cryptodev_vdev_parse_init_params(&init_params, input_args);
511 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
512 init_params.socket_id);
513 if (init_params.name[0] != '\0')
514 RTE_LOG(INFO, PMD, " User defined name = %s\n",
516 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
517 init_params.max_nb_queue_pairs);
518 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
519 init_params.max_nb_sessions);
521 return cryptodev_zuc_create(name, vdev, &init_params);
525 cryptodev_zuc_remove(struct rte_vdev_device *vdev)
529 name = rte_vdev_device_name(vdev);
533 RTE_LOG(INFO, PMD, "Closing ZUC crypto device %s"
534 " on numa socket %u\n",
535 name, rte_socket_id());
540 static struct rte_vdev_driver cryptodev_zuc_pmd_drv = {
541 .probe = cryptodev_zuc_probe,
542 .remove = cryptodev_zuc_remove
545 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_ZUC_PMD, cryptodev_zuc_pmd_drv);
546 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_ZUC_PMD,
547 "max_nb_queue_pairs=<int> "
548 "max_nb_sessions=<int> "