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