cryptodev: rename PMD symmetric session API
[dpdk.git] / drivers / crypto / qat / qat_sym.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2018 Intel Corporation
3  */
4
5 #ifndef _QAT_SYM_H_
6 #define _QAT_SYM_H_
7
8 #include <rte_cryptodev_pmd.h>
9
10 #ifdef BUILD_QAT_SYM
11 #include <openssl/evp.h>
12
13 #include "qat_common.h"
14 #include "qat_sym_session.h"
15 #include "qat_sym_pmd.h"
16 #include "qat_logs.h"
17
18 #define BYTE_LENGTH    8
19 /* bpi is only used for partial blocks of DES and AES
20  * so AES block len can be assumed as max len for iv, src and dst
21  */
22 #define BPI_MAX_ENCR_IV_LEN ICP_QAT_HW_AES_BLK_SZ
23
24 struct qat_sym_session;
25
26 struct qat_sym_op_cookie {
27         struct qat_sgl qat_sgl_src;
28         struct qat_sgl qat_sgl_dst;
29         phys_addr_t qat_sgl_src_phys_addr;
30         phys_addr_t qat_sgl_dst_phys_addr;
31 };
32
33 int
34 qat_sym_build_request(void *in_op, uint8_t *out_msg,
35                 void *op_cookie, enum qat_device_gen qat_dev_gen);
36
37
38 /** Encrypt a single partial block
39  *  Depends on openssl libcrypto
40  *  Uses ECB+XOR to do CFB encryption, same result, more performant
41  */
42 static inline int
43 bpi_cipher_encrypt(uint8_t *src, uint8_t *dst,
44                 uint8_t *iv, int ivlen, int srclen,
45                 void *bpi_ctx)
46 {
47         EVP_CIPHER_CTX *ctx = (EVP_CIPHER_CTX *)bpi_ctx;
48         int encrypted_ivlen;
49         uint8_t encrypted_iv[BPI_MAX_ENCR_IV_LEN];
50         uint8_t *encr = encrypted_iv;
51
52         /* ECB method: encrypt the IV, then XOR this with plaintext */
53         if (EVP_EncryptUpdate(ctx, encrypted_iv, &encrypted_ivlen, iv, ivlen)
54                                                                 <= 0)
55                 goto cipher_encrypt_err;
56
57         for (; srclen != 0; --srclen, ++dst, ++src, ++encr)
58                 *dst = *src ^ *encr;
59
60         return 0;
61
62 cipher_encrypt_err:
63         QAT_DP_LOG(ERR, "libcrypto ECB cipher encrypt failed");
64         return -EINVAL;
65 }
66
67 static inline uint32_t
68 qat_bpicipher_postprocess(struct qat_sym_session *ctx,
69                                 struct rte_crypto_op *op)
70 {
71         int block_len = qat_cipher_get_block_size(ctx->qat_cipher_alg);
72         struct rte_crypto_sym_op *sym_op = op->sym;
73         uint8_t last_block_len = block_len > 0 ?
74                         sym_op->cipher.data.length % block_len : 0;
75
76         if (last_block_len > 0 &&
77                         ctx->qat_dir == ICP_QAT_HW_CIPHER_ENCRYPT) {
78
79                 /* Encrypt last block */
80                 uint8_t *last_block, *dst, *iv;
81                 uint32_t last_block_offset;
82
83                 last_block_offset = sym_op->cipher.data.offset +
84                                 sym_op->cipher.data.length - last_block_len;
85                 last_block = (uint8_t *) rte_pktmbuf_mtod_offset(sym_op->m_src,
86                                 uint8_t *, last_block_offset);
87
88                 if (unlikely(sym_op->m_dst != NULL))
89                         /* out-of-place operation (OOP) */
90                         dst = (uint8_t *) rte_pktmbuf_mtod_offset(sym_op->m_dst,
91                                                 uint8_t *, last_block_offset);
92                 else
93                         dst = last_block;
94
95                 if (last_block_len < sym_op->cipher.data.length)
96                         /* use previous block ciphertext as IV */
97                         iv = dst - block_len;
98                 else
99                         /* runt block, i.e. less than one full block */
100                         iv = rte_crypto_op_ctod_offset(op, uint8_t *,
101                                         ctx->cipher_iv.offset);
102
103 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
104                 QAT_DP_HEXDUMP_LOG(DEBUG, "BPI: src before post-process:",
105                         last_block, last_block_len);
106                 if (sym_op->m_dst != NULL)
107                         QAT_DP_HEXDUMP_LOG(DEBUG,
108                                 "BPI: dst before post-process:",
109                                 dst, last_block_len);
110 #endif
111                 bpi_cipher_encrypt(last_block, dst, iv, block_len,
112                                 last_block_len, ctx->bpi_ctx);
113 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
114                 QAT_DP_HEXDUMP_LOG(DEBUG, "BPI: src after post-process:",
115                                 last_block, last_block_len);
116                 if (sym_op->m_dst != NULL)
117                         QAT_DP_HEXDUMP_LOG(DEBUG,
118                                 "BPI: dst after post-process:",
119                                 dst, last_block_len);
120 #endif
121         }
122         return sym_op->cipher.data.length - last_block_len;
123 }
124
125 static inline void
126 qat_sym_process_response(void **op, uint8_t *resp)
127 {
128
129         struct icp_qat_fw_comn_resp *resp_msg =
130                         (struct icp_qat_fw_comn_resp *)resp;
131         struct rte_crypto_op *rx_op = (struct rte_crypto_op *)(uintptr_t)
132                         (resp_msg->opaque_data);
133
134 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
135         QAT_DP_HEXDUMP_LOG(DEBUG, "qat_response:", (uint8_t *)resp_msg,
136                         sizeof(struct icp_qat_fw_comn_resp));
137 #endif
138
139         if (ICP_QAT_FW_COMN_STATUS_FLAG_OK !=
140                         ICP_QAT_FW_COMN_RESP_CRYPTO_STAT_GET(
141                         resp_msg->comn_hdr.comn_status)) {
142
143                 rx_op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
144         } else {
145                 struct qat_sym_session *sess = (struct qat_sym_session *)
146                                                 get_sym_session_private_data(
147                                                 rx_op->sym->session,
148                                                 cryptodev_qat_driver_id);
149
150
151                 if (sess->bpi_ctx)
152                         qat_bpicipher_postprocess(sess, rx_op);
153                 rx_op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
154         }
155         *op = (void *)rx_op;
156 }
157 #else
158
159 static inline void
160 qat_sym_process_response(void **op __rte_unused, uint8_t *resp __rte_unused)
161 {
162 }
163 #endif
164 #endif /* _QAT_SYM_H_ */