crypto/octeontx: support ECDSA
[dpdk.git] / drivers / common / cpt / cpt_mcode_defines.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Cavium, Inc
3  */
4
5 #ifndef _CPT_MCODE_DEFINES_H_
6 #define _CPT_MCODE_DEFINES_H_
7
8 #include <rte_byteorder.h>
9 #include <rte_crypto_asym.h>
10 #include <rte_memory.h>
11
12 /*
13  * This file defines macros and structures according to microcode spec
14  *
15  */
16 /* SE opcodes */
17 #define CPT_MAJOR_OP_FC         0x33
18 #define CPT_MAJOR_OP_HASH       0x34
19 #define CPT_MAJOR_OP_HMAC       0x35
20 #define CPT_MAJOR_OP_ZUC_SNOW3G 0x37
21 #define CPT_MAJOR_OP_KASUMI     0x38
22 #define CPT_MAJOR_OP_MISC       0x01
23
24 /* AE opcodes */
25 #define CPT_MAJOR_OP_MODEX      0x03
26 #define CPT_MAJOR_OP_ECDSA      0x04
27 #define CPT_MINOR_OP_MODEX      0x01
28 #define CPT_MINOR_OP_PKCS_ENC   0x02
29 #define CPT_MINOR_OP_PKCS_ENC_CRT       0x03
30 #define CPT_MINOR_OP_PKCS_DEC   0x04
31 #define CPT_MINOR_OP_PKCS_DEC_CRT       0x05
32 #define CPT_MINOR_OP_MODEX_CRT  0x06
33 #define CPT_MINOR_OP_ECDSA_SIGN 0x01
34 #define CPT_MINOR_OP_ECDSA_VERIFY       0x02
35
36 #define CPT_BLOCK_TYPE1 0
37 #define CPT_BLOCK_TYPE2 1
38
39 #define CPT_BYTE_16             16
40 #define CPT_BYTE_24             24
41 #define CPT_BYTE_32             32
42 #define CPT_MAX_SG_IN_OUT_CNT   32
43 #define CPT_MAX_SG_CNT          (CPT_MAX_SG_IN_OUT_CNT/2)
44
45 #define COMPLETION_CODE_SIZE    8
46 #define COMPLETION_CODE_INIT    0
47
48 #define SG_LIST_HDR_SIZE        (8u)
49 #define SG_ENTRY_SIZE           sizeof(sg_comp_t)
50
51 #define CPT_DMA_MODE            (1 << 7)
52
53 #define CPT_FROM_CTX            0
54 #define CPT_FROM_DPTR           1
55
56 #define FC_GEN                  0x1
57 #define ZUC_SNOW3G              0x2
58 #define KASUMI                  0x3
59 #define HASH_HMAC               0x4
60
61 #define ZS_EA                   0x1
62 #define ZS_IA                   0x2
63 #define K_F8                    0x4
64 #define K_F9                    0x8
65
66 #define CPT_OP_CIPHER_ENCRYPT   0x1
67 #define CPT_OP_CIPHER_DECRYPT   0x2
68 #define CPT_OP_CIPHER_MASK      0x3
69
70 #define CPT_OP_AUTH_VERIFY      0x4
71 #define CPT_OP_AUTH_GENERATE    0x8
72 #define CPT_OP_AUTH_MASK        0xC
73
74 #define CPT_OP_ENCODE   (CPT_OP_CIPHER_ENCRYPT | CPT_OP_AUTH_GENERATE)
75 #define CPT_OP_DECODE   (CPT_OP_CIPHER_DECRYPT | CPT_OP_AUTH_VERIFY)
76
77 /* #define CPT_ALWAYS_USE_SG_MODE */
78 #define CPT_ALWAYS_USE_SEPARATE_BUF
79
80 /*
81  * Parameters for Flexi Crypto
82  * requests
83  */
84 #define VALID_AAD_BUF 0x01
85 #define VALID_MAC_BUF 0x02
86 #define VALID_IV_BUF 0x04
87 #define SINGLE_BUF_INPLACE 0x08
88 #define SINGLE_BUF_HEADTAILROOM 0x10
89
90 #define ENCR_IV_OFFSET(__d_offs) ((__d_offs >> 32) & 0xffff)
91 #define ENCR_OFFSET(__d_offs) ((__d_offs >> 16) & 0xffff)
92 #define AUTH_OFFSET(__d_offs) (__d_offs & 0xffff)
93 #define ENCR_DLEN(__d_lens) (__d_lens >> 32)
94 #define AUTH_DLEN(__d_lens) (__d_lens & 0xffffffff)
95
96 /* FC offset_control at start of DPTR in bytes */
97 #define OFF_CTRL_LEN  8 /**< bytes */
98
99 typedef enum {
100         MD5_TYPE        = 1,
101         SHA1_TYPE       = 2,
102         SHA2_SHA224     = 3,
103         SHA2_SHA256     = 4,
104         SHA2_SHA384     = 5,
105         SHA2_SHA512     = 6,
106         GMAC_TYPE       = 7,
107         XCBC_TYPE       = 8,
108         SHA3_SHA224     = 10,
109         SHA3_SHA256     = 11,
110         SHA3_SHA384     = 12,
111         SHA3_SHA512     = 13,
112         SHA3_SHAKE256   = 14,
113         SHA3_SHAKE512   = 15,
114
115         /* These are only for software use */
116         ZUC_EIA3        = 0x90,
117         SNOW3G_UIA2     = 0x91,
118         KASUMI_F9_CBC   = 0x92,
119         KASUMI_F9_ECB   = 0x93,
120 } mc_hash_type_t;
121
122 typedef enum {
123         /* To support passthrough */
124         PASSTHROUGH  = 0x0,
125         /*
126          * These are defined by MC for Flexi crypto
127          * for field of 4 bits
128          */
129         DES3_CBC    = 0x1,
130         DES3_ECB    = 0x2,
131         AES_CBC     = 0x3,
132         AES_ECB     = 0x4,
133         AES_CFB     = 0x5,
134         AES_CTR     = 0x6,
135         AES_GCM     = 0x7,
136         AES_XTS     = 0x8,
137
138         /* These are only for software use */
139         ZUC_EEA3        = 0x90,
140         SNOW3G_UEA2     = 0x91,
141         KASUMI_F8_CBC   = 0x92,
142         KASUMI_F8_ECB   = 0x93,
143 } mc_cipher_type_t;
144
145 typedef enum {
146         AES_128_BIT = 0x1,
147         AES_192_BIT = 0x2,
148         AES_256_BIT = 0x3
149 } mc_aes_type_t;
150
151 typedef enum {
152         /* Microcode errors */
153         NO_ERR = 0x00,
154         ERR_OPCODE_UNSUPPORTED = 0x01,
155
156         /* SCATTER GATHER */
157         ERR_SCATTER_GATHER_WRITE_LENGTH = 0x02,
158         ERR_SCATTER_GATHER_LIST = 0x03,
159         ERR_SCATTER_GATHER_NOT_SUPPORTED = 0x04,
160
161         /* SE GC */
162         ERR_GC_LENGTH_INVALID = 0x41,
163         ERR_GC_RANDOM_LEN_INVALID = 0x42,
164         ERR_GC_DATA_LEN_INVALID = 0x43,
165         ERR_GC_DRBG_TYPE_INVALID = 0x44,
166         ERR_GC_CTX_LEN_INVALID = 0x45,
167         ERR_GC_CIPHER_UNSUPPORTED = 0x46,
168         ERR_GC_AUTH_UNSUPPORTED = 0x47,
169         ERR_GC_OFFSET_INVALID = 0x48,
170         ERR_GC_HASH_MODE_UNSUPPORTED = 0x49,
171         ERR_GC_DRBG_ENTROPY_LEN_INVALID = 0x4a,
172         ERR_GC_DRBG_ADDNL_LEN_INVALID = 0x4b,
173         ERR_GC_ICV_MISCOMPARE = 0x4c,
174         ERR_GC_DATA_UNALIGNED = 0x4d,
175
176         /* API Layer */
177         ERR_BAD_ALT_CCODE = 0xfd,
178         ERR_REQ_PENDING = 0xfe,
179         ERR_REQ_TIMEOUT = 0xff,
180
181         ERR_BAD_INPUT_LENGTH = (0x40000000 | 384),    /* 0x40000180 */
182         ERR_BAD_KEY_LENGTH,
183         ERR_BAD_KEY_HANDLE,
184         ERR_BAD_CONTEXT_HANDLE,
185         ERR_BAD_SCALAR_LENGTH,
186         ERR_BAD_DIGEST_LENGTH,
187         ERR_BAD_INPUT_ARG,
188         ERR_BAD_RECORD_PADDING,
189         ERR_NB_REQUEST_PENDING,
190         ERR_EIO,
191         ERR_ENODEV,
192 } mc_error_code_t;
193
194 /**
195  * Enumeration cpt_comp_e
196  *
197  * CPT Completion Enumeration
198  * Enumerates the values of CPT_RES_S[COMPCODE].
199  */
200 typedef enum {
201         CPT_8X_COMP_E_NOTDONE    = (0x00),
202         CPT_8X_COMP_E_GOOD       = (0x01),
203         CPT_8X_COMP_E_FAULT      = (0x02),
204         CPT_8X_COMP_E_SWERR      = (0x03),
205         CPT_8X_COMP_E_HWERR      = (0x04),
206         CPT_8X_COMP_E_LAST_ENTRY = (0xFF)
207 } cpt_comp_e_t;
208
209 /**
210  * Enumeration cpt_ec_id
211  *
212  * Enumerates supported elliptic curves
213  */
214 typedef enum {
215         CPT_EC_ID_P192 = 0,
216         CPT_EC_ID_P224 = 1,
217         CPT_EC_ID_P256 = 2,
218         CPT_EC_ID_P384 = 3,
219         CPT_EC_ID_P521 = 4,
220         CPT_EC_ID_PMAX = 5
221 } cpt_ec_id_t;
222
223 typedef struct sglist_comp {
224         union {
225                 uint64_t len;
226                 struct {
227                         uint16_t len[4];
228                 } s;
229         } u;
230         uint64_t ptr[4];
231 } sg_comp_t;
232
233 struct cpt_sess_misc {
234         /** CPT opcode */
235         uint16_t cpt_op:4;
236         /** ZUC, SNOW3G &  KASUMI flags */
237         uint16_t zsk_flag:4;
238         /** Flag for AES GCM */
239         uint16_t aes_gcm:1;
240         /** Flag for AES CTR */
241         uint16_t aes_ctr:1;
242         /** Flag for NULL cipher/auth */
243         uint16_t is_null:1;
244         /** Flag for GMAC */
245         uint16_t is_gmac:1;
246         /** Engine group */
247         uint16_t egrp:3;
248         /** AAD length */
249         uint16_t aad_length;
250         /** MAC len in bytes */
251         uint8_t mac_len;
252         /** IV length in bytes */
253         uint8_t iv_length;
254         /** Auth IV length in bytes */
255         uint8_t auth_iv_length;
256         /** Reserved field */
257         uint8_t rsvd1;
258         /** IV offset in bytes */
259         uint16_t iv_offset;
260         /** Auth IV offset in bytes */
261         uint16_t auth_iv_offset;
262         /** Salt */
263         uint32_t salt;
264         /** Context DMA address */
265         phys_addr_t ctx_dma_addr;
266 };
267
268 typedef union {
269         uint64_t flags;
270         struct {
271 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
272                 uint64_t enc_cipher   : 4;
273                 uint64_t reserved1    : 1;
274                 uint64_t aes_key      : 2;
275                 uint64_t iv_source    : 1;
276                 uint64_t hash_type    : 4;
277                 uint64_t reserved2    : 3;
278                 uint64_t auth_input_type : 1;
279                 uint64_t mac_len      : 8;
280                 uint64_t reserved3    : 8;
281                 uint64_t encr_offset  : 16;
282                 uint64_t iv_offset    : 8;
283                 uint64_t auth_offset  : 8;
284 #else
285                 uint64_t auth_offset  : 8;
286                 uint64_t iv_offset    : 8;
287                 uint64_t encr_offset  : 16;
288                 uint64_t reserved3    : 8;
289                 uint64_t mac_len      : 8;
290                 uint64_t auth_input_type : 1;
291                 uint64_t reserved2    : 3;
292                 uint64_t hash_type    : 4;
293                 uint64_t iv_source    : 1;
294                 uint64_t aes_key      : 2;
295                 uint64_t reserved1    : 1;
296                 uint64_t enc_cipher   : 4;
297 #endif
298         } e;
299 } encr_ctrl_t;
300
301 typedef struct {
302         encr_ctrl_t enc_ctrl;
303         uint8_t  encr_key[32];
304         uint8_t  encr_iv[16];
305 } mc_enc_context_t;
306
307 typedef struct {
308         uint8_t  ipad[64];
309         uint8_t  opad[64];
310 } mc_fc_hmac_context_t;
311
312 typedef struct {
313         mc_enc_context_t     enc;
314         mc_fc_hmac_context_t hmac;
315 } mc_fc_context_t;
316
317 typedef struct {
318         uint8_t encr_auth_iv[16];
319         uint8_t ci_key[16];
320         uint8_t zuc_const[32];
321 } mc_zuc_snow3g_ctx_t;
322
323 typedef struct {
324         uint8_t reg_A[8];
325         uint8_t ci_key[16];
326 } mc_kasumi_ctx_t;
327
328 struct cpt_ctx {
329         /* Below fields are accessed by sw */
330         uint64_t enc_cipher     :8;
331         uint64_t hash_type      :8;
332         uint64_t mac_len        :8;
333         uint64_t auth_key_len   :8;
334         uint64_t fc_type        :4;
335         uint64_t hmac           :1;
336         uint64_t zsk_flags      :3;
337         uint64_t k_ecb          :1;
338         uint64_t snow3g         :2;
339         uint64_t rsvd           :21;
340         /* Below fields are accessed by hardware */
341         union {
342                 mc_fc_context_t fctx;
343                 mc_zuc_snow3g_ctx_t zs_ctx;
344                 mc_kasumi_ctx_t k_ctx;
345         };
346         uint8_t  auth_key[64];
347 };
348
349 /* Prime and order fields of built-in elliptic curves */
350 struct cpt_ec_group {
351         struct {
352                 /* P521 maximum length */
353                 uint8_t data[66];
354                 unsigned int length;
355         } prime;
356
357         struct {
358                 /* P521 maximum length */
359                 uint8_t data[66];
360                 unsigned int length;
361         } order;
362 };
363
364 struct cpt_asym_ec_ctx {
365         /* Prime length defined by microcode for EC operations */
366         uint8_t curveid;
367 };
368
369 struct cpt_asym_sess_misc {
370         enum rte_crypto_asym_xform_type xfrm_type;
371         union {
372                 struct rte_crypto_rsa_xform rsa_ctx;
373                 struct rte_crypto_modex_xform mod_ctx;
374                 struct cpt_asym_ec_ctx ec_ctx;
375         };
376 };
377
378 /* Buffer pointer */
379 typedef struct buf_ptr {
380         void *vaddr;
381         phys_addr_t dma_addr;
382         uint32_t size;
383         uint32_t resv;
384 } buf_ptr_t;
385
386 /* IOV Pointer */
387 typedef struct{
388         int buf_cnt;
389         buf_ptr_t bufs[0];
390 } iov_ptr_t;
391
392 typedef union opcode_info {
393         uint16_t flags;
394         struct {
395                 uint8_t major;
396                 uint8_t minor;
397         } s;
398 } opcode_info_t;
399
400 typedef struct fc_params {
401         /* 0th cache line */
402         union {
403                 buf_ptr_t bufs[1];
404                 struct {
405                         iov_ptr_t *src_iov;
406                         iov_ptr_t *dst_iov;
407                 };
408         };
409         void *iv_buf;
410         void *auth_iv_buf;
411         buf_ptr_t meta_buf;
412         buf_ptr_t ctx_buf;
413         uint64_t rsvd2;
414
415         /* 1st cache line */
416         buf_ptr_t aad_buf;
417         buf_ptr_t mac_buf;
418
419 } fc_params_t;
420
421 /*
422  * Parameters for asymmetric operations
423  */
424 struct asym_op_params {
425         struct cpt_request_info *req;
426         phys_addr_t meta_buf;
427 };
428
429 /*
430  * Parameters for digest
431  * generate requests
432  * Only src_iov, op, ctx_buf, mac_buf, prep_req
433  * meta_buf, auth_data_len are used for digest gen.
434  */
435 typedef struct fc_params digest_params_t;
436
437 /* Cipher Algorithms */
438 typedef mc_cipher_type_t cipher_type_t;
439
440 /* Auth Algorithms */
441 typedef mc_hash_type_t auth_type_t;
442
443 /* Helper macros */
444
445 #define CPT_P_ENC_CTRL(fctx)  fctx->enc.enc_ctrl.e
446
447 #define SRC_IOV_SIZE \
448         (sizeof(iov_ptr_t) + (sizeof(buf_ptr_t) * CPT_MAX_SG_CNT))
449 #define DST_IOV_SIZE \
450         (sizeof(iov_ptr_t) + (sizeof(buf_ptr_t) * CPT_MAX_SG_CNT))
451
452 #define SESS_PRIV(__sess) \
453         (void *)((uint8_t *)__sess + sizeof(struct cpt_sess_misc))
454
455 /*
456  * Get the session size
457  *
458  * @return
459  *   - session size
460  */
461 static __rte_always_inline unsigned int
462 cpt_get_session_size(void)
463 {
464         unsigned int ctx_len = sizeof(struct cpt_ctx);
465         return (sizeof(struct cpt_sess_misc) + RTE_ALIGN_CEIL(ctx_len, 8));
466 }
467 #endif /* _CPT_MCODE_DEFINES_H_ */