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_snow3g_pmd_private.h"
45 #define SNOW3G_IV_LENGTH 16
46 #define SNOW3G_DIGEST_LENGTH 4
47 #define SNOW3G_MAX_BURST 8
50 /** Get xform chain order. */
51 static enum snow3g_operation
52 snow3g_get_mode(const struct rte_crypto_sym_xform *xform)
55 return SNOW3G_OP_NOT_SUPPORTED;
58 if (xform->next->next != NULL)
59 return SNOW3G_OP_NOT_SUPPORTED;
61 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
62 if (xform->next == NULL)
63 return SNOW3G_OP_ONLY_AUTH;
64 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
65 return SNOW3G_OP_AUTH_CIPHER;
67 return SNOW3G_OP_NOT_SUPPORTED;
70 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
71 if (xform->next == NULL)
72 return SNOW3G_OP_ONLY_CIPHER;
73 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
74 return SNOW3G_OP_CIPHER_AUTH;
76 return SNOW3G_OP_NOT_SUPPORTED;
79 return SNOW3G_OP_NOT_SUPPORTED;
83 /** Parse crypto xform chain and set private session parameters. */
85 snow3g_set_session_parameters(struct snow3g_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 snow3g_operation mode;
92 /* Select Crypto operation - hash then cipher / cipher then hash */
93 mode = snow3g_get_mode(xform);
96 case SNOW3G_OP_CIPHER_AUTH:
97 auth_xform = xform->next;
100 case SNOW3G_OP_ONLY_CIPHER:
101 cipher_xform = xform;
103 case SNOW3G_OP_AUTH_CIPHER:
104 cipher_xform = xform->next;
106 case SNOW3G_OP_ONLY_AUTH:
109 case SNOW3G_OP_NOT_SUPPORTED:
111 SNOW3G_LOG_ERR("Unsupported operation chain order parameter");
116 /* Only SNOW 3G UEA2 supported */
117 if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_SNOW3G_UEA2)
120 sso_snow3g_init_key_sched(cipher_xform->cipher.key.data,
121 &sess->pKeySched_cipher);
125 /* Only SNOW 3G UIA2 supported */
126 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_SNOW3G_UIA2)
128 sess->auth_op = auth_xform->auth.op;
130 sso_snow3g_init_key_sched(auth_xform->auth.key.data,
131 &sess->pKeySched_hash);
140 /** Get SNOW 3G session. */
141 static struct snow3g_session *
142 snow3g_get_session(struct snow3g_qp *qp, struct rte_crypto_op *op)
144 struct snow3g_session *sess;
146 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
147 if (unlikely(op->sym->session->dev_type !=
148 RTE_CRYPTODEV_SNOW3G_PMD))
151 sess = (struct snow3g_session *)op->sym->session->_private;
153 struct rte_cryptodev_session *c_sess = NULL;
155 if (rte_mempool_get(qp->sess_mp, (void **)&c_sess))
158 sess = (struct snow3g_session *)c_sess->_private;
160 if (unlikely(snow3g_set_session_parameters(sess,
161 op->sym->xform) != 0))
168 /** Encrypt/decrypt mbufs with same cipher key. */
170 process_snow3g_cipher_op(struct rte_crypto_op **ops,
171 struct snow3g_session *session,
175 uint8_t processed_ops = 0;
176 uint8_t *src[SNOW3G_MAX_BURST], *dst[SNOW3G_MAX_BURST];
177 uint8_t *iv[SNOW3G_MAX_BURST];
178 uint32_t num_bytes[SNOW3G_MAX_BURST];
180 for (i = 0; i < num_ops; i++) {
182 if (unlikely(ops[i]->sym->cipher.iv.length != SNOW3G_IV_LENGTH)) {
183 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
184 SNOW3G_LOG_ERR("iv");
188 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
189 (ops[i]->sym->cipher.data.offset >> 3);
190 dst[i] = ops[i]->sym->m_dst ?
191 rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
192 (ops[i]->sym->cipher.data.offset >> 3) :
193 rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
194 (ops[i]->sym->cipher.data.offset >> 3);
195 iv[i] = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
196 ops[i]->sym->cipher.iv.offset);
197 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
202 sso_snow3g_f8_n_buffer(&session->pKeySched_cipher, iv, src, dst,
203 num_bytes, processed_ops);
205 return processed_ops;
208 /** Encrypt/decrypt mbuf (bit level function). */
210 process_snow3g_cipher_op_bit(struct rte_crypto_op *op,
211 struct snow3g_session *session)
215 uint32_t length_in_bits, offset_in_bits;
218 if (unlikely(op->sym->cipher.iv.length != SNOW3G_IV_LENGTH)) {
219 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
220 SNOW3G_LOG_ERR("iv");
224 offset_in_bits = op->sym->cipher.data.offset;
225 src = rte_pktmbuf_mtod(op->sym->m_src, uint8_t *);
226 if (op->sym->m_dst == NULL) {
227 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
228 SNOW3G_LOG_ERR("bit-level in-place not supported\n");
231 dst = rte_pktmbuf_mtod(op->sym->m_dst, uint8_t *);
232 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
233 op->sym->cipher.iv.offset);
234 length_in_bits = op->sym->cipher.data.length;
236 sso_snow3g_f8_1_buffer_bit(&session->pKeySched_cipher, iv,
237 src, dst, length_in_bits, offset_in_bits);
242 /** Generate/verify hash from mbufs with same hash key. */
244 process_snow3g_hash_op(struct rte_crypto_op **ops,
245 struct snow3g_session *session,
249 uint8_t processed_ops = 0;
251 uint32_t length_in_bits;
253 for (i = 0; i < num_ops; i++) {
254 if (unlikely(ops[i]->sym->auth.aad.length != SNOW3G_IV_LENGTH)) {
255 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
256 SNOW3G_LOG_ERR("aad");
260 if (unlikely(ops[i]->sym->auth.digest.length != SNOW3G_DIGEST_LENGTH)) {
261 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
262 SNOW3G_LOG_ERR("digest");
266 /* Data must be byte aligned */
267 if ((ops[i]->sym->auth.data.offset % BYTE_LEN) != 0) {
268 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
269 SNOW3G_LOG_ERR("Offset");
273 length_in_bits = ops[i]->sym->auth.data.length;
275 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
276 (ops[i]->sym->auth.data.offset >> 3);
278 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
279 dst = (uint8_t *)rte_pktmbuf_append(ops[i]->sym->m_src,
280 ops[i]->sym->auth.digest.length);
282 sso_snow3g_f9_1_buffer(&session->pKeySched_hash,
283 ops[i]->sym->auth.aad.data, src,
284 length_in_bits, dst);
286 if (memcmp(dst, ops[i]->sym->auth.digest.data,
287 ops[i]->sym->auth.digest.length) != 0)
288 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
290 /* Trim area used for digest from mbuf. */
291 rte_pktmbuf_trim(ops[i]->sym->m_src,
292 ops[i]->sym->auth.digest.length);
294 dst = ops[i]->sym->auth.digest.data;
296 sso_snow3g_f9_1_buffer(&session->pKeySched_hash,
297 ops[i]->sym->auth.aad.data, src,
298 length_in_bits, dst);
303 return processed_ops;
306 /** Process a batch of crypto ops which shares the same session. */
308 process_ops(struct rte_crypto_op **ops, struct snow3g_session *session,
309 struct snow3g_qp *qp, uint8_t num_ops,
310 uint16_t *accumulated_enqueued_ops)
313 unsigned enqueued_ops, processed_ops;
315 #ifdef RTE_LIBRTE_PMD_SNOW3G_DEBUG
316 for (i = 0; i < num_ops; i++) {
317 if (!rte_pktmbuf_is_contiguous(ops[i]->sym->m_src) ||
318 (ops[i]->sym->m_dst != NULL &&
319 !rte_pktmbuf_is_contiguous(
320 ops[i]->sym->m_dst))) {
321 SNOW3G_LOG_ERR("PMD supports only contiguous mbufs, "
322 "op (%p) provides noncontiguous mbuf as "
323 "source/destination buffer.\n", ops[i]);
324 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
330 switch (session->op) {
331 case SNOW3G_OP_ONLY_CIPHER:
332 processed_ops = process_snow3g_cipher_op(ops,
335 case SNOW3G_OP_ONLY_AUTH:
336 processed_ops = process_snow3g_hash_op(ops, session,
339 case SNOW3G_OP_CIPHER_AUTH:
340 processed_ops = process_snow3g_cipher_op(ops, session,
342 process_snow3g_hash_op(ops, session, processed_ops);
344 case SNOW3G_OP_AUTH_CIPHER:
345 processed_ops = process_snow3g_hash_op(ops, session,
347 process_snow3g_cipher_op(ops, session, processed_ops);
350 /* Operation not supported. */
354 for (i = 0; i < num_ops; i++) {
356 * If there was no error/authentication failure,
357 * change status to successful.
359 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
360 ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
361 /* Free session if a session-less crypto op. */
362 if (ops[i]->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
363 rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
364 ops[i]->sym->session = NULL;
368 enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
369 (void **)ops, processed_ops, NULL);
370 qp->qp_stats.enqueued_count += enqueued_ops;
371 *accumulated_enqueued_ops += enqueued_ops;
376 /** Process a crypto op with length/offset in bits. */
378 process_op_bit(struct rte_crypto_op *op, struct snow3g_session *session,
379 struct snow3g_qp *qp, uint16_t *accumulated_enqueued_ops)
381 unsigned enqueued_op, processed_op;
383 switch (session->op) {
384 case SNOW3G_OP_ONLY_CIPHER:
385 processed_op = process_snow3g_cipher_op_bit(op,
388 case SNOW3G_OP_ONLY_AUTH:
389 processed_op = process_snow3g_hash_op(&op, session, 1);
391 case SNOW3G_OP_CIPHER_AUTH:
392 processed_op = process_snow3g_cipher_op_bit(op, session);
393 if (processed_op == 1)
394 process_snow3g_hash_op(&op, session, 1);
396 case SNOW3G_OP_AUTH_CIPHER:
397 processed_op = process_snow3g_hash_op(&op, session, 1);
398 if (processed_op == 1)
399 process_snow3g_cipher_op_bit(op, session);
402 /* Operation not supported. */
407 * If there was no error/authentication failure,
408 * change status to successful.
410 if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
411 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
413 /* Free session if a session-less crypto op. */
414 if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
415 rte_mempool_put(qp->sess_mp, op->sym->session);
416 op->sym->session = NULL;
419 enqueued_op = rte_ring_enqueue_burst(qp->processed_ops,
420 (void **)&op, processed_op, NULL);
421 qp->qp_stats.enqueued_count += enqueued_op;
422 *accumulated_enqueued_ops += enqueued_op;
428 snow3g_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
431 struct rte_crypto_op *c_ops[SNOW3G_MAX_BURST];
432 struct rte_crypto_op *curr_c_op;
434 struct snow3g_session *prev_sess = NULL, *curr_sess = NULL;
435 struct snow3g_qp *qp = queue_pair;
437 uint8_t burst_size = 0;
438 uint16_t enqueued_ops = 0;
439 uint8_t processed_ops;
441 for (i = 0; i < nb_ops; i++) {
444 /* Set status as enqueued (not processed yet) by default. */
445 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
447 curr_sess = snow3g_get_session(qp, curr_c_op);
448 if (unlikely(curr_sess == NULL ||
449 curr_sess->op == SNOW3G_OP_NOT_SUPPORTED)) {
451 RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
455 /* If length/offset is at bit-level, process this buffer alone. */
456 if (((curr_c_op->sym->cipher.data.length % BYTE_LEN) != 0)
457 || ((curr_c_op->sym->cipher.data.offset
459 /* Process the ops of the previous session. */
460 if (prev_sess != NULL) {
461 processed_ops = process_ops(c_ops, prev_sess,
462 qp, burst_size, &enqueued_ops);
463 if (processed_ops < burst_size) {
472 processed_ops = process_op_bit(curr_c_op, curr_sess,
474 if (processed_ops != 1)
480 /* Batch ops that share the same session. */
481 if (prev_sess == NULL) {
482 prev_sess = curr_sess;
483 c_ops[burst_size++] = curr_c_op;
484 } else if (curr_sess == prev_sess) {
485 c_ops[burst_size++] = curr_c_op;
487 * When there are enough ops to process in a batch,
488 * process them, and start a new batch.
490 if (burst_size == SNOW3G_MAX_BURST) {
491 processed_ops = process_ops(c_ops, prev_sess,
492 qp, burst_size, &enqueued_ops);
493 if (processed_ops < burst_size) {
503 * Different session, process the ops
504 * of the previous session.
506 processed_ops = process_ops(c_ops, prev_sess,
507 qp, burst_size, &enqueued_ops);
508 if (processed_ops < burst_size) {
514 prev_sess = curr_sess;
516 c_ops[burst_size++] = curr_c_op;
520 if (burst_size != 0) {
521 /* Process the crypto ops of the last session. */
522 processed_ops = process_ops(c_ops, prev_sess,
523 qp, burst_size, &enqueued_ops);
526 qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
531 snow3g_pmd_dequeue_burst(void *queue_pair,
532 struct rte_crypto_op **c_ops, uint16_t nb_ops)
534 struct snow3g_qp *qp = queue_pair;
536 unsigned nb_dequeued;
538 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
539 (void **)c_ops, nb_ops, NULL);
540 qp->qp_stats.dequeued_count += nb_dequeued;
545 static int cryptodev_snow3g_remove(struct rte_vdev_device *vdev);
548 cryptodev_snow3g_create(const char *name,
549 struct rte_vdev_device *vdev,
550 struct rte_crypto_vdev_init_params *init_params)
552 struct rte_cryptodev *dev;
553 struct snow3g_private *internals;
554 uint64_t cpu_flags = RTE_CRYPTODEV_FF_CPU_SSE;
556 if (init_params->name[0] == '\0')
557 snprintf(init_params->name, sizeof(init_params->name),
560 dev = rte_cryptodev_vdev_pmd_init(init_params->name,
561 sizeof(struct snow3g_private), init_params->socket_id,
564 SNOW3G_LOG_ERR("failed to create cryptodev vdev");
568 dev->dev_type = RTE_CRYPTODEV_SNOW3G_PMD;
569 dev->dev_ops = rte_snow3g_pmd_ops;
571 /* Register RX/TX burst functions for data path. */
572 dev->dequeue_burst = snow3g_pmd_dequeue_burst;
573 dev->enqueue_burst = snow3g_pmd_enqueue_burst;
575 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
576 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
579 internals = dev->data->dev_private;
581 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
582 internals->max_nb_sessions = init_params->max_nb_sessions;
586 SNOW3G_LOG_ERR("driver %s: cryptodev_snow3g_create failed",
589 cryptodev_snow3g_remove(vdev);
594 cryptodev_snow3g_probe(struct rte_vdev_device *vdev)
596 struct rte_crypto_vdev_init_params init_params = {
597 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
598 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
603 const char *input_args;
605 name = rte_vdev_device_name(vdev);
608 input_args = rte_vdev_device_args(vdev);
610 rte_cryptodev_vdev_parse_init_params(&init_params, input_args);
612 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
613 init_params.socket_id);
614 if (init_params.name[0] != '\0')
615 RTE_LOG(INFO, PMD, " User defined name = %s\n",
617 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
618 init_params.max_nb_queue_pairs);
619 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
620 init_params.max_nb_sessions);
622 return cryptodev_snow3g_create(name, vdev, &init_params);
626 cryptodev_snow3g_remove(struct rte_vdev_device *vdev)
630 name = rte_vdev_device_name(vdev);
634 RTE_LOG(INFO, PMD, "Closing SNOW 3G crypto device %s"
635 " on numa socket %u\n",
636 name, rte_socket_id());
641 static struct rte_vdev_driver cryptodev_snow3g_pmd_drv = {
642 .probe = cryptodev_snow3g_probe,
643 .remove = cryptodev_snow3g_remove
646 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_SNOW3G_PMD, cryptodev_snow3g_pmd_drv);
647 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_SNOW3G_PMD, cryptodev_snow3g_pmd);
648 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_SNOW3G_PMD,
649 "max_nb_queue_pairs=<int> "
650 "max_nb_sessions=<int> "