13cdb6e45269622a32764b048a1f2e44d3e9fd1c
[dpdk.git] / drivers / crypto / ccp / ccp_crypto.h
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright(c) 2018 Advanced Micro Devices, Inc. All rights reserved.
3  */
4
5 #ifndef _CCP_CRYPTO_H_
6 #define _CCP_CRYPTO_H_
7
8 #include <limits.h>
9 #include <stdbool.h>
10 #include <stdint.h>
11 #include <string.h>
12
13 #include <rte_atomic.h>
14 #include <rte_byteorder.h>
15 #include <rte_io.h>
16 #include <rte_pci.h>
17 #include <rte_spinlock.h>
18 #include <rte_crypto_sym.h>
19 #include <rte_cryptodev.h>
20
21 #include "ccp_dev.h"
22
23 #define AES_BLOCK_SIZE 16
24 #define CMAC_PAD_VALUE 0x80
25 #define CTR_NONCE_SIZE 4
26 #define CTR_IV_SIZE 8
27 #define CCP_SHA3_CTX_SIZE 200
28
29 /**Macro helpers for CCP command creation*/
30 #define CCP_AES_SIZE(p)         ((p)->aes.size)
31 #define CCP_AES_ENCRYPT(p)      ((p)->aes.encrypt)
32 #define CCP_AES_MODE(p)         ((p)->aes.mode)
33 #define CCP_AES_TYPE(p)         ((p)->aes.type)
34 #define CCP_PT_BYTESWAP(p)      ((p)->pt.byteswap)
35 #define CCP_PT_BITWISE(p)       ((p)->pt.bitwise)
36
37 /**
38  * CCP supported AES modes
39  */
40 enum ccp_aes_mode {
41         CCP_AES_MODE_ECB = 0,
42         CCP_AES_MODE_CBC,
43         CCP_AES_MODE_OFB,
44         CCP_AES_MODE_CFB,
45         CCP_AES_MODE_CTR,
46         CCP_AES_MODE_CMAC,
47         CCP_AES_MODE_GHASH,
48         CCP_AES_MODE_GCTR,
49         CCP_AES_MODE__LAST,
50 };
51
52 /**
53  * CCP AES GHASH mode
54  */
55 enum ccp_aes_ghash_mode {
56         CCP_AES_MODE_GHASH_AAD = 0,
57         CCP_AES_MODE_GHASH_FINAL
58 };
59
60 /**
61  * CCP supported AES types
62  */
63 enum ccp_aes_type {
64         CCP_AES_TYPE_128 = 0,
65         CCP_AES_TYPE_192,
66         CCP_AES_TYPE_256,
67         CCP_AES_TYPE__LAST,
68 };
69
70 /***** 3DES engine *****/
71
72 /**
73  * CCP supported DES/3DES modes
74  */
75 enum ccp_des_mode {
76         CCP_DES_MODE_ECB = 0, /* Not supported */
77         CCP_DES_MODE_CBC,
78         CCP_DES_MODE_CFB,
79 };
80
81 /**
82  * CCP supported DES types
83  */
84 enum ccp_des_type {
85         CCP_DES_TYPE_128 = 0,   /* 112 + 16 parity */
86         CCP_DES_TYPE_192,       /* 168 + 24 parity */
87         CCP_DES_TYPE__LAST,
88 };
89
90 /***** SHA engine *****/
91
92 /**
93  * ccp_sha_type - type of SHA operation
94  *
95  * @CCP_SHA_TYPE_1: SHA-1 operation
96  * @CCP_SHA_TYPE_224: SHA-224 operation
97  * @CCP_SHA_TYPE_256: SHA-256 operation
98  */
99 enum ccp_sha_type {
100         CCP_SHA_TYPE_1 = 1,
101         CCP_SHA_TYPE_224,
102         CCP_SHA_TYPE_256,
103         CCP_SHA_TYPE_384,
104         CCP_SHA_TYPE_512,
105         CCP_SHA_TYPE_RSVD1,
106         CCP_SHA_TYPE_RSVD2,
107         CCP_SHA3_TYPE_224,
108         CCP_SHA3_TYPE_256,
109         CCP_SHA3_TYPE_384,
110         CCP_SHA3_TYPE_512,
111         CCP_SHA_TYPE__LAST,
112 };
113
114 /**
115  * CCP supported cipher algorithms
116  */
117 enum ccp_cipher_algo {
118         CCP_CIPHER_ALGO_AES_CBC = 0,
119         CCP_CIPHER_ALGO_AES_ECB,
120         CCP_CIPHER_ALGO_AES_CTR,
121         CCP_CIPHER_ALGO_AES_GCM,
122         CCP_CIPHER_ALGO_3DES_CBC,
123 };
124
125 /**
126  * CCP cipher operation type
127  */
128 enum ccp_cipher_dir {
129         CCP_CIPHER_DIR_DECRYPT = 0,
130         CCP_CIPHER_DIR_ENCRYPT = 1,
131 };
132
133 /**
134  * CCP supported hash algorithms
135  */
136 enum ccp_hash_algo {
137         CCP_AUTH_ALGO_SHA1 = 0,
138         CCP_AUTH_ALGO_SHA1_HMAC,
139         CCP_AUTH_ALGO_SHA224,
140         CCP_AUTH_ALGO_SHA224_HMAC,
141         CCP_AUTH_ALGO_SHA3_224,
142         CCP_AUTH_ALGO_SHA3_224_HMAC,
143         CCP_AUTH_ALGO_SHA256,
144         CCP_AUTH_ALGO_SHA256_HMAC,
145         CCP_AUTH_ALGO_SHA3_256,
146         CCP_AUTH_ALGO_SHA3_256_HMAC,
147         CCP_AUTH_ALGO_SHA384,
148         CCP_AUTH_ALGO_SHA384_HMAC,
149         CCP_AUTH_ALGO_SHA3_384,
150         CCP_AUTH_ALGO_SHA3_384_HMAC,
151         CCP_AUTH_ALGO_SHA512,
152         CCP_AUTH_ALGO_SHA512_HMAC,
153         CCP_AUTH_ALGO_SHA3_512,
154         CCP_AUTH_ALGO_SHA3_512_HMAC,
155         CCP_AUTH_ALGO_AES_CMAC,
156         CCP_AUTH_ALGO_AES_GCM,
157 #ifdef RTE_LIBRTE_PMD_CCP_CPU_AUTH
158         CCP_AUTH_ALGO_MD5_HMAC,
159 #endif
160 };
161
162 /**
163  * CCP hash operation type
164  */
165 enum ccp_hash_op {
166         CCP_AUTH_OP_GENERATE = 0,
167         CCP_AUTH_OP_VERIFY = 1,
168 };
169
170 /* CCP crypto private session structure */
171 struct ccp_session {
172         enum ccp_cmd_order cmd_id;
173         /**< chain order mode */
174         struct {
175                 uint16_t length;
176                 uint16_t offset;
177         } iv;
178         /**< IV parameters */
179         struct {
180                 enum ccp_cipher_algo  algo;
181                 enum ccp_engine  engine;
182                 union {
183                         enum ccp_aes_mode aes_mode;
184                         enum ccp_des_mode des_mode;
185                 } um;
186                 union {
187                         enum ccp_aes_type aes_type;
188                         enum ccp_des_type des_type;
189                 } ut;
190                 enum ccp_cipher_dir dir;
191                 uint64_t key_length;
192                 /**< max cipher key size 256 bits */
193                 uint8_t key[32];
194                 /**ccp key format*/
195                 uint8_t key_ccp[32];
196                 phys_addr_t key_phys;
197                 /**AES-ctr nonce(4) iv(8) ctr*/
198                 uint8_t nonce[32];
199                 phys_addr_t nonce_phys;
200         } cipher;
201         /**< Cipher Parameters */
202
203         struct {
204                 enum ccp_hash_algo algo;
205                 enum ccp_engine  engine;
206                 union {
207                         enum ccp_aes_mode aes_mode;
208                 } um;
209                 union {
210                         enum ccp_sha_type sha_type;
211                         enum ccp_aes_type aes_type;
212                 } ut;
213                 enum ccp_hash_op op;
214                 uint64_t key_length;
215                 /**< max hash key size 144 bytes (struct capabilties) */
216                 uint8_t key[144];
217                 /**< max be key size of AES is 32*/
218                 uint8_t key_ccp[32];
219                 phys_addr_t key_phys;
220                 uint64_t digest_length;
221                 void *ctx;
222                 int ctx_len;
223                 int offset;
224                 int block_size;
225                 /**< Buffer to store  Software generated precomute values*/
226                 /**< For HMAC H(ipad ^ key) and H(opad ^ key) */
227                 /**< For CMAC K1 IV and K2 IV*/
228                 uint8_t pre_compute[2 * CCP_SHA3_CTX_SIZE];
229                 /**< SHA3 initial ctx all zeros*/
230                 uint8_t sha3_ctx[200];
231                 int aad_length;
232         } auth;
233         /**< Authentication Parameters */
234         enum rte_crypto_aead_algorithm aead_algo;
235         /**< AEAD Algorithm */
236
237         uint32_t reserved;
238 } __rte_cache_aligned;
239
240 extern uint8_t ccp_cryptodev_driver_id;
241
242 struct ccp_qp;
243
244 /**
245  * Set and validate CCP crypto session parameters
246  *
247  * @param sess ccp private session
248  * @param xform crypto xform for this session
249  * @return 0 on success otherwise -1
250  */
251 int ccp_set_session_parameters(struct ccp_session *sess,
252                                const struct rte_crypto_sym_xform *xform);
253
254 /**
255  * Find count of slots
256  *
257  * @param session CCP private session
258  * @return count of free slots available
259  */
260 int ccp_compute_slot_count(struct ccp_session *session);
261
262 /**
263  * process crypto ops to be enqueued
264  *
265  * @param qp CCP crypto queue-pair
266  * @param op crypto ops table
267  * @param cmd_q CCP cmd queue
268  * @param nb_ops No. of ops to be submitted
269  * @return 0 on success otherwise -1
270  */
271 int process_ops_to_enqueue(const struct ccp_qp *qp,
272                            struct rte_crypto_op **op,
273                            struct ccp_queue *cmd_q,
274                            uint16_t nb_ops,
275                            int slots_req);
276
277 /**
278  * process crypto ops to be dequeued
279  *
280  * @param qp CCP crypto queue-pair
281  * @param op crypto ops table
282  * @param nb_ops requested no. of ops
283  * @return 0 on success otherwise -1
284  */
285 int process_ops_to_dequeue(struct ccp_qp *qp,
286                            struct rte_crypto_op **op,
287                            uint16_t nb_ops);
288
289 #endif /* _CCP_CRYPTO_H_ */