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>
39 #include <rte_malloc.h>
40 #include <rte_cpuflags.h>
42 #include "rte_kasumi_pmd_private.h"
44 #define KASUMI_KEY_LENGTH 16
45 #define KASUMI_IV_LENGTH 8
46 #define KASUMI_MAX_BURST 4
49 static uint8_t cryptodev_driver_id;
51 /** Get xform chain order. */
52 static enum kasumi_operation
53 kasumi_get_mode(const struct rte_crypto_sym_xform *xform)
56 return KASUMI_OP_NOT_SUPPORTED;
59 if (xform->next->next != NULL)
60 return KASUMI_OP_NOT_SUPPORTED;
62 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
63 if (xform->next == NULL)
64 return KASUMI_OP_ONLY_AUTH;
65 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
66 return KASUMI_OP_AUTH_CIPHER;
68 return KASUMI_OP_NOT_SUPPORTED;
71 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
72 if (xform->next == NULL)
73 return KASUMI_OP_ONLY_CIPHER;
74 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
75 return KASUMI_OP_CIPHER_AUTH;
77 return KASUMI_OP_NOT_SUPPORTED;
80 return KASUMI_OP_NOT_SUPPORTED;
84 /** Parse crypto xform chain and set private session parameters. */
86 kasumi_set_session_parameters(struct kasumi_session *sess,
87 const struct rte_crypto_sym_xform *xform)
89 const struct rte_crypto_sym_xform *auth_xform = NULL;
90 const struct rte_crypto_sym_xform *cipher_xform = NULL;
91 enum kasumi_operation mode;
93 /* Select Crypto operation - hash then cipher / cipher then hash */
94 mode = kasumi_get_mode(xform);
97 case KASUMI_OP_CIPHER_AUTH:
98 auth_xform = xform->next;
100 case KASUMI_OP_ONLY_CIPHER:
101 cipher_xform = xform;
103 case KASUMI_OP_AUTH_CIPHER:
104 cipher_xform = xform->next;
106 case KASUMI_OP_ONLY_AUTH:
109 case KASUMI_OP_NOT_SUPPORTED:
111 KASUMI_LOG_ERR("Unsupported operation chain order parameter");
116 /* Only KASUMI F8 supported */
117 if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_KASUMI_F8)
120 sess->cipher_iv_offset = cipher_xform->cipher.iv.offset;
121 if (cipher_xform->cipher.iv.length != KASUMI_IV_LENGTH) {
122 KASUMI_LOG_ERR("Wrong IV length");
127 sso_kasumi_init_f8_key_sched(cipher_xform->cipher.key.data,
128 &sess->pKeySched_cipher);
132 /* Only KASUMI F9 supported */
133 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_KASUMI_F9)
136 if (auth_xform->auth.digest_length != KASUMI_DIGEST_LENGTH) {
137 KASUMI_LOG_ERR("Wrong digest length");
141 sess->auth_op = auth_xform->auth.op;
144 sso_kasumi_init_f9_key_sched(auth_xform->auth.key.data,
145 &sess->pKeySched_hash);
154 /** Get KASUMI session. */
155 static struct kasumi_session *
156 kasumi_get_session(struct kasumi_qp *qp, struct rte_crypto_op *op)
158 struct kasumi_session *sess = NULL;
160 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
161 if (likely(op->sym->session != NULL))
162 sess = (struct kasumi_session *)
163 get_session_private_data(
165 cryptodev_driver_id);
168 void *_sess_private_data = NULL;
170 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
173 if (rte_mempool_get(qp->sess_mp, (void **)&_sess_private_data))
176 sess = (struct kasumi_session *)_sess_private_data;
178 if (unlikely(kasumi_set_session_parameters(sess,
179 op->sym->xform) != 0)) {
180 rte_mempool_put(qp->sess_mp, _sess);
181 rte_mempool_put(qp->sess_mp, _sess_private_data);
184 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
185 set_session_private_data(op->sym->session, cryptodev_driver_id,
189 if (unlikely(sess == NULL))
190 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
195 /** Encrypt/decrypt mbufs with same cipher key. */
197 process_kasumi_cipher_op(struct rte_crypto_op **ops,
198 struct kasumi_session *session,
202 uint8_t processed_ops = 0;
203 uint8_t *src[num_ops], *dst[num_ops];
205 uint64_t iv[num_ops];
206 uint32_t num_bytes[num_ops];
208 for (i = 0; i < num_ops; i++) {
209 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
210 (ops[i]->sym->cipher.data.offset >> 3);
211 dst[i] = ops[i]->sym->m_dst ?
212 rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
213 (ops[i]->sym->cipher.data.offset >> 3) :
214 rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
215 (ops[i]->sym->cipher.data.offset >> 3);
216 iv_ptr = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
217 session->cipher_iv_offset);
218 iv[i] = *((uint64_t *)(iv_ptr));
219 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
224 if (processed_ops != 0)
225 sso_kasumi_f8_n_buffer(&session->pKeySched_cipher, iv,
226 src, dst, num_bytes, processed_ops);
228 return processed_ops;
231 /** Encrypt/decrypt mbuf (bit level function). */
233 process_kasumi_cipher_op_bit(struct rte_crypto_op *op,
234 struct kasumi_session *session)
239 uint32_t length_in_bits, offset_in_bits;
241 offset_in_bits = op->sym->cipher.data.offset;
242 src = rte_pktmbuf_mtod(op->sym->m_src, uint8_t *);
243 if (op->sym->m_dst == NULL) {
244 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
245 KASUMI_LOG_ERR("bit-level in-place not supported\n");
248 dst = rte_pktmbuf_mtod(op->sym->m_dst, uint8_t *);
249 iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
250 session->cipher_iv_offset);
251 iv = *((uint64_t *)(iv_ptr));
252 length_in_bits = op->sym->cipher.data.length;
254 sso_kasumi_f8_1_buffer_bit(&session->pKeySched_cipher, iv,
255 src, dst, length_in_bits, offset_in_bits);
260 /** Generate/verify hash from mbufs with same hash key. */
262 process_kasumi_hash_op(struct kasumi_qp *qp, struct rte_crypto_op **ops,
263 struct kasumi_session *session,
267 uint8_t processed_ops = 0;
269 uint32_t length_in_bits;
272 for (i = 0; i < num_ops; i++) {
273 /* Data must be byte aligned */
274 if ((ops[i]->sym->auth.data.offset % BYTE_LEN) != 0) {
275 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
276 KASUMI_LOG_ERR("offset");
280 length_in_bits = ops[i]->sym->auth.data.length;
282 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
283 (ops[i]->sym->auth.data.offset >> 3);
284 /* Direction from next bit after end of message */
285 num_bytes = length_in_bits >> 3;
287 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
288 dst = qp->temp_digest;
289 sso_kasumi_f9_1_buffer(&session->pKeySched_hash, src,
293 if (memcmp(dst, ops[i]->sym->auth.digest.data,
294 KASUMI_DIGEST_LENGTH) != 0)
295 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
297 dst = ops[i]->sym->auth.digest.data;
299 sso_kasumi_f9_1_buffer(&session->pKeySched_hash, src,
305 return processed_ops;
308 /** Process a batch of crypto ops which shares the same session. */
310 process_ops(struct rte_crypto_op **ops, struct kasumi_session *session,
311 struct kasumi_qp *qp, uint8_t num_ops,
312 uint16_t *accumulated_enqueued_ops)
315 unsigned enqueued_ops, processed_ops;
317 switch (session->op) {
318 case KASUMI_OP_ONLY_CIPHER:
319 processed_ops = process_kasumi_cipher_op(ops,
322 case KASUMI_OP_ONLY_AUTH:
323 processed_ops = process_kasumi_hash_op(qp, ops, session,
326 case KASUMI_OP_CIPHER_AUTH:
327 processed_ops = process_kasumi_cipher_op(ops, session,
329 process_kasumi_hash_op(qp, ops, session, processed_ops);
331 case KASUMI_OP_AUTH_CIPHER:
332 processed_ops = process_kasumi_hash_op(qp, ops, session,
334 process_kasumi_cipher_op(ops, session, processed_ops);
337 /* Operation not supported. */
341 for (i = 0; i < num_ops; i++) {
343 * If there was no error/authentication failure,
344 * change status to successful.
346 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
347 ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
348 /* Free session if a session-less crypto op. */
349 if (ops[i]->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
350 memset(session, 0, sizeof(struct kasumi_session));
351 memset(ops[i]->sym->session, 0,
352 rte_cryptodev_get_header_session_size());
353 rte_mempool_put(qp->sess_mp, session);
354 rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
355 ops[i]->sym->session = NULL;
359 enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
360 (void **)ops, processed_ops, NULL);
361 qp->qp_stats.enqueued_count += enqueued_ops;
362 *accumulated_enqueued_ops += enqueued_ops;
367 /** Process a crypto op with length/offset in bits. */
369 process_op_bit(struct rte_crypto_op *op, struct kasumi_session *session,
370 struct kasumi_qp *qp, uint16_t *accumulated_enqueued_ops)
372 unsigned enqueued_op, processed_op;
374 switch (session->op) {
375 case KASUMI_OP_ONLY_CIPHER:
376 processed_op = process_kasumi_cipher_op_bit(op,
379 case KASUMI_OP_ONLY_AUTH:
380 processed_op = process_kasumi_hash_op(qp, &op, session, 1);
382 case KASUMI_OP_CIPHER_AUTH:
383 processed_op = process_kasumi_cipher_op_bit(op, session);
384 if (processed_op == 1)
385 process_kasumi_hash_op(qp, &op, session, 1);
387 case KASUMI_OP_AUTH_CIPHER:
388 processed_op = process_kasumi_hash_op(qp, &op, session, 1);
389 if (processed_op == 1)
390 process_kasumi_cipher_op_bit(op, session);
393 /* Operation not supported. */
398 * If there was no error/authentication failure,
399 * change status to successful.
401 if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
402 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
404 /* Free session if a session-less crypto op. */
405 if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
406 memset(op->sym->session, 0, sizeof(struct kasumi_session));
407 rte_cryptodev_sym_session_free(op->sym->session);
408 op->sym->session = NULL;
411 enqueued_op = rte_ring_enqueue_burst(qp->processed_ops, (void **)&op,
413 qp->qp_stats.enqueued_count += enqueued_op;
414 *accumulated_enqueued_ops += enqueued_op;
420 kasumi_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
423 struct rte_crypto_op *c_ops[nb_ops];
424 struct rte_crypto_op *curr_c_op;
426 struct kasumi_session *prev_sess = NULL, *curr_sess = NULL;
427 struct kasumi_qp *qp = queue_pair;
429 uint8_t burst_size = 0;
430 uint16_t enqueued_ops = 0;
431 uint8_t processed_ops;
433 for (i = 0; i < nb_ops; i++) {
436 #ifdef RTE_LIBRTE_PMD_KASUMI_DEBUG
437 if (!rte_pktmbuf_is_contiguous(curr_c_op->sym->m_src) ||
438 (curr_c_op->sym->m_dst != NULL &&
439 !rte_pktmbuf_is_contiguous(
440 curr_c_op->sym->m_dst))) {
441 KASUMI_LOG_ERR("PMD supports only contiguous mbufs, "
442 "op (%p) provides noncontiguous mbuf as "
443 "source/destination buffer.\n", curr_c_op);
444 curr_c_op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
449 /* Set status as enqueued (not processed yet) by default. */
450 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
452 curr_sess = kasumi_get_session(qp, curr_c_op);
453 if (unlikely(curr_sess == NULL ||
454 curr_sess->op == KASUMI_OP_NOT_SUPPORTED)) {
456 RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
460 /* If length/offset is at bit-level, process this buffer alone. */
461 if (((curr_c_op->sym->cipher.data.length % BYTE_LEN) != 0)
462 || ((ops[i]->sym->cipher.data.offset
464 /* Process the ops of the previous session. */
465 if (prev_sess != NULL) {
466 processed_ops = process_ops(c_ops, prev_sess,
467 qp, burst_size, &enqueued_ops);
468 if (processed_ops < burst_size) {
477 processed_ops = process_op_bit(curr_c_op, curr_sess,
479 if (processed_ops != 1)
485 /* Batch ops that share the same session. */
486 if (prev_sess == NULL) {
487 prev_sess = curr_sess;
488 c_ops[burst_size++] = curr_c_op;
489 } else if (curr_sess == prev_sess) {
490 c_ops[burst_size++] = curr_c_op;
492 * When there are enough ops to process in a batch,
493 * process them, and start a new batch.
495 if (burst_size == KASUMI_MAX_BURST) {
496 processed_ops = process_ops(c_ops, prev_sess,
497 qp, burst_size, &enqueued_ops);
498 if (processed_ops < burst_size) {
508 * Different session, process the ops
509 * of the previous session.
511 processed_ops = process_ops(c_ops, prev_sess,
512 qp, burst_size, &enqueued_ops);
513 if (processed_ops < burst_size) {
519 prev_sess = curr_sess;
521 c_ops[burst_size++] = curr_c_op;
525 if (burst_size != 0) {
526 /* Process the crypto ops of the last session. */
527 processed_ops = process_ops(c_ops, prev_sess,
528 qp, burst_size, &enqueued_ops);
531 qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
536 kasumi_pmd_dequeue_burst(void *queue_pair,
537 struct rte_crypto_op **c_ops, uint16_t nb_ops)
539 struct kasumi_qp *qp = queue_pair;
541 unsigned nb_dequeued;
543 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
544 (void **)c_ops, nb_ops, NULL);
545 qp->qp_stats.dequeued_count += nb_dequeued;
550 static int cryptodev_kasumi_remove(struct rte_vdev_device *vdev);
553 cryptodev_kasumi_create(const char *name,
554 struct rte_vdev_device *vdev,
555 struct rte_cryptodev_pmd_init_params *init_params)
557 struct rte_cryptodev *dev;
558 struct kasumi_private *internals;
559 uint64_t cpu_flags = 0;
561 dev = rte_cryptodev_pmd_create(name, &vdev->device, init_params);
563 KASUMI_LOG_ERR("failed to create cryptodev vdev");
567 /* Check CPU for supported vector instruction set */
568 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX))
569 cpu_flags |= RTE_CRYPTODEV_FF_CPU_AVX;
571 cpu_flags |= RTE_CRYPTODEV_FF_CPU_SSE;
573 dev->driver_id = cryptodev_driver_id;
574 dev->dev_ops = rte_kasumi_pmd_ops;
576 /* Register RX/TX burst functions for data path. */
577 dev->dequeue_burst = kasumi_pmd_dequeue_burst;
578 dev->enqueue_burst = kasumi_pmd_enqueue_burst;
580 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
581 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
584 internals = dev->data->dev_private;
586 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
587 internals->max_nb_sessions = init_params->max_nb_sessions;
591 KASUMI_LOG_ERR("driver %s: cryptodev_kasumi_create failed",
594 cryptodev_kasumi_remove(vdev);
599 cryptodev_kasumi_probe(struct rte_vdev_device *vdev)
601 struct rte_cryptodev_pmd_init_params init_params = {
603 sizeof(struct kasumi_private),
605 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS,
606 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_SESSIONS
609 const char *input_args;
611 name = rte_vdev_device_name(vdev);
614 input_args = rte_vdev_device_args(vdev);
616 rte_cryptodev_pmd_parse_input_args(&init_params, input_args);
618 return cryptodev_kasumi_create(name, vdev, &init_params);
622 cryptodev_kasumi_remove(struct rte_vdev_device *vdev)
624 struct rte_cryptodev *cryptodev;
627 name = rte_vdev_device_name(vdev);
631 cryptodev = rte_cryptodev_pmd_get_named_dev(name);
632 if (cryptodev == NULL)
635 return rte_cryptodev_pmd_destroy(cryptodev);
638 static struct rte_vdev_driver cryptodev_kasumi_pmd_drv = {
639 .probe = cryptodev_kasumi_probe,
640 .remove = cryptodev_kasumi_remove
643 static struct cryptodev_driver kasumi_crypto_drv;
645 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_KASUMI_PMD, cryptodev_kasumi_pmd_drv);
646 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_KASUMI_PMD, cryptodev_kasumi_pmd);
647 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_KASUMI_PMD,
648 "max_nb_queue_pairs=<int> "
649 "max_nb_sessions=<int> "
651 RTE_PMD_REGISTER_CRYPTO_DRIVER(kasumi_crypto_drv, cryptodev_kasumi_pmd_drv,
652 cryptodev_driver_id);