61be723ee7b5fa9effca681989699145d0f56b31
[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_op *op,
184                 struct aesni_gcm_session *session)
185 {
186         uint8_t *src, *dst;
187         uint8_t *iv_ptr;
188         struct rte_crypto_sym_op *sym_op = op->sym;
189         struct rte_mbuf *m_src = sym_op->m_src;
190         uint32_t offset = sym_op->cipher.data.offset;
191         uint32_t part_len, total_len, data_len;
192
193         RTE_ASSERT(m_src != NULL);
194
195         while (offset >= m_src->data_len) {
196                 offset -= m_src->data_len;
197                 m_src = m_src->next;
198
199                 RTE_ASSERT(m_src != NULL);
200         }
201
202         data_len = m_src->data_len - offset;
203         part_len = (data_len < sym_op->cipher.data.length) ? data_len :
204                         sym_op->cipher.data.length;
205
206         /* Destination buffer is required when segmented source buffer */
207         RTE_ASSERT((part_len == sym_op->cipher.data.length) ||
208                         ((part_len != sym_op->cipher.data.length) &&
209                                         (sym_op->m_dst != NULL)));
210         /* Segmented destination buffer is not supported */
211         RTE_ASSERT((sym_op->m_dst == NULL) ||
212                         ((sym_op->m_dst != NULL) &&
213                                         rte_pktmbuf_is_contiguous(sym_op->m_dst)));
214
215
216         dst = sym_op->m_dst ?
217                         rte_pktmbuf_mtod_offset(sym_op->m_dst, uint8_t *,
218                                         sym_op->cipher.data.offset) :
219                         rte_pktmbuf_mtod_offset(sym_op->m_src, uint8_t *,
220                                         sym_op->cipher.data.offset);
221
222         src = rte_pktmbuf_mtod_offset(m_src, uint8_t *, offset);
223
224         /* sanity checks */
225         if (sym_op->cipher.iv.length != 16 && sym_op->cipher.iv.length != 12 &&
226                         sym_op->cipher.iv.length != 0) {
227                 GCM_LOG_ERR("iv");
228                 return -1;
229         }
230
231         iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
232                                 sym_op->cipher.iv.offset);
233         /*
234          * GCM working in 12B IV mode => 16B pre-counter block we need
235          * to set BE LSB to 1, driver expects that 16B is allocated
236          */
237         if (sym_op->cipher.iv.length == 12) {
238                 uint32_t *iv_padd = (uint32_t *)&(iv_ptr[12]);
239                 *iv_padd = rte_bswap32(1);
240         }
241
242         if (sym_op->auth.digest.length != 16 &&
243                         sym_op->auth.digest.length != 12 &&
244                         sym_op->auth.digest.length != 8) {
245                 GCM_LOG_ERR("digest");
246                 return -1;
247         }
248
249         if (session->op == AESNI_GCM_OP_AUTHENTICATED_ENCRYPTION) {
250
251                 aesni_gcm_enc[session->key].init(&session->gdata,
252                                 iv_ptr,
253                                 sym_op->auth.aad.data,
254                                 (uint64_t)sym_op->auth.aad.length);
255
256                 aesni_gcm_enc[session->key].update(&session->gdata, dst, src,
257                                 (uint64_t)part_len);
258                 total_len = sym_op->cipher.data.length - part_len;
259
260                 while (total_len) {
261                         dst += part_len;
262                         m_src = m_src->next;
263
264                         RTE_ASSERT(m_src != NULL);
265
266                         src = rte_pktmbuf_mtod(m_src, uint8_t *);
267                         part_len = (m_src->data_len < total_len) ?
268                                         m_src->data_len : total_len;
269
270                         aesni_gcm_enc[session->key].update(&session->gdata,
271                                         dst, src,
272                                         (uint64_t)part_len);
273                         total_len -= part_len;
274                 }
275
276                 aesni_gcm_enc[session->key].finalize(&session->gdata,
277                                 sym_op->auth.digest.data,
278                                 (uint64_t)sym_op->auth.digest.length);
279         } else { /* session->op == AESNI_GCM_OP_AUTHENTICATED_DECRYPTION */
280                 uint8_t *auth_tag = (uint8_t *)rte_pktmbuf_append(sym_op->m_dst ?
281                                 sym_op->m_dst : sym_op->m_src,
282                                 sym_op->auth.digest.length);
283
284                 if (!auth_tag) {
285                         GCM_LOG_ERR("auth_tag");
286                         return -1;
287                 }
288
289                 aesni_gcm_dec[session->key].init(&session->gdata,
290                                 iv_ptr,
291                                 sym_op->auth.aad.data,
292                                 (uint64_t)sym_op->auth.aad.length);
293
294                 aesni_gcm_dec[session->key].update(&session->gdata, dst, src,
295                                 (uint64_t)part_len);
296                 total_len = sym_op->cipher.data.length - part_len;
297
298                 while (total_len) {
299                         dst += part_len;
300                         m_src = m_src->next;
301
302                         RTE_ASSERT(m_src != NULL);
303
304                         src = rte_pktmbuf_mtod(m_src, uint8_t *);
305                         part_len = (m_src->data_len < total_len) ?
306                                         m_src->data_len : total_len;
307
308                         aesni_gcm_dec[session->key].update(&session->gdata,
309                                         dst, src,
310                                         (uint64_t)part_len);
311                         total_len -= part_len;
312                 }
313
314                 aesni_gcm_dec[session->key].finalize(&session->gdata,
315                                 auth_tag,
316                                 (uint64_t)sym_op->auth.digest.length);
317         }
318
319         return 0;
320 }
321
322 /**
323  * Process a completed job and return rte_mbuf which job processed
324  *
325  * @param job   JOB_AES_HMAC job to process
326  *
327  * @return
328  * - Returns processed mbuf which is trimmed of output digest used in
329  * verification of supplied digest in the case of a HASH_CIPHER operation
330  * - Returns NULL on invalid job
331  */
332 static void
333 post_process_gcm_crypto_op(struct rte_crypto_op *op)
334 {
335         struct rte_mbuf *m = op->sym->m_dst ? op->sym->m_dst : op->sym->m_src;
336
337         struct aesni_gcm_session *session =
338                 (struct aesni_gcm_session *)op->sym->session->_private;
339
340         op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
341
342         /* Verify digest if required */
343         if (session->op == AESNI_GCM_OP_AUTHENTICATED_DECRYPTION) {
344
345                 uint8_t *tag = rte_pktmbuf_mtod_offset(m, uint8_t *,
346                                 m->data_len - op->sym->auth.digest.length);
347
348 #ifdef RTE_LIBRTE_PMD_AESNI_GCM_DEBUG
349                 rte_hexdump(stdout, "auth tag (orig):",
350                                 op->sym->auth.digest.data, op->sym->auth.digest.length);
351                 rte_hexdump(stdout, "auth tag (calc):",
352                                 tag, op->sym->auth.digest.length);
353 #endif
354
355                 if (memcmp(tag, op->sym->auth.digest.data,
356                                 op->sym->auth.digest.length) != 0)
357                         op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
358
359                 /* trim area used for digest from mbuf */
360                 rte_pktmbuf_trim(m, op->sym->auth.digest.length);
361         }
362 }
363
364 /**
365  * Process a completed GCM request
366  *
367  * @param qp            Queue Pair to process
368  * @param job           JOB_AES_HMAC job
369  *
370  * @return
371  * - Number of processed jobs
372  */
373 static void
374 handle_completed_gcm_crypto_op(struct aesni_gcm_qp *qp,
375                 struct rte_crypto_op *op)
376 {
377         post_process_gcm_crypto_op(op);
378
379         /* Free session if a session-less crypto op */
380         if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
381                 rte_mempool_put(qp->sess_mp, op->sym->session);
382                 op->sym->session = NULL;
383         }
384 }
385
386 static uint16_t
387 aesni_gcm_pmd_dequeue_burst(void *queue_pair,
388                 struct rte_crypto_op **ops, uint16_t nb_ops)
389 {
390         struct aesni_gcm_session *sess;
391         struct aesni_gcm_qp *qp = queue_pair;
392
393         int retval = 0;
394         unsigned int i, nb_dequeued;
395
396         nb_dequeued = rte_ring_dequeue_burst(qp->processed_pkts,
397                         (void **)ops, nb_ops, NULL);
398
399         for (i = 0; i < nb_dequeued; i++) {
400
401                 sess = aesni_gcm_get_session(qp, ops[i]);
402                 if (unlikely(sess == NULL)) {
403                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
404                         qp->qp_stats.dequeue_err_count++;
405                         break;
406                 }
407
408                 retval = process_gcm_crypto_op(ops[i], sess);
409                 if (retval < 0) {
410                         ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
411                         qp->qp_stats.dequeue_err_count++;
412                         break;
413                 }
414
415                 handle_completed_gcm_crypto_op(qp, ops[i]);
416         }
417
418         qp->qp_stats.dequeued_count += i;
419
420         return i;
421 }
422
423 static uint16_t
424 aesni_gcm_pmd_enqueue_burst(void *queue_pair,
425                 struct rte_crypto_op **ops, uint16_t nb_ops)
426 {
427         struct aesni_gcm_qp *qp = queue_pair;
428
429         unsigned int nb_enqueued;
430
431         nb_enqueued = rte_ring_enqueue_burst(qp->processed_pkts,
432                         (void **)ops, nb_ops, NULL);
433         qp->qp_stats.enqueued_count += nb_enqueued;
434
435         return nb_enqueued;
436 }
437
438 static int aesni_gcm_remove(struct rte_vdev_device *vdev);
439
440 static int
441 aesni_gcm_create(const char *name,
442                 struct rte_vdev_device *vdev,
443                 struct rte_crypto_vdev_init_params *init_params)
444 {
445         struct rte_cryptodev *dev;
446         struct aesni_gcm_private *internals;
447
448         if (init_params->name[0] == '\0')
449                 snprintf(init_params->name, sizeof(init_params->name),
450                                 "%s", name);
451
452         /* Check CPU for support for AES instruction set */
453         if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_AES)) {
454                 GCM_LOG_ERR("AES instructions not supported by CPU");
455                 return -EFAULT;
456         }
457
458         dev = rte_cryptodev_vdev_pmd_init(init_params->name,
459                         sizeof(struct aesni_gcm_private), init_params->socket_id,
460                         vdev);
461         if (dev == NULL) {
462                 GCM_LOG_ERR("failed to create cryptodev vdev");
463                 goto init_error;
464         }
465
466         dev->dev_type = RTE_CRYPTODEV_AESNI_GCM_PMD;
467         dev->dev_ops = rte_aesni_gcm_pmd_ops;
468
469         /* register rx/tx burst functions for data path */
470         dev->dequeue_burst = aesni_gcm_pmd_dequeue_burst;
471         dev->enqueue_burst = aesni_gcm_pmd_enqueue_burst;
472
473         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
474                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
475                         RTE_CRYPTODEV_FF_CPU_AESNI |
476                         RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER;
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
485 init_error:
486         GCM_LOG_ERR("driver %s: create failed", init_params->name);
487
488         aesni_gcm_remove(vdev);
489         return -EFAULT;
490 }
491
492 static int
493 aesni_gcm_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         rte_cryptodev_vdev_parse_init_params(&init_params, input_args);
509
510         RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
511                         init_params.socket_id);
512         if (init_params.name[0] != '\0')
513                 RTE_LOG(INFO, PMD, "  User defined name = %s\n",
514                         init_params.name);
515         RTE_LOG(INFO, PMD, "  Max number of queue pairs = %d\n",
516                         init_params.max_nb_queue_pairs);
517         RTE_LOG(INFO, PMD, "  Max number of sessions = %d\n",
518                         init_params.max_nb_sessions);
519
520         return aesni_gcm_create(name, vdev, &init_params);
521 }
522
523 static int
524 aesni_gcm_remove(struct rte_vdev_device *vdev)
525 {
526         const char *name;
527
528         name = rte_vdev_device_name(vdev);
529         if (name == NULL)
530                 return -EINVAL;
531
532         GCM_LOG_INFO("Closing AESNI crypto device %s on numa socket %u\n",
533                         name, rte_socket_id());
534
535         return 0;
536 }
537
538 static struct rte_vdev_driver aesni_gcm_pmd_drv = {
539         .probe = aesni_gcm_probe,
540         .remove = aesni_gcm_remove
541 };
542
543 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_AESNI_GCM_PMD, aesni_gcm_pmd_drv);
544 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_AESNI_GCM_PMD, cryptodev_aesni_gcm_pmd);
545 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_AESNI_GCM_PMD,
546         "max_nb_queue_pairs=<int> "
547         "max_nb_sessions=<int> "
548         "socket_id=<int>");