xref: /freebsd/sys/dev/cxgbe/crypto/t4_crypto.h (revision a64729f5077d77e13b9497cb33ecb3c82e606ee8)
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 
143 #define SCMD_CIPH_MODE_NOP               0
144 #define SCMD_CIPH_MODE_AES_CBC           1
145 #define SCMD_CIPH_MODE_AES_GCM           2
146 #define SCMD_CIPH_MODE_AES_CTR           3
147 #define SCMD_CIPH_MODE_GENERIC_AES       4
148 #define SCMD_CIPH_MODE_AES_XTS           6
149 #define SCMD_CIPH_MODE_AES_CCM           7
150 
151 #define SCMD_AUTH_MODE_NOP             0
152 #define SCMD_AUTH_MODE_SHA1            1
153 #define SCMD_AUTH_MODE_SHA224          2
154 #define SCMD_AUTH_MODE_SHA256          3
155 #define SCMD_AUTH_MODE_GHASH           4
156 #define SCMD_AUTH_MODE_SHA512_224      5
157 #define SCMD_AUTH_MODE_SHA512_256      6
158 #define SCMD_AUTH_MODE_SHA512_384      7
159 #define SCMD_AUTH_MODE_SHA512_512      8
160 #define SCMD_AUTH_MODE_CBCMAC          9
161 #define SCMD_AUTH_MODE_CMAC            10
162 
163 #define SCMD_HMAC_CTRL_NOP             0
164 #define SCMD_HMAC_CTRL_NO_TRUNC        1
165 #define SCMD_HMAC_CTRL_TRUNC_RFC4366   2
166 #define SCMD_HMAC_CTRL_IPSEC_96BIT     3
167 #define SCMD_HMAC_CTRL_PL1             4
168 #define SCMD_HMAC_CTRL_PL2             5
169 #define SCMD_HMAC_CTRL_PL3             6
170 #define SCMD_HMAC_CTRL_DIV2            7
171 
172 /* This are not really mac key size. They are intermediate values
173  * of sha engine and its size
174  */
175 #define CHCR_KEYCTX_MAC_KEY_SIZE_128        0
176 #define CHCR_KEYCTX_MAC_KEY_SIZE_160        1
177 #define CHCR_KEYCTX_MAC_KEY_SIZE_192        2
178 #define CHCR_KEYCTX_MAC_KEY_SIZE_256        3
179 #define CHCR_KEYCTX_MAC_KEY_SIZE_512        4
180 #define CHCR_KEYCTX_CIPHER_KEY_SIZE_128     0
181 #define CHCR_KEYCTX_CIPHER_KEY_SIZE_192     1
182 #define CHCR_KEYCTX_CIPHER_KEY_SIZE_256     2
183 #define CHCR_KEYCTX_NO_KEY                  15
184 
185 #define IV_NOP                  0
186 #define IV_IMMEDIATE            1
187 #define IV_DSGL			2
188 
189 #define CHCR_HASH_MAX_BLOCK_SIZE_64  64
190 #define CHCR_HASH_MAX_BLOCK_SIZE_128 128
191 
192 /*
193  * TODO: Should coalesce ccr's key context with the TLS key context.
194  * Lookaside requests use the TX context header.
195  */
196 
197 struct tls_key_req {
198 	/* FW_ULPTX_WR */
199 	__be32 wr_hi;
200 	__be32 wr_mid;
201         __be32 ftid;
202         __u8   reneg_to_write_rx;
203         __u8   protocol;
204         __be16 mfs;
205 	/* master command */
206 	__be32 cmd;
207 	__be32 len16;             /* command length */
208 	__be32 dlen;              /* data length in 32-byte units */
209 	__be32 kaddr;
210 	/* sub-command */
211 	__be32 sc_more;
212 	__be32 sc_len;
213 }__packed;
214 
215 struct tls_keyctx {
216         union key_ctx {
217                 struct tx_keyctx_hdr {
218                         __u8   ctxlen;
219                         __u8   r2;
220                         __be16 dualck_to_txvalid;
221                         __u8   txsalt[4];
222                         __be64 r5;
223                 } txhdr;
224                 struct rx_keyctx_hdr {
225                         __u8   flitcnt_hmacctrl;
226                         __u8   protover_ciphmode;
227                         __u8   authmode_to_rxvalid;
228                         __u8   ivpresent_to_rxmk_size;
229                         __u8   rxsalt[4];
230                         __be64 ivinsert_to_authinsrt;
231                 } rxhdr;
232         } u;
233         struct keys {
234                 __u8   edkey[32];
235                 __u8   ipad[64];
236                 __u8   opad[64];
237         } keys;
238 };
239 
240 #define SALT_SIZE		4
241 
242 #define	TLS_KEY_CONTEXT_SZ	roundup2(sizeof(struct tls_keyctx), 32)
243 
244 #define	TLS_KEY_WR_SZ							\
245 	roundup2(sizeof(struct tls_key_req) + TLS_KEY_CONTEXT_SZ, 16)
246 
247 #endif /* !__T4_CRYPTO_H__ */
248