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>
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 memcpy(sess->pKey_cipher, cipher_xform->cipher.key.data,
124 /* Only ZUC EIA3 supported */
125 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_ZUC_EIA3)
127 sess->auth_op = auth_xform->auth.op;
129 memcpy(sess->pKey_hash, auth_xform->auth.key.data,
139 /** Get ZUC session. */
140 static struct zuc_session *
141 zuc_get_session(struct zuc_qp *qp, struct rte_crypto_op *op)
143 struct zuc_session *sess;
145 if (op->sym->sess_type == RTE_CRYPTO_SYM_OP_WITH_SESSION) {
146 if (unlikely(op->sym->session->dev_type !=
147 RTE_CRYPTODEV_ZUC_PMD))
150 sess = (struct zuc_session *)op->sym->session->_private;
152 struct rte_cryptodev_session *c_sess = NULL;
154 if (rte_mempool_get(qp->sess_mp, (void **)&c_sess))
157 sess = (struct zuc_session *)c_sess->_private;
159 if (unlikely(zuc_set_session_parameters(sess,
160 op->sym->xform) != 0))
167 /** Encrypt/decrypt mbufs with same cipher key. */
169 process_zuc_cipher_op(struct rte_crypto_op **ops,
170 struct zuc_session *session,
174 uint8_t processed_ops = 0;
175 uint8_t *src[ZUC_MAX_BURST], *dst[ZUC_MAX_BURST];
176 uint8_t *IV[ZUC_MAX_BURST];
177 uint32_t num_bytes[ZUC_MAX_BURST];
178 uint8_t *cipher_keys[ZUC_MAX_BURST];
180 for (i = 0; i < num_ops; i++) {
182 if (unlikely(ops[i]->sym->cipher.iv.length != ZUC_IV_KEY_LENGTH)) {
183 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
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] = ops[i]->sym->cipher.iv.data;
217 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
219 cipher_keys[i] = session->pKey_cipher;
224 sso_zuc_eea3_n_buffer(cipher_keys, IV, src, dst,
225 num_bytes, processed_ops);
227 return processed_ops;
230 /** Generate/verify hash from mbufs with same hash key. */
232 process_zuc_hash_op(struct rte_crypto_op **ops,
233 struct zuc_session *session,
237 uint8_t processed_ops = 0;
240 uint32_t length_in_bits;
242 for (i = 0; i < num_ops; i++) {
243 if (unlikely(ops[i]->sym->auth.aad.length != ZUC_IV_KEY_LENGTH)) {
244 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
249 if (unlikely(ops[i]->sym->auth.digest.length != ZUC_DIGEST_LENGTH)) {
250 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
251 ZUC_LOG_ERR("digest");
255 /* Data must be byte aligned */
256 if ((ops[i]->sym->auth.data.offset % BYTE_LEN) != 0) {
257 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
258 ZUC_LOG_ERR("Offset");
262 length_in_bits = ops[i]->sym->auth.data.length;
264 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
265 (ops[i]->sym->auth.data.offset >> 3);
267 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
268 dst = (uint32_t *)rte_pktmbuf_append(ops[i]->sym->m_src,
269 ops[i]->sym->auth.digest.length);
271 sso_zuc_eia3_1_buffer(session->pKey_hash,
272 ops[i]->sym->auth.aad.data, src,
273 length_in_bits, dst);
275 if (memcmp(dst, ops[i]->sym->auth.digest.data,
276 ops[i]->sym->auth.digest.length) != 0)
277 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
279 /* Trim area used for digest from mbuf. */
280 rte_pktmbuf_trim(ops[i]->sym->m_src,
281 ops[i]->sym->auth.digest.length);
283 dst = (uint32_t *)ops[i]->sym->auth.digest.data;
285 sso_zuc_eia3_1_buffer(session->pKey_hash,
286 ops[i]->sym->auth.aad.data, src,
287 length_in_bits, dst);
292 return processed_ops;
295 /** Process a batch of crypto ops which shares the same session. */
297 process_ops(struct rte_crypto_op **ops, struct zuc_session *session,
298 struct zuc_qp *qp, uint8_t num_ops,
299 uint16_t *accumulated_enqueued_ops)
302 unsigned enqueued_ops, processed_ops;
304 switch (session->op) {
305 case ZUC_OP_ONLY_CIPHER:
306 processed_ops = process_zuc_cipher_op(ops,
309 case ZUC_OP_ONLY_AUTH:
310 processed_ops = process_zuc_hash_op(ops, session,
313 case ZUC_OP_CIPHER_AUTH:
314 processed_ops = process_zuc_cipher_op(ops, session,
316 process_zuc_hash_op(ops, session, processed_ops);
318 case ZUC_OP_AUTH_CIPHER:
319 processed_ops = process_zuc_hash_op(ops, session,
321 process_zuc_cipher_op(ops, session, processed_ops);
324 /* Operation not supported. */
328 for (i = 0; i < num_ops; i++) {
330 * If there was no error/authentication failure,
331 * change status to successful.
333 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
334 ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
335 /* Free session if a session-less crypto op. */
336 if (ops[i]->sym->sess_type == RTE_CRYPTO_SYM_OP_SESSIONLESS) {
337 rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
338 ops[i]->sym->session = NULL;
342 enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
343 (void **)ops, processed_ops, NULL);
344 qp->qp_stats.enqueued_count += enqueued_ops;
345 *accumulated_enqueued_ops += enqueued_ops;
351 zuc_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
354 struct rte_crypto_op *c_ops[ZUC_MAX_BURST];
355 struct rte_crypto_op *curr_c_op;
357 struct zuc_session *prev_sess = NULL, *curr_sess = NULL;
358 struct zuc_qp *qp = queue_pair;
360 uint8_t burst_size = 0;
361 uint16_t enqueued_ops = 0;
362 uint8_t processed_ops;
364 for (i = 0; i < nb_ops; i++) {
367 /* Set status as enqueued (not processed yet) by default. */
368 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
370 curr_sess = zuc_get_session(qp, curr_c_op);
371 if (unlikely(curr_sess == NULL ||
372 curr_sess->op == ZUC_OP_NOT_SUPPORTED)) {
374 RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
378 /* Batch ops that share the same session. */
379 if (prev_sess == NULL) {
380 prev_sess = curr_sess;
381 c_ops[burst_size++] = curr_c_op;
382 } else if (curr_sess == prev_sess) {
383 c_ops[burst_size++] = curr_c_op;
385 * When there are enough ops to process in a batch,
386 * process them, and start a new batch.
388 if (burst_size == ZUC_MAX_BURST) {
389 processed_ops = process_ops(c_ops, prev_sess,
390 qp, burst_size, &enqueued_ops);
391 if (processed_ops < burst_size) {
401 * Different session, process the ops
402 * of the previous session.
404 processed_ops = process_ops(c_ops, prev_sess,
405 qp, burst_size, &enqueued_ops);
406 if (processed_ops < burst_size) {
412 prev_sess = curr_sess;
414 c_ops[burst_size++] = curr_c_op;
418 if (burst_size != 0) {
419 /* Process the crypto ops of the last session. */
420 processed_ops = process_ops(c_ops, prev_sess,
421 qp, burst_size, &enqueued_ops);
424 qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
429 zuc_pmd_dequeue_burst(void *queue_pair,
430 struct rte_crypto_op **c_ops, uint16_t nb_ops)
432 struct zuc_qp *qp = queue_pair;
434 unsigned nb_dequeued;
436 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
437 (void **)c_ops, nb_ops, NULL);
438 qp->qp_stats.dequeued_count += nb_dequeued;
443 static int cryptodev_zuc_remove(struct rte_vdev_device *vdev);
446 cryptodev_zuc_create(const char *name,
447 struct rte_vdev_device *vdev,
448 struct rte_crypto_vdev_init_params *init_params)
450 struct rte_cryptodev *dev;
451 struct zuc_private *internals;
452 uint64_t cpu_flags = 0;
454 if (init_params->name[0] == '\0')
455 snprintf(init_params->name, sizeof(init_params->name),
458 /* Check CPU for supported vector instruction set */
459 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_1))
460 cpu_flags |= RTE_CRYPTODEV_FF_CPU_SSE;
462 ZUC_LOG_ERR("Vector instructions are not supported by CPU");
466 dev = rte_cryptodev_vdev_pmd_init(init_params->name,
467 sizeof(struct zuc_private), init_params->socket_id,
470 ZUC_LOG_ERR("failed to create cryptodev vdev");
474 dev->dev_type = RTE_CRYPTODEV_ZUC_PMD;
475 dev->dev_ops = rte_zuc_pmd_ops;
477 /* Register RX/TX burst functions for data path. */
478 dev->dequeue_burst = zuc_pmd_dequeue_burst;
479 dev->enqueue_burst = zuc_pmd_enqueue_burst;
481 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
482 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
485 internals = dev->data->dev_private;
487 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
488 internals->max_nb_sessions = init_params->max_nb_sessions;
492 ZUC_LOG_ERR("driver %s: cryptodev_zuc_create failed",
495 cryptodev_zuc_remove(vdev);
500 cryptodev_zuc_probe(struct rte_vdev_device *vdev)
502 struct rte_crypto_vdev_init_params init_params = {
503 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
504 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
509 const char *input_args;
511 name = rte_vdev_device_name(vdev);
514 input_args = rte_vdev_device_args(vdev);
516 rte_cryptodev_vdev_parse_init_params(&init_params, input_args);
518 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
519 init_params.socket_id);
520 if (init_params.name[0] != '\0')
521 RTE_LOG(INFO, PMD, " User defined name = %s\n",
523 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
524 init_params.max_nb_queue_pairs);
525 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
526 init_params.max_nb_sessions);
528 return cryptodev_zuc_create(name, vdev, &init_params);
532 cryptodev_zuc_remove(struct rte_vdev_device *vdev)
536 name = rte_vdev_device_name(vdev);
540 RTE_LOG(INFO, PMD, "Closing ZUC crypto device %s"
541 " on numa socket %u\n",
542 name, rte_socket_id());
547 static struct rte_vdev_driver cryptodev_zuc_pmd_drv = {
548 .probe = cryptodev_zuc_probe,
549 .remove = cryptodev_zuc_remove
552 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_ZUC_PMD, cryptodev_zuc_pmd_drv);
553 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_ZUC_PMD,
554 "max_nb_queue_pairs=<int> "
555 "max_nb_sessions=<int> "