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>
41 #include <rte_kvargs.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
51 * Global static parameter used to create a unique name for each SNOW 3G
54 static unsigned unique_name_id;
57 create_unique_device_name(char *name, size_t size)
64 ret = snprintf(name, size, "%s_%u", CRYPTODEV_NAME_SNOW3G_PMD,
71 /** Get xform chain order. */
72 static enum snow3g_operation
73 snow3g_get_mode(const struct rte_crypto_sym_xform *xform)
76 return SNOW3G_OP_NOT_SUPPORTED;
79 if (xform->next->next != NULL)
80 return SNOW3G_OP_NOT_SUPPORTED;
82 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
83 if (xform->next == NULL)
84 return SNOW3G_OP_ONLY_AUTH;
85 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
86 return SNOW3G_OP_AUTH_CIPHER;
88 return SNOW3G_OP_NOT_SUPPORTED;
91 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
92 if (xform->next == NULL)
93 return SNOW3G_OP_ONLY_CIPHER;
94 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
95 return SNOW3G_OP_CIPHER_AUTH;
97 return SNOW3G_OP_NOT_SUPPORTED;
100 return SNOW3G_OP_NOT_SUPPORTED;
104 /** Parse crypto xform chain and set private session parameters. */
106 snow3g_set_session_parameters(struct snow3g_session *sess,
107 const struct rte_crypto_sym_xform *xform)
109 const struct rte_crypto_sym_xform *auth_xform = NULL;
110 const struct rte_crypto_sym_xform *cipher_xform = NULL;
113 /* Select Crypto operation - hash then cipher / cipher then hash */
114 mode = snow3g_get_mode(xform);
117 case SNOW3G_OP_CIPHER_AUTH:
118 auth_xform = xform->next;
121 case SNOW3G_OP_ONLY_CIPHER:
122 cipher_xform = xform;
124 case SNOW3G_OP_AUTH_CIPHER:
125 cipher_xform = xform->next;
127 case SNOW3G_OP_ONLY_AUTH:
131 if (mode == SNOW3G_OP_NOT_SUPPORTED) {
132 SNOW3G_LOG_ERR("Unsupported operation chain order parameter");
137 /* Only SNOW 3G UEA2 supported */
138 if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_SNOW3G_UEA2)
141 sso_snow3g_init_key_sched(xform->cipher.key.data,
142 &sess->pKeySched_cipher);
146 /* Only SNOW 3G UIA2 supported */
147 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_SNOW3G_UIA2)
149 sess->auth_op = auth_xform->auth.op;
151 sso_snow3g_init_key_sched(xform->auth.key.data,
152 &sess->pKeySched_hash);
161 /** Get SNOW 3G session. */
162 static struct snow3g_session *
163 snow3g_get_session(struct snow3g_qp *qp, struct rte_crypto_op *op)
165 struct snow3g_session *sess;
167 if (op->sym->sess_type == RTE_CRYPTO_SYM_OP_WITH_SESSION) {
168 if (unlikely(op->sym->session->dev_type !=
169 RTE_CRYPTODEV_SNOW3G_PMD))
172 sess = (struct snow3g_session *)op->sym->session->_private;
174 struct rte_cryptodev_session *c_sess = NULL;
176 if (rte_mempool_get(qp->sess_mp, (void **)&c_sess))
179 sess = (struct snow3g_session *)c_sess->_private;
181 if (unlikely(snow3g_set_session_parameters(sess,
182 op->sym->xform) != 0))
189 /** Encrypt/decrypt mbufs with same cipher key. */
191 process_snow3g_cipher_op(struct rte_crypto_op **ops,
192 struct snow3g_session *session,
196 uint8_t processed_ops = 0;
197 uint8_t *src[SNOW3G_MAX_BURST], *dst[SNOW3G_MAX_BURST];
198 uint8_t *IV[SNOW3G_MAX_BURST];
199 uint32_t num_bytes[SNOW3G_MAX_BURST];
201 for (i = 0; i < num_ops; i++) {
203 if (unlikely(ops[i]->sym->cipher.iv.length != SNOW3G_IV_LENGTH)) {
204 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
205 SNOW3G_LOG_ERR("iv");
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[i] = ops[i]->sym->cipher.iv.data;
217 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
222 sso_snow3g_f8_n_buffer(&session->pKeySched_cipher, IV, src, dst,
223 num_bytes, processed_ops);
225 return processed_ops;
228 /** Encrypt/decrypt mbuf (bit level function). */
230 process_snow3g_cipher_op_bit(struct rte_crypto_op *op,
231 struct snow3g_session *session)
235 uint32_t length_in_bits, offset_in_bits;
238 if (unlikely(op->sym->cipher.iv.length != SNOW3G_IV_LENGTH)) {
239 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
240 SNOW3G_LOG_ERR("iv");
244 offset_in_bits = op->sym->cipher.data.offset;
245 src = rte_pktmbuf_mtod(op->sym->m_src, uint8_t *);
246 if (op->sym->m_dst == NULL) {
247 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
248 SNOW3G_LOG_ERR("bit-level in-place not supported\n");
251 dst = rte_pktmbuf_mtod(op->sym->m_dst, uint8_t *);
252 IV = op->sym->cipher.iv.data;
253 length_in_bits = op->sym->cipher.data.length;
255 sso_snow3g_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_snow3g_hash_op(struct rte_crypto_op **ops,
264 struct snow3g_session *session,
268 uint8_t processed_ops = 0;
270 uint32_t length_in_bits;
272 for (i = 0; i < num_ops; i++) {
273 if (unlikely(ops[i]->sym->auth.aad.length != SNOW3G_IV_LENGTH)) {
274 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
275 SNOW3G_LOG_ERR("aad");
279 if (unlikely(ops[i]->sym->auth.digest.length != SNOW3G_DIGEST_LENGTH)) {
280 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
281 SNOW3G_LOG_ERR("digest");
285 /* Data must be byte aligned */
286 if ((ops[i]->sym->auth.data.offset % BYTE_LEN) != 0) {
287 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
288 SNOW3G_LOG_ERR("Offset");
292 length_in_bits = ops[i]->sym->auth.data.length;
294 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
295 (ops[i]->sym->auth.data.offset >> 3);
297 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
298 dst = (uint8_t *)rte_pktmbuf_append(ops[i]->sym->m_src,
299 ops[i]->sym->auth.digest.length);
301 sso_snow3g_f9_1_buffer(&session->pKeySched_hash,
302 ops[i]->sym->auth.aad.data, src,
303 length_in_bits, dst);
305 if (memcmp(dst, ops[i]->sym->auth.digest.data,
306 ops[i]->sym->auth.digest.length) != 0)
307 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
309 /* Trim area used for digest from mbuf. */
310 rte_pktmbuf_trim(ops[i]->sym->m_src,
311 ops[i]->sym->auth.digest.length);
313 dst = ops[i]->sym->auth.digest.data;
315 sso_snow3g_f9_1_buffer(&session->pKeySched_hash,
316 ops[i]->sym->auth.aad.data, src,
317 length_in_bits, dst);
322 return processed_ops;
325 /** Process a batch of crypto ops which shares the same session. */
327 process_ops(struct rte_crypto_op **ops, struct snow3g_session *session,
328 struct snow3g_qp *qp, uint8_t num_ops,
329 uint16_t *accumulated_enqueued_ops)
332 unsigned enqueued_ops, processed_ops;
334 switch (session->op) {
335 case SNOW3G_OP_ONLY_CIPHER:
336 processed_ops = process_snow3g_cipher_op(ops,
339 case SNOW3G_OP_ONLY_AUTH:
340 processed_ops = process_snow3g_hash_op(ops, session,
343 case SNOW3G_OP_CIPHER_AUTH:
344 processed_ops = process_snow3g_cipher_op(ops, session,
346 process_snow3g_hash_op(ops, session, processed_ops);
348 case SNOW3G_OP_AUTH_CIPHER:
349 processed_ops = process_snow3g_hash_op(ops, session,
351 process_snow3g_cipher_op(ops, session, processed_ops);
354 /* Operation not supported. */
358 for (i = 0; i < num_ops; i++) {
360 * If there was no error/authentication failure,
361 * change status to successful.
363 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
364 ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
365 /* Free session if a session-less crypto op. */
366 if (ops[i]->sym->sess_type == RTE_CRYPTO_SYM_OP_SESSIONLESS) {
367 rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
368 ops[i]->sym->session = NULL;
372 enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
373 (void **)ops, processed_ops);
374 qp->qp_stats.enqueued_count += enqueued_ops;
375 *accumulated_enqueued_ops += enqueued_ops;
380 /** Process a crypto op with length/offset in bits. */
382 process_op_bit(struct rte_crypto_op *op, struct snow3g_session *session,
383 struct snow3g_qp *qp, uint16_t *accumulated_enqueued_ops)
385 unsigned enqueued_op, processed_op;
387 switch (session->op) {
388 case SNOW3G_OP_ONLY_CIPHER:
389 processed_op = process_snow3g_cipher_op_bit(op,
392 case SNOW3G_OP_ONLY_AUTH:
393 processed_op = process_snow3g_hash_op(&op, session, 1);
395 case SNOW3G_OP_CIPHER_AUTH:
396 processed_op = process_snow3g_cipher_op_bit(op, session);
397 if (processed_op == 1)
398 process_snow3g_hash_op(&op, session, 1);
400 case SNOW3G_OP_AUTH_CIPHER:
401 processed_op = process_snow3g_hash_op(&op, session, 1);
402 if (processed_op == 1)
403 process_snow3g_cipher_op_bit(op, session);
406 /* Operation not supported. */
411 * If there was no error/authentication failure,
412 * change status to successful.
414 if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
415 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
417 /* Free session if a session-less crypto op. */
418 if (op->sym->sess_type == RTE_CRYPTO_SYM_OP_SESSIONLESS) {
419 rte_mempool_put(qp->sess_mp, op->sym->session);
420 op->sym->session = NULL;
423 enqueued_op = rte_ring_enqueue_burst(qp->processed_ops,
424 (void **)&op, processed_op);
425 qp->qp_stats.enqueued_count += enqueued_op;
426 *accumulated_enqueued_ops += enqueued_op;
432 snow3g_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
435 struct rte_crypto_op *c_ops[SNOW3G_MAX_BURST];
436 struct rte_crypto_op *curr_c_op;
438 struct snow3g_session *prev_sess = NULL, *curr_sess = NULL;
439 struct snow3g_qp *qp = queue_pair;
441 uint8_t burst_size = 0;
442 uint16_t enqueued_ops = 0;
443 uint8_t processed_ops;
445 for (i = 0; i < nb_ops; i++) {
448 /* Set status as enqueued (not processed yet) by default. */
449 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
451 curr_sess = snow3g_get_session(qp, curr_c_op);
452 if (unlikely(curr_sess == NULL ||
453 curr_sess->op == SNOW3G_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 || ((curr_c_op->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 == SNOW3G_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 snow3g_pmd_dequeue_burst(void *queue_pair,
536 struct rte_crypto_op **c_ops, uint16_t nb_ops)
538 struct snow3g_qp *qp = queue_pair;
540 unsigned nb_dequeued;
542 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
543 (void **)c_ops, nb_ops);
544 qp->qp_stats.dequeued_count += nb_dequeued;
549 static int cryptodev_snow3g_uninit(const char *name);
552 cryptodev_snow3g_create(const char *name,
553 struct rte_crypto_vdev_init_params *init_params)
555 struct rte_cryptodev *dev;
556 char crypto_dev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
557 struct snow3g_private *internals;
558 uint64_t cpu_flags = 0;
560 /* Check CPU for supported vector instruction set */
561 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_1))
562 cpu_flags |= RTE_CRYPTODEV_FF_CPU_SSE;
564 SNOW3G_LOG_ERR("Vector instructions are not supported by CPU");
569 /* Create a unique device name. */
570 if (create_unique_device_name(crypto_dev_name,
571 RTE_CRYPTODEV_NAME_MAX_LEN) != 0) {
572 SNOW3G_LOG_ERR("failed to create unique cryptodev name");
576 dev = rte_cryptodev_pmd_virtual_dev_init(crypto_dev_name,
577 sizeof(struct snow3g_private), init_params->socket_id);
579 SNOW3G_LOG_ERR("failed to create cryptodev vdev");
583 dev->dev_type = RTE_CRYPTODEV_SNOW3G_PMD;
584 dev->dev_ops = rte_snow3g_pmd_ops;
586 /* Register RX/TX burst functions for data path. */
587 dev->dequeue_burst = snow3g_pmd_dequeue_burst;
588 dev->enqueue_burst = snow3g_pmd_enqueue_burst;
590 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
591 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
594 internals = dev->data->dev_private;
596 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
597 internals->max_nb_sessions = init_params->max_nb_sessions;
601 SNOW3G_LOG_ERR("driver %s: cryptodev_snow3g_create failed", name);
603 cryptodev_snow3g_uninit(crypto_dev_name);
608 cryptodev_snow3g_init(const char *name,
609 const char *input_args)
611 struct rte_crypto_vdev_init_params init_params = {
612 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
613 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
617 rte_cryptodev_parse_vdev_init_params(&init_params, input_args);
619 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
620 init_params.socket_id);
621 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
622 init_params.max_nb_queue_pairs);
623 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
624 init_params.max_nb_sessions);
626 return cryptodev_snow3g_create(name, &init_params);
630 cryptodev_snow3g_uninit(const char *name)
635 RTE_LOG(INFO, PMD, "Closing SNOW3G crypto device %s"
636 " on numa socket %u\n",
637 name, rte_socket_id());
642 static struct rte_driver cryptodev_snow3g_pmd_drv = {
643 .name = CRYPTODEV_NAME_SNOW3G_PMD,
645 .init = cryptodev_snow3g_init,
646 .uninit = cryptodev_snow3g_uninit
649 PMD_REGISTER_DRIVER(cryptodev_snow3g_pmd_drv);