crypto/snow3g: define IV and digest length constants
[dpdk.git] / drivers / crypto / snow3g / rte_snow3g_pmd.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016 Intel Corporation. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
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
15  *       distribution.
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.
19  *
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.
31  */
32
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_dev.h>
39 #include <rte_malloc.h>
40 #include <rte_cpuflags.h>
41 #include <rte_kvargs.h>
42
43 #include "rte_snow3g_pmd_private.h"
44
45 #define SNOW3G_IV_LENGTH 16
46 #define SNOW3G_DIGEST_LENGTH 4
47 #define SNOW3G_MAX_BURST 8
48 #define BYTE_LEN 8
49
50 /**
51  * Global static parameter used to create a unique name for each SNOW 3G
52  * crypto device.
53  */
54 static unsigned unique_name_id;
55
56 static inline int
57 create_unique_device_name(char *name, size_t size)
58 {
59         int ret;
60
61         if (name == NULL)
62                 return -EINVAL;
63
64         ret = snprintf(name, size, "%s_%u", CRYPTODEV_NAME_SNOW3G_PMD,
65                         unique_name_id++);
66         if (ret < 0)
67                 return ret;
68         return 0;
69 }
70
71 /** Get xform chain order. */
72 static enum snow3g_operation
73 snow3g_get_mode(const struct rte_crypto_sym_xform *xform)
74 {
75         if (xform == NULL)
76                 return SNOW3G_OP_NOT_SUPPORTED;
77
78         if (xform->next)
79                 if (xform->next->next != NULL)
80                         return SNOW3G_OP_NOT_SUPPORTED;
81
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;
87                 else
88                         return SNOW3G_OP_NOT_SUPPORTED;
89         }
90
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;
96                 else
97                         return SNOW3G_OP_NOT_SUPPORTED;
98         }
99
100         return SNOW3G_OP_NOT_SUPPORTED;
101 }
102
103
104 /** Parse crypto xform chain and set private session parameters. */
105 int
106 snow3g_set_session_parameters(struct snow3g_session *sess,
107                 const struct rte_crypto_sym_xform *xform)
108 {
109         const struct rte_crypto_sym_xform *auth_xform = NULL;
110         const struct rte_crypto_sym_xform *cipher_xform = NULL;
111         int mode;
112
113         /* Select Crypto operation - hash then cipher / cipher then hash */
114         mode = snow3g_get_mode(xform);
115
116         switch (mode) {
117         case SNOW3G_OP_CIPHER_AUTH:
118                 auth_xform = xform->next;
119
120                 /* Fall-through */
121         case SNOW3G_OP_ONLY_CIPHER:
122                 cipher_xform = xform;
123                 break;
124         case SNOW3G_OP_AUTH_CIPHER:
125                 cipher_xform = xform->next;
126                 /* Fall-through */
127         case SNOW3G_OP_ONLY_AUTH:
128                 auth_xform = xform;
129         }
130
131         if (mode == SNOW3G_OP_NOT_SUPPORTED) {
132                 SNOW3G_LOG_ERR("Unsupported operation chain order parameter");
133                 return -EINVAL;
134         }
135
136         if (cipher_xform) {
137                 /* Only SNOW 3G UEA2 supported */
138                 if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_SNOW3G_UEA2)
139                         return -EINVAL;
140                 /* Initialize key */
141                 sso_snow3g_init_key_sched(xform->cipher.key.data,
142                                 &sess->pKeySched_cipher);
143         }
144
145         if (auth_xform) {
146                 /* Only SNOW 3G UIA2 supported */
147                 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_SNOW3G_UIA2)
148                         return -EINVAL;
149                 sess->auth_op = auth_xform->auth.op;
150                 /* Initialize key */
151                 sso_snow3g_init_key_sched(xform->auth.key.data,
152                                 &sess->pKeySched_hash);
153         }
154
155
156         sess->op = mode;
157
158         return 0;
159 }
160
161 /** Get SNOW 3G session. */
162 static struct snow3g_session *
163 snow3g_get_session(struct snow3g_qp *qp, struct rte_crypto_op *op)
164 {
165         struct snow3g_session *sess;
166
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))
170                         return NULL;
171
172                 sess = (struct snow3g_session *)op->sym->session->_private;
173         } else  {
174                 struct rte_cryptodev_session *c_sess = NULL;
175
176                 if (rte_mempool_get(qp->sess_mp, (void **)&c_sess))
177                         return NULL;
178
179                 sess = (struct snow3g_session *)c_sess->_private;
180
181                 if (unlikely(snow3g_set_session_parameters(sess,
182                                 op->sym->xform) != 0))
183                         return NULL;
184         }
185
186         return sess;
187 }
188
189 /** Encrypt/decrypt mbufs with same cipher key. */
190 static uint8_t
191 process_snow3g_cipher_op(struct rte_crypto_op **ops,
192                 struct snow3g_session *session,
193                 uint8_t num_ops)
194 {
195         unsigned i;
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];
200
201         for (i = 0; i < num_ops; i++) {
202                 /* Sanity checks. */
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");
206                         break;
207                 }
208
209                 if (((ops[i]->sym->cipher.data.length % BYTE_LEN) != 0)
210                                 || ((ops[i]->sym->cipher.data.offset
211                                         % BYTE_LEN) != 0)) {
212                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
213                         SNOW3G_LOG_ERR("Data Length or offset");
214                         break;
215                 }
216
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;
226
227                 processed_ops++;
228         }
229
230         sso_snow3g_f8_n_buffer(&session->pKeySched_cipher, IV, src, dst,
231                         num_bytes, processed_ops);
232
233         return processed_ops;
234 }
235
236 /** Generate/verify hash from mbufs with same hash key. */
237 static int
238 process_snow3g_hash_op(struct rte_crypto_op **ops,
239                 struct snow3g_session *session,
240                 uint8_t num_ops)
241 {
242         unsigned i;
243         uint8_t processed_ops = 0;
244         uint8_t *src, *dst;
245         uint32_t length_in_bits;
246
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");
251                         break;
252                 }
253
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");
257                         break;
258                 }
259
260                 if (((ops[i]->sym->auth.data.length % BYTE_LEN) != 0)
261                                 || ((ops[i]->sym->auth.data.offset
262                                         % BYTE_LEN) != 0)) {
263                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
264                         SNOW3G_LOG_ERR("Data Length or offset");
265                         break;
266                 }
267
268                 length_in_bits = ops[i]->sym->auth.data.length;
269
270                 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
271                                 (ops[i]->sym->auth.data.offset >> 3);
272
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);
276
277                         sso_snow3g_f9_1_buffer(&session->pKeySched_hash,
278                                         ops[i]->sym->auth.aad.data, src,
279                                         length_in_bits, dst);
280                         /* Verify digest. */
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;
284
285                         /* Trim area used for digest from mbuf. */
286                         rte_pktmbuf_trim(ops[i]->sym->m_src,
287                                         ops[i]->sym->auth.digest.length);
288                 } else  {
289                         dst = ops[i]->sym->auth.digest.data;
290
291                         sso_snow3g_f9_1_buffer(&session->pKeySched_hash,
292                                         ops[i]->sym->auth.aad.data, src,
293                                         length_in_bits, dst);
294                 }
295                 processed_ops++;
296         }
297
298         return processed_ops;
299 }
300
301 /** Process a batch of crypto ops which shares the same session. */
302 static int
303 process_ops(struct rte_crypto_op **ops, struct snow3g_session *session,
304                 struct snow3g_qp *qp, uint8_t num_ops)
305 {
306         unsigned i;
307         unsigned processed_ops;
308
309         switch (session->op) {
310         case SNOW3G_OP_ONLY_CIPHER:
311                 processed_ops = process_snow3g_cipher_op(ops,
312                                 session, num_ops);
313                 break;
314         case SNOW3G_OP_ONLY_AUTH:
315                 processed_ops = process_snow3g_hash_op(ops, session,
316                                 num_ops);
317                 break;
318         case SNOW3G_OP_CIPHER_AUTH:
319                 processed_ops = process_snow3g_cipher_op(ops, session,
320                                 num_ops);
321                 process_snow3g_hash_op(ops, session, processed_ops);
322                 break;
323         case SNOW3G_OP_AUTH_CIPHER:
324                 processed_ops = process_snow3g_hash_op(ops, session,
325                                 num_ops);
326                 process_snow3g_cipher_op(ops, session, processed_ops);
327                 break;
328         default:
329                 /* Operation not supported. */
330                 processed_ops = 0;
331         }
332
333         for (i = 0; i < num_ops; i++) {
334                 /*
335                  * If there was no error/authentication failure,
336                  * change status to successful.
337                  */
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;
344                 }
345         }
346
347         return processed_ops;
348 }
349
350 static uint16_t
351 snow3g_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
352                 uint16_t nb_ops)
353 {
354         struct rte_crypto_op *c_ops[SNOW3G_MAX_BURST];
355         struct rte_crypto_op *curr_c_op;
356
357         struct snow3g_session *prev_sess = NULL, *curr_sess = NULL;
358         struct snow3g_qp *qp = queue_pair;
359         unsigned i, n;
360         uint8_t burst_size = 0;
361         uint16_t enqueued_ops = 0;
362         uint8_t processed_ops;
363
364         for (i = 0; i < nb_ops; i++) {
365                 curr_c_op = ops[i];
366
367                 /* Set status as enqueued (not processed yet) by default. */
368                 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
369
370                 curr_sess = snow3g_get_session(qp, curr_c_op);
371                 if (unlikely(curr_sess == NULL ||
372                                 curr_sess->op == SNOW3G_OP_NOT_SUPPORTED)) {
373                         curr_c_op->status =
374                                         RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
375                         qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
376                         return enqueued_ops;
377                 }
378
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;
385                         /*
386                          * When there are enough ops to process in a batch,
387                          * process them, and start a new batch.
388                          */
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,
393                                                 (void **)c_ops,
394                                                 processed_ops);
395                                 qp->qp_stats.enqueued_count += n;
396                                 enqueued_ops += n;
397                                 if (n < burst_size) {
398                                         qp->qp_stats.enqueue_err_count +=
399                                                         nb_ops - enqueued_ops;
400                                         return enqueued_ops;
401                                 }
402                                 burst_size = 0;
403
404                                 prev_sess = NULL;
405                         }
406                 } else {
407                         /*
408                          * Different session, process the ops
409                          * of the previous session.
410                          */
411                         processed_ops = process_ops(c_ops,
412                                         prev_sess, qp, burst_size);
413                         n = rte_ring_enqueue_burst(qp->processed_ops,
414                                         (void **)c_ops,
415                                         processed_ops);
416                         qp->qp_stats.enqueued_count += n;
417                         enqueued_ops += n;
418                         if (n < burst_size) {
419                                 qp->qp_stats.enqueue_err_count +=
420                                                 nb_ops - enqueued_ops;
421                                 return enqueued_ops;
422                         }
423                         burst_size = 0;
424
425                         prev_sess = curr_sess;
426                         c_ops[burst_size++] = curr_c_op;
427                 }
428         }
429
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,
435                                 (void **)c_ops,
436                                 processed_ops);
437                 qp->qp_stats.enqueued_count += n;
438                 enqueued_ops += n;
439                 if (n < burst_size) {
440                         qp->qp_stats.enqueue_err_count +=
441                                         nb_ops - enqueued_ops;
442                         return enqueued_ops;
443                 }
444         }
445
446         return enqueued_ops;
447 }
448
449 static uint16_t
450 snow3g_pmd_dequeue_burst(void *queue_pair,
451                 struct rte_crypto_op **c_ops, uint16_t nb_ops)
452 {
453         struct snow3g_qp *qp = queue_pair;
454
455         unsigned nb_dequeued;
456
457         nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
458                         (void **)c_ops, nb_ops);
459         qp->qp_stats.dequeued_count += nb_dequeued;
460
461         return nb_dequeued;
462 }
463
464 static int cryptodev_snow3g_uninit(const char *name);
465
466 static int
467 cryptodev_snow3g_create(const char *name,
468                 struct rte_crypto_vdev_init_params *init_params)
469 {
470         struct rte_cryptodev *dev;
471         char crypto_dev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
472         struct snow3g_private *internals;
473
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");
478                 return -EINVAL;
479         }
480
481         dev = rte_cryptodev_pmd_virtual_dev_init(crypto_dev_name,
482                         sizeof(struct snow3g_private), init_params->socket_id);
483         if (dev == NULL) {
484                 SNOW3G_LOG_ERR("failed to create cryptodev vdev");
485                 goto init_error;
486         }
487
488         dev->dev_type = RTE_CRYPTODEV_SNOW3G_PMD;
489         dev->dev_ops = rte_snow3g_pmd_ops;
490
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;
494
495         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
496                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING;
497
498         internals = dev->data->dev_private;
499
500         internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
501         internals->max_nb_sessions = init_params->max_nb_sessions;
502
503         return 0;
504 init_error:
505         SNOW3G_LOG_ERR("driver %s: cryptodev_snow3g_create failed", name);
506
507         cryptodev_snow3g_uninit(crypto_dev_name);
508         return -EFAULT;
509 }
510
511 static int
512 cryptodev_snow3g_init(const char *name,
513                 const char *input_args)
514 {
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,
518                 rte_socket_id()
519         };
520
521         rte_cryptodev_parse_vdev_init_params(&init_params, input_args);
522
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);
529
530         return cryptodev_snow3g_create(name, &init_params);
531 }
532
533 static int
534 cryptodev_snow3g_uninit(const char *name)
535 {
536         if (name == NULL)
537                 return -EINVAL;
538
539         RTE_LOG(INFO, PMD, "Closing SNOW3G crypto device %s"
540                         " on numa socket %u\n",
541                         name, rte_socket_id());
542
543         return 0;
544 }
545
546 static struct rte_driver cryptodev_snow3g_pmd_drv = {
547         .name = CRYPTODEV_NAME_SNOW3G_PMD,
548         .type = PMD_VDEV,
549         .init = cryptodev_snow3g_init,
550         .uninit = cryptodev_snow3g_uninit
551 };
552
553 PMD_REGISTER_DRIVER(cryptodev_snow3g_pmd_drv);