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_kasumi_pmd_private.h"
44 #define KASUMI_KEY_LENGTH 16
45 #define KASUMI_IV_LENGTH 8
46 #define KASUMI_DIGEST_LENGTH 4
47 #define KASUMI_MAX_BURST 4
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 sso_kasumi_init_f8_key_sched(cipher_xform->cipher.key.data,
120 &sess->pKeySched_cipher);
124 /* Only KASUMI F9 supported */
125 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_KASUMI_F9)
127 sess->auth_op = auth_xform->auth.op;
129 sso_kasumi_init_f9_key_sched(auth_xform->auth.key.data,
130 &sess->pKeySched_hash);
139 /** Get KASUMI session. */
140 static struct kasumi_session *
141 kasumi_get_session(struct kasumi_qp *qp, struct rte_crypto_op *op)
143 struct kasumi_session *sess;
145 if (op->sym->sess_type == RTE_CRYPTO_SYM_OP_WITH_SESSION) {
146 if (unlikely(op->sym->session->dev_type !=
147 RTE_CRYPTODEV_KASUMI_PMD))
150 sess = (struct kasumi_session *)op->sym->session->_private;
152 struct rte_cryptodev_session *c_sess = NULL;
154 if (rte_mempool_get(qp->sess_mp, (void **)&c_sess))
157 sess = (struct kasumi_session *)c_sess->_private;
159 if (unlikely(kasumi_set_session_parameters(sess,
160 op->sym->xform) != 0))
167 /** Encrypt/decrypt mbufs with same cipher key. */
169 process_kasumi_cipher_op(struct rte_crypto_op **ops,
170 struct kasumi_session *session,
174 uint8_t processed_ops = 0;
175 uint8_t *src[num_ops], *dst[num_ops];
176 uint64_t IV[num_ops];
177 uint32_t num_bytes[num_ops];
179 for (i = 0; i < num_ops; i++) {
181 if (ops[i]->sym->cipher.iv.length != KASUMI_IV_LENGTH) {
182 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
183 KASUMI_LOG_ERR("iv");
187 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
188 (ops[i]->sym->cipher.data.offset >> 3);
189 dst[i] = ops[i]->sym->m_dst ?
190 rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
191 (ops[i]->sym->cipher.data.offset >> 3) :
192 rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
193 (ops[i]->sym->cipher.data.offset >> 3);
194 IV[i] = *((uint64_t *)(ops[i]->sym->cipher.iv.data));
195 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
200 if (processed_ops != 0)
201 sso_kasumi_f8_n_buffer(&session->pKeySched_cipher, IV,
202 src, dst, num_bytes, processed_ops);
204 return processed_ops;
207 /** Encrypt/decrypt mbuf (bit level function). */
209 process_kasumi_cipher_op_bit(struct rte_crypto_op *op,
210 struct kasumi_session *session)
214 uint32_t length_in_bits, offset_in_bits;
217 if (unlikely(op->sym->cipher.iv.length != KASUMI_IV_LENGTH)) {
218 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
219 KASUMI_LOG_ERR("iv");
223 offset_in_bits = op->sym->cipher.data.offset;
224 src = rte_pktmbuf_mtod(op->sym->m_src, uint8_t *);
225 if (op->sym->m_dst == NULL) {
226 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
227 KASUMI_LOG_ERR("bit-level in-place not supported\n");
230 dst = rte_pktmbuf_mtod(op->sym->m_dst, uint8_t *);
231 IV = *((uint64_t *)(op->sym->cipher.iv.data));
232 length_in_bits = op->sym->cipher.data.length;
234 sso_kasumi_f8_1_buffer_bit(&session->pKeySched_cipher, IV,
235 src, dst, length_in_bits, offset_in_bits);
240 /** Generate/verify hash from mbufs with same hash key. */
242 process_kasumi_hash_op(struct rte_crypto_op **ops,
243 struct kasumi_session *session,
247 uint8_t processed_ops = 0;
249 uint32_t length_in_bits;
255 for (i = 0; i < num_ops; i++) {
256 if (unlikely(ops[i]->sym->auth.aad.length != KASUMI_IV_LENGTH)) {
257 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
258 KASUMI_LOG_ERR("aad");
262 if (unlikely(ops[i]->sym->auth.digest.length != KASUMI_DIGEST_LENGTH)) {
263 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
264 KASUMI_LOG_ERR("digest");
268 /* Data must be byte aligned */
269 if ((ops[i]->sym->auth.data.offset % BYTE_LEN) != 0) {
270 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
271 KASUMI_LOG_ERR("offset");
275 length_in_bits = ops[i]->sym->auth.data.length;
277 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
278 (ops[i]->sym->auth.data.offset >> 3);
280 IV = *((uint64_t *)(ops[i]->sym->auth.aad.data));
281 /* Direction from next bit after end of message */
282 num_bytes = (length_in_bits >> 3) + 1;
283 shift_bits = (BYTE_LEN - 1 - length_in_bits) % BYTE_LEN;
284 direction = (src[num_bytes - 1] >> shift_bits) & 0x01;
286 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
287 dst = (uint8_t *)rte_pktmbuf_append(ops[i]->sym->m_src,
288 ops[i]->sym->auth.digest.length);
290 sso_kasumi_f9_1_buffer_user(&session->pKeySched_hash,
292 length_in_bits, dst, direction);
294 if (memcmp(dst, ops[i]->sym->auth.digest.data,
295 ops[i]->sym->auth.digest.length) != 0)
296 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
298 /* Trim area used for digest from mbuf. */
299 rte_pktmbuf_trim(ops[i]->sym->m_src,
300 ops[i]->sym->auth.digest.length);
302 dst = ops[i]->sym->auth.digest.data;
304 sso_kasumi_f9_1_buffer_user(&session->pKeySched_hash,
306 length_in_bits, dst, direction);
311 return processed_ops;
314 /** Process a batch of crypto ops which shares the same session. */
316 process_ops(struct rte_crypto_op **ops, struct kasumi_session *session,
317 struct kasumi_qp *qp, uint8_t num_ops,
318 uint16_t *accumulated_enqueued_ops)
321 unsigned enqueued_ops, processed_ops;
323 switch (session->op) {
324 case KASUMI_OP_ONLY_CIPHER:
325 processed_ops = process_kasumi_cipher_op(ops,
328 case KASUMI_OP_ONLY_AUTH:
329 processed_ops = process_kasumi_hash_op(ops, session,
332 case KASUMI_OP_CIPHER_AUTH:
333 processed_ops = process_kasumi_cipher_op(ops, session,
335 process_kasumi_hash_op(ops, session, processed_ops);
337 case KASUMI_OP_AUTH_CIPHER:
338 processed_ops = process_kasumi_hash_op(ops, session,
340 process_kasumi_cipher_op(ops, session, processed_ops);
343 /* Operation not supported. */
347 for (i = 0; i < num_ops; i++) {
349 * If there was no error/authentication failure,
350 * change status to successful.
352 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
353 ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
354 /* Free session if a session-less crypto op. */
355 if (ops[i]->sym->sess_type == RTE_CRYPTO_SYM_OP_SESSIONLESS) {
356 rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
357 ops[i]->sym->session = NULL;
361 enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
362 (void **)ops, processed_ops, NULL);
363 qp->qp_stats.enqueued_count += enqueued_ops;
364 *accumulated_enqueued_ops += enqueued_ops;
369 /** Process a crypto op with length/offset in bits. */
371 process_op_bit(struct rte_crypto_op *op, struct kasumi_session *session,
372 struct kasumi_qp *qp, uint16_t *accumulated_enqueued_ops)
374 unsigned enqueued_op, processed_op;
376 switch (session->op) {
377 case KASUMI_OP_ONLY_CIPHER:
378 processed_op = process_kasumi_cipher_op_bit(op,
381 case KASUMI_OP_ONLY_AUTH:
382 processed_op = process_kasumi_hash_op(&op, session, 1);
384 case KASUMI_OP_CIPHER_AUTH:
385 processed_op = process_kasumi_cipher_op_bit(op, session);
386 if (processed_op == 1)
387 process_kasumi_hash_op(&op, session, 1);
389 case KASUMI_OP_AUTH_CIPHER:
390 processed_op = process_kasumi_hash_op(&op, session, 1);
391 if (processed_op == 1)
392 process_kasumi_cipher_op_bit(op, session);
395 /* Operation not supported. */
400 * If there was no error/authentication failure,
401 * change status to successful.
403 if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
404 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
406 /* Free session if a session-less crypto op. */
407 if (op->sym->sess_type == RTE_CRYPTO_SYM_OP_SESSIONLESS) {
408 rte_mempool_put(qp->sess_mp, 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(struct rte_vdev_device *vdev,
555 struct rte_crypto_vdev_init_params *init_params)
557 struct rte_cryptodev *dev;
558 struct kasumi_private *internals;
559 uint64_t cpu_flags = 0;
561 if (init_params->name[0] == '\0') {
562 int ret = rte_cryptodev_pmd_create_dev_name(
564 RTE_STR(CRYPTODEV_NAME_KASUMI_PMD));
567 KASUMI_LOG_ERR("failed to create unique name");
572 /* Check CPU for supported vector instruction set */
573 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX))
574 cpu_flags |= RTE_CRYPTODEV_FF_CPU_AVX;
575 else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_1))
576 cpu_flags |= RTE_CRYPTODEV_FF_CPU_SSE;
578 KASUMI_LOG_ERR("Vector instructions are not supported by CPU");
582 dev = rte_cryptodev_pmd_virtual_dev_init(init_params->name,
583 sizeof(struct kasumi_private), init_params->socket_id);
585 KASUMI_LOG_ERR("failed to create cryptodev vdev");
589 dev->dev_type = RTE_CRYPTODEV_KASUMI_PMD;
590 dev->dev_ops = rte_kasumi_pmd_ops;
592 /* Register RX/TX burst functions for data path. */
593 dev->dequeue_burst = kasumi_pmd_dequeue_burst;
594 dev->enqueue_burst = kasumi_pmd_enqueue_burst;
596 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
597 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
600 internals = dev->data->dev_private;
602 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
603 internals->max_nb_sessions = init_params->max_nb_sessions;
607 KASUMI_LOG_ERR("driver %s: cryptodev_kasumi_create failed",
610 cryptodev_kasumi_remove(vdev);
615 cryptodev_kasumi_probe(struct rte_vdev_device *vdev)
617 struct rte_crypto_vdev_init_params init_params = {
618 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
619 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
624 const char *input_args;
626 name = rte_vdev_device_name(vdev);
627 input_args = rte_vdev_device_args(vdev);
629 rte_cryptodev_parse_vdev_init_params(&init_params, input_args);
631 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
632 init_params.socket_id);
633 if (init_params.name[0] != '\0')
634 RTE_LOG(INFO, PMD, " User defined name = %s\n",
636 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
637 init_params.max_nb_queue_pairs);
638 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
639 init_params.max_nb_sessions);
641 return cryptodev_kasumi_create(vdev, &init_params);
645 cryptodev_kasumi_remove(struct rte_vdev_device *vdev)
649 name = rte_vdev_device_name(vdev);
653 RTE_LOG(INFO, PMD, "Closing KASUMI crypto device %s"
654 " on numa socket %u\n",
655 name, rte_socket_id());
660 static struct rte_vdev_driver cryptodev_kasumi_pmd_drv = {
661 .probe = cryptodev_kasumi_probe,
662 .remove = cryptodev_kasumi_remove
665 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_KASUMI_PMD, cryptodev_kasumi_pmd_drv);
666 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_KASUMI_PMD, cryptodev_kasumi_pmd);
667 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_KASUMI_PMD,
668 "max_nb_queue_pairs=<int> "
669 "max_nb_sessions=<int> "