cryptodev: move session type to generic crypto op
[dpdk.git] / drivers / crypto / aesni_gcm / aesni_gcm_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 #include <rte_byteorder.h>
43
44 #include "aesni_gcm_pmd_private.h"
45
46 /** GCM encode functions pointer table */
47 static const struct aesni_gcm_ops aesni_gcm_enc[] = {
48                 [AESNI_GCM_KEY_128] = {
49                                 aesni_gcm128_init,
50                                 aesni_gcm128_enc_update,
51                                 aesni_gcm128_enc_finalize
52                 },
53                 [AESNI_GCM_KEY_256] = {
54                                 aesni_gcm256_init,
55                                 aesni_gcm256_enc_update,
56                                 aesni_gcm256_enc_finalize
57                 }
58 };
59
60 /** GCM decode functions pointer table */
61 static const struct aesni_gcm_ops aesni_gcm_dec[] = {
62                 [AESNI_GCM_KEY_128] = {
63                                 aesni_gcm128_init,
64                                 aesni_gcm128_dec_update,
65                                 aesni_gcm128_dec_finalize
66                 },
67                 [AESNI_GCM_KEY_256] = {
68                                 aesni_gcm256_init,
69                                 aesni_gcm256_dec_update,
70                                 aesni_gcm256_dec_finalize
71                 }
72 };
73
74 /** Parse crypto xform chain and set private session parameters */
75 int
76 aesni_gcm_set_session_parameters(struct aesni_gcm_session *sess,
77                 const struct rte_crypto_sym_xform *xform)
78 {
79         const struct rte_crypto_sym_xform *auth_xform;
80         const struct rte_crypto_sym_xform *cipher_xform;
81
82         if (xform->next == NULL || xform->next->next != NULL) {
83                 GCM_LOG_ERR("Two and only two chained xform required");
84                 return -EINVAL;
85         }
86
87         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
88                         xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
89                 auth_xform = xform->next;
90                 cipher_xform = xform;
91         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
92                         xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
93                 auth_xform = xform;
94                 cipher_xform = xform->next;
95         } else {
96                 GCM_LOG_ERR("Cipher and auth xform required");
97                 return -EINVAL;
98         }
99
100         if (!(cipher_xform->cipher.algo == RTE_CRYPTO_CIPHER_AES_GCM &&
101                 (auth_xform->auth.algo == RTE_CRYPTO_AUTH_AES_GCM ||
102                         auth_xform->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC))) {
103                 GCM_LOG_ERR("We only support AES GCM and AES GMAC");
104                 return -EINVAL;
105         }
106
107         /* Select Crypto operation */
108         if (cipher_xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT &&
109                         auth_xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE)
110                 sess->op = AESNI_GCM_OP_AUTHENTICATED_ENCRYPTION;
111         else if (cipher_xform->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT &&
112                         auth_xform->auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
113                 sess->op = AESNI_GCM_OP_AUTHENTICATED_DECRYPTION;
114         else {
115                 GCM_LOG_ERR("Cipher/Auth operations: Encrypt/Generate or"
116                                 " Decrypt/Verify are valid only");
117                 return -EINVAL;
118         }
119
120         /* Check key length and calculate GCM pre-compute. */
121         switch (cipher_xform->cipher.key.length) {
122         case 16:
123                 aesni_gcm128_pre(cipher_xform->cipher.key.data, &sess->gdata);
124                 sess->key = AESNI_GCM_KEY_128;
125
126                 break;
127         case 32:
128                 aesni_gcm256_pre(cipher_xform->cipher.key.data, &sess->gdata);
129                 sess->key = AESNI_GCM_KEY_256;
130
131                 break;
132         default:
133                 GCM_LOG_ERR("Unsupported cipher key length");
134                 return -EINVAL;
135         }
136
137         return 0;
138 }
139
140 /** Get gcm session */
141 static struct aesni_gcm_session *
142 aesni_gcm_get_session(struct aesni_gcm_qp *qp, struct rte_crypto_op *op)
143 {
144         struct aesni_gcm_session *sess = NULL;
145         struct rte_crypto_sym_op *sym_op = op->sym;
146
147         if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
148                 if (unlikely(sym_op->session->dev_type
149                                         != RTE_CRYPTODEV_AESNI_GCM_PMD))
150                         return sess;
151
152                 sess = (struct aesni_gcm_session *)sym_op->session->_private;
153         } else  {
154                 void *_sess;
155
156                 if (rte_mempool_get(qp->sess_mp, &_sess))
157                         return sess;
158
159                 sess = (struct aesni_gcm_session *)
160                         ((struct rte_cryptodev_sym_session *)_sess)->_private;
161
162                 if (unlikely(aesni_gcm_set_session_parameters(sess,
163                                 sym_op->xform) != 0)) {
164                         rte_mempool_put(qp->sess_mp, _sess);
165                         sess = NULL;
166                 }
167         }
168         return sess;
169 }
170
171 /**
172  * Process a crypto operation and complete a JOB_AES_HMAC job structure for
173  * submission to the multi buffer library for processing.
174  *
175  * @param       qp              queue pair
176  * @param       op              symmetric crypto operation
177  * @param       session         GCM session
178  *
179  * @return
180  *
181  */
182 static int
183 process_gcm_crypto_op(struct rte_crypto_sym_op *op,
184                 struct aesni_gcm_session *session)
185 {
186         uint8_t *src, *dst;
187         struct rte_mbuf *m_src = op->m_src;
188         uint32_t offset = op->cipher.data.offset;
189         uint32_t part_len, total_len, data_len;
190
191         RTE_ASSERT(m_src != NULL);
192
193         while (offset >= m_src->data_len) {
194                 offset -= m_src->data_len;
195                 m_src = m_src->next;
196
197                 RTE_ASSERT(m_src != NULL);
198         }
199
200         data_len = m_src->data_len - offset;
201         part_len = (data_len < op->cipher.data.length) ? data_len :
202                         op->cipher.data.length;
203
204         /* Destination buffer is required when segmented source buffer */
205         RTE_ASSERT((part_len == op->cipher.data.length) ||
206                         ((part_len != op->cipher.data.length) &&
207                                         (op->m_dst != NULL)));
208         /* Segmented destination buffer is not supported */
209         RTE_ASSERT((op->m_dst == NULL) ||
210                         ((op->m_dst != NULL) &&
211                                         rte_pktmbuf_is_contiguous(op->m_dst)));
212
213
214         dst = op->m_dst ?
215                         rte_pktmbuf_mtod_offset(op->m_dst, uint8_t *,
216                                         op->cipher.data.offset) :
217                         rte_pktmbuf_mtod_offset(op->m_src, uint8_t *,
218                                         op->cipher.data.offset);
219
220         src = rte_pktmbuf_mtod_offset(m_src, uint8_t *, offset);
221
222         /* sanity checks */
223         if (op->cipher.iv.length != 16 && op->cipher.iv.length != 12 &&
224                         op->cipher.iv.length != 0) {
225                 GCM_LOG_ERR("iv");
226                 return -1;
227         }
228
229         /*
230          * GCM working in 12B IV mode => 16B pre-counter block we need
231          * to set BE LSB to 1, driver expects that 16B is allocated
232          */
233         if (op->cipher.iv.length == 12) {
234                 uint32_t *iv_padd = (uint32_t *)&op->cipher.iv.data[12];
235                 *iv_padd = rte_bswap32(1);
236         }
237
238         if (op->auth.digest.length != 16 &&
239                         op->auth.digest.length != 12 &&
240                         op->auth.digest.length != 8) {
241                 GCM_LOG_ERR("digest");
242                 return -1;
243         }
244
245         if (session->op == AESNI_GCM_OP_AUTHENTICATED_ENCRYPTION) {
246
247                 aesni_gcm_enc[session->key].init(&session->gdata,
248                                 op->cipher.iv.data,
249                                 op->auth.aad.data,
250                                 (uint64_t)op->auth.aad.length);
251
252                 aesni_gcm_enc[session->key].update(&session->gdata, dst, src,
253                                 (uint64_t)part_len);
254                 total_len = op->cipher.data.length - part_len;
255
256                 while (total_len) {
257                         dst += part_len;
258                         m_src = m_src->next;
259
260                         RTE_ASSERT(m_src != NULL);
261
262                         src = rte_pktmbuf_mtod(m_src, uint8_t *);
263                         part_len = (m_src->data_len < total_len) ?
264                                         m_src->data_len : total_len;
265
266                         aesni_gcm_enc[session->key].update(&session->gdata,
267                                         dst, src,
268                                         (uint64_t)part_len);
269                         total_len -= part_len;
270                 }
271
272                 aesni_gcm_enc[session->key].finalize(&session->gdata,
273                                 op->auth.digest.data,
274                                 (uint64_t)op->auth.digest.length);
275         } else { /* session->op == AESNI_GCM_OP_AUTHENTICATED_DECRYPTION */
276                 uint8_t *auth_tag = (uint8_t *)rte_pktmbuf_append(op->m_dst ?
277                                 op->m_dst : op->m_src,
278                                 op->auth.digest.length);
279
280                 if (!auth_tag) {
281                         GCM_LOG_ERR("auth_tag");
282                         return -1;
283                 }
284
285                 aesni_gcm_dec[session->key].init(&session->gdata,
286                                 op->cipher.iv.data,
287                                 op->auth.aad.data,
288                                 (uint64_t)op->auth.aad.length);
289
290                 aesni_gcm_dec[session->key].update(&session->gdata, dst, src,
291                                 (uint64_t)part_len);
292                 total_len = op->cipher.data.length - part_len;
293
294                 while (total_len) {
295                         dst += part_len;
296                         m_src = m_src->next;
297
298                         RTE_ASSERT(m_src != NULL);
299
300                         src = rte_pktmbuf_mtod(m_src, uint8_t *);
301                         part_len = (m_src->data_len < total_len) ?
302                                         m_src->data_len : total_len;
303
304                         aesni_gcm_dec[session->key].update(&session->gdata,
305                                         dst, src,
306                                         (uint64_t)part_len);
307                         total_len -= part_len;
308                 }
309
310                 aesni_gcm_dec[session->key].finalize(&session->gdata,
311                                 auth_tag,
312                                 (uint64_t)op->auth.digest.length);
313         }
314
315         return 0;
316 }
317
318 /**
319  * Process a completed job and return rte_mbuf which job processed
320  *
321  * @param job   JOB_AES_HMAC job to process
322  *
323  * @return
324  * - Returns processed mbuf which is trimmed of output digest used in
325  * verification of supplied digest in the case of a HASH_CIPHER operation
326  * - Returns NULL on invalid job
327  */
328 static void
329 post_process_gcm_crypto_op(struct rte_crypto_op *op)
330 {
331         struct rte_mbuf *m = op->sym->m_dst ? op->sym->m_dst : op->sym->m_src;
332
333         struct aesni_gcm_session *session =
334                 (struct aesni_gcm_session *)op->sym->session->_private;
335
336         op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
337
338         /* Verify digest if required */
339         if (session->op == AESNI_GCM_OP_AUTHENTICATED_DECRYPTION) {
340
341                 uint8_t *tag = rte_pktmbuf_mtod_offset(m, uint8_t *,
342                                 m->data_len - op->sym->auth.digest.length);
343
344 #ifdef RTE_LIBRTE_PMD_AESNI_GCM_DEBUG
345                 rte_hexdump(stdout, "auth tag (orig):",
346                                 op->sym->auth.digest.data, op->sym->auth.digest.length);
347                 rte_hexdump(stdout, "auth tag (calc):",
348                                 tag, op->sym->auth.digest.length);
349 #endif
350
351                 if (memcmp(tag, op->sym->auth.digest.data,
352                                 op->sym->auth.digest.length) != 0)
353                         op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
354
355                 /* trim area used for digest from mbuf */
356                 rte_pktmbuf_trim(m, op->sym->auth.digest.length);
357         }
358 }
359
360 /**
361  * Process a completed GCM request
362  *
363  * @param qp            Queue Pair to process
364  * @param job           JOB_AES_HMAC job
365  *
366  * @return
367  * - Number of processed jobs
368  */
369 static void
370 handle_completed_gcm_crypto_op(struct aesni_gcm_qp *qp,
371                 struct rte_crypto_op *op)
372 {
373         post_process_gcm_crypto_op(op);
374
375         /* Free session if a session-less crypto op */
376         if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
377                 rte_mempool_put(qp->sess_mp, op->sym->session);
378                 op->sym->session = NULL;
379         }
380 }
381
382 static uint16_t
383 aesni_gcm_pmd_dequeue_burst(void *queue_pair,
384                 struct rte_crypto_op **ops, uint16_t nb_ops)
385 {
386         struct aesni_gcm_session *sess;
387         struct aesni_gcm_qp *qp = queue_pair;
388
389         int retval = 0;
390         unsigned int i, nb_dequeued;
391
392         nb_dequeued = rte_ring_dequeue_burst(qp->processed_pkts,
393                         (void **)ops, nb_ops, NULL);
394
395         for (i = 0; i < nb_dequeued; i++) {
396
397                 sess = aesni_gcm_get_session(qp, ops[i]);
398                 if (unlikely(sess == NULL)) {
399                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
400                         qp->qp_stats.dequeue_err_count++;
401                         break;
402                 }
403
404                 retval = process_gcm_crypto_op(ops[i]->sym, sess);
405                 if (retval < 0) {
406                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
407                         qp->qp_stats.dequeue_err_count++;
408                         break;
409                 }
410
411                 handle_completed_gcm_crypto_op(qp, ops[i]);
412         }
413
414         qp->qp_stats.dequeued_count += i;
415
416         return i;
417 }
418
419 static uint16_t
420 aesni_gcm_pmd_enqueue_burst(void *queue_pair,
421                 struct rte_crypto_op **ops, uint16_t nb_ops)
422 {
423         struct aesni_gcm_qp *qp = queue_pair;
424
425         unsigned int nb_enqueued;
426
427         nb_enqueued = rte_ring_enqueue_burst(qp->processed_pkts,
428                         (void **)ops, nb_ops, NULL);
429         qp->qp_stats.enqueued_count += nb_enqueued;
430
431         return nb_enqueued;
432 }
433
434 static int aesni_gcm_remove(struct rte_vdev_device *vdev);
435
436 static int
437 aesni_gcm_create(const char *name,
438                 struct rte_vdev_device *vdev,
439                 struct rte_crypto_vdev_init_params *init_params)
440 {
441         struct rte_cryptodev *dev;
442         struct aesni_gcm_private *internals;
443
444         if (init_params->name[0] == '\0')
445                 snprintf(init_params->name, sizeof(init_params->name),
446                                 "%s", name);
447
448         /* Check CPU for support for AES instruction set */
449         if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_AES)) {
450                 GCM_LOG_ERR("AES instructions not supported by CPU");
451                 return -EFAULT;
452         }
453
454         dev = rte_cryptodev_vdev_pmd_init(init_params->name,
455                         sizeof(struct aesni_gcm_private), init_params->socket_id,
456                         vdev);
457         if (dev == NULL) {
458                 GCM_LOG_ERR("failed to create cryptodev vdev");
459                 goto init_error;
460         }
461
462         dev->dev_type = RTE_CRYPTODEV_AESNI_GCM_PMD;
463         dev->dev_ops = rte_aesni_gcm_pmd_ops;
464
465         /* register rx/tx burst functions for data path */
466         dev->dequeue_burst = aesni_gcm_pmd_dequeue_burst;
467         dev->enqueue_burst = aesni_gcm_pmd_enqueue_burst;
468
469         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
470                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
471                         RTE_CRYPTODEV_FF_CPU_AESNI |
472                         RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER;
473
474         internals = dev->data->dev_private;
475
476         internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
477         internals->max_nb_sessions = init_params->max_nb_sessions;
478
479         return 0;
480
481 init_error:
482         GCM_LOG_ERR("driver %s: create failed", init_params->name);
483
484         aesni_gcm_remove(vdev);
485         return -EFAULT;
486 }
487
488 static int
489 aesni_gcm_probe(struct rte_vdev_device *vdev)
490 {
491         struct rte_crypto_vdev_init_params init_params = {
492                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
493                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
494                 rte_socket_id(),
495                 {0}
496         };
497         const char *name;
498         const char *input_args;
499
500         name = rte_vdev_device_name(vdev);
501         if (name == NULL)
502                 return -EINVAL;
503         input_args = rte_vdev_device_args(vdev);
504         rte_cryptodev_vdev_parse_init_params(&init_params, input_args);
505
506         RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
507                         init_params.socket_id);
508         if (init_params.name[0] != '\0')
509                 RTE_LOG(INFO, PMD, "  User defined name = %s\n",
510                         init_params.name);
511         RTE_LOG(INFO, PMD, "  Max number of queue pairs = %d\n",
512                         init_params.max_nb_queue_pairs);
513         RTE_LOG(INFO, PMD, "  Max number of sessions = %d\n",
514                         init_params.max_nb_sessions);
515
516         return aesni_gcm_create(name, vdev, &init_params);
517 }
518
519 static int
520 aesni_gcm_remove(struct rte_vdev_device *vdev)
521 {
522         const char *name;
523
524         name = rte_vdev_device_name(vdev);
525         if (name == NULL)
526                 return -EINVAL;
527
528         GCM_LOG_INFO("Closing AESNI crypto device %s on numa socket %u\n",
529                         name, rte_socket_id());
530
531         return 0;
532 }
533
534 static struct rte_vdev_driver aesni_gcm_pmd_drv = {
535         .probe = aesni_gcm_probe,
536         .remove = aesni_gcm_remove
537 };
538
539 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_AESNI_GCM_PMD, aesni_gcm_pmd_drv);
540 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_AESNI_GCM_PMD, cryptodev_aesni_gcm_pmd);
541 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_AESNI_GCM_PMD,
542         "max_nb_queue_pairs=<int> "
543         "max_nb_sessions=<int> "
544         "socket_id=<int>");