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