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>
39 #include <rte_malloc.h>
40 #include <rte_cpuflags.h>
42 #include "rte_zuc_pmd_private.h"
44 #define ZUC_MAX_BURST 8
47 static uint8_t cryptodev_driver_id;
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->cipher_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)
135 if (auth_xform->auth.digest_length != ZUC_DIGEST_LENGTH) {
136 ZUC_LOG_ERR("Wrong digest length");
140 sess->auth_op = auth_xform->auth.op;
142 if (auth_xform->auth.iv.length != ZUC_IV_KEY_LENGTH) {
143 ZUC_LOG_ERR("Wrong IV length");
146 sess->auth_iv_offset = auth_xform->auth.iv.offset;
149 memcpy(sess->pKey_hash, auth_xform->auth.key.data,
159 /** Get ZUC session. */
160 static struct zuc_session *
161 zuc_get_session(struct zuc_qp *qp, struct rte_crypto_op *op)
163 struct zuc_session *sess = NULL;
165 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
166 if (likely(op->sym->session != NULL))
167 sess = (struct zuc_session *)get_session_private_data(
169 cryptodev_driver_id);
172 void *_sess_private_data = NULL;
174 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
177 if (rte_mempool_get(qp->sess_mp, (void **)&_sess_private_data))
180 sess = (struct zuc_session *)_sess_private_data;
182 if (unlikely(zuc_set_session_parameters(sess,
183 op->sym->xform) != 0)) {
184 rte_mempool_put(qp->sess_mp, _sess);
185 rte_mempool_put(qp->sess_mp, _sess_private_data);
188 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
189 set_session_private_data(op->sym->session, cryptodev_driver_id,
193 if (unlikely(sess == NULL))
194 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
200 /** Encrypt/decrypt mbufs with same cipher key. */
202 process_zuc_cipher_op(struct rte_crypto_op **ops,
203 struct zuc_session *session,
207 uint8_t processed_ops = 0;
208 uint8_t *src[ZUC_MAX_BURST], *dst[ZUC_MAX_BURST];
209 uint8_t *iv[ZUC_MAX_BURST];
210 uint32_t num_bytes[ZUC_MAX_BURST];
211 uint8_t *cipher_keys[ZUC_MAX_BURST];
213 for (i = 0; i < num_ops; i++) {
214 if (((ops[i]->sym->cipher.data.length % BYTE_LEN) != 0)
215 || ((ops[i]->sym->cipher.data.offset
217 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
218 ZUC_LOG_ERR("Data Length or offset");
222 #ifdef RTE_LIBRTE_PMD_ZUC_DEBUG
223 if (!rte_pktmbuf_is_contiguous(ops[i]->sym->m_src) ||
224 (ops[i]->sym->m_dst != NULL &&
225 !rte_pktmbuf_is_contiguous(
226 ops[i]->sym->m_dst))) {
227 ZUC_LOG_ERR("PMD supports only contiguous mbufs, "
228 "op (%p) provides noncontiguous mbuf as "
229 "source/destination buffer.\n", ops[i]);
230 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
235 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
236 (ops[i]->sym->cipher.data.offset >> 3);
237 dst[i] = ops[i]->sym->m_dst ?
238 rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
239 (ops[i]->sym->cipher.data.offset >> 3) :
240 rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
241 (ops[i]->sym->cipher.data.offset >> 3);
242 iv[i] = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
243 session->cipher_iv_offset);
244 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
246 cipher_keys[i] = session->pKey_cipher;
251 sso_zuc_eea3_n_buffer(cipher_keys, iv, src, dst,
252 num_bytes, processed_ops);
254 return processed_ops;
257 /** Generate/verify hash from mbufs with same hash key. */
259 process_zuc_hash_op(struct zuc_qp *qp, struct rte_crypto_op **ops,
260 struct zuc_session *session,
264 uint8_t processed_ops = 0;
267 uint32_t length_in_bits;
270 for (i = 0; i < num_ops; i++) {
271 /* Data must be byte aligned */
272 if ((ops[i]->sym->auth.data.offset % BYTE_LEN) != 0) {
273 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
274 ZUC_LOG_ERR("Offset");
278 length_in_bits = ops[i]->sym->auth.data.length;
280 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
281 (ops[i]->sym->auth.data.offset >> 3);
282 iv = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
283 session->auth_iv_offset);
285 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
286 dst = (uint32_t *)qp->temp_digest;
288 sso_zuc_eia3_1_buffer(session->pKey_hash,
290 length_in_bits, dst);
292 if (memcmp(dst, ops[i]->sym->auth.digest.data,
293 ZUC_DIGEST_LENGTH) != 0)
294 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
296 dst = (uint32_t *)ops[i]->sym->auth.digest.data;
298 sso_zuc_eia3_1_buffer(session->pKey_hash,
300 length_in_bits, dst);
305 return processed_ops;
308 /** Process a batch of crypto ops which shares the same session. */
310 process_ops(struct rte_crypto_op **ops, struct zuc_session *session,
311 struct zuc_qp *qp, uint8_t num_ops,
312 uint16_t *accumulated_enqueued_ops)
315 unsigned enqueued_ops, processed_ops;
317 switch (session->op) {
318 case ZUC_OP_ONLY_CIPHER:
319 processed_ops = process_zuc_cipher_op(ops,
322 case ZUC_OP_ONLY_AUTH:
323 processed_ops = process_zuc_hash_op(qp, ops, session,
326 case ZUC_OP_CIPHER_AUTH:
327 processed_ops = process_zuc_cipher_op(ops, session,
329 process_zuc_hash_op(qp, ops, session, processed_ops);
331 case ZUC_OP_AUTH_CIPHER:
332 processed_ops = process_zuc_hash_op(qp, ops, session,
334 process_zuc_cipher_op(ops, session, processed_ops);
337 /* Operation not supported. */
341 for (i = 0; i < num_ops; i++) {
343 * If there was no error/authentication failure,
344 * change status to successful.
346 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
347 ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
348 /* Free session if a session-less crypto op. */
349 if (ops[i]->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
350 memset(session, 0, sizeof(struct zuc_session));
351 memset(ops[i]->sym->session, 0,
352 rte_cryptodev_get_header_session_size());
353 rte_mempool_put(qp->sess_mp, session);
354 rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
355 ops[i]->sym->session = NULL;
359 enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
360 (void **)ops, processed_ops, NULL);
361 qp->qp_stats.enqueued_count += enqueued_ops;
362 *accumulated_enqueued_ops += enqueued_ops;
368 zuc_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
371 struct rte_crypto_op *c_ops[ZUC_MAX_BURST];
372 struct rte_crypto_op *curr_c_op;
374 struct zuc_session *prev_sess = NULL, *curr_sess = NULL;
375 struct zuc_qp *qp = queue_pair;
377 uint8_t burst_size = 0;
378 uint16_t enqueued_ops = 0;
379 uint8_t processed_ops;
381 for (i = 0; i < nb_ops; i++) {
384 /* Set status as enqueued (not processed yet) by default. */
385 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
387 curr_sess = zuc_get_session(qp, curr_c_op);
388 if (unlikely(curr_sess == NULL ||
389 curr_sess->op == ZUC_OP_NOT_SUPPORTED)) {
391 RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
395 /* Batch ops that share the same session. */
396 if (prev_sess == NULL) {
397 prev_sess = curr_sess;
398 c_ops[burst_size++] = curr_c_op;
399 } else if (curr_sess == prev_sess) {
400 c_ops[burst_size++] = curr_c_op;
402 * When there are enough ops to process in a batch,
403 * process them, and start a new batch.
405 if (burst_size == ZUC_MAX_BURST) {
406 processed_ops = process_ops(c_ops, prev_sess,
407 qp, burst_size, &enqueued_ops);
408 if (processed_ops < burst_size) {
418 * Different session, process the ops
419 * of the previous session.
421 processed_ops = process_ops(c_ops, prev_sess,
422 qp, burst_size, &enqueued_ops);
423 if (processed_ops < burst_size) {
429 prev_sess = curr_sess;
431 c_ops[burst_size++] = curr_c_op;
435 if (burst_size != 0) {
436 /* Process the crypto ops of the last session. */
437 processed_ops = process_ops(c_ops, prev_sess,
438 qp, burst_size, &enqueued_ops);
441 qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
446 zuc_pmd_dequeue_burst(void *queue_pair,
447 struct rte_crypto_op **c_ops, uint16_t nb_ops)
449 struct zuc_qp *qp = queue_pair;
451 unsigned nb_dequeued;
453 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
454 (void **)c_ops, nb_ops, NULL);
455 qp->qp_stats.dequeued_count += nb_dequeued;
460 static int cryptodev_zuc_remove(struct rte_vdev_device *vdev);
463 cryptodev_zuc_create(const char *name,
464 struct rte_vdev_device *vdev,
465 struct rte_cryptodev_pmd_init_params *init_params)
467 struct rte_cryptodev *dev;
468 struct zuc_private *internals;
469 uint64_t cpu_flags = RTE_CRYPTODEV_FF_CPU_SSE;
472 dev = rte_cryptodev_pmd_create(name, &vdev->device, init_params);
474 ZUC_LOG_ERR("failed to create cryptodev vdev");
478 dev->driver_id = cryptodev_driver_id;
479 dev->dev_ops = rte_zuc_pmd_ops;
481 /* Register RX/TX burst functions for data path. */
482 dev->dequeue_burst = zuc_pmd_dequeue_burst;
483 dev->enqueue_burst = zuc_pmd_enqueue_burst;
485 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
486 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
489 internals = dev->data->dev_private;
491 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
492 internals->max_nb_sessions = init_params->max_nb_sessions;
496 ZUC_LOG_ERR("driver %s: cryptodev_zuc_create failed",
499 cryptodev_zuc_remove(vdev);
504 cryptodev_zuc_probe(struct rte_vdev_device *vdev)
506 struct rte_cryptodev_pmd_init_params init_params = {
508 sizeof(struct zuc_private),
510 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS,
511 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_SESSIONS
514 const char *input_args;
516 name = rte_vdev_device_name(vdev);
519 input_args = rte_vdev_device_args(vdev);
521 rte_cryptodev_pmd_parse_input_args(&init_params, input_args);
523 return cryptodev_zuc_create(name, vdev, &init_params);
527 cryptodev_zuc_remove(struct rte_vdev_device *vdev)
530 struct rte_cryptodev *cryptodev;
533 name = rte_vdev_device_name(vdev);
537 cryptodev = rte_cryptodev_pmd_get_named_dev(name);
538 if (cryptodev == NULL)
541 return rte_cryptodev_pmd_destroy(cryptodev);
544 static struct rte_vdev_driver cryptodev_zuc_pmd_drv = {
545 .probe = cryptodev_zuc_probe,
546 .remove = cryptodev_zuc_remove
549 static struct cryptodev_driver zuc_crypto_drv;
551 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_ZUC_PMD, cryptodev_zuc_pmd_drv);
552 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_ZUC_PMD,
553 "max_nb_queue_pairs=<int> "
554 "max_nb_sessions=<int> "
556 RTE_PMD_REGISTER_CRYPTO_DRIVER(zuc_crypto_drv, cryptodev_zuc_pmd_drv,
557 cryptodev_driver_id);