crypto/ccp: support SHA1
[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_DES_ENCRYPT(p)      ((p)->des.encrypt)
35 #define CCP_DES_MODE(p)         ((p)->des.mode)
36 #define CCP_DES_TYPE(p)         ((p)->des.type)
37 #define CCP_SHA_TYPE(p)         ((p)->sha.type)
38 #define CCP_PT_BYTESWAP(p)      ((p)->pt.byteswap)
39 #define CCP_PT_BITWISE(p)       ((p)->pt.bitwise)
40
41 /* HMAC */
42 #define HMAC_IPAD_VALUE 0x36
43 #define HMAC_OPAD_VALUE 0x5c
44
45 #ifdef RTE_LIBRTE_PMD_CCP_CPU_AUTH
46 #define MD5_DIGEST_SIZE         16
47 #define MD5_BLOCK_SIZE          64
48 #endif
49
50 /* SHA */
51 #define SHA_COMMON_DIGEST_SIZE  32
52 #define SHA1_DIGEST_SIZE        20
53 #define SHA1_BLOCK_SIZE         64
54
55 /* SHA LSB intialiazation values */
56
57 #define SHA1_H0         0x67452301UL
58 #define SHA1_H1         0xefcdab89UL
59 #define SHA1_H2         0x98badcfeUL
60 #define SHA1_H3         0x10325476UL
61 #define SHA1_H4         0xc3d2e1f0UL
62
63 /**
64  * CCP supported AES modes
65  */
66 enum ccp_aes_mode {
67         CCP_AES_MODE_ECB = 0,
68         CCP_AES_MODE_CBC,
69         CCP_AES_MODE_OFB,
70         CCP_AES_MODE_CFB,
71         CCP_AES_MODE_CTR,
72         CCP_AES_MODE_CMAC,
73         CCP_AES_MODE_GHASH,
74         CCP_AES_MODE_GCTR,
75         CCP_AES_MODE__LAST,
76 };
77
78 /**
79  * CCP AES GHASH mode
80  */
81 enum ccp_aes_ghash_mode {
82         CCP_AES_MODE_GHASH_AAD = 0,
83         CCP_AES_MODE_GHASH_FINAL
84 };
85
86 /**
87  * CCP supported AES types
88  */
89 enum ccp_aes_type {
90         CCP_AES_TYPE_128 = 0,
91         CCP_AES_TYPE_192,
92         CCP_AES_TYPE_256,
93         CCP_AES_TYPE__LAST,
94 };
95
96 /***** 3DES engine *****/
97
98 /**
99  * CCP supported DES/3DES modes
100  */
101 enum ccp_des_mode {
102         CCP_DES_MODE_ECB = 0, /* Not supported */
103         CCP_DES_MODE_CBC,
104         CCP_DES_MODE_CFB,
105 };
106
107 /**
108  * CCP supported DES types
109  */
110 enum ccp_des_type {
111         CCP_DES_TYPE_128 = 0,   /* 112 + 16 parity */
112         CCP_DES_TYPE_192,       /* 168 + 24 parity */
113         CCP_DES_TYPE__LAST,
114 };
115
116 /***** SHA engine *****/
117
118 /**
119  * ccp_sha_type - type of SHA operation
120  *
121  * @CCP_SHA_TYPE_1: SHA-1 operation
122  * @CCP_SHA_TYPE_224: SHA-224 operation
123  * @CCP_SHA_TYPE_256: SHA-256 operation
124  */
125 enum ccp_sha_type {
126         CCP_SHA_TYPE_1 = 1,
127         CCP_SHA_TYPE_224,
128         CCP_SHA_TYPE_256,
129         CCP_SHA_TYPE_384,
130         CCP_SHA_TYPE_512,
131         CCP_SHA_TYPE_RSVD1,
132         CCP_SHA_TYPE_RSVD2,
133         CCP_SHA3_TYPE_224,
134         CCP_SHA3_TYPE_256,
135         CCP_SHA3_TYPE_384,
136         CCP_SHA3_TYPE_512,
137         CCP_SHA_TYPE__LAST,
138 };
139
140 /**
141  * CCP supported cipher algorithms
142  */
143 enum ccp_cipher_algo {
144         CCP_CIPHER_ALGO_AES_CBC = 0,
145         CCP_CIPHER_ALGO_AES_ECB,
146         CCP_CIPHER_ALGO_AES_CTR,
147         CCP_CIPHER_ALGO_AES_GCM,
148         CCP_CIPHER_ALGO_3DES_CBC,
149 };
150
151 /**
152  * CCP cipher operation type
153  */
154 enum ccp_cipher_dir {
155         CCP_CIPHER_DIR_DECRYPT = 0,
156         CCP_CIPHER_DIR_ENCRYPT = 1,
157 };
158
159 /**
160  * CCP supported hash algorithms
161  */
162 enum ccp_hash_algo {
163         CCP_AUTH_ALGO_SHA1 = 0,
164         CCP_AUTH_ALGO_SHA1_HMAC,
165         CCP_AUTH_ALGO_SHA224,
166         CCP_AUTH_ALGO_SHA224_HMAC,
167         CCP_AUTH_ALGO_SHA3_224,
168         CCP_AUTH_ALGO_SHA3_224_HMAC,
169         CCP_AUTH_ALGO_SHA256,
170         CCP_AUTH_ALGO_SHA256_HMAC,
171         CCP_AUTH_ALGO_SHA3_256,
172         CCP_AUTH_ALGO_SHA3_256_HMAC,
173         CCP_AUTH_ALGO_SHA384,
174         CCP_AUTH_ALGO_SHA384_HMAC,
175         CCP_AUTH_ALGO_SHA3_384,
176         CCP_AUTH_ALGO_SHA3_384_HMAC,
177         CCP_AUTH_ALGO_SHA512,
178         CCP_AUTH_ALGO_SHA512_HMAC,
179         CCP_AUTH_ALGO_SHA3_512,
180         CCP_AUTH_ALGO_SHA3_512_HMAC,
181         CCP_AUTH_ALGO_AES_CMAC,
182         CCP_AUTH_ALGO_AES_GCM,
183 #ifdef RTE_LIBRTE_PMD_CCP_CPU_AUTH
184         CCP_AUTH_ALGO_MD5_HMAC,
185 #endif
186 };
187
188 /**
189  * CCP hash operation type
190  */
191 enum ccp_hash_op {
192         CCP_AUTH_OP_GENERATE = 0,
193         CCP_AUTH_OP_VERIFY = 1,
194 };
195
196 /* CCP crypto private session structure */
197 struct ccp_session {
198         enum ccp_cmd_order cmd_id;
199         /**< chain order mode */
200         struct {
201                 uint16_t length;
202                 uint16_t offset;
203         } iv;
204         /**< IV parameters */
205         struct {
206                 enum ccp_cipher_algo  algo;
207                 enum ccp_engine  engine;
208                 union {
209                         enum ccp_aes_mode aes_mode;
210                         enum ccp_des_mode des_mode;
211                 } um;
212                 union {
213                         enum ccp_aes_type aes_type;
214                         enum ccp_des_type des_type;
215                 } ut;
216                 enum ccp_cipher_dir dir;
217                 uint64_t key_length;
218                 /**< max cipher key size 256 bits */
219                 uint8_t key[32];
220                 /**ccp key format*/
221                 uint8_t key_ccp[32];
222                 phys_addr_t key_phys;
223                 /**AES-ctr nonce(4) iv(8) ctr*/
224                 uint8_t nonce[32];
225                 phys_addr_t nonce_phys;
226         } cipher;
227         /**< Cipher Parameters */
228
229         struct {
230                 enum ccp_hash_algo algo;
231                 enum ccp_engine  engine;
232                 union {
233                         enum ccp_aes_mode aes_mode;
234                 } um;
235                 union {
236                         enum ccp_sha_type sha_type;
237                         enum ccp_aes_type aes_type;
238                 } ut;
239                 enum ccp_hash_op op;
240                 uint64_t key_length;
241                 /**< max hash key size 144 bytes (struct capabilties) */
242                 uint8_t key[144];
243                 /**< max be key size of AES is 32*/
244                 uint8_t key_ccp[32];
245                 phys_addr_t key_phys;
246                 uint64_t digest_length;
247                 void *ctx;
248                 int ctx_len;
249                 int offset;
250                 int block_size;
251                 /**< Buffer to store  Software generated precomute values*/
252                 /**< For HMAC H(ipad ^ key) and H(opad ^ key) */
253                 /**< For CMAC K1 IV and K2 IV*/
254                 uint8_t pre_compute[2 * CCP_SHA3_CTX_SIZE];
255                 /**< SHA3 initial ctx all zeros*/
256                 uint8_t sha3_ctx[200];
257                 int aad_length;
258         } auth;
259         /**< Authentication Parameters */
260         enum rte_crypto_aead_algorithm aead_algo;
261         /**< AEAD Algorithm */
262
263         uint32_t reserved;
264 } __rte_cache_aligned;
265
266 extern uint8_t ccp_cryptodev_driver_id;
267
268 struct ccp_qp;
269
270 /**
271  * Set and validate CCP crypto session parameters
272  *
273  * @param sess ccp private session
274  * @param xform crypto xform for this session
275  * @return 0 on success otherwise -1
276  */
277 int ccp_set_session_parameters(struct ccp_session *sess,
278                                const struct rte_crypto_sym_xform *xform);
279
280 /**
281  * Find count of slots
282  *
283  * @param session CCP private session
284  * @return count of free slots available
285  */
286 int ccp_compute_slot_count(struct ccp_session *session);
287
288 /**
289  * process crypto ops to be enqueued
290  *
291  * @param qp CCP crypto queue-pair
292  * @param op crypto ops table
293  * @param cmd_q CCP cmd queue
294  * @param nb_ops No. of ops to be submitted
295  * @return 0 on success otherwise -1
296  */
297 int process_ops_to_enqueue(const struct ccp_qp *qp,
298                            struct rte_crypto_op **op,
299                            struct ccp_queue *cmd_q,
300                            uint16_t nb_ops,
301                            int slots_req);
302
303 /**
304  * process crypto ops to be dequeued
305  *
306  * @param qp CCP crypto queue-pair
307  * @param op crypto ops table
308  * @param nb_ops requested no. of ops
309  * @return 0 on success otherwise -1
310  */
311 int process_ops_to_dequeue(struct ccp_qp *qp,
312                            struct rte_crypto_op **op,
313                            uint16_t nb_ops);
314
315 #endif /* _CCP_CRYPTO_H_ */