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_hexdump.h>
35 #include <rte_cryptodev.h>
36 #include <rte_cryptodev_pmd.h>
37 #include <rte_bus_vdev.h>
38 #include <rte_malloc.h>
39 #include <rte_cpuflags.h>
41 #include "rte_zuc_pmd_private.h"
43 #define ZUC_MAX_BURST 8
46 static uint8_t cryptodev_driver_id;
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 if (cipher_xform->cipher.iv.length != ZUC_IV_KEY_LENGTH) {
119 ZUC_LOG_ERR("Wrong IV length");
122 sess->cipher_iv_offset = cipher_xform->cipher.iv.offset;
125 memcpy(sess->pKey_cipher, cipher_xform->cipher.key.data,
130 /* Only ZUC EIA3 supported */
131 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_ZUC_EIA3)
134 if (auth_xform->auth.digest_length != ZUC_DIGEST_LENGTH) {
135 ZUC_LOG_ERR("Wrong digest length");
139 sess->auth_op = auth_xform->auth.op;
141 if (auth_xform->auth.iv.length != ZUC_IV_KEY_LENGTH) {
142 ZUC_LOG_ERR("Wrong IV length");
145 sess->auth_iv_offset = auth_xform->auth.iv.offset;
148 memcpy(sess->pKey_hash, auth_xform->auth.key.data,
158 /** Get ZUC session. */
159 static struct zuc_session *
160 zuc_get_session(struct zuc_qp *qp, struct rte_crypto_op *op)
162 struct zuc_session *sess = NULL;
164 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
165 if (likely(op->sym->session != NULL))
166 sess = (struct zuc_session *)get_session_private_data(
168 cryptodev_driver_id);
171 void *_sess_private_data = NULL;
173 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
176 if (rte_mempool_get(qp->sess_mp, (void **)&_sess_private_data))
179 sess = (struct zuc_session *)_sess_private_data;
181 if (unlikely(zuc_set_session_parameters(sess,
182 op->sym->xform) != 0)) {
183 rte_mempool_put(qp->sess_mp, _sess);
184 rte_mempool_put(qp->sess_mp, _sess_private_data);
187 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
188 set_session_private_data(op->sym->session, cryptodev_driver_id,
192 if (unlikely(sess == NULL))
193 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
199 /** Encrypt/decrypt mbufs with same cipher key. */
201 process_zuc_cipher_op(struct rte_crypto_op **ops,
202 struct zuc_session *session,
206 uint8_t processed_ops = 0;
207 uint8_t *src[ZUC_MAX_BURST], *dst[ZUC_MAX_BURST];
208 uint8_t *iv[ZUC_MAX_BURST];
209 uint32_t num_bytes[ZUC_MAX_BURST];
210 uint8_t *cipher_keys[ZUC_MAX_BURST];
212 for (i = 0; i < num_ops; i++) {
213 if (((ops[i]->sym->cipher.data.length % BYTE_LEN) != 0)
214 || ((ops[i]->sym->cipher.data.offset
216 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
217 ZUC_LOG_ERR("Data Length or offset");
221 #ifdef RTE_LIBRTE_PMD_ZUC_DEBUG
222 if (!rte_pktmbuf_is_contiguous(ops[i]->sym->m_src) ||
223 (ops[i]->sym->m_dst != NULL &&
224 !rte_pktmbuf_is_contiguous(
225 ops[i]->sym->m_dst))) {
226 ZUC_LOG_ERR("PMD supports only contiguous mbufs, "
227 "op (%p) provides noncontiguous mbuf as "
228 "source/destination buffer.\n", ops[i]);
229 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
234 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
235 (ops[i]->sym->cipher.data.offset >> 3);
236 dst[i] = ops[i]->sym->m_dst ?
237 rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
238 (ops[i]->sym->cipher.data.offset >> 3) :
239 rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
240 (ops[i]->sym->cipher.data.offset >> 3);
241 iv[i] = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
242 session->cipher_iv_offset);
243 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
245 cipher_keys[i] = session->pKey_cipher;
250 sso_zuc_eea3_n_buffer(cipher_keys, iv, src, dst,
251 num_bytes, processed_ops);
253 return processed_ops;
256 /** Generate/verify hash from mbufs with same hash key. */
258 process_zuc_hash_op(struct zuc_qp *qp, struct rte_crypto_op **ops,
259 struct zuc_session *session,
263 uint8_t processed_ops = 0;
266 uint32_t length_in_bits;
269 for (i = 0; i < num_ops; i++) {
270 /* Data must be byte aligned */
271 if ((ops[i]->sym->auth.data.offset % BYTE_LEN) != 0) {
272 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
273 ZUC_LOG_ERR("Offset");
277 length_in_bits = ops[i]->sym->auth.data.length;
279 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
280 (ops[i]->sym->auth.data.offset >> 3);
281 iv = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
282 session->auth_iv_offset);
284 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
285 dst = (uint32_t *)qp->temp_digest;
287 sso_zuc_eia3_1_buffer(session->pKey_hash,
289 length_in_bits, dst);
291 if (memcmp(dst, ops[i]->sym->auth.digest.data,
292 ZUC_DIGEST_LENGTH) != 0)
293 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
295 dst = (uint32_t *)ops[i]->sym->auth.digest.data;
297 sso_zuc_eia3_1_buffer(session->pKey_hash,
299 length_in_bits, dst);
304 return processed_ops;
307 /** Process a batch of crypto ops which shares the same session. */
309 process_ops(struct rte_crypto_op **ops, struct zuc_session *session,
310 struct zuc_qp *qp, uint8_t num_ops,
311 uint16_t *accumulated_enqueued_ops)
314 unsigned enqueued_ops, processed_ops;
316 switch (session->op) {
317 case ZUC_OP_ONLY_CIPHER:
318 processed_ops = process_zuc_cipher_op(ops,
321 case ZUC_OP_ONLY_AUTH:
322 processed_ops = process_zuc_hash_op(qp, ops, session,
325 case ZUC_OP_CIPHER_AUTH:
326 processed_ops = process_zuc_cipher_op(ops, session,
328 process_zuc_hash_op(qp, ops, session, processed_ops);
330 case ZUC_OP_AUTH_CIPHER:
331 processed_ops = process_zuc_hash_op(qp, ops, session,
333 process_zuc_cipher_op(ops, session, processed_ops);
336 /* Operation not supported. */
340 for (i = 0; i < num_ops; i++) {
342 * If there was no error/authentication failure,
343 * change status to successful.
345 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
346 ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
347 /* Free session if a session-less crypto op. */
348 if (ops[i]->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
349 memset(session, 0, sizeof(struct zuc_session));
350 memset(ops[i]->sym->session, 0,
351 rte_cryptodev_get_header_session_size());
352 rte_mempool_put(qp->sess_mp, session);
353 rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
354 ops[i]->sym->session = NULL;
358 enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
359 (void **)ops, processed_ops, NULL);
360 qp->qp_stats.enqueued_count += enqueued_ops;
361 *accumulated_enqueued_ops += enqueued_ops;
367 zuc_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
370 struct rte_crypto_op *c_ops[ZUC_MAX_BURST];
371 struct rte_crypto_op *curr_c_op;
373 struct zuc_session *prev_sess = NULL, *curr_sess = NULL;
374 struct zuc_qp *qp = queue_pair;
376 uint8_t burst_size = 0;
377 uint16_t enqueued_ops = 0;
378 uint8_t processed_ops;
380 for (i = 0; i < nb_ops; i++) {
383 /* Set status as enqueued (not processed yet) by default. */
384 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
386 curr_sess = zuc_get_session(qp, curr_c_op);
387 if (unlikely(curr_sess == NULL ||
388 curr_sess->op == ZUC_OP_NOT_SUPPORTED)) {
390 RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
394 /* Batch ops that share the same session. */
395 if (prev_sess == NULL) {
396 prev_sess = curr_sess;
397 c_ops[burst_size++] = curr_c_op;
398 } else if (curr_sess == prev_sess) {
399 c_ops[burst_size++] = curr_c_op;
401 * When there are enough ops to process in a batch,
402 * process them, and start a new batch.
404 if (burst_size == ZUC_MAX_BURST) {
405 processed_ops = process_ops(c_ops, prev_sess,
406 qp, burst_size, &enqueued_ops);
407 if (processed_ops < burst_size) {
417 * Different session, process the ops
418 * of the previous session.
420 processed_ops = process_ops(c_ops, prev_sess,
421 qp, burst_size, &enqueued_ops);
422 if (processed_ops < burst_size) {
428 prev_sess = curr_sess;
430 c_ops[burst_size++] = curr_c_op;
434 if (burst_size != 0) {
435 /* Process the crypto ops of the last session. */
436 processed_ops = process_ops(c_ops, prev_sess,
437 qp, burst_size, &enqueued_ops);
440 qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
445 zuc_pmd_dequeue_burst(void *queue_pair,
446 struct rte_crypto_op **c_ops, uint16_t nb_ops)
448 struct zuc_qp *qp = queue_pair;
450 unsigned nb_dequeued;
452 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
453 (void **)c_ops, nb_ops, NULL);
454 qp->qp_stats.dequeued_count += nb_dequeued;
459 static int cryptodev_zuc_remove(struct rte_vdev_device *vdev);
462 cryptodev_zuc_create(const char *name,
463 struct rte_vdev_device *vdev,
464 struct rte_cryptodev_pmd_init_params *init_params)
466 struct rte_cryptodev *dev;
467 struct zuc_private *internals;
468 uint64_t cpu_flags = RTE_CRYPTODEV_FF_CPU_SSE;
471 dev = rte_cryptodev_pmd_create(name, &vdev->device, init_params);
473 ZUC_LOG_ERR("failed to create cryptodev vdev");
477 dev->driver_id = cryptodev_driver_id;
478 dev->dev_ops = rte_zuc_pmd_ops;
480 /* Register RX/TX burst functions for data path. */
481 dev->dequeue_burst = zuc_pmd_dequeue_burst;
482 dev->enqueue_burst = zuc_pmd_enqueue_burst;
484 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
485 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
488 internals = dev->data->dev_private;
490 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
491 internals->max_nb_sessions = init_params->max_nb_sessions;
495 ZUC_LOG_ERR("driver %s: cryptodev_zuc_create failed",
498 cryptodev_zuc_remove(vdev);
503 cryptodev_zuc_probe(struct rte_vdev_device *vdev)
505 struct rte_cryptodev_pmd_init_params init_params = {
507 sizeof(struct zuc_private),
509 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS,
510 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_SESSIONS
513 const char *input_args;
515 name = rte_vdev_device_name(vdev);
518 input_args = rte_vdev_device_args(vdev);
520 rte_cryptodev_pmd_parse_input_args(&init_params, input_args);
522 return cryptodev_zuc_create(name, vdev, &init_params);
526 cryptodev_zuc_remove(struct rte_vdev_device *vdev)
529 struct rte_cryptodev *cryptodev;
532 name = rte_vdev_device_name(vdev);
536 cryptodev = rte_cryptodev_pmd_get_named_dev(name);
537 if (cryptodev == NULL)
540 return rte_cryptodev_pmd_destroy(cryptodev);
543 static struct rte_vdev_driver cryptodev_zuc_pmd_drv = {
544 .probe = cryptodev_zuc_probe,
545 .remove = cryptodev_zuc_remove
548 static struct cryptodev_driver zuc_crypto_drv;
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> "
555 RTE_PMD_REGISTER_CRYPTO_DRIVER(zuc_crypto_drv, cryptodev_zuc_pmd_drv,
556 cryptodev_driver_id);