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>
39 #include <rte_malloc.h>
40 #include <rte_cpuflags.h>
42 #include "rte_zuc_pmd_private.h"
44 #define ZUC_DIGEST_LENGTH 4
45 #define ZUC_MAX_BURST 8
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 memcpy(sess->pKey_cipher, xform->cipher.key.data, ZUC_IV_KEY_LENGTH);
122 /* Only ZUC EIA3 supported */
123 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_ZUC_EIA3)
125 sess->auth_op = auth_xform->auth.op;
127 memcpy(sess->pKey_hash, xform->auth.key.data, ZUC_IV_KEY_LENGTH);
136 /** Get ZUC session. */
137 static struct zuc_session *
138 zuc_get_session(struct zuc_qp *qp, struct rte_crypto_op *op)
140 struct zuc_session *sess;
142 if (op->sym->sess_type == RTE_CRYPTO_SYM_OP_WITH_SESSION) {
143 if (unlikely(op->sym->session->dev_type !=
144 RTE_CRYPTODEV_ZUC_PMD))
147 sess = (struct zuc_session *)op->sym->session->_private;
149 struct rte_cryptodev_session *c_sess = NULL;
151 if (rte_mempool_get(qp->sess_mp, (void **)&c_sess))
154 sess = (struct zuc_session *)c_sess->_private;
156 if (unlikely(zuc_set_session_parameters(sess,
157 op->sym->xform) != 0))
164 /** Encrypt/decrypt mbufs with same cipher key. */
166 process_zuc_cipher_op(struct rte_crypto_op **ops,
167 struct zuc_session *session,
171 uint8_t processed_ops = 0;
172 uint8_t *src[ZUC_MAX_BURST], *dst[ZUC_MAX_BURST];
173 uint8_t *IV[ZUC_MAX_BURST];
174 uint32_t num_bytes[ZUC_MAX_BURST];
175 uint8_t *cipher_keys[ZUC_MAX_BURST];
177 for (i = 0; i < num_ops; i++) {
179 if (unlikely(ops[i]->sym->cipher.iv.length != ZUC_IV_KEY_LENGTH)) {
180 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
185 if (((ops[i]->sym->cipher.data.length % BYTE_LEN) != 0)
186 || ((ops[i]->sym->cipher.data.offset
188 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
189 ZUC_LOG_ERR("Data Length or offset");
193 #ifdef RTE_LIBRTE_PMD_ZUC_DEBUG
194 if (!rte_pktmbuf_is_contiguous(ops[i]->sym->m_src) ||
195 (ops[i]->sym->m_dst != NULL &&
196 !rte_pktmbuf_is_contiguous(
197 ops[i]->sym->m_dst))) {
198 ZUC_LOG_ERR("PMD supports only contiguous mbufs, "
199 "op (%p) provides noncontiguous mbuf as "
200 "source/destination buffer.\n", ops[i]);
201 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
206 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
207 (ops[i]->sym->cipher.data.offset >> 3);
208 dst[i] = ops[i]->sym->m_dst ?
209 rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
210 (ops[i]->sym->cipher.data.offset >> 3) :
211 rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
212 (ops[i]->sym->cipher.data.offset >> 3);
213 IV[i] = ops[i]->sym->cipher.iv.data;
214 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
216 cipher_keys[i] = session->pKey_cipher;
221 sso_zuc_eea3_n_buffer(cipher_keys, IV, src, dst,
222 num_bytes, processed_ops);
224 return processed_ops;
227 /** Generate/verify hash from mbufs with same hash key. */
229 process_zuc_hash_op(struct rte_crypto_op **ops,
230 struct zuc_session *session,
234 uint8_t processed_ops = 0;
237 uint32_t length_in_bits;
239 for (i = 0; i < num_ops; i++) {
240 if (unlikely(ops[i]->sym->auth.aad.length != ZUC_IV_KEY_LENGTH)) {
241 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
246 if (unlikely(ops[i]->sym->auth.digest.length != ZUC_DIGEST_LENGTH)) {
247 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
248 ZUC_LOG_ERR("digest");
252 /* Data must be byte aligned */
253 if ((ops[i]->sym->auth.data.offset % BYTE_LEN) != 0) {
254 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
255 ZUC_LOG_ERR("Offset");
259 length_in_bits = ops[i]->sym->auth.data.length;
261 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
262 (ops[i]->sym->auth.data.offset >> 3);
264 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
265 dst = (uint32_t *)rte_pktmbuf_append(ops[i]->sym->m_src,
266 ops[i]->sym->auth.digest.length);
268 sso_zuc_eia3_1_buffer(session->pKey_hash,
269 ops[i]->sym->auth.aad.data, src,
270 length_in_bits, dst);
272 if (memcmp(dst, ops[i]->sym->auth.digest.data,
273 ops[i]->sym->auth.digest.length) != 0)
274 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
276 /* Trim area used for digest from mbuf. */
277 rte_pktmbuf_trim(ops[i]->sym->m_src,
278 ops[i]->sym->auth.digest.length);
280 dst = (uint32_t *)ops[i]->sym->auth.digest.data;
282 sso_zuc_eia3_1_buffer(session->pKey_hash,
283 ops[i]->sym->auth.aad.data, src,
284 length_in_bits, dst);
289 return processed_ops;
292 /** Process a batch of crypto ops which shares the same session. */
294 process_ops(struct rte_crypto_op **ops, struct zuc_session *session,
295 struct zuc_qp *qp, uint8_t num_ops,
296 uint16_t *accumulated_enqueued_ops)
299 unsigned enqueued_ops, processed_ops;
301 switch (session->op) {
302 case ZUC_OP_ONLY_CIPHER:
303 processed_ops = process_zuc_cipher_op(ops,
306 case ZUC_OP_ONLY_AUTH:
307 processed_ops = process_zuc_hash_op(ops, session,
310 case ZUC_OP_CIPHER_AUTH:
311 processed_ops = process_zuc_cipher_op(ops, session,
313 process_zuc_hash_op(ops, session, processed_ops);
315 case ZUC_OP_AUTH_CIPHER:
316 processed_ops = process_zuc_hash_op(ops, session,
318 process_zuc_cipher_op(ops, session, processed_ops);
321 /* Operation not supported. */
325 for (i = 0; i < num_ops; i++) {
327 * If there was no error/authentication failure,
328 * change status to successful.
330 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
331 ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
332 /* Free session if a session-less crypto op. */
333 if (ops[i]->sym->sess_type == RTE_CRYPTO_SYM_OP_SESSIONLESS) {
334 rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
335 ops[i]->sym->session = NULL;
339 enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
340 (void **)ops, processed_ops);
341 qp->qp_stats.enqueued_count += enqueued_ops;
342 *accumulated_enqueued_ops += enqueued_ops;
348 zuc_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
351 struct rte_crypto_op *c_ops[ZUC_MAX_BURST];
352 struct rte_crypto_op *curr_c_op;
354 struct zuc_session *prev_sess = NULL, *curr_sess = NULL;
355 struct zuc_qp *qp = queue_pair;
357 uint8_t burst_size = 0;
358 uint16_t enqueued_ops = 0;
359 uint8_t processed_ops;
361 for (i = 0; i < nb_ops; i++) {
364 /* Set status as enqueued (not processed yet) by default. */
365 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
367 curr_sess = zuc_get_session(qp, curr_c_op);
368 if (unlikely(curr_sess == NULL ||
369 curr_sess->op == ZUC_OP_NOT_SUPPORTED)) {
371 RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
375 /* Batch ops that share the same session. */
376 if (prev_sess == NULL) {
377 prev_sess = curr_sess;
378 c_ops[burst_size++] = curr_c_op;
379 } else if (curr_sess == prev_sess) {
380 c_ops[burst_size++] = curr_c_op;
382 * When there are enough ops to process in a batch,
383 * process them, and start a new batch.
385 if (burst_size == ZUC_MAX_BURST) {
386 processed_ops = process_ops(c_ops, prev_sess,
387 qp, burst_size, &enqueued_ops);
388 if (processed_ops < burst_size) {
398 * Different session, process the ops
399 * of the previous session.
401 processed_ops = process_ops(c_ops, prev_sess,
402 qp, burst_size, &enqueued_ops);
403 if (processed_ops < burst_size) {
409 prev_sess = curr_sess;
411 c_ops[burst_size++] = curr_c_op;
415 if (burst_size != 0) {
416 /* Process the crypto ops of the last session. */
417 processed_ops = process_ops(c_ops, prev_sess,
418 qp, burst_size, &enqueued_ops);
421 qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
426 zuc_pmd_dequeue_burst(void *queue_pair,
427 struct rte_crypto_op **c_ops, uint16_t nb_ops)
429 struct zuc_qp *qp = queue_pair;
431 unsigned nb_dequeued;
433 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
434 (void **)c_ops, nb_ops);
435 qp->qp_stats.dequeued_count += nb_dequeued;
440 static int cryptodev_zuc_remove(const char *name);
443 cryptodev_zuc_create(struct rte_crypto_vdev_init_params *init_params)
445 struct rte_cryptodev *dev;
446 struct zuc_private *internals;
447 uint64_t cpu_flags = 0;
449 if (init_params->name[0] == '\0') {
450 int ret = rte_cryptodev_pmd_create_dev_name(
452 RTE_STR(CRYPTODEV_NAME_ZUC_PMD));
455 ZUC_LOG_ERR("failed to create unique name");
460 /* Check CPU for supported vector instruction set */
461 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_1))
462 cpu_flags |= RTE_CRYPTODEV_FF_CPU_SSE;
464 ZUC_LOG_ERR("Vector instructions are not supported by CPU");
468 dev = rte_cryptodev_pmd_virtual_dev_init(init_params->name,
469 sizeof(struct zuc_private), init_params->socket_id);
471 ZUC_LOG_ERR("failed to create cryptodev vdev");
475 dev->dev_type = RTE_CRYPTODEV_ZUC_PMD;
476 dev->dev_ops = rte_zuc_pmd_ops;
478 /* Register RX/TX burst functions for data path. */
479 dev->dequeue_burst = zuc_pmd_dequeue_burst;
480 dev->enqueue_burst = zuc_pmd_enqueue_burst;
482 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
483 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
486 internals = dev->data->dev_private;
488 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
489 internals->max_nb_sessions = init_params->max_nb_sessions;
493 ZUC_LOG_ERR("driver %s: cryptodev_zuc_create failed",
496 cryptodev_zuc_remove(init_params->name);
501 cryptodev_zuc_probe(const char *name,
502 const char *input_args)
504 struct rte_crypto_vdev_init_params init_params = {
505 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
506 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
511 rte_cryptodev_parse_vdev_init_params(&init_params, input_args);
513 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
514 init_params.socket_id);
515 if (init_params.name[0] != '\0')
516 RTE_LOG(INFO, PMD, " User defined name = %s\n",
518 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
519 init_params.max_nb_queue_pairs);
520 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
521 init_params.max_nb_sessions);
523 return cryptodev_zuc_create(&init_params);
527 cryptodev_zuc_remove(const char *name)
532 RTE_LOG(INFO, PMD, "Closing ZUC crypto device %s"
533 " on numa socket %u\n",
534 name, rte_socket_id());
539 static struct rte_vdev_driver cryptodev_zuc_pmd_drv = {
540 .probe = cryptodev_zuc_probe,
541 .remove = cryptodev_zuc_remove
544 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_ZUC_PMD, cryptodev_zuc_pmd_drv);
545 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_ZUC_PMD,
546 "max_nb_queue_pairs=<int> "
547 "max_nb_sessions=<int> "