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