xref: /freebsd/sys/dev/cxgbe/crypto/t4_crypto.h (revision 6154961e2e4c7e1d998f4c9e22c5370d17edb160)
1 /*-
2  * Copyright (c) 2017 Chelsio Communications, Inc.
3  * All rights reserved.
4  * Written by: John Baldwin <jhb@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #ifndef __T4_CRYPTO_H__
29 #define	__T4_CRYPTO_H__
30 
31 /* From chr_core.h */
32 #define PAD_ERROR_BIT		1
33 #define CHK_PAD_ERR_BIT(x)	(((x) >> PAD_ERROR_BIT) & 1)
34 
35 #define MAC_ERROR_BIT		0
36 #define CHK_MAC_ERR_BIT(x)	(((x) >> MAC_ERROR_BIT) & 1)
37 #define MAX_SALT                4
38 
39 struct _key_ctx {
40 	__be32 ctx_hdr;
41 	u8 salt[MAX_SALT];
42 	__be64 reserverd;
43 	unsigned char key[0];
44 };
45 
46 struct chcr_wr {
47 	struct fw_crypto_lookaside_wr wreq;
48 	struct ulp_txpkt ulptx;
49 	struct ulptx_idata sc_imm;
50 	struct cpl_tx_sec_pdu sec_cpl;
51 	struct _key_ctx key_ctx;
52 };
53 
54 /* From chr_algo.h */
55 
56 /* Crypto key context */
57 #define S_KEY_CONTEXT_CTX_LEN           24
58 #define M_KEY_CONTEXT_CTX_LEN           0xff
59 #define V_KEY_CONTEXT_CTX_LEN(x)        ((x) << S_KEY_CONTEXT_CTX_LEN)
60 #define G_KEY_CONTEXT_CTX_LEN(x) \
61 	(((x) >> S_KEY_CONTEXT_CTX_LEN) & M_KEY_CONTEXT_CTX_LEN)
62 
63 #define S_KEY_CONTEXT_DUAL_CK      12
64 #define M_KEY_CONTEXT_DUAL_CK      0x1
65 #define V_KEY_CONTEXT_DUAL_CK(x)   ((x) << S_KEY_CONTEXT_DUAL_CK)
66 #define G_KEY_CONTEXT_DUAL_CK(x)   \
67 (((x) >> S_KEY_CONTEXT_DUAL_CK) & M_KEY_CONTEXT_DUAL_CK)
68 #define F_KEY_CONTEXT_DUAL_CK      V_KEY_CONTEXT_DUAL_CK(1U)
69 
70 #define S_KEY_CONTEXT_OPAD_PRESENT      11
71 #define M_KEY_CONTEXT_OPAD_PRESENT      0x1
72 #define V_KEY_CONTEXT_OPAD_PRESENT(x)   ((x) << S_KEY_CONTEXT_OPAD_PRESENT)
73 #define G_KEY_CONTEXT_OPAD_PRESENT(x)   \
74 	(((x) >> S_KEY_CONTEXT_OPAD_PRESENT) & \
75 	 M_KEY_CONTEXT_OPAD_PRESENT)
76 #define F_KEY_CONTEXT_OPAD_PRESENT      V_KEY_CONTEXT_OPAD_PRESENT(1U)
77 
78 #define S_KEY_CONTEXT_SALT_PRESENT      10
79 #define M_KEY_CONTEXT_SALT_PRESENT      0x1
80 #define V_KEY_CONTEXT_SALT_PRESENT(x)   ((x) << S_KEY_CONTEXT_SALT_PRESENT)
81 #define G_KEY_CONTEXT_SALT_PRESENT(x)   \
82 	(((x) >> S_KEY_CONTEXT_SALT_PRESENT) & \
83 	 M_KEY_CONTEXT_SALT_PRESENT)
84 #define F_KEY_CONTEXT_SALT_PRESENT      V_KEY_CONTEXT_SALT_PRESENT(1U)
85 
86 #define S_KEY_CONTEXT_CK_SIZE           6
87 #define M_KEY_CONTEXT_CK_SIZE           0xf
88 #define V_KEY_CONTEXT_CK_SIZE(x)        ((x) << S_KEY_CONTEXT_CK_SIZE)
89 #define G_KEY_CONTEXT_CK_SIZE(x)        \
90 	(((x) >> S_KEY_CONTEXT_CK_SIZE) & M_KEY_CONTEXT_CK_SIZE)
91 
92 #define S_KEY_CONTEXT_MK_SIZE           2
93 #define M_KEY_CONTEXT_MK_SIZE           0xf
94 #define V_KEY_CONTEXT_MK_SIZE(x)        ((x) << S_KEY_CONTEXT_MK_SIZE)
95 #define G_KEY_CONTEXT_MK_SIZE(x)        \
96 	(((x) >> S_KEY_CONTEXT_MK_SIZE) & M_KEY_CONTEXT_MK_SIZE)
97 
98 #define S_KEY_CONTEXT_VALID     0
99 #define M_KEY_CONTEXT_VALID     0x1
100 #define V_KEY_CONTEXT_VALID(x)  ((x) << S_KEY_CONTEXT_VALID)
101 #define G_KEY_CONTEXT_VALID(x)  \
102 	(((x) >> S_KEY_CONTEXT_VALID) & \
103 	 M_KEY_CONTEXT_VALID)
104 #define F_KEY_CONTEXT_VALID     V_KEY_CONTEXT_VALID(1U)
105 
106 #define CHCR_HASH_MAX_DIGEST_SIZE 64
107 
108 #define DUMMY_BYTES 16
109 
110 #define TRANSHDR_SIZE(kctx_len)\
111 	(sizeof(struct chcr_wr) +\
112 	 kctx_len)
113 #define CIPHER_TRANSHDR_SIZE(kctx_len, sge_pairs) \
114 	(TRANSHDR_SIZE((kctx_len)) + (sge_pairs) +\
115 	 sizeof(struct cpl_rx_phys_dsgl))
116 #define HASH_TRANSHDR_SIZE(kctx_len)\
117 	(TRANSHDR_SIZE(kctx_len) + DUMMY_BYTES)
118 
119 #define CRYPTO_MAX_IMM_TX_PKT_LEN 256
120 
121 struct phys_sge_pairs {
122 	__be16 len[8];
123 	__be64 addr[8];
124 };
125 
126 /* From chr_crypto.h */
127 #define CCM_B0_SIZE             16
128 #define CCM_AAD_FIELD_SIZE      2
129 
130 #define CHCR_AES_MAX_KEY_LEN  (AES_XTS_MAX_KEY)
131 #define CHCR_MAX_CRYPTO_IV_LEN 16 /* AES IV len */
132 
133 #define CHCR_ENCRYPT_OP 0
134 #define CHCR_DECRYPT_OP 1
135 
136 #define SCMD_ENCDECCTRL_ENCRYPT 0
137 #define SCMD_ENCDECCTRL_DECRYPT 1
138 
139 #define SCMD_PROTO_VERSION_TLS_1_2 0
140 #define SCMD_PROTO_VERSION_TLS_1_1 1
141 #define SCMD_PROTO_VERSION_GENERIC 4
142 #define SCMD_PROTO_VERSION_TLS_1_3 8
143 
144 #define SCMD_CIPH_MODE_NOP               0
145 #define SCMD_CIPH_MODE_AES_CBC           1
146 #define SCMD_CIPH_MODE_AES_GCM           2
147 #define SCMD_CIPH_MODE_AES_CTR           3
148 #define SCMD_CIPH_MODE_GENERIC_AES       4
149 #define SCMD_CIPH_MODE_AES_XTS           6
150 #define SCMD_CIPH_MODE_AES_CCM           7
151 
152 #define SCMD_AUTH_MODE_NOP             0
153 #define SCMD_AUTH_MODE_SHA1            1
154 #define SCMD_AUTH_MODE_SHA224          2
155 #define SCMD_AUTH_MODE_SHA256          3
156 #define SCMD_AUTH_MODE_GHASH           4
157 #define SCMD_AUTH_MODE_SHA512_224      5
158 #define SCMD_AUTH_MODE_SHA512_256      6
159 #define SCMD_AUTH_MODE_SHA512_384      7
160 #define SCMD_AUTH_MODE_SHA512_512      8
161 #define SCMD_AUTH_MODE_CBCMAC          9
162 #define SCMD_AUTH_MODE_CMAC            10
163 
164 #define SCMD_HMAC_CTRL_NOP             0
165 #define SCMD_HMAC_CTRL_NO_TRUNC        1
166 #define SCMD_HMAC_CTRL_TRUNC_RFC4366   2
167 #define SCMD_HMAC_CTRL_IPSEC_96BIT     3
168 #define SCMD_HMAC_CTRL_PL1             4
169 #define SCMD_HMAC_CTRL_PL2             5
170 #define SCMD_HMAC_CTRL_PL3             6
171 #define SCMD_HMAC_CTRL_DIV2            7
172 
173 /* This are not really mac key size. They are intermediate values
174  * of sha engine and its size
175  */
176 #define CHCR_KEYCTX_MAC_KEY_SIZE_128        0
177 #define CHCR_KEYCTX_MAC_KEY_SIZE_160        1
178 #define CHCR_KEYCTX_MAC_KEY_SIZE_192        2
179 #define CHCR_KEYCTX_MAC_KEY_SIZE_256        3
180 #define CHCR_KEYCTX_MAC_KEY_SIZE_512        4
181 #define CHCR_KEYCTX_CIPHER_KEY_SIZE_128     0
182 #define CHCR_KEYCTX_CIPHER_KEY_SIZE_192     1
183 #define CHCR_KEYCTX_CIPHER_KEY_SIZE_256     2
184 #define CHCR_KEYCTX_NO_KEY                  15
185 
186 #define IV_NOP                  0
187 #define IV_IMMEDIATE            1
188 #define IV_DSGL			2
189 
190 #define CHCR_HASH_MAX_BLOCK_SIZE_64  64
191 #define CHCR_HASH_MAX_BLOCK_SIZE_128 128
192 
193 /*
194  * TODO: Should coalesce ccr's key context with the TLS key context.
195  * Lookaside requests use the TX context header.
196  */
197 
198 struct tls_key_req {
199 	/* FW_ULPTX_WR */
200 	__be32 wr_hi;
201 	__be32 wr_mid;
202         __be32 ftid;
203         __u8   reneg_to_write_rx;
204         __u8   protocol;
205         __be16 mfs;
206 	/* master command */
207 	__be32 cmd;
208 	__be32 len16;             /* command length */
209 	__be32 dlen;              /* data length in 32-byte units */
210 	__be32 kaddr;
211 	/* sub-command */
212 	__be32 sc_more;
213 	__be32 sc_len;
214 }__packed;
215 
216 struct tls_keyctx {
217         union key_ctx {
218                 struct tx_keyctx_hdr {
219                         __u8   ctxlen;
220                         __u8   r2;
221                         __be16 dualck_to_txvalid;
222                         __u8   txsalt[4];
223                         __be64 r5;
224                 } txhdr;
225                 struct rx_keyctx_hdr {
226                         __u8   flitcnt_hmacctrl;
227                         __u8   protover_ciphmode;
228                         __u8   authmode_to_rxvalid;
229                         __u8   ivpresent_to_rxmk_size;
230                         __u8   rxsalt[4];
231                         __be64 ivinsert_to_authinsrt;
232                 } rxhdr;
233         } u;
234         struct keys {
235                 __u8   edkey[32];
236                 __u8   ipad[64];
237                 __u8   opad[64];
238         } keys;
239 };
240 
241 #define SALT_SIZE		4
242 
243 #define	TLS_KEY_CONTEXT_SZ	roundup2(sizeof(struct tls_keyctx), 32)
244 
245 #define	TLS_KEY_WR_SZ							\
246 	roundup2(sizeof(struct tls_key_req) + TLS_KEY_CONTEXT_SZ, 16)
247 
248 #endif /* !__T4_CRYPTO_H__ */
249