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 if (((ops[i]->sym->cipher.data.length % BYTE_LEN) != 0)
210 || ((ops[i]->sym->cipher.data.offset
212 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
213 SNOW3G_LOG_ERR("Data Length or offset");
217 src[i] = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
218 (ops[i]->sym->cipher.data.offset >> 3);
219 dst[i] = ops[i]->sym->m_dst ?
220 rte_pktmbuf_mtod(ops[i]->sym->m_dst, uint8_t *) +
221 (ops[i]->sym->cipher.data.offset >> 3) :
222 rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
223 (ops[i]->sym->cipher.data.offset >> 3);
224 IV[i] = ops[i]->sym->cipher.iv.data;
225 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
230 sso_snow3g_f8_n_buffer(&session->pKeySched_cipher, IV, src, dst,
231 num_bytes, processed_ops);
233 return processed_ops;
236 /** Generate/verify hash from mbufs with same hash key. */
238 process_snow3g_hash_op(struct rte_crypto_op **ops,
239 struct snow3g_session *session,
243 uint8_t processed_ops = 0;
245 uint32_t length_in_bits;
247 for (i = 0; i < num_ops; i++) {
248 if (unlikely(ops[i]->sym->auth.aad.length != SNOW3G_IV_LENGTH)) {
249 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
250 SNOW3G_LOG_ERR("aad");
254 if (unlikely(ops[i]->sym->auth.digest.length != SNOW3G_DIGEST_LENGTH)) {
255 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
256 SNOW3G_LOG_ERR("digest");
260 if (((ops[i]->sym->auth.data.length % BYTE_LEN) != 0)
261 || ((ops[i]->sym->auth.data.offset
263 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
264 SNOW3G_LOG_ERR("Data Length or offset");
268 length_in_bits = ops[i]->sym->auth.data.length;
270 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
271 (ops[i]->sym->auth.data.offset >> 3);
273 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
274 dst = (uint8_t *)rte_pktmbuf_append(ops[i]->sym->m_src,
275 ops[i]->sym->auth.digest.length);
277 sso_snow3g_f9_1_buffer(&session->pKeySched_hash,
278 ops[i]->sym->auth.aad.data, src,
279 length_in_bits, dst);
281 if (memcmp(dst, ops[i]->sym->auth.digest.data,
282 ops[i]->sym->auth.digest.length) != 0)
283 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
285 /* Trim area used for digest from mbuf. */
286 rte_pktmbuf_trim(ops[i]->sym->m_src,
287 ops[i]->sym->auth.digest.length);
289 dst = ops[i]->sym->auth.digest.data;
291 sso_snow3g_f9_1_buffer(&session->pKeySched_hash,
292 ops[i]->sym->auth.aad.data, src,
293 length_in_bits, dst);
298 return processed_ops;
301 /** Process a batch of crypto ops which shares the same session. */
303 process_ops(struct rte_crypto_op **ops, struct snow3g_session *session,
304 struct snow3g_qp *qp, uint8_t num_ops)
307 unsigned processed_ops;
309 switch (session->op) {
310 case SNOW3G_OP_ONLY_CIPHER:
311 processed_ops = process_snow3g_cipher_op(ops,
314 case SNOW3G_OP_ONLY_AUTH:
315 processed_ops = process_snow3g_hash_op(ops, session,
318 case SNOW3G_OP_CIPHER_AUTH:
319 processed_ops = process_snow3g_cipher_op(ops, session,
321 process_snow3g_hash_op(ops, session, processed_ops);
323 case SNOW3G_OP_AUTH_CIPHER:
324 processed_ops = process_snow3g_hash_op(ops, session,
326 process_snow3g_cipher_op(ops, session, processed_ops);
329 /* Operation not supported. */
333 for (i = 0; i < num_ops; i++) {
335 * If there was no error/authentication failure,
336 * change status to successful.
338 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
339 ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
340 /* Free session if a session-less crypto op. */
341 if (ops[i]->sym->sess_type == RTE_CRYPTO_SYM_OP_SESSIONLESS) {
342 rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
343 ops[i]->sym->session = NULL;
347 return processed_ops;
351 snow3g_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
354 struct rte_crypto_op *c_ops[SNOW3G_MAX_BURST];
355 struct rte_crypto_op *curr_c_op;
357 struct snow3g_session *prev_sess = NULL, *curr_sess = NULL;
358 struct snow3g_qp *qp = queue_pair;
360 uint8_t burst_size = 0;
361 uint16_t enqueued_ops = 0;
362 uint8_t processed_ops;
364 for (i = 0; i < nb_ops; i++) {
367 /* Set status as enqueued (not processed yet) by default. */
368 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
370 curr_sess = snow3g_get_session(qp, curr_c_op);
371 if (unlikely(curr_sess == NULL ||
372 curr_sess->op == SNOW3G_OP_NOT_SUPPORTED)) {
374 RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
375 qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
379 /* Batch ops that share the same session. */
380 if (prev_sess == NULL) {
381 prev_sess = curr_sess;
382 c_ops[burst_size++] = curr_c_op;
383 } else if (curr_sess == prev_sess) {
384 c_ops[burst_size++] = curr_c_op;
386 * When there are enough ops to process in a batch,
387 * process them, and start a new batch.
389 if (burst_size == SNOW3G_MAX_BURST) {
390 processed_ops = process_ops(c_ops,
391 prev_sess, qp, burst_size);
392 n = rte_ring_enqueue_burst(qp->processed_ops,
395 qp->qp_stats.enqueued_count += n;
397 if (n < burst_size) {
398 qp->qp_stats.enqueue_err_count +=
399 nb_ops - enqueued_ops;
408 * Different session, process the ops
409 * of the previous session.
411 processed_ops = process_ops(c_ops,
412 prev_sess, qp, burst_size);
413 n = rte_ring_enqueue_burst(qp->processed_ops,
416 qp->qp_stats.enqueued_count += n;
418 if (n < burst_size) {
419 qp->qp_stats.enqueue_err_count +=
420 nb_ops - enqueued_ops;
425 prev_sess = curr_sess;
426 c_ops[burst_size++] = curr_c_op;
430 if (burst_size != 0) {
431 /* Process the crypto ops of the last session. */
432 processed_ops = process_ops(c_ops,
433 prev_sess, qp, burst_size);
434 n = rte_ring_enqueue_burst(qp->processed_ops,
437 qp->qp_stats.enqueued_count += n;
439 if (n < burst_size) {
440 qp->qp_stats.enqueue_err_count +=
441 nb_ops - enqueued_ops;
450 snow3g_pmd_dequeue_burst(void *queue_pair,
451 struct rte_crypto_op **c_ops, uint16_t nb_ops)
453 struct snow3g_qp *qp = queue_pair;
455 unsigned nb_dequeued;
457 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
458 (void **)c_ops, nb_ops);
459 qp->qp_stats.dequeued_count += nb_dequeued;
464 static int cryptodev_snow3g_uninit(const char *name);
467 cryptodev_snow3g_create(const char *name,
468 struct rte_crypto_vdev_init_params *init_params)
470 struct rte_cryptodev *dev;
471 char crypto_dev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
472 struct snow3g_private *internals;
474 /* Create a unique device name. */
475 if (create_unique_device_name(crypto_dev_name,
476 RTE_CRYPTODEV_NAME_MAX_LEN) != 0) {
477 SNOW3G_LOG_ERR("failed to create unique cryptodev name");
481 dev = rte_cryptodev_pmd_virtual_dev_init(crypto_dev_name,
482 sizeof(struct snow3g_private), init_params->socket_id);
484 SNOW3G_LOG_ERR("failed to create cryptodev vdev");
488 dev->dev_type = RTE_CRYPTODEV_SNOW3G_PMD;
489 dev->dev_ops = rte_snow3g_pmd_ops;
491 /* Register RX/TX burst functions for data path. */
492 dev->dequeue_burst = snow3g_pmd_dequeue_burst;
493 dev->enqueue_burst = snow3g_pmd_enqueue_burst;
495 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
496 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING;
498 internals = dev->data->dev_private;
500 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
501 internals->max_nb_sessions = init_params->max_nb_sessions;
505 SNOW3G_LOG_ERR("driver %s: cryptodev_snow3g_create failed", name);
507 cryptodev_snow3g_uninit(crypto_dev_name);
512 cryptodev_snow3g_init(const char *name,
513 const char *input_args)
515 struct rte_crypto_vdev_init_params init_params = {
516 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
517 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
521 rte_cryptodev_parse_vdev_init_params(&init_params, input_args);
523 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
524 init_params.socket_id);
525 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
526 init_params.max_nb_queue_pairs);
527 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
528 init_params.max_nb_sessions);
530 return cryptodev_snow3g_create(name, &init_params);
534 cryptodev_snow3g_uninit(const char *name)
539 RTE_LOG(INFO, PMD, "Closing SNOW3G crypto device %s"
540 " on numa socket %u\n",
541 name, rte_socket_id());
546 static struct rte_driver cryptodev_snow3g_pmd_drv = {
547 .name = CRYPTODEV_NAME_SNOW3G_PMD,
549 .init = cryptodev_snow3g_init,
550 .uninit = cryptodev_snow3g_uninit
553 PMD_REGISTER_DRIVER(cryptodev_snow3g_pmd_drv);