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_kasumi_pmd_private.h"
43 #define KASUMI_KEY_LENGTH 16
44 #define KASUMI_IV_LENGTH 8
45 #define KASUMI_MAX_BURST 4
48 static uint8_t cryptodev_driver_id;
50 /** Get xform chain order. */
51 static enum kasumi_operation
52 kasumi_get_mode(const struct rte_crypto_sym_xform *xform)
55 return KASUMI_OP_NOT_SUPPORTED;
58 if (xform->next->next != NULL)
59 return KASUMI_OP_NOT_SUPPORTED;
61 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
62 if (xform->next == NULL)
63 return KASUMI_OP_ONLY_AUTH;
64 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
65 return KASUMI_OP_AUTH_CIPHER;
67 return KASUMI_OP_NOT_SUPPORTED;
70 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
71 if (xform->next == NULL)
72 return KASUMI_OP_ONLY_CIPHER;
73 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
74 return KASUMI_OP_CIPHER_AUTH;
76 return KASUMI_OP_NOT_SUPPORTED;
79 return KASUMI_OP_NOT_SUPPORTED;
83 /** Parse crypto xform chain and set private session parameters. */
85 kasumi_set_session_parameters(struct kasumi_session *sess,
86 const struct rte_crypto_sym_xform *xform)
88 const struct rte_crypto_sym_xform *auth_xform = NULL;
89 const struct rte_crypto_sym_xform *cipher_xform = NULL;
90 enum kasumi_operation mode;
92 /* Select Crypto operation - hash then cipher / cipher then hash */
93 mode = kasumi_get_mode(xform);
96 case KASUMI_OP_CIPHER_AUTH:
97 auth_xform = xform->next;
99 case KASUMI_OP_ONLY_CIPHER:
100 cipher_xform = xform;
102 case KASUMI_OP_AUTH_CIPHER:
103 cipher_xform = xform->next;
105 case KASUMI_OP_ONLY_AUTH:
108 case KASUMI_OP_NOT_SUPPORTED:
110 KASUMI_LOG_ERR("Unsupported operation chain order parameter");
115 /* Only KASUMI F8 supported */
116 if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_KASUMI_F8)
119 sess->cipher_iv_offset = cipher_xform->cipher.iv.offset;
120 if (cipher_xform->cipher.iv.length != KASUMI_IV_LENGTH) {
121 KASUMI_LOG_ERR("Wrong IV length");
126 sso_kasumi_init_f8_key_sched(cipher_xform->cipher.key.data,
127 &sess->pKeySched_cipher);
131 /* Only KASUMI F9 supported */
132 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_KASUMI_F9)
135 if (auth_xform->auth.digest_length != KASUMI_DIGEST_LENGTH) {
136 KASUMI_LOG_ERR("Wrong digest length");
140 sess->auth_op = auth_xform->auth.op;
143 sso_kasumi_init_f9_key_sched(auth_xform->auth.key.data,
144 &sess->pKeySched_hash);
153 /** Get KASUMI session. */
154 static struct kasumi_session *
155 kasumi_get_session(struct kasumi_qp *qp, struct rte_crypto_op *op)
157 struct kasumi_session *sess = NULL;
159 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
160 if (likely(op->sym->session != NULL))
161 sess = (struct kasumi_session *)
162 get_session_private_data(
164 cryptodev_driver_id);
167 void *_sess_private_data = NULL;
169 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
172 if (rte_mempool_get(qp->sess_mp, (void **)&_sess_private_data))
175 sess = (struct kasumi_session *)_sess_private_data;
177 if (unlikely(kasumi_set_session_parameters(sess,
178 op->sym->xform) != 0)) {
179 rte_mempool_put(qp->sess_mp, _sess);
180 rte_mempool_put(qp->sess_mp, _sess_private_data);
183 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
184 set_session_private_data(op->sym->session, cryptodev_driver_id,
188 if (unlikely(sess == NULL))
189 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
194 /** Encrypt/decrypt mbufs with same cipher key. */
196 process_kasumi_cipher_op(struct rte_crypto_op **ops,
197 struct kasumi_session *session,
201 uint8_t processed_ops = 0;
202 uint8_t *src[num_ops], *dst[num_ops];
204 uint64_t iv[num_ops];
205 uint32_t num_bytes[num_ops];
207 for (i = 0; i < num_ops; i++) {
208 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
209 (ops[i]->sym->cipher.data.offset >> 3);
210 dst[i] = ops[i]->sym->m_dst ?
211 rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
212 (ops[i]->sym->cipher.data.offset >> 3) :
213 rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
214 (ops[i]->sym->cipher.data.offset >> 3);
215 iv_ptr = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
216 session->cipher_iv_offset);
217 iv[i] = *((uint64_t *)(iv_ptr));
218 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
223 if (processed_ops != 0)
224 sso_kasumi_f8_n_buffer(&session->pKeySched_cipher, iv,
225 src, dst, num_bytes, processed_ops);
227 return processed_ops;
230 /** Encrypt/decrypt mbuf (bit level function). */
232 process_kasumi_cipher_op_bit(struct rte_crypto_op *op,
233 struct kasumi_session *session)
238 uint32_t length_in_bits, offset_in_bits;
240 offset_in_bits = op->sym->cipher.data.offset;
241 src = rte_pktmbuf_mtod(op->sym->m_src, uint8_t *);
242 if (op->sym->m_dst == NULL) {
243 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
244 KASUMI_LOG_ERR("bit-level in-place not supported\n");
247 dst = rte_pktmbuf_mtod(op->sym->m_dst, uint8_t *);
248 iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
249 session->cipher_iv_offset);
250 iv = *((uint64_t *)(iv_ptr));
251 length_in_bits = op->sym->cipher.data.length;
253 sso_kasumi_f8_1_buffer_bit(&session->pKeySched_cipher, iv,
254 src, dst, length_in_bits, offset_in_bits);
259 /** Generate/verify hash from mbufs with same hash key. */
261 process_kasumi_hash_op(struct kasumi_qp *qp, struct rte_crypto_op **ops,
262 struct kasumi_session *session,
266 uint8_t processed_ops = 0;
268 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 /* Direction from next bit after end of message */
284 num_bytes = length_in_bits >> 3;
286 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
287 dst = qp->temp_digest;
288 sso_kasumi_f9_1_buffer(&session->pKeySched_hash, src,
292 if (memcmp(dst, ops[i]->sym->auth.digest.data,
293 KASUMI_DIGEST_LENGTH) != 0)
294 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
296 dst = ops[i]->sym->auth.digest.data;
298 sso_kasumi_f9_1_buffer(&session->pKeySched_hash, src,
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 kasumi_session *session,
310 struct kasumi_qp *qp, uint8_t num_ops,
311 uint16_t *accumulated_enqueued_ops)
314 unsigned enqueued_ops, processed_ops;
316 switch (session->op) {
317 case KASUMI_OP_ONLY_CIPHER:
318 processed_ops = process_kasumi_cipher_op(ops,
321 case KASUMI_OP_ONLY_AUTH:
322 processed_ops = process_kasumi_hash_op(qp, ops, session,
325 case KASUMI_OP_CIPHER_AUTH:
326 processed_ops = process_kasumi_cipher_op(ops, session,
328 process_kasumi_hash_op(qp, ops, session, processed_ops);
330 case KASUMI_OP_AUTH_CIPHER:
331 processed_ops = process_kasumi_hash_op(qp, ops, session,
333 process_kasumi_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 kasumi_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;
366 /** Process a crypto op with length/offset in bits. */
368 process_op_bit(struct rte_crypto_op *op, struct kasumi_session *session,
369 struct kasumi_qp *qp, uint16_t *accumulated_enqueued_ops)
371 unsigned enqueued_op, processed_op;
373 switch (session->op) {
374 case KASUMI_OP_ONLY_CIPHER:
375 processed_op = process_kasumi_cipher_op_bit(op,
378 case KASUMI_OP_ONLY_AUTH:
379 processed_op = process_kasumi_hash_op(qp, &op, session, 1);
381 case KASUMI_OP_CIPHER_AUTH:
382 processed_op = process_kasumi_cipher_op_bit(op, session);
383 if (processed_op == 1)
384 process_kasumi_hash_op(qp, &op, session, 1);
386 case KASUMI_OP_AUTH_CIPHER:
387 processed_op = process_kasumi_hash_op(qp, &op, session, 1);
388 if (processed_op == 1)
389 process_kasumi_cipher_op_bit(op, session);
392 /* Operation not supported. */
397 * If there was no error/authentication failure,
398 * change status to successful.
400 if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
401 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
403 /* Free session if a session-less crypto op. */
404 if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
405 memset(op->sym->session, 0, sizeof(struct kasumi_session));
406 rte_cryptodev_sym_session_free(op->sym->session);
407 op->sym->session = NULL;
410 enqueued_op = rte_ring_enqueue_burst(qp->processed_ops, (void **)&op,
412 qp->qp_stats.enqueued_count += enqueued_op;
413 *accumulated_enqueued_ops += enqueued_op;
419 kasumi_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
422 struct rte_crypto_op *c_ops[nb_ops];
423 struct rte_crypto_op *curr_c_op;
425 struct kasumi_session *prev_sess = NULL, *curr_sess = NULL;
426 struct kasumi_qp *qp = queue_pair;
428 uint8_t burst_size = 0;
429 uint16_t enqueued_ops = 0;
430 uint8_t processed_ops;
432 for (i = 0; i < nb_ops; i++) {
435 #ifdef RTE_LIBRTE_PMD_KASUMI_DEBUG
436 if (!rte_pktmbuf_is_contiguous(curr_c_op->sym->m_src) ||
437 (curr_c_op->sym->m_dst != NULL &&
438 !rte_pktmbuf_is_contiguous(
439 curr_c_op->sym->m_dst))) {
440 KASUMI_LOG_ERR("PMD supports only contiguous mbufs, "
441 "op (%p) provides noncontiguous mbuf as "
442 "source/destination buffer.\n", curr_c_op);
443 curr_c_op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
448 /* Set status as enqueued (not processed yet) by default. */
449 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
451 curr_sess = kasumi_get_session(qp, curr_c_op);
452 if (unlikely(curr_sess == NULL ||
453 curr_sess->op == KASUMI_OP_NOT_SUPPORTED)) {
455 RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
459 /* If length/offset is at bit-level, process this buffer alone. */
460 if (((curr_c_op->sym->cipher.data.length % BYTE_LEN) != 0)
461 || ((ops[i]->sym->cipher.data.offset
463 /* Process the ops of the previous session. */
464 if (prev_sess != NULL) {
465 processed_ops = process_ops(c_ops, prev_sess,
466 qp, burst_size, &enqueued_ops);
467 if (processed_ops < burst_size) {
476 processed_ops = process_op_bit(curr_c_op, curr_sess,
478 if (processed_ops != 1)
484 /* Batch ops that share the same session. */
485 if (prev_sess == NULL) {
486 prev_sess = curr_sess;
487 c_ops[burst_size++] = curr_c_op;
488 } else if (curr_sess == prev_sess) {
489 c_ops[burst_size++] = curr_c_op;
491 * When there are enough ops to process in a batch,
492 * process them, and start a new batch.
494 if (burst_size == KASUMI_MAX_BURST) {
495 processed_ops = process_ops(c_ops, prev_sess,
496 qp, burst_size, &enqueued_ops);
497 if (processed_ops < burst_size) {
507 * Different session, process the ops
508 * of the previous session.
510 processed_ops = process_ops(c_ops, prev_sess,
511 qp, burst_size, &enqueued_ops);
512 if (processed_ops < burst_size) {
518 prev_sess = curr_sess;
520 c_ops[burst_size++] = curr_c_op;
524 if (burst_size != 0) {
525 /* Process the crypto ops of the last session. */
526 processed_ops = process_ops(c_ops, prev_sess,
527 qp, burst_size, &enqueued_ops);
530 qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
535 kasumi_pmd_dequeue_burst(void *queue_pair,
536 struct rte_crypto_op **c_ops, uint16_t nb_ops)
538 struct kasumi_qp *qp = queue_pair;
540 unsigned nb_dequeued;
542 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
543 (void **)c_ops, nb_ops, NULL);
544 qp->qp_stats.dequeued_count += nb_dequeued;
549 static int cryptodev_kasumi_remove(struct rte_vdev_device *vdev);
552 cryptodev_kasumi_create(const char *name,
553 struct rte_vdev_device *vdev,
554 struct rte_cryptodev_pmd_init_params *init_params)
556 struct rte_cryptodev *dev;
557 struct kasumi_private *internals;
558 uint64_t cpu_flags = 0;
560 dev = rte_cryptodev_pmd_create(name, &vdev->device, init_params);
562 KASUMI_LOG_ERR("failed to create cryptodev vdev");
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->driver_id = cryptodev_driver_id;
573 dev->dev_ops = rte_kasumi_pmd_ops;
575 /* Register RX/TX burst functions for data path. */
576 dev->dequeue_burst = kasumi_pmd_dequeue_burst;
577 dev->enqueue_burst = kasumi_pmd_enqueue_burst;
579 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
580 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
583 internals = dev->data->dev_private;
585 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
586 internals->max_nb_sessions = init_params->max_nb_sessions;
590 KASUMI_LOG_ERR("driver %s: cryptodev_kasumi_create failed",
593 cryptodev_kasumi_remove(vdev);
598 cryptodev_kasumi_probe(struct rte_vdev_device *vdev)
600 struct rte_cryptodev_pmd_init_params init_params = {
602 sizeof(struct kasumi_private),
604 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS,
605 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_SESSIONS
608 const char *input_args;
610 name = rte_vdev_device_name(vdev);
613 input_args = rte_vdev_device_args(vdev);
615 rte_cryptodev_pmd_parse_input_args(&init_params, input_args);
617 return cryptodev_kasumi_create(name, vdev, &init_params);
621 cryptodev_kasumi_remove(struct rte_vdev_device *vdev)
623 struct rte_cryptodev *cryptodev;
626 name = rte_vdev_device_name(vdev);
630 cryptodev = rte_cryptodev_pmd_get_named_dev(name);
631 if (cryptodev == NULL)
634 return rte_cryptodev_pmd_destroy(cryptodev);
637 static struct rte_vdev_driver cryptodev_kasumi_pmd_drv = {
638 .probe = cryptodev_kasumi_probe,
639 .remove = cryptodev_kasumi_remove
642 static struct cryptodev_driver kasumi_crypto_drv;
644 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_KASUMI_PMD, cryptodev_kasumi_pmd_drv);
645 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_KASUMI_PMD, cryptodev_kasumi_pmd);
646 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_KASUMI_PMD,
647 "max_nb_queue_pairs=<int> "
648 "max_nb_sessions=<int> "
650 RTE_PMD_REGISTER_CRYPTO_DRIVER(kasumi_crypto_drv, cryptodev_kasumi_pmd_drv,
651 cryptodev_driver_id);