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>
38 #include <rte_cryptodev_vdev.h>
40 #include <rte_malloc.h>
41 #include <rte_cpuflags.h>
43 #include "rte_kasumi_pmd_private.h"
45 #define KASUMI_KEY_LENGTH 16
46 #define KASUMI_IV_LENGTH 8
47 #define KASUMI_DIGEST_LENGTH 4
48 #define KASUMI_MAX_BURST 4
51 static uint8_t cryptodev_driver_id;
53 /** Get xform chain order. */
54 static enum kasumi_operation
55 kasumi_get_mode(const struct rte_crypto_sym_xform *xform)
58 return KASUMI_OP_NOT_SUPPORTED;
61 if (xform->next->next != NULL)
62 return KASUMI_OP_NOT_SUPPORTED;
64 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
65 if (xform->next == NULL)
66 return KASUMI_OP_ONLY_AUTH;
67 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
68 return KASUMI_OP_AUTH_CIPHER;
70 return KASUMI_OP_NOT_SUPPORTED;
73 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
74 if (xform->next == NULL)
75 return KASUMI_OP_ONLY_CIPHER;
76 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
77 return KASUMI_OP_CIPHER_AUTH;
79 return KASUMI_OP_NOT_SUPPORTED;
82 return KASUMI_OP_NOT_SUPPORTED;
86 /** Parse crypto xform chain and set private session parameters. */
88 kasumi_set_session_parameters(struct kasumi_session *sess,
89 const struct rte_crypto_sym_xform *xform)
91 const struct rte_crypto_sym_xform *auth_xform = NULL;
92 const struct rte_crypto_sym_xform *cipher_xform = NULL;
93 enum kasumi_operation mode;
95 /* Select Crypto operation - hash then cipher / cipher then hash */
96 mode = kasumi_get_mode(xform);
99 case KASUMI_OP_CIPHER_AUTH:
100 auth_xform = xform->next;
102 case KASUMI_OP_ONLY_CIPHER:
103 cipher_xform = xform;
105 case KASUMI_OP_AUTH_CIPHER:
106 cipher_xform = xform->next;
108 case KASUMI_OP_ONLY_AUTH:
111 case KASUMI_OP_NOT_SUPPORTED:
113 KASUMI_LOG_ERR("Unsupported operation chain order parameter");
118 /* Only KASUMI F8 supported */
119 if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_KASUMI_F8)
122 sess->cipher_iv_offset = cipher_xform->cipher.iv.offset;
123 if (cipher_xform->cipher.iv.length != KASUMI_IV_LENGTH) {
124 KASUMI_LOG_ERR("Wrong IV length");
129 sso_kasumi_init_f8_key_sched(cipher_xform->cipher.key.data,
130 &sess->pKeySched_cipher);
134 /* Only KASUMI F9 supported */
135 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_KASUMI_F9)
138 if (auth_xform->auth.digest_length != KASUMI_DIGEST_LENGTH) {
139 KASUMI_LOG_ERR("Wrong digest length");
143 sess->auth_op = auth_xform->auth.op;
145 sess->auth_iv_offset = auth_xform->auth.iv.offset;
146 if (auth_xform->auth.iv.length != KASUMI_IV_LENGTH) {
147 KASUMI_LOG_ERR("Wrong IV length");
152 sso_kasumi_init_f9_key_sched(auth_xform->auth.key.data,
153 &sess->pKeySched_hash);
162 /** Get KASUMI session. */
163 static struct kasumi_session *
164 kasumi_get_session(struct kasumi_qp *qp, struct rte_crypto_op *op)
166 struct kasumi_session *sess;
168 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
169 if (unlikely(op->sym->session->driver_id !=
170 cryptodev_driver_id))
173 sess = (struct kasumi_session *)op->sym->session->_private;
175 struct rte_cryptodev_session *c_sess = NULL;
177 if (rte_mempool_get(qp->sess_mp, (void **)&c_sess))
180 sess = (struct kasumi_session *)c_sess->_private;
182 if (unlikely(kasumi_set_session_parameters(sess,
183 op->sym->xform) != 0))
190 /** Encrypt/decrypt mbufs with same cipher key. */
192 process_kasumi_cipher_op(struct rte_crypto_op **ops,
193 struct kasumi_session *session,
197 uint8_t processed_ops = 0;
198 uint8_t *src[num_ops], *dst[num_ops];
200 uint64_t iv[num_ops];
201 uint32_t num_bytes[num_ops];
203 for (i = 0; i < num_ops; i++) {
204 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
205 (ops[i]->sym->cipher.data.offset >> 3);
206 dst[i] = ops[i]->sym->m_dst ?
207 rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
208 (ops[i]->sym->cipher.data.offset >> 3) :
209 rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
210 (ops[i]->sym->cipher.data.offset >> 3);
211 iv_ptr = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
212 session->cipher_iv_offset);
213 iv[i] = *((uint64_t *)(iv_ptr));
214 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
219 if (processed_ops != 0)
220 sso_kasumi_f8_n_buffer(&session->pKeySched_cipher, iv,
221 src, dst, num_bytes, processed_ops);
223 return processed_ops;
226 /** Encrypt/decrypt mbuf (bit level function). */
228 process_kasumi_cipher_op_bit(struct rte_crypto_op *op,
229 struct kasumi_session *session)
234 uint32_t length_in_bits, offset_in_bits;
236 offset_in_bits = op->sym->cipher.data.offset;
237 src = rte_pktmbuf_mtod(op->sym->m_src, uint8_t *);
238 if (op->sym->m_dst == NULL) {
239 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
240 KASUMI_LOG_ERR("bit-level in-place not supported\n");
243 dst = rte_pktmbuf_mtod(op->sym->m_dst, uint8_t *);
244 iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
245 session->cipher_iv_offset);
246 iv = *((uint64_t *)(iv_ptr));
247 length_in_bits = op->sym->cipher.data.length;
249 sso_kasumi_f8_1_buffer_bit(&session->pKeySched_cipher, iv,
250 src, dst, length_in_bits, offset_in_bits);
255 /** Generate/verify hash from mbufs with same hash key. */
257 process_kasumi_hash_op(struct rte_crypto_op **ops,
258 struct kasumi_session *session,
262 uint8_t processed_ops = 0;
265 uint32_t length_in_bits;
271 for (i = 0; i < num_ops; i++) {
272 /* Data must be byte aligned */
273 if ((ops[i]->sym->auth.data.offset % BYTE_LEN) != 0) {
274 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
275 KASUMI_LOG_ERR("offset");
279 length_in_bits = ops[i]->sym->auth.data.length;
281 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
282 (ops[i]->sym->auth.data.offset >> 3);
283 iv_ptr = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
284 session->auth_iv_offset);
285 iv = *((uint64_t *)(iv_ptr));
286 /* Direction from next bit after end of message */
287 num_bytes = (length_in_bits >> 3) + 1;
288 shift_bits = (BYTE_LEN - 1 - length_in_bits) % BYTE_LEN;
289 direction = (src[num_bytes - 1] >> shift_bits) & 0x01;
291 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
292 dst = (uint8_t *)rte_pktmbuf_append(ops[i]->sym->m_src,
293 KASUMI_DIGEST_LENGTH);
295 sso_kasumi_f9_1_buffer_user(&session->pKeySched_hash,
297 length_in_bits, dst, direction);
299 if (memcmp(dst, ops[i]->sym->auth.digest.data,
300 KASUMI_DIGEST_LENGTH) != 0)
301 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
303 /* Trim area used for digest from mbuf. */
304 rte_pktmbuf_trim(ops[i]->sym->m_src,
305 KASUMI_DIGEST_LENGTH);
307 dst = ops[i]->sym->auth.digest.data;
309 sso_kasumi_f9_1_buffer_user(&session->pKeySched_hash,
311 length_in_bits, dst, direction);
316 return processed_ops;
319 /** Process a batch of crypto ops which shares the same session. */
321 process_ops(struct rte_crypto_op **ops, struct kasumi_session *session,
322 struct kasumi_qp *qp, uint8_t num_ops,
323 uint16_t *accumulated_enqueued_ops)
326 unsigned enqueued_ops, processed_ops;
328 switch (session->op) {
329 case KASUMI_OP_ONLY_CIPHER:
330 processed_ops = process_kasumi_cipher_op(ops,
333 case KASUMI_OP_ONLY_AUTH:
334 processed_ops = process_kasumi_hash_op(ops, session,
337 case KASUMI_OP_CIPHER_AUTH:
338 processed_ops = process_kasumi_cipher_op(ops, session,
340 process_kasumi_hash_op(ops, session, processed_ops);
342 case KASUMI_OP_AUTH_CIPHER:
343 processed_ops = process_kasumi_hash_op(ops, session,
345 process_kasumi_cipher_op(ops, session, processed_ops);
348 /* Operation not supported. */
352 for (i = 0; i < num_ops; i++) {
354 * If there was no error/authentication failure,
355 * change status to successful.
357 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
358 ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
359 /* Free session if a session-less crypto op. */
360 if (ops[i]->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
361 rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
362 ops[i]->sym->session = NULL;
366 enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
367 (void **)ops, processed_ops, NULL);
368 qp->qp_stats.enqueued_count += enqueued_ops;
369 *accumulated_enqueued_ops += enqueued_ops;
374 /** Process a crypto op with length/offset in bits. */
376 process_op_bit(struct rte_crypto_op *op, struct kasumi_session *session,
377 struct kasumi_qp *qp, uint16_t *accumulated_enqueued_ops)
379 unsigned enqueued_op, processed_op;
381 switch (session->op) {
382 case KASUMI_OP_ONLY_CIPHER:
383 processed_op = process_kasumi_cipher_op_bit(op,
386 case KASUMI_OP_ONLY_AUTH:
387 processed_op = process_kasumi_hash_op(&op, session, 1);
389 case KASUMI_OP_CIPHER_AUTH:
390 processed_op = process_kasumi_cipher_op_bit(op, session);
391 if (processed_op == 1)
392 process_kasumi_hash_op(&op, session, 1);
394 case KASUMI_OP_AUTH_CIPHER:
395 processed_op = process_kasumi_hash_op(&op, session, 1);
396 if (processed_op == 1)
397 process_kasumi_cipher_op_bit(op, session);
400 /* Operation not supported. */
405 * If there was no error/authentication failure,
406 * change status to successful.
408 if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
409 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
411 /* Free session if a session-less crypto op. */
412 if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
413 rte_mempool_put(qp->sess_mp, op->sym->session);
414 op->sym->session = NULL;
417 enqueued_op = rte_ring_enqueue_burst(qp->processed_ops, (void **)&op,
419 qp->qp_stats.enqueued_count += enqueued_op;
420 *accumulated_enqueued_ops += enqueued_op;
426 kasumi_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
429 struct rte_crypto_op *c_ops[nb_ops];
430 struct rte_crypto_op *curr_c_op;
432 struct kasumi_session *prev_sess = NULL, *curr_sess = NULL;
433 struct kasumi_qp *qp = queue_pair;
435 uint8_t burst_size = 0;
436 uint16_t enqueued_ops = 0;
437 uint8_t processed_ops;
439 for (i = 0; i < nb_ops; i++) {
442 #ifdef RTE_LIBRTE_PMD_KASUMI_DEBUG
443 if (!rte_pktmbuf_is_contiguous(curr_c_op->sym->m_src) ||
444 (curr_c_op->sym->m_dst != NULL &&
445 !rte_pktmbuf_is_contiguous(
446 curr_c_op->sym->m_dst))) {
447 KASUMI_LOG_ERR("PMD supports only contiguous mbufs, "
448 "op (%p) provides noncontiguous mbuf as "
449 "source/destination buffer.\n", curr_c_op);
450 curr_c_op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
455 /* Set status as enqueued (not processed yet) by default. */
456 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
458 curr_sess = kasumi_get_session(qp, curr_c_op);
459 if (unlikely(curr_sess == NULL ||
460 curr_sess->op == KASUMI_OP_NOT_SUPPORTED)) {
462 RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
466 /* If length/offset is at bit-level, process this buffer alone. */
467 if (((curr_c_op->sym->cipher.data.length % BYTE_LEN) != 0)
468 || ((ops[i]->sym->cipher.data.offset
470 /* Process the ops of the previous session. */
471 if (prev_sess != NULL) {
472 processed_ops = process_ops(c_ops, prev_sess,
473 qp, burst_size, &enqueued_ops);
474 if (processed_ops < burst_size) {
483 processed_ops = process_op_bit(curr_c_op, curr_sess,
485 if (processed_ops != 1)
491 /* Batch ops that share the same session. */
492 if (prev_sess == NULL) {
493 prev_sess = curr_sess;
494 c_ops[burst_size++] = curr_c_op;
495 } else if (curr_sess == prev_sess) {
496 c_ops[burst_size++] = curr_c_op;
498 * When there are enough ops to process in a batch,
499 * process them, and start a new batch.
501 if (burst_size == KASUMI_MAX_BURST) {
502 processed_ops = process_ops(c_ops, prev_sess,
503 qp, burst_size, &enqueued_ops);
504 if (processed_ops < burst_size) {
514 * Different session, process the ops
515 * of the previous session.
517 processed_ops = process_ops(c_ops, prev_sess,
518 qp, burst_size, &enqueued_ops);
519 if (processed_ops < burst_size) {
525 prev_sess = curr_sess;
527 c_ops[burst_size++] = curr_c_op;
531 if (burst_size != 0) {
532 /* Process the crypto ops of the last session. */
533 processed_ops = process_ops(c_ops, prev_sess,
534 qp, burst_size, &enqueued_ops);
537 qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
542 kasumi_pmd_dequeue_burst(void *queue_pair,
543 struct rte_crypto_op **c_ops, uint16_t nb_ops)
545 struct kasumi_qp *qp = queue_pair;
547 unsigned nb_dequeued;
549 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
550 (void **)c_ops, nb_ops, NULL);
551 qp->qp_stats.dequeued_count += nb_dequeued;
556 static int cryptodev_kasumi_remove(struct rte_vdev_device *vdev);
559 cryptodev_kasumi_create(const char *name,
560 struct rte_vdev_device *vdev,
561 struct rte_crypto_vdev_init_params *init_params)
563 struct rte_cryptodev *dev;
564 struct kasumi_private *internals;
565 uint64_t cpu_flags = 0;
567 if (init_params->name[0] == '\0')
568 snprintf(init_params->name, sizeof(init_params->name),
571 /* Check CPU for supported vector instruction set */
572 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX))
573 cpu_flags |= RTE_CRYPTODEV_FF_CPU_AVX;
575 cpu_flags |= RTE_CRYPTODEV_FF_CPU_SSE;
577 dev = rte_cryptodev_vdev_pmd_init(init_params->name,
578 sizeof(struct kasumi_private), init_params->socket_id,
581 KASUMI_LOG_ERR("failed to create cryptodev vdev");
585 dev->driver_id = cryptodev_driver_id;
586 dev->dev_ops = rte_kasumi_pmd_ops;
588 /* Register RX/TX burst functions for data path. */
589 dev->dequeue_burst = kasumi_pmd_dequeue_burst;
590 dev->enqueue_burst = kasumi_pmd_enqueue_burst;
592 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
593 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
596 internals = dev->data->dev_private;
598 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
599 internals->max_nb_sessions = init_params->max_nb_sessions;
603 KASUMI_LOG_ERR("driver %s: cryptodev_kasumi_create failed",
606 cryptodev_kasumi_remove(vdev);
611 cryptodev_kasumi_probe(struct rte_vdev_device *vdev)
613 struct rte_crypto_vdev_init_params init_params = {
614 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
615 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
620 const char *input_args;
622 name = rte_vdev_device_name(vdev);
625 input_args = rte_vdev_device_args(vdev);
627 rte_cryptodev_vdev_parse_init_params(&init_params, input_args);
629 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
630 init_params.socket_id);
631 if (init_params.name[0] != '\0')
632 RTE_LOG(INFO, PMD, " User defined name = %s\n",
634 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
635 init_params.max_nb_queue_pairs);
636 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
637 init_params.max_nb_sessions);
639 return cryptodev_kasumi_create(name, vdev, &init_params);
643 cryptodev_kasumi_remove(struct rte_vdev_device *vdev)
647 name = rte_vdev_device_name(vdev);
651 RTE_LOG(INFO, PMD, "Closing KASUMI crypto device %s"
652 " on numa socket %u\n",
653 name, rte_socket_id());
658 static struct rte_vdev_driver cryptodev_kasumi_pmd_drv = {
659 .probe = cryptodev_kasumi_probe,
660 .remove = cryptodev_kasumi_remove
663 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_KASUMI_PMD, cryptodev_kasumi_pmd_drv);
664 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_KASUMI_PMD, cryptodev_kasumi_pmd);
665 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_KASUMI_PMD,
666 "max_nb_queue_pairs=<int> "
667 "max_nb_sessions=<int> "
669 RTE_PMD_REGISTER_CRYPTO_DRIVER(cryptodev_kasumi_pmd_drv, cryptodev_driver_id);