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