e4b644559385a03d868fe3f59418c5a52122a6fd
[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 #define SHA224_DIGEST_SIZE      28
56 #define SHA224_BLOCK_SIZE       64
57
58 #define SHA256_DIGEST_SIZE      32
59 #define SHA256_BLOCK_SIZE       64
60
61 #define SHA384_DIGEST_SIZE      48
62 #define SHA384_BLOCK_SIZE       128
63
64 #define SHA512_DIGEST_SIZE      64
65 #define SHA512_BLOCK_SIZE       128
66
67 /* SHA LSB intialiazation values */
68
69 #define SHA1_H0         0x67452301UL
70 #define SHA1_H1         0xefcdab89UL
71 #define SHA1_H2         0x98badcfeUL
72 #define SHA1_H3         0x10325476UL
73 #define SHA1_H4         0xc3d2e1f0UL
74
75 #define SHA224_H0       0xc1059ed8UL
76 #define SHA224_H1       0x367cd507UL
77 #define SHA224_H2       0x3070dd17UL
78 #define SHA224_H3       0xf70e5939UL
79 #define SHA224_H4       0xffc00b31UL
80 #define SHA224_H5       0x68581511UL
81 #define SHA224_H6       0x64f98fa7UL
82 #define SHA224_H7       0xbefa4fa4UL
83
84 #define SHA256_H0       0x6a09e667UL
85 #define SHA256_H1       0xbb67ae85UL
86 #define SHA256_H2       0x3c6ef372UL
87 #define SHA256_H3       0xa54ff53aUL
88 #define SHA256_H4       0x510e527fUL
89 #define SHA256_H5       0x9b05688cUL
90 #define SHA256_H6       0x1f83d9abUL
91 #define SHA256_H7       0x5be0cd19UL
92
93 #define SHA384_H0       0xcbbb9d5dc1059ed8ULL
94 #define SHA384_H1       0x629a292a367cd507ULL
95 #define SHA384_H2       0x9159015a3070dd17ULL
96 #define SHA384_H3       0x152fecd8f70e5939ULL
97 #define SHA384_H4       0x67332667ffc00b31ULL
98 #define SHA384_H5       0x8eb44a8768581511ULL
99 #define SHA384_H6       0xdb0c2e0d64f98fa7ULL
100 #define SHA384_H7       0x47b5481dbefa4fa4ULL
101
102 #define SHA512_H0       0x6a09e667f3bcc908ULL
103 #define SHA512_H1       0xbb67ae8584caa73bULL
104 #define SHA512_H2       0x3c6ef372fe94f82bULL
105 #define SHA512_H3       0xa54ff53a5f1d36f1ULL
106 #define SHA512_H4       0x510e527fade682d1ULL
107 #define SHA512_H5       0x9b05688c2b3e6c1fULL
108 #define SHA512_H6       0x1f83d9abfb41bd6bULL
109 #define SHA512_H7       0x5be0cd19137e2179ULL
110
111 /**
112  * CCP supported AES modes
113  */
114 enum ccp_aes_mode {
115         CCP_AES_MODE_ECB = 0,
116         CCP_AES_MODE_CBC,
117         CCP_AES_MODE_OFB,
118         CCP_AES_MODE_CFB,
119         CCP_AES_MODE_CTR,
120         CCP_AES_MODE_CMAC,
121         CCP_AES_MODE_GHASH,
122         CCP_AES_MODE_GCTR,
123         CCP_AES_MODE__LAST,
124 };
125
126 /**
127  * CCP AES GHASH mode
128  */
129 enum ccp_aes_ghash_mode {
130         CCP_AES_MODE_GHASH_AAD = 0,
131         CCP_AES_MODE_GHASH_FINAL
132 };
133
134 /**
135  * CCP supported AES types
136  */
137 enum ccp_aes_type {
138         CCP_AES_TYPE_128 = 0,
139         CCP_AES_TYPE_192,
140         CCP_AES_TYPE_256,
141         CCP_AES_TYPE__LAST,
142 };
143
144 /***** 3DES engine *****/
145
146 /**
147  * CCP supported DES/3DES modes
148  */
149 enum ccp_des_mode {
150         CCP_DES_MODE_ECB = 0, /* Not supported */
151         CCP_DES_MODE_CBC,
152         CCP_DES_MODE_CFB,
153 };
154
155 /**
156  * CCP supported DES types
157  */
158 enum ccp_des_type {
159         CCP_DES_TYPE_128 = 0,   /* 112 + 16 parity */
160         CCP_DES_TYPE_192,       /* 168 + 24 parity */
161         CCP_DES_TYPE__LAST,
162 };
163
164 /***** SHA engine *****/
165
166 /**
167  * ccp_sha_type - type of SHA operation
168  *
169  * @CCP_SHA_TYPE_1: SHA-1 operation
170  * @CCP_SHA_TYPE_224: SHA-224 operation
171  * @CCP_SHA_TYPE_256: SHA-256 operation
172  */
173 enum ccp_sha_type {
174         CCP_SHA_TYPE_1 = 1,
175         CCP_SHA_TYPE_224,
176         CCP_SHA_TYPE_256,
177         CCP_SHA_TYPE_384,
178         CCP_SHA_TYPE_512,
179         CCP_SHA_TYPE_RSVD1,
180         CCP_SHA_TYPE_RSVD2,
181         CCP_SHA3_TYPE_224,
182         CCP_SHA3_TYPE_256,
183         CCP_SHA3_TYPE_384,
184         CCP_SHA3_TYPE_512,
185         CCP_SHA_TYPE__LAST,
186 };
187
188 /**
189  * CCP supported cipher algorithms
190  */
191 enum ccp_cipher_algo {
192         CCP_CIPHER_ALGO_AES_CBC = 0,
193         CCP_CIPHER_ALGO_AES_ECB,
194         CCP_CIPHER_ALGO_AES_CTR,
195         CCP_CIPHER_ALGO_AES_GCM,
196         CCP_CIPHER_ALGO_3DES_CBC,
197 };
198
199 /**
200  * CCP cipher operation type
201  */
202 enum ccp_cipher_dir {
203         CCP_CIPHER_DIR_DECRYPT = 0,
204         CCP_CIPHER_DIR_ENCRYPT = 1,
205 };
206
207 /**
208  * CCP supported hash algorithms
209  */
210 enum ccp_hash_algo {
211         CCP_AUTH_ALGO_SHA1 = 0,
212         CCP_AUTH_ALGO_SHA1_HMAC,
213         CCP_AUTH_ALGO_SHA224,
214         CCP_AUTH_ALGO_SHA224_HMAC,
215         CCP_AUTH_ALGO_SHA3_224,
216         CCP_AUTH_ALGO_SHA3_224_HMAC,
217         CCP_AUTH_ALGO_SHA256,
218         CCP_AUTH_ALGO_SHA256_HMAC,
219         CCP_AUTH_ALGO_SHA3_256,
220         CCP_AUTH_ALGO_SHA3_256_HMAC,
221         CCP_AUTH_ALGO_SHA384,
222         CCP_AUTH_ALGO_SHA384_HMAC,
223         CCP_AUTH_ALGO_SHA3_384,
224         CCP_AUTH_ALGO_SHA3_384_HMAC,
225         CCP_AUTH_ALGO_SHA512,
226         CCP_AUTH_ALGO_SHA512_HMAC,
227         CCP_AUTH_ALGO_SHA3_512,
228         CCP_AUTH_ALGO_SHA3_512_HMAC,
229         CCP_AUTH_ALGO_AES_CMAC,
230         CCP_AUTH_ALGO_AES_GCM,
231 #ifdef RTE_LIBRTE_PMD_CCP_CPU_AUTH
232         CCP_AUTH_ALGO_MD5_HMAC,
233 #endif
234 };
235
236 /**
237  * CCP hash operation type
238  */
239 enum ccp_hash_op {
240         CCP_AUTH_OP_GENERATE = 0,
241         CCP_AUTH_OP_VERIFY = 1,
242 };
243
244 /* CCP crypto private session structure */
245 struct ccp_session {
246         enum ccp_cmd_order cmd_id;
247         /**< chain order mode */
248         struct {
249                 uint16_t length;
250                 uint16_t offset;
251         } iv;
252         /**< IV parameters */
253         struct {
254                 enum ccp_cipher_algo  algo;
255                 enum ccp_engine  engine;
256                 union {
257                         enum ccp_aes_mode aes_mode;
258                         enum ccp_des_mode des_mode;
259                 } um;
260                 union {
261                         enum ccp_aes_type aes_type;
262                         enum ccp_des_type des_type;
263                 } ut;
264                 enum ccp_cipher_dir dir;
265                 uint64_t key_length;
266                 /**< max cipher key size 256 bits */
267                 uint8_t key[32];
268                 /**ccp key format*/
269                 uint8_t key_ccp[32];
270                 phys_addr_t key_phys;
271                 /**AES-ctr nonce(4) iv(8) ctr*/
272                 uint8_t nonce[32];
273                 phys_addr_t nonce_phys;
274         } cipher;
275         /**< Cipher Parameters */
276
277         struct {
278                 enum ccp_hash_algo algo;
279                 enum ccp_engine  engine;
280                 union {
281                         enum ccp_aes_mode aes_mode;
282                 } um;
283                 union {
284                         enum ccp_sha_type sha_type;
285                         enum ccp_aes_type aes_type;
286                 } ut;
287                 enum ccp_hash_op op;
288                 uint64_t key_length;
289                 /**< max hash key size 144 bytes (struct capabilties) */
290                 uint8_t key[144];
291                 /**< max be key size of AES is 32*/
292                 uint8_t key_ccp[32];
293                 phys_addr_t key_phys;
294                 uint64_t digest_length;
295                 void *ctx;
296                 int ctx_len;
297                 int offset;
298                 int block_size;
299                 /**< Buffer to store  Software generated precomute values*/
300                 /**< For HMAC H(ipad ^ key) and H(opad ^ key) */
301                 /**< For CMAC K1 IV and K2 IV*/
302                 uint8_t pre_compute[2 * CCP_SHA3_CTX_SIZE];
303                 /**< SHA3 initial ctx all zeros*/
304                 uint8_t sha3_ctx[200];
305                 int aad_length;
306         } auth;
307         /**< Authentication Parameters */
308         enum rte_crypto_aead_algorithm aead_algo;
309         /**< AEAD Algorithm */
310
311         uint32_t reserved;
312 } __rte_cache_aligned;
313
314 extern uint8_t ccp_cryptodev_driver_id;
315
316 struct ccp_qp;
317
318 /**
319  * Set and validate CCP crypto session parameters
320  *
321  * @param sess ccp private session
322  * @param xform crypto xform for this session
323  * @return 0 on success otherwise -1
324  */
325 int ccp_set_session_parameters(struct ccp_session *sess,
326                                const struct rte_crypto_sym_xform *xform);
327
328 /**
329  * Find count of slots
330  *
331  * @param session CCP private session
332  * @return count of free slots available
333  */
334 int ccp_compute_slot_count(struct ccp_session *session);
335
336 /**
337  * process crypto ops to be enqueued
338  *
339  * @param qp CCP crypto queue-pair
340  * @param op crypto ops table
341  * @param cmd_q CCP cmd queue
342  * @param nb_ops No. of ops to be submitted
343  * @return 0 on success otherwise -1
344  */
345 int process_ops_to_enqueue(const struct ccp_qp *qp,
346                            struct rte_crypto_op **op,
347                            struct ccp_queue *cmd_q,
348                            uint16_t nb_ops,
349                            int slots_req);
350
351 /**
352  * process crypto ops to be dequeued
353  *
354  * @param qp CCP crypto queue-pair
355  * @param op crypto ops table
356  * @param nb_ops requested no. of ops
357  * @return 0 on success otherwise -1
358  */
359 int process_ops_to_dequeue(struct ccp_qp *qp,
360                            struct rte_crypto_op **op,
361                            uint16_t nb_ops);
362
363 #endif /* _CCP_CRYPTO_H_ */