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