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