3923e3c66702cb1c6d0ca414c9670af4bdb307c6
[dpdk.git] / drivers / crypto / zuc / rte_zuc_pmd.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016-2017 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_cryptodev_vdev.h>
39 #include <rte_vdev.h>
40 #include <rte_malloc.h>
41 #include <rte_cpuflags.h>
42
43 #include "rte_zuc_pmd_private.h"
44
45 #define ZUC_DIGEST_LENGTH 4
46 #define ZUC_MAX_BURST 8
47 #define BYTE_LEN 8
48
49 /** Get xform chain order. */
50 static enum zuc_operation
51 zuc_get_mode(const struct rte_crypto_sym_xform *xform)
52 {
53         if (xform == NULL)
54                 return ZUC_OP_NOT_SUPPORTED;
55
56         if (xform->next)
57                 if (xform->next->next != NULL)
58                         return ZUC_OP_NOT_SUPPORTED;
59
60         if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
61                 if (xform->next == NULL)
62                         return ZUC_OP_ONLY_AUTH;
63                 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
64                         return ZUC_OP_AUTH_CIPHER;
65                 else
66                         return ZUC_OP_NOT_SUPPORTED;
67         }
68
69         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
70                 if (xform->next == NULL)
71                         return ZUC_OP_ONLY_CIPHER;
72                 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
73                         return ZUC_OP_CIPHER_AUTH;
74                 else
75                         return ZUC_OP_NOT_SUPPORTED;
76         }
77
78         return ZUC_OP_NOT_SUPPORTED;
79 }
80
81
82 /** Parse crypto xform chain and set private session parameters. */
83 int
84 zuc_set_session_parameters(struct zuc_session *sess,
85                 const struct rte_crypto_sym_xform *xform)
86 {
87         const struct rte_crypto_sym_xform *auth_xform = NULL;
88         const struct rte_crypto_sym_xform *cipher_xform = NULL;
89         enum zuc_operation mode;
90
91         /* Select Crypto operation - hash then cipher / cipher then hash */
92         mode = zuc_get_mode(xform);
93
94         switch (mode) {
95         case ZUC_OP_CIPHER_AUTH:
96                 auth_xform = xform->next;
97
98                 /* Fall-through */
99         case ZUC_OP_ONLY_CIPHER:
100                 cipher_xform = xform;
101                 break;
102         case ZUC_OP_AUTH_CIPHER:
103                 cipher_xform = xform->next;
104                 /* Fall-through */
105         case ZUC_OP_ONLY_AUTH:
106                 auth_xform = xform;
107                 break;
108         case ZUC_OP_NOT_SUPPORTED:
109         default:
110                 ZUC_LOG_ERR("Unsupported operation chain order parameter");
111                 return -EINVAL;
112         }
113
114         if (cipher_xform) {
115                 /* Only ZUC EEA3 supported */
116                 if (cipher_xform->cipher.algo != RTE_CRYPTO_CIPHER_ZUC_EEA3)
117                         return -EINVAL;
118                 /* Copy the key */
119                 memcpy(sess->pKey_cipher, cipher_xform->cipher.key.data,
120                                 ZUC_IV_KEY_LENGTH);
121         }
122
123         if (auth_xform) {
124                 /* Only ZUC EIA3 supported */
125                 if (auth_xform->auth.algo != RTE_CRYPTO_AUTH_ZUC_EIA3)
126                         return -EINVAL;
127                 sess->auth_op = auth_xform->auth.op;
128                 /* Copy the key */
129                 memcpy(sess->pKey_hash, auth_xform->auth.key.data,
130                                 ZUC_IV_KEY_LENGTH);
131         }
132
133
134         sess->op = mode;
135
136         return 0;
137 }
138
139 /** Get ZUC session. */
140 static struct zuc_session *
141 zuc_get_session(struct zuc_qp *qp, struct rte_crypto_op *op)
142 {
143         struct zuc_session *sess;
144
145         if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
146                 if (unlikely(op->sym->session->dev_type !=
147                                 RTE_CRYPTODEV_ZUC_PMD))
148                         return NULL;
149
150                 sess = (struct zuc_session *)op->sym->session->_private;
151         } else  {
152                 struct rte_cryptodev_session *c_sess = NULL;
153
154                 if (rte_mempool_get(qp->sess_mp, (void **)&c_sess))
155                         return NULL;
156
157                 sess = (struct zuc_session *)c_sess->_private;
158
159                 if (unlikely(zuc_set_session_parameters(sess,
160                                 op->sym->xform) != 0))
161                         return NULL;
162         }
163
164         return sess;
165 }
166
167 /** Encrypt/decrypt mbufs with same cipher key. */
168 static uint8_t
169 process_zuc_cipher_op(struct rte_crypto_op **ops,
170                 struct zuc_session *session,
171                 uint8_t num_ops)
172 {
173         unsigned i;
174         uint8_t processed_ops = 0;
175         uint8_t *src[ZUC_MAX_BURST], *dst[ZUC_MAX_BURST];
176         uint8_t *iv[ZUC_MAX_BURST];
177         uint32_t num_bytes[ZUC_MAX_BURST];
178         uint8_t *cipher_keys[ZUC_MAX_BURST];
179
180         for (i = 0; i < num_ops; i++) {
181                 /* Sanity checks. */
182                 if (unlikely(ops[i]->sym->cipher.iv.length != ZUC_IV_KEY_LENGTH)) {
183                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
184                         ZUC_LOG_ERR("iv");
185                         break;
186                 }
187
188                 if (((ops[i]->sym->cipher.data.length % BYTE_LEN) != 0)
189                                 || ((ops[i]->sym->cipher.data.offset
190                                         % BYTE_LEN) != 0)) {
191                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
192                         ZUC_LOG_ERR("Data Length or offset");
193                         break;
194                 }
195
196 #ifdef RTE_LIBRTE_PMD_ZUC_DEBUG
197                 if (!rte_pktmbuf_is_contiguous(ops[i]->sym->m_src) ||
198                                 (ops[i]->sym->m_dst != NULL &&
199                                 !rte_pktmbuf_is_contiguous(
200                                                 ops[i]->sym->m_dst))) {
201                         ZUC_LOG_ERR("PMD supports only contiguous mbufs, "
202                                 "op (%p) provides noncontiguous mbuf as "
203                                 "source/destination buffer.\n", ops[i]);
204                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
205                         break;
206                 }
207 #endif
208
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] = rte_crypto_op_ctod_offset(ops[i], uint8_t *,
217                                 ops[i]->sym->cipher.iv.offset);
218                 num_bytes[i] = ops[i]->sym->cipher.data.length >> 3;
219
220                 cipher_keys[i] = session->pKey_cipher;
221
222                 processed_ops++;
223         }
224
225         sso_zuc_eea3_n_buffer(cipher_keys, iv, src, dst,
226                         num_bytes, processed_ops);
227
228         return processed_ops;
229 }
230
231 /** Generate/verify hash from mbufs with same hash key. */
232 static int
233 process_zuc_hash_op(struct rte_crypto_op **ops,
234                 struct zuc_session *session,
235                 uint8_t num_ops)
236 {
237         unsigned i;
238         uint8_t processed_ops = 0;
239         uint8_t *src;
240         uint32_t *dst;
241         uint32_t length_in_bits;
242
243         for (i = 0; i < num_ops; i++) {
244                 if (unlikely(ops[i]->sym->auth.aad.length != ZUC_IV_KEY_LENGTH)) {
245                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
246                         ZUC_LOG_ERR("aad");
247                         break;
248                 }
249
250                 if (unlikely(ops[i]->sym->auth.digest.length != ZUC_DIGEST_LENGTH)) {
251                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
252                         ZUC_LOG_ERR("digest");
253                         break;
254                 }
255
256                 /* Data must be byte aligned */
257                 if ((ops[i]->sym->auth.data.offset % BYTE_LEN) != 0) {
258                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
259                         ZUC_LOG_ERR("Offset");
260                         break;
261                 }
262
263                 length_in_bits = ops[i]->sym->auth.data.length;
264
265                 src = rte_pktmbuf_mtod(ops[i]->sym->m_src, uint8_t *) +
266                                 (ops[i]->sym->auth.data.offset >> 3);
267
268                 if (session->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
269                         dst = (uint32_t *)rte_pktmbuf_append(ops[i]->sym->m_src,
270                                         ops[i]->sym->auth.digest.length);
271
272                         sso_zuc_eia3_1_buffer(session->pKey_hash,
273                                         ops[i]->sym->auth.aad.data, src,
274                                         length_in_bits, dst);
275                         /* Verify digest. */
276                         if (memcmp(dst, ops[i]->sym->auth.digest.data,
277                                         ops[i]->sym->auth.digest.length) != 0)
278                                 ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
279
280                         /* Trim area used for digest from mbuf. */
281                         rte_pktmbuf_trim(ops[i]->sym->m_src,
282                                         ops[i]->sym->auth.digest.length);
283                 } else  {
284                         dst = (uint32_t *)ops[i]->sym->auth.digest.data;
285
286                         sso_zuc_eia3_1_buffer(session->pKey_hash,
287                                         ops[i]->sym->auth.aad.data, src,
288                                         length_in_bits, dst);
289                 }
290                 processed_ops++;
291         }
292
293         return processed_ops;
294 }
295
296 /** Process a batch of crypto ops which shares the same session. */
297 static int
298 process_ops(struct rte_crypto_op **ops, struct zuc_session *session,
299                 struct zuc_qp *qp, uint8_t num_ops,
300                 uint16_t *accumulated_enqueued_ops)
301 {
302         unsigned i;
303         unsigned enqueued_ops, processed_ops;
304
305         switch (session->op) {
306         case ZUC_OP_ONLY_CIPHER:
307                 processed_ops = process_zuc_cipher_op(ops,
308                                 session, num_ops);
309                 break;
310         case ZUC_OP_ONLY_AUTH:
311                 processed_ops = process_zuc_hash_op(ops, session,
312                                 num_ops);
313                 break;
314         case ZUC_OP_CIPHER_AUTH:
315                 processed_ops = process_zuc_cipher_op(ops, session,
316                                 num_ops);
317                 process_zuc_hash_op(ops, session, processed_ops);
318                 break;
319         case ZUC_OP_AUTH_CIPHER:
320                 processed_ops = process_zuc_hash_op(ops, session,
321                                 num_ops);
322                 process_zuc_cipher_op(ops, session, processed_ops);
323                 break;
324         default:
325                 /* Operation not supported. */
326                 processed_ops = 0;
327         }
328
329         for (i = 0; i < num_ops; i++) {
330                 /*
331                  * If there was no error/authentication failure,
332                  * change status to successful.
333                  */
334                 if (ops[i]->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
335                         ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
336                 /* Free session if a session-less crypto op. */
337                 if (ops[i]->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
338                         rte_mempool_put(qp->sess_mp, ops[i]->sym->session);
339                         ops[i]->sym->session = NULL;
340                 }
341         }
342
343         enqueued_ops = rte_ring_enqueue_burst(qp->processed_ops,
344                         (void **)ops, processed_ops, NULL);
345         qp->qp_stats.enqueued_count += enqueued_ops;
346         *accumulated_enqueued_ops += enqueued_ops;
347
348         return enqueued_ops;
349 }
350
351 static uint16_t
352 zuc_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
353                 uint16_t nb_ops)
354 {
355         struct rte_crypto_op *c_ops[ZUC_MAX_BURST];
356         struct rte_crypto_op *curr_c_op;
357
358         struct zuc_session *prev_sess = NULL, *curr_sess = NULL;
359         struct zuc_qp *qp = queue_pair;
360         unsigned i;
361         uint8_t burst_size = 0;
362         uint16_t enqueued_ops = 0;
363         uint8_t processed_ops;
364
365         for (i = 0; i < nb_ops; i++) {
366                 curr_c_op = ops[i];
367
368                 /* Set status as enqueued (not processed yet) by default. */
369                 curr_c_op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
370
371                 curr_sess = zuc_get_session(qp, curr_c_op);
372                 if (unlikely(curr_sess == NULL ||
373                                 curr_sess->op == ZUC_OP_NOT_SUPPORTED)) {
374                         curr_c_op->status =
375                                         RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
376                         break;
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 == ZUC_MAX_BURST) {
390                                 processed_ops = process_ops(c_ops, prev_sess,
391                                                 qp, burst_size, &enqueued_ops);
392                                 if (processed_ops < burst_size) {
393                                         burst_size = 0;
394                                         break;
395                                 }
396
397                                 burst_size = 0;
398                                 prev_sess = NULL;
399                         }
400                 } else {
401                         /*
402                          * Different session, process the ops
403                          * of the previous session.
404                          */
405                         processed_ops = process_ops(c_ops, prev_sess,
406                                         qp, burst_size, &enqueued_ops);
407                         if (processed_ops < burst_size) {
408                                 burst_size = 0;
409                                 break;
410                         }
411
412                         burst_size = 0;
413                         prev_sess = curr_sess;
414
415                         c_ops[burst_size++] = curr_c_op;
416                 }
417         }
418
419         if (burst_size != 0) {
420                 /* Process the crypto ops of the last session. */
421                 processed_ops = process_ops(c_ops, prev_sess,
422                                 qp, burst_size, &enqueued_ops);
423         }
424
425         qp->qp_stats.enqueue_err_count += nb_ops - enqueued_ops;
426         return enqueued_ops;
427 }
428
429 static uint16_t
430 zuc_pmd_dequeue_burst(void *queue_pair,
431                 struct rte_crypto_op **c_ops, uint16_t nb_ops)
432 {
433         struct zuc_qp *qp = queue_pair;
434
435         unsigned nb_dequeued;
436
437         nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
438                         (void **)c_ops, nb_ops, NULL);
439         qp->qp_stats.dequeued_count += nb_dequeued;
440
441         return nb_dequeued;
442 }
443
444 static int cryptodev_zuc_remove(struct rte_vdev_device *vdev);
445
446 static int
447 cryptodev_zuc_create(const char *name,
448                 struct rte_vdev_device *vdev,
449                 struct rte_crypto_vdev_init_params *init_params)
450 {
451         struct rte_cryptodev *dev;
452         struct zuc_private *internals;
453         uint64_t cpu_flags = RTE_CRYPTODEV_FF_CPU_SSE;
454
455         if (init_params->name[0] == '\0')
456                 snprintf(init_params->name, sizeof(init_params->name),
457                                 "%s", name);
458
459         dev = rte_cryptodev_vdev_pmd_init(init_params->name,
460                         sizeof(struct zuc_private), init_params->socket_id,
461                         vdev);
462         if (dev == NULL) {
463                 ZUC_LOG_ERR("failed to create cryptodev vdev");
464                 goto init_error;
465         }
466
467         dev->dev_type = RTE_CRYPTODEV_ZUC_PMD;
468         dev->dev_ops = rte_zuc_pmd_ops;
469
470         /* Register RX/TX burst functions for data path. */
471         dev->dequeue_burst = zuc_pmd_dequeue_burst;
472         dev->enqueue_burst = zuc_pmd_enqueue_burst;
473
474         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
475                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
476                         cpu_flags;
477
478         internals = dev->data->dev_private;
479
480         internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
481         internals->max_nb_sessions = init_params->max_nb_sessions;
482
483         return 0;
484 init_error:
485         ZUC_LOG_ERR("driver %s: cryptodev_zuc_create failed",
486                         init_params->name);
487
488         cryptodev_zuc_remove(vdev);
489         return -EFAULT;
490 }
491
492 static int
493 cryptodev_zuc_probe(struct rte_vdev_device *vdev)
494 {
495         struct rte_crypto_vdev_init_params init_params = {
496                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
497                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
498                 rte_socket_id(),
499                 {0}
500         };
501         const char *name;
502         const char *input_args;
503
504         name = rte_vdev_device_name(vdev);
505         if (name == NULL)
506                 return -EINVAL;
507         input_args = rte_vdev_device_args(vdev);
508
509         rte_cryptodev_vdev_parse_init_params(&init_params, input_args);
510
511         RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
512                         init_params.socket_id);
513         if (init_params.name[0] != '\0')
514                 RTE_LOG(INFO, PMD, "  User defined name = %s\n",
515                         init_params.name);
516         RTE_LOG(INFO, PMD, "  Max number of queue pairs = %d\n",
517                         init_params.max_nb_queue_pairs);
518         RTE_LOG(INFO, PMD, "  Max number of sessions = %d\n",
519                         init_params.max_nb_sessions);
520
521         return cryptodev_zuc_create(name, vdev, &init_params);
522 }
523
524 static int
525 cryptodev_zuc_remove(struct rte_vdev_device *vdev)
526 {
527         const char *name;
528
529         name = rte_vdev_device_name(vdev);
530         if (name == NULL)
531                 return -EINVAL;
532
533         RTE_LOG(INFO, PMD, "Closing ZUC crypto device %s"
534                         " on numa socket %u\n",
535                         name, rte_socket_id());
536
537         return 0;
538 }
539
540 static struct rte_vdev_driver cryptodev_zuc_pmd_drv = {
541         .probe = cryptodev_zuc_probe,
542         .remove = cryptodev_zuc_remove
543 };
544
545 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_ZUC_PMD, cryptodev_zuc_pmd_drv);
546 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_ZUC_PMD,
547         "max_nb_queue_pairs=<int> "
548         "max_nb_sessions=<int> "
549         "socket_id=<int>");