4 * Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
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.
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>
40 #include <rte_malloc.h>
41 #include <rte_cpuflags.h>
42 #include <rte_byteorder.h>
44 #include "aesni_gcm_pmd_private.h"
46 /** GCM encode functions pointer table */
47 static const struct aesni_gcm_ops aesni_gcm_enc[] = {
48 [AESNI_GCM_KEY_128] = {
50 aesni_gcm128_enc_update,
51 aesni_gcm128_enc_finalize
53 [AESNI_GCM_KEY_256] = {
55 aesni_gcm256_enc_update,
56 aesni_gcm256_enc_finalize
60 /** GCM decode functions pointer table */
61 static const struct aesni_gcm_ops aesni_gcm_dec[] = {
62 [AESNI_GCM_KEY_128] = {
64 aesni_gcm128_dec_update,
65 aesni_gcm128_dec_finalize
67 [AESNI_GCM_KEY_256] = {
69 aesni_gcm256_dec_update,
70 aesni_gcm256_dec_finalize
74 /** Parse crypto xform chain and set private session parameters */
76 aesni_gcm_set_session_parameters(struct aesni_gcm_session *sess,
77 const struct rte_crypto_sym_xform *xform)
79 const struct rte_crypto_sym_xform *auth_xform;
80 const struct rte_crypto_sym_xform *cipher_xform;
82 if (xform->next == NULL || xform->next->next != NULL) {
83 GCM_LOG_ERR("Two and only two chained xform required");
87 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
88 xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
89 auth_xform = xform->next;
91 } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
92 xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
94 cipher_xform = xform->next;
96 GCM_LOG_ERR("Cipher and auth xform required");
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");
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;
115 GCM_LOG_ERR("Cipher/Auth operations: Encrypt/Generate or"
116 " Decrypt/Verify are valid only");
120 /* Check key length and calculate GCM pre-compute. */
121 switch (cipher_xform->cipher.key.length) {
123 aesni_gcm128_pre(cipher_xform->cipher.key.data, &sess->gdata);
124 sess->key = AESNI_GCM_KEY_128;
128 aesni_gcm256_pre(cipher_xform->cipher.key.data, &sess->gdata);
129 sess->key = AESNI_GCM_KEY_256;
133 GCM_LOG_ERR("Unsupported cipher key length");
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)
144 struct aesni_gcm_session *sess = NULL;
145 struct rte_crypto_sym_op *sym_op = op->sym;
147 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
148 if (unlikely(sym_op->session->dev_type
149 != RTE_CRYPTODEV_AESNI_GCM_PMD))
152 sess = (struct aesni_gcm_session *)sym_op->session->_private;
156 if (rte_mempool_get(qp->sess_mp, &_sess))
159 sess = (struct aesni_gcm_session *)
160 ((struct rte_cryptodev_sym_session *)_sess)->_private;
162 if (unlikely(aesni_gcm_set_session_parameters(sess,
163 sym_op->xform) != 0)) {
164 rte_mempool_put(qp->sess_mp, _sess);
172 * Process a crypto operation and complete a JOB_AES_HMAC job structure for
173 * submission to the multi buffer library for processing.
175 * @param qp queue pair
176 * @param op symmetric crypto operation
177 * @param session GCM session
183 process_gcm_crypto_op(struct rte_crypto_op *op,
184 struct aesni_gcm_session *session)
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;
193 RTE_ASSERT(m_src != NULL);
195 while (offset >= m_src->data_len) {
196 offset -= m_src->data_len;
199 RTE_ASSERT(m_src != NULL);
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;
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)));
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);
222 src = rte_pktmbuf_mtod_offset(m_src, uint8_t *, offset);
225 if (sym_op->cipher.iv.length != 16 && sym_op->cipher.iv.length != 12 &&
226 sym_op->cipher.iv.length != 0) {
231 iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
232 sym_op->cipher.iv.offset);
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
237 if (sym_op->cipher.iv.length == 12) {
238 uint32_t *iv_padd = (uint32_t *)&(iv_ptr[12]);
239 *iv_padd = rte_bswap32(1);
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");
249 if (session->op == AESNI_GCM_OP_AUTHENTICATED_ENCRYPTION) {
251 aesni_gcm_enc[session->key].init(&session->gdata,
253 sym_op->auth.aad.data,
254 (uint64_t)sym_op->auth.aad.length);
256 aesni_gcm_enc[session->key].update(&session->gdata, dst, src,
258 total_len = sym_op->cipher.data.length - part_len;
264 RTE_ASSERT(m_src != NULL);
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;
270 aesni_gcm_enc[session->key].update(&session->gdata,
273 total_len -= part_len;
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);
285 GCM_LOG_ERR("auth_tag");
289 aesni_gcm_dec[session->key].init(&session->gdata,
291 sym_op->auth.aad.data,
292 (uint64_t)sym_op->auth.aad.length);
294 aesni_gcm_dec[session->key].update(&session->gdata, dst, src,
296 total_len = sym_op->cipher.data.length - part_len;
302 RTE_ASSERT(m_src != NULL);
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;
308 aesni_gcm_dec[session->key].update(&session->gdata,
311 total_len -= part_len;
314 aesni_gcm_dec[session->key].finalize(&session->gdata,
316 (uint64_t)sym_op->auth.digest.length);
323 * Process a completed job and return rte_mbuf which job processed
325 * @param job JOB_AES_HMAC job to process
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
333 post_process_gcm_crypto_op(struct rte_crypto_op *op)
335 struct rte_mbuf *m = op->sym->m_dst ? op->sym->m_dst : op->sym->m_src;
337 struct aesni_gcm_session *session =
338 (struct aesni_gcm_session *)op->sym->session->_private;
340 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
342 /* Verify digest if required */
343 if (session->op == AESNI_GCM_OP_AUTHENTICATED_DECRYPTION) {
345 uint8_t *tag = rte_pktmbuf_mtod_offset(m, uint8_t *,
346 m->data_len - op->sym->auth.digest.length);
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);
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;
359 /* trim area used for digest from mbuf */
360 rte_pktmbuf_trim(m, op->sym->auth.digest.length);
365 * Process a completed GCM request
367 * @param qp Queue Pair to process
368 * @param job JOB_AES_HMAC job
371 * - Number of processed jobs
374 handle_completed_gcm_crypto_op(struct aesni_gcm_qp *qp,
375 struct rte_crypto_op *op)
377 post_process_gcm_crypto_op(op);
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;
387 aesni_gcm_pmd_dequeue_burst(void *queue_pair,
388 struct rte_crypto_op **ops, uint16_t nb_ops)
390 struct aesni_gcm_session *sess;
391 struct aesni_gcm_qp *qp = queue_pair;
394 unsigned int i, nb_dequeued;
396 nb_dequeued = rte_ring_dequeue_burst(qp->processed_pkts,
397 (void **)ops, nb_ops, NULL);
399 for (i = 0; i < nb_dequeued; i++) {
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++;
408 retval = process_gcm_crypto_op(ops[i], sess);
410 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
411 qp->qp_stats.dequeue_err_count++;
415 handle_completed_gcm_crypto_op(qp, ops[i]);
418 qp->qp_stats.dequeued_count += i;
424 aesni_gcm_pmd_enqueue_burst(void *queue_pair,
425 struct rte_crypto_op **ops, uint16_t nb_ops)
427 struct aesni_gcm_qp *qp = queue_pair;
429 unsigned int nb_enqueued;
431 nb_enqueued = rte_ring_enqueue_burst(qp->processed_pkts,
432 (void **)ops, nb_ops, NULL);
433 qp->qp_stats.enqueued_count += nb_enqueued;
438 static int aesni_gcm_remove(struct rte_vdev_device *vdev);
441 aesni_gcm_create(const char *name,
442 struct rte_vdev_device *vdev,
443 struct rte_crypto_vdev_init_params *init_params)
445 struct rte_cryptodev *dev;
446 struct aesni_gcm_private *internals;
448 if (init_params->name[0] == '\0')
449 snprintf(init_params->name, sizeof(init_params->name),
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");
458 dev = rte_cryptodev_vdev_pmd_init(init_params->name,
459 sizeof(struct aesni_gcm_private), init_params->socket_id,
462 GCM_LOG_ERR("failed to create cryptodev vdev");
466 dev->dev_type = RTE_CRYPTODEV_AESNI_GCM_PMD;
467 dev->dev_ops = rte_aesni_gcm_pmd_ops;
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;
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;
478 internals = dev->data->dev_private;
480 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
481 internals->max_nb_sessions = init_params->max_nb_sessions;
486 GCM_LOG_ERR("driver %s: create failed", init_params->name);
488 aesni_gcm_remove(vdev);
493 aesni_gcm_probe(struct rte_vdev_device *vdev)
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,
502 const char *input_args;
504 name = rte_vdev_device_name(vdev);
507 input_args = rte_vdev_device_args(vdev);
508 rte_cryptodev_vdev_parse_init_params(&init_params, input_args);
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",
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);
520 return aesni_gcm_create(name, vdev, &init_params);
524 aesni_gcm_remove(struct rte_vdev_device *vdev)
528 name = rte_vdev_device_name(vdev);
532 GCM_LOG_INFO("Closing AESNI crypto device %s on numa socket %u\n",
533 name, rte_socket_id());
538 static struct rte_vdev_driver aesni_gcm_pmd_drv = {
539 .probe = aesni_gcm_probe,
540 .remove = aesni_gcm_remove
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> "