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