xref: /openbmc/linux/net/tls/tls.h (revision 2b3082c6ef3b0104d822f6f18d2afbe5fc9a5c2c)
1 /*
2  * Copyright (c) 2016 Tom Herbert <tom@herbertland.com>
3  * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
4  * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34 
35 #ifndef _TLS_INT_H
36 #define _TLS_INT_H
37 
38 #include <asm/byteorder.h>
39 #include <linux/types.h>
40 #include <linux/skmsg.h>
41 #include <net/tls.h>
42 #include <net/tls_prot.h>
43 
44 #define TLS_PAGE_ORDER	(min_t(unsigned int, PAGE_ALLOC_COSTLY_ORDER,	\
45 			       TLS_MAX_PAYLOAD_SIZE >> PAGE_SHIFT))
46 
47 #define __TLS_INC_STATS(net, field)				\
48 	__SNMP_INC_STATS((net)->mib.tls_statistics, field)
49 #define TLS_INC_STATS(net, field)				\
50 	SNMP_INC_STATS((net)->mib.tls_statistics, field)
51 #define TLS_DEC_STATS(net, field)				\
52 	SNMP_DEC_STATS((net)->mib.tls_statistics, field)
53 
54 /* TLS records are maintained in 'struct tls_rec'. It stores the memory pages
55  * allocated or mapped for each TLS record. After encryption, the records are
56  * stores in a linked list.
57  */
58 struct tls_rec {
59 	struct list_head list;
60 	int tx_ready;
61 	int tx_flags;
62 
63 	struct sk_msg msg_plaintext;
64 	struct sk_msg msg_encrypted;
65 
66 	/* AAD | msg_plaintext.sg.data | sg_tag */
67 	struct scatterlist sg_aead_in[2];
68 	/* AAD | msg_encrypted.sg.data (data contains overhead for hdr & iv & tag) */
69 	struct scatterlist sg_aead_out[2];
70 
71 	char content_type;
72 	struct scatterlist sg_content_type;
73 
74 	struct sock *sk;
75 
76 	char aad_space[TLS_AAD_SPACE_SIZE];
77 	u8 iv_data[MAX_IV_SIZE];
78 	struct aead_request aead_req;
79 	u8 aead_req_ctx[];
80 };
81 
82 int __net_init tls_proc_init(struct net *net);
83 void __net_exit tls_proc_fini(struct net *net);
84 
85 struct tls_context *tls_ctx_create(struct sock *sk);
86 void tls_ctx_free(struct sock *sk, struct tls_context *ctx);
87 void update_sk_prot(struct sock *sk, struct tls_context *ctx);
88 
89 int wait_on_pending_writer(struct sock *sk, long *timeo);
90 int tls_sk_query(struct sock *sk, int optname, char __user *optval,
91 		 int __user *optlen);
92 int tls_sk_attach(struct sock *sk, int optname, char __user *optval,
93 		  unsigned int optlen);
94 void tls_err_abort(struct sock *sk, int err);
95 
96 int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx);
97 void tls_update_rx_zc_capable(struct tls_context *tls_ctx);
98 void tls_sw_strparser_arm(struct sock *sk, struct tls_context *ctx);
99 void tls_sw_strparser_done(struct tls_context *tls_ctx);
100 int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
101 void tls_sw_splice_eof(struct socket *sock);
102 void tls_sw_cancel_work_tx(struct tls_context *tls_ctx);
103 void tls_sw_release_resources_tx(struct sock *sk);
104 void tls_sw_free_ctx_tx(struct tls_context *tls_ctx);
105 void tls_sw_free_resources_rx(struct sock *sk);
106 void tls_sw_release_resources_rx(struct sock *sk);
107 void tls_sw_free_ctx_rx(struct tls_context *tls_ctx);
108 int tls_sw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
109 		   int flags, int *addr_len);
110 bool tls_sw_sock_is_readable(struct sock *sk);
111 ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
112 			   struct pipe_inode_info *pipe,
113 			   size_t len, unsigned int flags);
114 int tls_sw_read_sock(struct sock *sk, read_descriptor_t *desc,
115 		     sk_read_actor_t read_actor);
116 
117 int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
118 void tls_device_splice_eof(struct socket *sock);
119 int tls_tx_records(struct sock *sk, int flags);
120 
121 void tls_sw_write_space(struct sock *sk, struct tls_context *ctx);
122 void tls_device_write_space(struct sock *sk, struct tls_context *ctx);
123 
124 int tls_process_cmsg(struct sock *sk, struct msghdr *msg,
125 		     unsigned char *record_type);
126 int decrypt_skb(struct sock *sk, struct scatterlist *sgout);
127 
128 int tls_sw_fallback_init(struct sock *sk,
129 			 struct tls_offload_context_tx *offload_ctx,
130 			 struct tls_crypto_info *crypto_info);
131 
132 int tls_strp_dev_init(void);
133 void tls_strp_dev_exit(void);
134 
135 void tls_strp_done(struct tls_strparser *strp);
136 void tls_strp_stop(struct tls_strparser *strp);
137 int tls_strp_init(struct tls_strparser *strp, struct sock *sk);
138 void tls_strp_data_ready(struct tls_strparser *strp);
139 
140 void tls_strp_check_rcv(struct tls_strparser *strp);
141 void tls_strp_msg_done(struct tls_strparser *strp);
142 
143 int tls_rx_msg_size(struct tls_strparser *strp, struct sk_buff *skb);
144 void tls_rx_msg_ready(struct tls_strparser *strp);
145 
146 void tls_strp_msg_load(struct tls_strparser *strp, bool force_refresh);
147 int tls_strp_msg_cow(struct tls_sw_context_rx *ctx);
148 struct sk_buff *tls_strp_msg_detach(struct tls_sw_context_rx *ctx);
149 int tls_strp_msg_hold(struct tls_strparser *strp, struct sk_buff_head *dst);
150 
151 static inline struct tls_msg *tls_msg(struct sk_buff *skb)
152 {
153 	struct sk_skb_cb *scb = (struct sk_skb_cb *)skb->cb;
154 
155 	return &scb->tls;
156 }
157 
158 static inline struct sk_buff *tls_strp_msg(struct tls_sw_context_rx *ctx)
159 {
160 	DEBUG_NET_WARN_ON_ONCE(!ctx->strp.msg_ready || !ctx->strp.anchor->len);
161 	return ctx->strp.anchor;
162 }
163 
164 static inline bool tls_strp_msg_ready(struct tls_sw_context_rx *ctx)
165 {
166 	return ctx->strp.msg_ready;
167 }
168 
169 static inline bool tls_strp_msg_mixed_decrypted(struct tls_sw_context_rx *ctx)
170 {
171 	return ctx->strp.mixed_decrypted;
172 }
173 
174 #ifdef CONFIG_TLS_DEVICE
175 int tls_device_init(void);
176 void tls_device_cleanup(void);
177 int tls_set_device_offload(struct sock *sk, struct tls_context *ctx);
178 void tls_device_free_resources_tx(struct sock *sk);
179 int tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx);
180 void tls_device_offload_cleanup_rx(struct sock *sk);
181 void tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq);
182 int tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx);
183 #else
184 static inline int tls_device_init(void) { return 0; }
185 static inline void tls_device_cleanup(void) {}
186 
187 static inline int
188 tls_set_device_offload(struct sock *sk, struct tls_context *ctx)
189 {
190 	return -EOPNOTSUPP;
191 }
192 
193 static inline void tls_device_free_resources_tx(struct sock *sk) {}
194 
195 static inline int
196 tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx)
197 {
198 	return -EOPNOTSUPP;
199 }
200 
201 static inline void tls_device_offload_cleanup_rx(struct sock *sk) {}
202 static inline void
203 tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq) {}
204 
205 static inline int
206 tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx)
207 {
208 	return 0;
209 }
210 #endif
211 
212 int tls_push_sg(struct sock *sk, struct tls_context *ctx,
213 		struct scatterlist *sg, u16 first_offset,
214 		int flags);
215 int tls_push_partial_record(struct sock *sk, struct tls_context *ctx,
216 			    int flags);
217 void tls_free_partial_record(struct sock *sk, struct tls_context *ctx);
218 
219 static inline bool tls_is_partially_sent_record(struct tls_context *ctx)
220 {
221 	return !!ctx->partially_sent_record;
222 }
223 
224 static inline bool tls_is_pending_open_record(struct tls_context *tls_ctx)
225 {
226 	return tls_ctx->pending_open_record_frags;
227 }
228 
229 static inline bool tls_bigint_increment(unsigned char *seq, int len)
230 {
231 	int i;
232 
233 	for (i = len - 1; i >= 0; i--) {
234 		++seq[i];
235 		if (seq[i] != 0)
236 			break;
237 	}
238 
239 	return (i == -1);
240 }
241 
242 static inline void tls_bigint_subtract(unsigned char *seq, int  n)
243 {
244 	u64 rcd_sn;
245 	__be64 *p;
246 
247 	BUILD_BUG_ON(TLS_MAX_REC_SEQ_SIZE != 8);
248 
249 	p = (__be64 *)seq;
250 	rcd_sn = be64_to_cpu(*p);
251 	*p = cpu_to_be64(rcd_sn - n);
252 }
253 
254 static inline void
255 tls_advance_record_sn(struct sock *sk, struct tls_prot_info *prot,
256 		      struct cipher_context *ctx)
257 {
258 	if (tls_bigint_increment(ctx->rec_seq, prot->rec_seq_size))
259 		tls_err_abort(sk, -EBADMSG);
260 
261 	if (prot->version != TLS_1_3_VERSION &&
262 	    prot->cipher_type != TLS_CIPHER_CHACHA20_POLY1305)
263 		tls_bigint_increment(ctx->iv + prot->salt_size,
264 				     prot->iv_size);
265 }
266 
267 static inline void
268 tls_xor_iv_with_seq(struct tls_prot_info *prot, char *iv, char *seq)
269 {
270 	int i;
271 
272 	if (prot->version == TLS_1_3_VERSION ||
273 	    prot->cipher_type == TLS_CIPHER_CHACHA20_POLY1305) {
274 		for (i = 0; i < 8; i++)
275 			iv[i + 4] ^= seq[i];
276 	}
277 }
278 
279 static inline void
280 tls_fill_prepend(struct tls_context *ctx, char *buf, size_t plaintext_len,
281 		 unsigned char record_type)
282 {
283 	struct tls_prot_info *prot = &ctx->prot_info;
284 	size_t pkt_len, iv_size = prot->iv_size;
285 
286 	pkt_len = plaintext_len + prot->tag_size;
287 	if (prot->version != TLS_1_3_VERSION &&
288 	    prot->cipher_type != TLS_CIPHER_CHACHA20_POLY1305) {
289 		pkt_len += iv_size;
290 
291 		memcpy(buf + TLS_NONCE_OFFSET,
292 		       ctx->tx.iv + prot->salt_size, iv_size);
293 	}
294 
295 	/* we cover nonce explicit here as well, so buf should be of
296 	 * size KTLS_DTLS_HEADER_SIZE + KTLS_DTLS_NONCE_EXPLICIT_SIZE
297 	 */
298 	buf[0] = prot->version == TLS_1_3_VERSION ?
299 		   TLS_RECORD_TYPE_DATA : record_type;
300 	/* Note that VERSION must be TLS_1_2 for both TLS1.2 and TLS1.3 */
301 	buf[1] = TLS_1_2_VERSION_MINOR;
302 	buf[2] = TLS_1_2_VERSION_MAJOR;
303 	/* we can use IV for nonce explicit according to spec */
304 	buf[3] = pkt_len >> 8;
305 	buf[4] = pkt_len & 0xFF;
306 }
307 
308 static inline
309 void tls_make_aad(char *buf, size_t size, char *record_sequence,
310 		  unsigned char record_type, struct tls_prot_info *prot)
311 {
312 	if (prot->version != TLS_1_3_VERSION) {
313 		memcpy(buf, record_sequence, prot->rec_seq_size);
314 		buf += 8;
315 	} else {
316 		size += prot->tag_size;
317 	}
318 
319 	buf[0] = prot->version == TLS_1_3_VERSION ?
320 		  TLS_RECORD_TYPE_DATA : record_type;
321 	buf[1] = TLS_1_2_VERSION_MAJOR;
322 	buf[2] = TLS_1_2_VERSION_MINOR;
323 	buf[3] = size >> 8;
324 	buf[4] = size & 0xFF;
325 }
326 
327 #endif
328