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