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 4
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. */
201 process_zuc_cipher_op(struct rte_crypto_op **ops,
202 struct zuc_session **sessions,
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];
211 struct zuc_session *sess;
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");
224 #ifdef RTE_LIBRTE_PMD_ZUC_DEBUG
225 if (!rte_pktmbuf_is_contiguous(ops[i]->sym->m_src) ||
226 (ops[i]->sym->m_dst != NULL &&
227 !rte_pktmbuf_is_contiguous(
228 ops[i]->sym->m_dst))) {
229 ZUC_LOG_ERR("PMD supports only contiguous mbufs, "
230 "op (%p) provides noncontiguous mbuf as "
231 "source/destination buffer.\n", ops[i]);
232 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
237 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
238 (ops[i]->sym->cipher.data.offset >> 3);
239 dst[i] = ops[i]->sym->m_dst ?
240 rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
241 (ops[i]->sym->cipher.data.offset >> 3) :
242 rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
243 (ops[i]->sym->cipher.data.offset >> 3);
244 iv[i] = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
245 sess->cipher_iv_offset);
246 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
248 cipher_keys[i] = sess->pKey_cipher;
253 sso_zuc_eea3_n_buffer(cipher_keys, iv, src, dst,
254 num_bytes, processed_ops);
256 return processed_ops;
259 /** Generate/verify hash from mbufs. */
261 process_zuc_hash_op(struct zuc_qp *qp, struct rte_crypto_op **ops,
262 struct zuc_session **sessions,
266 uint8_t processed_ops = 0;
269 uint32_t length_in_bits;
271 struct zuc_session *sess;
273 for (i = 0; i < num_ops; i++) {
274 /* Data must be byte aligned */
275 if ((ops[i]->sym->auth.data.offset % BYTE_LEN) != 0) {
276 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
277 ZUC_LOG_ERR("Offset");
283 length_in_bits = ops[i]->sym->auth.data.length;
285 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
286 (ops[i]->sym->auth.data.offset >> 3);
287 iv = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
288 sess->auth_iv_offset);
290 if (sess->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
291 dst = (uint32_t *)qp->temp_digest;
293 sso_zuc_eia3_1_buffer(sess->pKey_hash,
295 length_in_bits, dst);
297 if (memcmp(dst, ops[i]->sym->auth.digest.data,
298 ZUC_DIGEST_LENGTH) != 0)
299 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
301 dst = (uint32_t *)ops[i]->sym->auth.digest.data;
303 sso_zuc_eia3_1_buffer(sess->pKey_hash,
305 length_in_bits, dst);
310 return processed_ops;
313 /** Process a batch of crypto ops which shares the same operation type. */
315 process_ops(struct rte_crypto_op **ops, enum zuc_operation op_type,
316 struct zuc_session **sessions,
317 struct zuc_qp *qp, uint8_t num_ops,
318 uint16_t *accumulated_enqueued_ops)
321 unsigned enqueued_ops, processed_ops;
324 case ZUC_OP_ONLY_CIPHER:
325 processed_ops = process_zuc_cipher_op(ops,
328 case ZUC_OP_ONLY_AUTH:
329 processed_ops = process_zuc_hash_op(qp, ops, sessions,
332 case ZUC_OP_CIPHER_AUTH:
333 processed_ops = process_zuc_cipher_op(ops, sessions,
335 process_zuc_hash_op(qp, ops, sessions, processed_ops);
337 case ZUC_OP_AUTH_CIPHER:
338 processed_ops = process_zuc_hash_op(qp, ops, sessions,
340 process_zuc_cipher_op(ops, sessions, processed_ops);
343 /* Operation not supported. */
347 for (i = 0; i < num_ops; i++) {
349 * If there was no error/authentication failure,
350 * change status to successful.
352 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
353 ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
354 /* Free session if a session-less crypto op. */
355 if (ops[i]->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
356 memset(sessions[i], 0, sizeof(struct zuc_session));
357 memset(ops[i]->sym->session, 0,
358 rte_cryptodev_get_header_session_size());
359 rte_mempool_put(qp->sess_mp, sessions[i]);
360 rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
361 ops[i]->sym->session = NULL;
365 enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
366 (void **)ops, processed_ops, NULL);
367 qp->qp_stats.enqueued_count += enqueued_ops;
368 *accumulated_enqueued_ops += enqueued_ops;
374 zuc_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
377 struct rte_crypto_op *c_ops[ZUC_MAX_BURST];
378 struct rte_crypto_op *curr_c_op;
380 struct zuc_session *curr_sess;
381 struct zuc_session *sessions[ZUC_MAX_BURST];
382 enum zuc_operation prev_zuc_op = ZUC_OP_NOT_SUPPORTED;
383 enum zuc_operation curr_zuc_op;
384 struct zuc_qp *qp = queue_pair;
386 uint8_t burst_size = 0;
387 uint16_t enqueued_ops = 0;
388 uint8_t processed_ops;
390 for (i = 0; i < nb_ops; i++) {
393 curr_sess = zuc_get_session(qp, curr_c_op);
394 if (unlikely(curr_sess == NULL ||
395 curr_sess->op == ZUC_OP_NOT_SUPPORTED)) {
397 RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
401 curr_zuc_op = curr_sess->op;
404 * Batch ops that share the same operation type
405 * (cipher only, auth only...).
407 if (burst_size == 0) {
408 prev_zuc_op = curr_zuc_op;
409 c_ops[0] = curr_c_op;
410 sessions[0] = curr_sess;
412 } else if (curr_zuc_op == prev_zuc_op) {
413 c_ops[burst_size] = curr_c_op;
414 sessions[burst_size] = curr_sess;
417 * When there are enough ops to process in a batch,
418 * process them, and start a new batch.
420 if (burst_size == ZUC_MAX_BURST) {
421 processed_ops = process_ops(c_ops, curr_zuc_op,
422 sessions, qp, burst_size,
424 if (processed_ops < burst_size) {
433 * Different operation type, process the ops
434 * of the previous type.
436 processed_ops = process_ops(c_ops, prev_zuc_op,
437 sessions, qp, burst_size,
439 if (processed_ops < burst_size) {
445 prev_zuc_op = curr_zuc_op;
447 c_ops[0] = curr_c_op;
448 sessions[0] = curr_sess;
453 if (burst_size != 0) {
454 /* Process the crypto ops of the last operation type. */
455 processed_ops = process_ops(c_ops, prev_zuc_op,
456 sessions, qp, burst_size,
460 qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
465 zuc_pmd_dequeue_burst(void *queue_pair,
466 struct rte_crypto_op **c_ops, uint16_t nb_ops)
468 struct zuc_qp *qp = queue_pair;
470 unsigned nb_dequeued;
472 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
473 (void **)c_ops, nb_ops, NULL);
474 qp->qp_stats.dequeued_count += nb_dequeued;
479 static int cryptodev_zuc_remove(struct rte_vdev_device *vdev);
482 cryptodev_zuc_create(const char *name,
483 struct rte_vdev_device *vdev,
484 struct rte_cryptodev_pmd_init_params *init_params)
486 struct rte_cryptodev *dev;
487 struct zuc_private *internals;
488 uint64_t cpu_flags = RTE_CRYPTODEV_FF_CPU_SSE;
491 dev = rte_cryptodev_pmd_create(name, &vdev->device, init_params);
493 ZUC_LOG_ERR("failed to create cryptodev vdev");
497 dev->driver_id = cryptodev_driver_id;
498 dev->dev_ops = rte_zuc_pmd_ops;
500 /* Register RX/TX burst functions for data path. */
501 dev->dequeue_burst = zuc_pmd_dequeue_burst;
502 dev->enqueue_burst = zuc_pmd_enqueue_burst;
504 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
505 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
508 internals = dev->data->dev_private;
510 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
511 internals->max_nb_sessions = init_params->max_nb_sessions;
515 ZUC_LOG_ERR("driver %s: cryptodev_zuc_create failed",
518 cryptodev_zuc_remove(vdev);
523 cryptodev_zuc_probe(struct rte_vdev_device *vdev)
525 struct rte_cryptodev_pmd_init_params init_params = {
527 sizeof(struct zuc_private),
529 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS,
530 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_SESSIONS
533 const char *input_args;
535 name = rte_vdev_device_name(vdev);
538 input_args = rte_vdev_device_args(vdev);
540 rte_cryptodev_pmd_parse_input_args(&init_params, input_args);
542 return cryptodev_zuc_create(name, vdev, &init_params);
546 cryptodev_zuc_remove(struct rte_vdev_device *vdev)
549 struct rte_cryptodev *cryptodev;
552 name = rte_vdev_device_name(vdev);
556 cryptodev = rte_cryptodev_pmd_get_named_dev(name);
557 if (cryptodev == NULL)
560 return rte_cryptodev_pmd_destroy(cryptodev);
563 static struct rte_vdev_driver cryptodev_zuc_pmd_drv = {
564 .probe = cryptodev_zuc_probe,
565 .remove = cryptodev_zuc_remove
568 static struct cryptodev_driver zuc_crypto_drv;
570 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_ZUC_PMD, cryptodev_zuc_pmd_drv);
571 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_ZUC_PMD,
572 "max_nb_queue_pairs=<int> "
573 "max_nb_sessions=<int> "
575 RTE_PMD_REGISTER_CRYPTO_DRIVER(zuc_crypto_drv, cryptodev_zuc_pmd_drv,
576 cryptodev_driver_id);