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 /* Set IV parameters */
108 sess->iv.offset = cipher_xform->cipher.iv.offset;
109 sess->iv.length = cipher_xform->cipher.iv.length;
112 if (sess->iv.length != 16 && sess->iv.length != 12 &&
113 sess->iv.length != 0) {
114 GCM_LOG_ERR("Wrong IV length");
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;
126 GCM_LOG_ERR("Cipher/Auth operations: Encrypt/Generate or"
127 " Decrypt/Verify are valid only");
131 /* Check key length and calculate GCM pre-compute. */
132 switch (cipher_xform->cipher.key.length) {
134 aesni_gcm128_pre(cipher_xform->cipher.key.data, &sess->gdata);
135 sess->key = AESNI_GCM_KEY_128;
139 aesni_gcm256_pre(cipher_xform->cipher.key.data, &sess->gdata);
140 sess->key = AESNI_GCM_KEY_256;
144 GCM_LOG_ERR("Unsupported cipher key length");
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)
155 struct aesni_gcm_session *sess = NULL;
156 struct rte_crypto_sym_op *sym_op = op->sym;
158 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
159 if (unlikely(sym_op->session->dev_type
160 != RTE_CRYPTODEV_AESNI_GCM_PMD))
163 sess = (struct aesni_gcm_session *)sym_op->session->_private;
167 if (rte_mempool_get(qp->sess_mp, &_sess))
170 sess = (struct aesni_gcm_session *)
171 ((struct rte_cryptodev_sym_session *)_sess)->_private;
173 if (unlikely(aesni_gcm_set_session_parameters(sess,
174 sym_op->xform) != 0)) {
175 rte_mempool_put(qp->sess_mp, _sess);
183 * Process a crypto operation and complete a JOB_AES_HMAC job structure for
184 * submission to the multi buffer library for processing.
186 * @param qp queue pair
187 * @param op symmetric crypto operation
188 * @param session GCM session
194 process_gcm_crypto_op(struct rte_crypto_op *op,
195 struct aesni_gcm_session *session)
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;
204 RTE_ASSERT(m_src != NULL);
206 while (offset >= m_src->data_len) {
207 offset -= m_src->data_len;
210 RTE_ASSERT(m_src != NULL);
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;
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)));
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);
233 src = rte_pktmbuf_mtod_offset(m_src, uint8_t *, offset);
235 iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
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
241 if (session->iv.length == 12) {
242 uint32_t *iv_padd = (uint32_t *)&(iv_ptr[12]);
243 *iv_padd = rte_bswap32(1);
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");
253 if (session->op == AESNI_GCM_OP_AUTHENTICATED_ENCRYPTION) {
255 aesni_gcm_enc[session->key].init(&session->gdata,
257 sym_op->auth.aad.data,
258 (uint64_t)sym_op->auth.aad.length);
260 aesni_gcm_enc[session->key].update(&session->gdata, dst, src,
262 total_len = sym_op->cipher.data.length - part_len;
268 RTE_ASSERT(m_src != NULL);
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;
274 aesni_gcm_enc[session->key].update(&session->gdata,
277 total_len -= part_len;
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);
289 GCM_LOG_ERR("auth_tag");
293 aesni_gcm_dec[session->key].init(&session->gdata,
295 sym_op->auth.aad.data,
296 (uint64_t)sym_op->auth.aad.length);
298 aesni_gcm_dec[session->key].update(&session->gdata, dst, src,
300 total_len = sym_op->cipher.data.length - part_len;
306 RTE_ASSERT(m_src != NULL);
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;
312 aesni_gcm_dec[session->key].update(&session->gdata,
315 total_len -= part_len;
318 aesni_gcm_dec[session->key].finalize(&session->gdata,
320 (uint64_t)sym_op->auth.digest.length);
327 * Process a completed job and return rte_mbuf which job processed
329 * @param job JOB_AES_HMAC job to process
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
337 post_process_gcm_crypto_op(struct rte_crypto_op *op)
339 struct rte_mbuf *m = op->sym->m_dst ? op->sym->m_dst : op->sym->m_src;
341 struct aesni_gcm_session *session =
342 (struct aesni_gcm_session *)op->sym->session->_private;
344 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
346 /* Verify digest if required */
347 if (session->op == AESNI_GCM_OP_AUTHENTICATED_DECRYPTION) {
349 uint8_t *tag = rte_pktmbuf_mtod_offset(m, uint8_t *,
350 m->data_len - op->sym->auth.digest.length);
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);
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;
363 /* trim area used for digest from mbuf */
364 rte_pktmbuf_trim(m, op->sym->auth.digest.length);
369 * Process a completed GCM request
371 * @param qp Queue Pair to process
372 * @param job JOB_AES_HMAC job
375 * - Number of processed jobs
378 handle_completed_gcm_crypto_op(struct aesni_gcm_qp *qp,
379 struct rte_crypto_op *op)
381 post_process_gcm_crypto_op(op);
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;
391 aesni_gcm_pmd_dequeue_burst(void *queue_pair,
392 struct rte_crypto_op **ops, uint16_t nb_ops)
394 struct aesni_gcm_session *sess;
395 struct aesni_gcm_qp *qp = queue_pair;
398 unsigned int i, nb_dequeued;
400 nb_dequeued = rte_ring_dequeue_burst(qp->processed_pkts,
401 (void **)ops, nb_ops, NULL);
403 for (i = 0; i < nb_dequeued; i++) {
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++;
412 retval = process_gcm_crypto_op(ops[i], sess);
414 ops[i]->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
415 qp->qp_stats.dequeue_err_count++;
419 handle_completed_gcm_crypto_op(qp, ops[i]);
422 qp->qp_stats.dequeued_count += i;
428 aesni_gcm_pmd_enqueue_burst(void *queue_pair,
429 struct rte_crypto_op **ops, uint16_t nb_ops)
431 struct aesni_gcm_qp *qp = queue_pair;
433 unsigned int nb_enqueued;
435 nb_enqueued = rte_ring_enqueue_burst(qp->processed_pkts,
436 (void **)ops, nb_ops, NULL);
437 qp->qp_stats.enqueued_count += nb_enqueued;
442 static int aesni_gcm_remove(struct rte_vdev_device *vdev);
445 aesni_gcm_create(const char *name,
446 struct rte_vdev_device *vdev,
447 struct rte_crypto_vdev_init_params *init_params)
449 struct rte_cryptodev *dev;
450 struct aesni_gcm_private *internals;
452 if (init_params->name[0] == '\0')
453 snprintf(init_params->name, sizeof(init_params->name),
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");
462 dev = rte_cryptodev_vdev_pmd_init(init_params->name,
463 sizeof(struct aesni_gcm_private), init_params->socket_id,
466 GCM_LOG_ERR("failed to create cryptodev vdev");
470 dev->dev_type = RTE_CRYPTODEV_AESNI_GCM_PMD;
471 dev->dev_ops = rte_aesni_gcm_pmd_ops;
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;
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;
482 internals = dev->data->dev_private;
484 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
485 internals->max_nb_sessions = init_params->max_nb_sessions;
490 GCM_LOG_ERR("driver %s: create failed", init_params->name);
492 aesni_gcm_remove(vdev);
497 aesni_gcm_probe(struct rte_vdev_device *vdev)
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,
506 const char *input_args;
508 name = rte_vdev_device_name(vdev);
511 input_args = rte_vdev_device_args(vdev);
512 rte_cryptodev_vdev_parse_init_params(&init_params, input_args);
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",
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);
524 return aesni_gcm_create(name, vdev, &init_params);
528 aesni_gcm_remove(struct rte_vdev_device *vdev)
532 name = rte_vdev_device_name(vdev);
536 GCM_LOG_INFO("Closing AESNI crypto device %s on numa socket %u\n",
537 name, rte_socket_id());
542 static struct rte_vdev_driver aesni_gcm_pmd_drv = {
543 .probe = aesni_gcm_probe,
544 .remove = aesni_gcm_remove
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> "