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_MAX_BURST 4
50 static uint8_t cryptodev_driver_id;
52 /** Get xform chain order. */
53 static enum kasumi_operation
54 kasumi_get_mode(const struct rte_crypto_sym_xform *xform)
57 return KASUMI_OP_NOT_SUPPORTED;
60 if (xform->next->next != NULL)
61 return KASUMI_OP_NOT_SUPPORTED;
63 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
64 if (xform->next == NULL)
65 return KASUMI_OP_ONLY_AUTH;
66 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
67 return KASUMI_OP_AUTH_CIPHER;
69 return KASUMI_OP_NOT_SUPPORTED;
72 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
73 if (xform->next == NULL)
74 return KASUMI_OP_ONLY_CIPHER;
75 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
76 return KASUMI_OP_CIPHER_AUTH;
78 return KASUMI_OP_NOT_SUPPORTED;
81 return KASUMI_OP_NOT_SUPPORTED;
85 /** Parse crypto xform chain and set private session parameters. */
87 kasumi_set_session_parameters(struct kasumi_session *sess,
88 const struct rte_crypto_sym_xform *xform)
90 const struct rte_crypto_sym_xform *auth_xform = NULL;
91 const struct rte_crypto_sym_xform *cipher_xform = NULL;
92 enum kasumi_operation mode;
94 /* Select Crypto operation - hash then cipher / cipher then hash */
95 mode = kasumi_get_mode(xform);
98 case KASUMI_OP_CIPHER_AUTH:
99 auth_xform = xform->next;
101 case KASUMI_OP_ONLY_CIPHER:
102 cipher_xform = xform;
104 case KASUMI_OP_AUTH_CIPHER:
105 cipher_xform = xform->next;
107 case KASUMI_OP_ONLY_AUTH:
110 case KASUMI_OP_NOT_SUPPORTED:
112 KASUMI_LOG_ERR("Unsupported operation chain order parameter");
117 /* Only KASUMI F8 supported */
118 if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_KASUMI_F8)
121 sess->cipher_iv_offset = cipher_xform->cipher.iv.offset;
122 if (cipher_xform->cipher.iv.length != KASUMI_IV_LENGTH) {
123 KASUMI_LOG_ERR("Wrong IV length");
128 sso_kasumi_init_f8_key_sched(cipher_xform->cipher.key.data,
129 &sess->pKeySched_cipher);
133 /* Only KASUMI F9 supported */
134 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_KASUMI_F9)
137 if (auth_xform->auth.digest_length != KASUMI_DIGEST_LENGTH) {
138 KASUMI_LOG_ERR("Wrong digest length");
142 sess->auth_op = auth_xform->auth.op;
145 sso_kasumi_init_f9_key_sched(auth_xform->auth.key.data,
146 &sess->pKeySched_hash);
155 /** Get KASUMI session. */
156 static struct kasumi_session *
157 kasumi_get_session(struct kasumi_qp *qp, struct rte_crypto_op *op)
159 struct kasumi_session *sess = NULL;
161 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
162 if (likely(op->sym->session != NULL))
163 sess = (struct kasumi_session *)
164 get_session_private_data(
166 cryptodev_driver_id);
169 void *_sess_private_data = NULL;
171 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
174 if (rte_mempool_get(qp->sess_mp, (void **)&_sess_private_data))
177 sess = (struct kasumi_session *)_sess_private_data;
179 if (unlikely(kasumi_set_session_parameters(sess,
180 op->sym->xform) != 0)) {
181 rte_mempool_put(qp->sess_mp, _sess);
182 rte_mempool_put(qp->sess_mp, _sess_private_data);
185 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
186 set_session_private_data(op->sym->session, cryptodev_driver_id,
190 if (unlikely(sess == NULL))
191 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
196 /** Encrypt/decrypt mbufs with same cipher key. */
198 process_kasumi_cipher_op(struct rte_crypto_op **ops,
199 struct kasumi_session *session,
203 uint8_t processed_ops = 0;
204 uint8_t *src[num_ops], *dst[num_ops];
206 uint64_t iv[num_ops];
207 uint32_t num_bytes[num_ops];
209 for (i = 0; i < num_ops; i++) {
210 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
211 (ops[i]->sym->cipher.data.offset >> 3);
212 dst[i] = ops[i]->sym->m_dst ?
213 rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
214 (ops[i]->sym->cipher.data.offset >> 3) :
215 rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
216 (ops[i]->sym->cipher.data.offset >> 3);
217 iv_ptr = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
218 session->cipher_iv_offset);
219 iv[i] = *((uint64_t *)(iv_ptr));
220 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
225 if (processed_ops != 0)
226 sso_kasumi_f8_n_buffer(&session->pKeySched_cipher, iv,
227 src, dst, num_bytes, processed_ops);
229 return processed_ops;
232 /** Encrypt/decrypt mbuf (bit level function). */
234 process_kasumi_cipher_op_bit(struct rte_crypto_op *op,
235 struct kasumi_session *session)
240 uint32_t length_in_bits, offset_in_bits;
242 offset_in_bits = op->sym->cipher.data.offset;
243 src = rte_pktmbuf_mtod(op->sym->m_src, uint8_t *);
244 if (op->sym->m_dst == NULL) {
245 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
246 KASUMI_LOG_ERR("bit-level in-place not supported\n");
249 dst = rte_pktmbuf_mtod(op->sym->m_dst, uint8_t *);
250 iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
251 session->cipher_iv_offset);
252 iv = *((uint64_t *)(iv_ptr));
253 length_in_bits = op->sym->cipher.data.length;
255 sso_kasumi_f8_1_buffer_bit(&session->pKeySched_cipher, iv,
256 src, dst, length_in_bits, offset_in_bits);
261 /** Generate/verify hash from mbufs with same hash key. */
263 process_kasumi_hash_op(struct kasumi_qp *qp, struct rte_crypto_op **ops,
264 struct kasumi_session *session,
268 uint8_t processed_ops = 0;
270 uint32_t length_in_bits;
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 KASUMI_LOG_ERR("offset");
281 length_in_bits = ops[i]->sym->auth.data.length;
283 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
284 (ops[i]->sym->auth.data.offset >> 3);
285 /* Direction from next bit after end of message */
286 num_bytes = length_in_bits >> 3;
288 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
289 dst = qp->temp_digest;
290 sso_kasumi_f9_1_buffer(&session->pKeySched_hash, src,
294 if (memcmp(dst, ops[i]->sym->auth.digest.data,
295 KASUMI_DIGEST_LENGTH) != 0)
296 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
298 dst = ops[i]->sym->auth.digest.data;
300 sso_kasumi_f9_1_buffer(&session->pKeySched_hash, src,
306 return processed_ops;
309 /** Process a batch of crypto ops which shares the same session. */
311 process_ops(struct rte_crypto_op **ops, struct kasumi_session *session,
312 struct kasumi_qp *qp, uint8_t num_ops,
313 uint16_t *accumulated_enqueued_ops)
316 unsigned enqueued_ops, processed_ops;
318 switch (session->op) {
319 case KASUMI_OP_ONLY_CIPHER:
320 processed_ops = process_kasumi_cipher_op(ops,
323 case KASUMI_OP_ONLY_AUTH:
324 processed_ops = process_kasumi_hash_op(qp, ops, session,
327 case KASUMI_OP_CIPHER_AUTH:
328 processed_ops = process_kasumi_cipher_op(ops, session,
330 process_kasumi_hash_op(qp, ops, session, processed_ops);
332 case KASUMI_OP_AUTH_CIPHER:
333 processed_ops = process_kasumi_hash_op(qp, ops, session,
335 process_kasumi_cipher_op(ops, session, processed_ops);
338 /* Operation not supported. */
342 for (i = 0; i < num_ops; i++) {
344 * If there was no error/authentication failure,
345 * change status to successful.
347 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
348 ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
349 /* Free session if a session-less crypto op. */
350 if (ops[i]->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
351 memset(session, 0, sizeof(struct kasumi_session));
352 memset(ops[i]->sym->session, 0,
353 rte_cryptodev_get_header_session_size());
354 rte_mempool_put(qp->sess_mp, session);
355 rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
356 ops[i]->sym->session = NULL;
360 enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
361 (void **)ops, processed_ops, NULL);
362 qp->qp_stats.enqueued_count += enqueued_ops;
363 *accumulated_enqueued_ops += enqueued_ops;
368 /** Process a crypto op with length/offset in bits. */
370 process_op_bit(struct rte_crypto_op *op, struct kasumi_session *session,
371 struct kasumi_qp *qp, uint16_t *accumulated_enqueued_ops)
373 unsigned enqueued_op, processed_op;
375 switch (session->op) {
376 case KASUMI_OP_ONLY_CIPHER:
377 processed_op = process_kasumi_cipher_op_bit(op,
380 case KASUMI_OP_ONLY_AUTH:
381 processed_op = process_kasumi_hash_op(qp, &op, session, 1);
383 case KASUMI_OP_CIPHER_AUTH:
384 processed_op = process_kasumi_cipher_op_bit(op, session);
385 if (processed_op == 1)
386 process_kasumi_hash_op(qp, &op, session, 1);
388 case KASUMI_OP_AUTH_CIPHER:
389 processed_op = process_kasumi_hash_op(qp, &op, session, 1);
390 if (processed_op == 1)
391 process_kasumi_cipher_op_bit(op, session);
394 /* Operation not supported. */
399 * If there was no error/authentication failure,
400 * change status to successful.
402 if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
403 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
405 /* Free session if a session-less crypto op. */
406 if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
407 memset(op->sym->session, 0, sizeof(struct kasumi_session));
408 rte_cryptodev_sym_session_free(op->sym->session);
409 op->sym->session = NULL;
412 enqueued_op = rte_ring_enqueue_burst(qp->processed_ops, (void **)&op,
414 qp->qp_stats.enqueued_count += enqueued_op;
415 *accumulated_enqueued_ops += enqueued_op;
421 kasumi_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
424 struct rte_crypto_op *c_ops[nb_ops];
425 struct rte_crypto_op *curr_c_op;
427 struct kasumi_session *prev_sess = NULL, *curr_sess = NULL;
428 struct kasumi_qp *qp = queue_pair;
430 uint8_t burst_size = 0;
431 uint16_t enqueued_ops = 0;
432 uint8_t processed_ops;
434 for (i = 0; i < nb_ops; i++) {
437 #ifdef RTE_LIBRTE_PMD_KASUMI_DEBUG
438 if (!rte_pktmbuf_is_contiguous(curr_c_op->sym->m_src) ||
439 (curr_c_op->sym->m_dst != NULL &&
440 !rte_pktmbuf_is_contiguous(
441 curr_c_op->sym->m_dst))) {
442 KASUMI_LOG_ERR("PMD supports only contiguous mbufs, "
443 "op (%p) provides noncontiguous mbuf as "
444 "source/destination buffer.\n", curr_c_op);
445 curr_c_op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
450 /* Set status as enqueued (not processed yet) by default. */
451 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
453 curr_sess = kasumi_get_session(qp, curr_c_op);
454 if (unlikely(curr_sess == NULL ||
455 curr_sess->op == KASUMI_OP_NOT_SUPPORTED)) {
457 RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
461 /* If length/offset is at bit-level, process this buffer alone. */
462 if (((curr_c_op->sym->cipher.data.length % BYTE_LEN) != 0)
463 || ((ops[i]->sym->cipher.data.offset
465 /* Process the ops of the previous session. */
466 if (prev_sess != NULL) {
467 processed_ops = process_ops(c_ops, prev_sess,
468 qp, burst_size, &enqueued_ops);
469 if (processed_ops < burst_size) {
478 processed_ops = process_op_bit(curr_c_op, curr_sess,
480 if (processed_ops != 1)
486 /* Batch ops that share the same session. */
487 if (prev_sess == NULL) {
488 prev_sess = curr_sess;
489 c_ops[burst_size++] = curr_c_op;
490 } else if (curr_sess == prev_sess) {
491 c_ops[burst_size++] = curr_c_op;
493 * When there are enough ops to process in a batch,
494 * process them, and start a new batch.
496 if (burst_size == KASUMI_MAX_BURST) {
497 processed_ops = process_ops(c_ops, prev_sess,
498 qp, burst_size, &enqueued_ops);
499 if (processed_ops < burst_size) {
509 * Different session, process the ops
510 * of the previous session.
512 processed_ops = process_ops(c_ops, prev_sess,
513 qp, burst_size, &enqueued_ops);
514 if (processed_ops < burst_size) {
520 prev_sess = curr_sess;
522 c_ops[burst_size++] = curr_c_op;
526 if (burst_size != 0) {
527 /* Process the crypto ops of the last session. */
528 processed_ops = process_ops(c_ops, prev_sess,
529 qp, burst_size, &enqueued_ops);
532 qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
537 kasumi_pmd_dequeue_burst(void *queue_pair,
538 struct rte_crypto_op **c_ops, uint16_t nb_ops)
540 struct kasumi_qp *qp = queue_pair;
542 unsigned nb_dequeued;
544 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
545 (void **)c_ops, nb_ops, NULL);
546 qp->qp_stats.dequeued_count += nb_dequeued;
551 static int cryptodev_kasumi_remove(struct rte_vdev_device *vdev);
554 cryptodev_kasumi_create(const char *name,
555 struct rte_vdev_device *vdev,
556 struct rte_crypto_vdev_init_params *init_params)
558 struct rte_cryptodev *dev;
559 struct kasumi_private *internals;
560 uint64_t cpu_flags = 0;
562 if (init_params->name[0] == '\0')
563 snprintf(init_params->name, sizeof(init_params->name),
566 /* Check CPU for supported vector instruction set */
567 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX))
568 cpu_flags |= RTE_CRYPTODEV_FF_CPU_AVX;
570 cpu_flags |= RTE_CRYPTODEV_FF_CPU_SSE;
572 dev = rte_cryptodev_vdev_pmd_init(init_params->name,
573 sizeof(struct kasumi_private), init_params->socket_id,
576 KASUMI_LOG_ERR("failed to create cryptodev vdev");
580 dev->driver_id = cryptodev_driver_id;
581 dev->dev_ops = rte_kasumi_pmd_ops;
583 /* Register RX/TX burst functions for data path. */
584 dev->dequeue_burst = kasumi_pmd_dequeue_burst;
585 dev->enqueue_burst = kasumi_pmd_enqueue_burst;
587 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
588 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
591 internals = dev->data->dev_private;
593 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
594 internals->max_nb_sessions = init_params->max_nb_sessions;
598 KASUMI_LOG_ERR("driver %s: cryptodev_kasumi_create failed",
601 cryptodev_kasumi_remove(vdev);
606 cryptodev_kasumi_probe(struct rte_vdev_device *vdev)
608 struct rte_crypto_vdev_init_params init_params = {
609 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
610 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
615 const char *input_args;
617 name = rte_vdev_device_name(vdev);
620 input_args = rte_vdev_device_args(vdev);
622 rte_cryptodev_vdev_parse_init_params(&init_params, input_args);
624 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
625 init_params.socket_id);
626 if (init_params.name[0] != '\0')
627 RTE_LOG(INFO, PMD, " User defined name = %s\n",
629 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
630 init_params.max_nb_queue_pairs);
631 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
632 init_params.max_nb_sessions);
634 return cryptodev_kasumi_create(name, vdev, &init_params);
638 cryptodev_kasumi_remove(struct rte_vdev_device *vdev)
642 name = rte_vdev_device_name(vdev);
646 RTE_LOG(INFO, PMD, "Closing KASUMI crypto device %s"
647 " on numa socket %u\n",
648 name, rte_socket_id());
653 static struct rte_vdev_driver cryptodev_kasumi_pmd_drv = {
654 .probe = cryptodev_kasumi_probe,
655 .remove = cryptodev_kasumi_remove
658 static struct cryptodev_driver kasumi_crypto_drv;
660 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_KASUMI_PMD, cryptodev_kasumi_pmd_drv);
661 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_KASUMI_PMD, cryptodev_kasumi_pmd);
662 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_KASUMI_PMD,
663 "max_nb_queue_pairs=<int> "
664 "max_nb_sessions=<int> "
666 RTE_PMD_REGISTER_CRYPTO_DRIVER(kasumi_crypto_drv, cryptodev_kasumi_pmd_drv,
667 cryptodev_driver_id);