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