xref: /linux/net/tls/tls.h (revision 84c61fe1a75b4255df1e1e7c054c9e6d048da417)
158790314SJakub Kicinski /*
2*84c61fe1SJakub Kicinski  * Copyright (c) 2016 Tom Herbert <tom@herbertland.com>
358790314SJakub Kicinski  * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
458790314SJakub Kicinski  * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved.
558790314SJakub Kicinski  *
658790314SJakub Kicinski  * This software is available to you under a choice of one of two
758790314SJakub Kicinski  * licenses.  You may choose to be licensed under the terms of the GNU
858790314SJakub Kicinski  * General Public License (GPL) Version 2, available from the file
958790314SJakub Kicinski  * COPYING in the main directory of this source tree, or the
1058790314SJakub Kicinski  * OpenIB.org BSD license below:
1158790314SJakub Kicinski  *
1258790314SJakub Kicinski  *     Redistribution and use in source and binary forms, with or
1358790314SJakub Kicinski  *     without modification, are permitted provided that the following
1458790314SJakub Kicinski  *     conditions are met:
1558790314SJakub Kicinski  *
1658790314SJakub Kicinski  *      - Redistributions of source code must retain the above
1758790314SJakub Kicinski  *        copyright notice, this list of conditions and the following
1858790314SJakub Kicinski  *        disclaimer.
1958790314SJakub Kicinski  *
2058790314SJakub Kicinski  *      - Redistributions in binary form must reproduce the above
2158790314SJakub Kicinski  *        copyright notice, this list of conditions and the following
2258790314SJakub Kicinski  *        disclaimer in the documentation and/or other materials
2358790314SJakub Kicinski  *        provided with the distribution.
2458790314SJakub Kicinski  *
2558790314SJakub Kicinski  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2658790314SJakub Kicinski  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2758790314SJakub Kicinski  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2858790314SJakub Kicinski  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2958790314SJakub Kicinski  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
3058790314SJakub Kicinski  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
3158790314SJakub Kicinski  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3258790314SJakub Kicinski  * SOFTWARE.
3358790314SJakub Kicinski  */
3458790314SJakub Kicinski 
3558790314SJakub Kicinski #ifndef _TLS_INT_H
3658790314SJakub Kicinski #define _TLS_INT_H
3758790314SJakub Kicinski 
3858790314SJakub Kicinski #include <asm/byteorder.h>
3958790314SJakub Kicinski #include <linux/types.h>
4058790314SJakub Kicinski #include <linux/skmsg.h>
4158790314SJakub Kicinski #include <net/tls.h>
4258790314SJakub Kicinski 
43fd31f399SJakub Kicinski #define TLS_PAGE_ORDER	(min_t(unsigned int, PAGE_ALLOC_COSTLY_ORDER,	\
44fd31f399SJakub Kicinski 			       TLS_MAX_PAYLOAD_SIZE >> PAGE_SHIFT))
45fd31f399SJakub Kicinski 
4658790314SJakub Kicinski #define __TLS_INC_STATS(net, field)				\
4758790314SJakub Kicinski 	__SNMP_INC_STATS((net)->mib.tls_statistics, field)
4858790314SJakub Kicinski #define TLS_INC_STATS(net, field)				\
4958790314SJakub Kicinski 	SNMP_INC_STATS((net)->mib.tls_statistics, field)
5058790314SJakub Kicinski #define TLS_DEC_STATS(net, field)				\
5158790314SJakub Kicinski 	SNMP_DEC_STATS((net)->mib.tls_statistics, field)
5258790314SJakub Kicinski 
5358790314SJakub Kicinski /* TLS records are maintained in 'struct tls_rec'. It stores the memory pages
5458790314SJakub Kicinski  * allocated or mapped for each TLS record. After encryption, the records are
5558790314SJakub Kicinski  * stores in a linked list.
5658790314SJakub Kicinski  */
5758790314SJakub Kicinski struct tls_rec {
5858790314SJakub Kicinski 	struct list_head list;
5958790314SJakub Kicinski 	int tx_ready;
6058790314SJakub Kicinski 	int tx_flags;
6158790314SJakub Kicinski 
6258790314SJakub Kicinski 	struct sk_msg msg_plaintext;
6358790314SJakub Kicinski 	struct sk_msg msg_encrypted;
6458790314SJakub Kicinski 
6558790314SJakub Kicinski 	/* AAD | msg_plaintext.sg.data | sg_tag */
6658790314SJakub Kicinski 	struct scatterlist sg_aead_in[2];
6758790314SJakub Kicinski 	/* AAD | msg_encrypted.sg.data (data contains overhead for hdr & iv & tag) */
6858790314SJakub Kicinski 	struct scatterlist sg_aead_out[2];
6958790314SJakub Kicinski 
7058790314SJakub Kicinski 	char content_type;
7158790314SJakub Kicinski 	struct scatterlist sg_content_type;
7258790314SJakub Kicinski 
7358790314SJakub Kicinski 	char aad_space[TLS_AAD_SPACE_SIZE];
7458790314SJakub Kicinski 	u8 iv_data[MAX_IV_SIZE];
7558790314SJakub Kicinski 	struct aead_request aead_req;
7658790314SJakub Kicinski 	u8 aead_req_ctx[];
7758790314SJakub Kicinski };
7858790314SJakub Kicinski 
7958790314SJakub Kicinski int __net_init tls_proc_init(struct net *net);
8058790314SJakub Kicinski void __net_exit tls_proc_fini(struct net *net);
8158790314SJakub Kicinski 
8258790314SJakub Kicinski struct tls_context *tls_ctx_create(struct sock *sk);
8358790314SJakub Kicinski void tls_ctx_free(struct sock *sk, struct tls_context *ctx);
8458790314SJakub Kicinski void update_sk_prot(struct sock *sk, struct tls_context *ctx);
8558790314SJakub Kicinski 
8658790314SJakub Kicinski int wait_on_pending_writer(struct sock *sk, long *timeo);
8758790314SJakub Kicinski int tls_sk_query(struct sock *sk, int optname, char __user *optval,
8858790314SJakub Kicinski 		 int __user *optlen);
8958790314SJakub Kicinski int tls_sk_attach(struct sock *sk, int optname, char __user *optval,
9058790314SJakub Kicinski 		  unsigned int optlen);
9158790314SJakub Kicinski void tls_err_abort(struct sock *sk, int err);
9258790314SJakub Kicinski 
9358790314SJakub Kicinski int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx);
9458790314SJakub Kicinski void tls_update_rx_zc_capable(struct tls_context *tls_ctx);
9558790314SJakub Kicinski void tls_sw_strparser_arm(struct sock *sk, struct tls_context *ctx);
9658790314SJakub Kicinski void tls_sw_strparser_done(struct tls_context *tls_ctx);
9758790314SJakub Kicinski int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
9858790314SJakub Kicinski int tls_sw_sendpage_locked(struct sock *sk, struct page *page,
9958790314SJakub Kicinski 			   int offset, size_t size, int flags);
10058790314SJakub Kicinski int tls_sw_sendpage(struct sock *sk, struct page *page,
10158790314SJakub Kicinski 		    int offset, size_t size, int flags);
10258790314SJakub Kicinski void tls_sw_cancel_work_tx(struct tls_context *tls_ctx);
10358790314SJakub Kicinski void tls_sw_release_resources_tx(struct sock *sk);
10458790314SJakub Kicinski void tls_sw_free_ctx_tx(struct tls_context *tls_ctx);
10558790314SJakub Kicinski void tls_sw_free_resources_rx(struct sock *sk);
10658790314SJakub Kicinski void tls_sw_release_resources_rx(struct sock *sk);
10758790314SJakub Kicinski void tls_sw_free_ctx_rx(struct tls_context *tls_ctx);
10858790314SJakub Kicinski int tls_sw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
10958790314SJakub Kicinski 		   int flags, int *addr_len);
11058790314SJakub Kicinski bool tls_sw_sock_is_readable(struct sock *sk);
11158790314SJakub Kicinski ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
11258790314SJakub Kicinski 			   struct pipe_inode_info *pipe,
11358790314SJakub Kicinski 			   size_t len, unsigned int flags);
11458790314SJakub Kicinski 
11558790314SJakub Kicinski int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
11658790314SJakub Kicinski int tls_device_sendpage(struct sock *sk, struct page *page,
11758790314SJakub Kicinski 			int offset, size_t size, int flags);
11858790314SJakub Kicinski int tls_tx_records(struct sock *sk, int flags);
11958790314SJakub Kicinski 
12058790314SJakub Kicinski void tls_sw_write_space(struct sock *sk, struct tls_context *ctx);
12158790314SJakub Kicinski void tls_device_write_space(struct sock *sk, struct tls_context *ctx);
12258790314SJakub Kicinski 
12358790314SJakub Kicinski int tls_process_cmsg(struct sock *sk, struct msghdr *msg,
12458790314SJakub Kicinski 		     unsigned char *record_type);
125541cc48bSJakub Kicinski int decrypt_skb(struct sock *sk, struct scatterlist *sgout);
12658790314SJakub Kicinski 
12758790314SJakub Kicinski int tls_sw_fallback_init(struct sock *sk,
12858790314SJakub Kicinski 			 struct tls_offload_context_tx *offload_ctx,
12958790314SJakub Kicinski 			 struct tls_crypto_info *crypto_info);
13058790314SJakub Kicinski 
131*84c61fe1SJakub Kicinski int tls_strp_dev_init(void);
132*84c61fe1SJakub Kicinski void tls_strp_dev_exit(void);
133*84c61fe1SJakub Kicinski 
134*84c61fe1SJakub Kicinski void tls_strp_done(struct tls_strparser *strp);
135*84c61fe1SJakub Kicinski void tls_strp_stop(struct tls_strparser *strp);
136*84c61fe1SJakub Kicinski int tls_strp_init(struct tls_strparser *strp, struct sock *sk);
137*84c61fe1SJakub Kicinski void tls_strp_data_ready(struct tls_strparser *strp);
138*84c61fe1SJakub Kicinski 
139*84c61fe1SJakub Kicinski void tls_strp_check_rcv(struct tls_strparser *strp);
140*84c61fe1SJakub Kicinski void tls_strp_msg_done(struct tls_strparser *strp);
141*84c61fe1SJakub Kicinski 
142*84c61fe1SJakub Kicinski int tls_rx_msg_size(struct tls_strparser *strp, struct sk_buff *skb);
143*84c61fe1SJakub Kicinski void tls_rx_msg_ready(struct tls_strparser *strp);
144*84c61fe1SJakub Kicinski 
145*84c61fe1SJakub Kicinski void tls_strp_msg_load(struct tls_strparser *strp, bool force_refresh);
1468b3c59a7SJakub Kicinski int tls_strp_msg_cow(struct tls_sw_context_rx *ctx);
147d4e5db64SJakub Kicinski struct sk_buff *tls_strp_msg_detach(struct tls_sw_context_rx *ctx);
148*84c61fe1SJakub Kicinski int tls_strp_msg_hold(struct tls_strparser *strp, struct sk_buff_head *dst);
149c618db2aSJakub Kicinski 
15058790314SJakub Kicinski static inline struct tls_msg *tls_msg(struct sk_buff *skb)
15158790314SJakub Kicinski {
15258790314SJakub Kicinski 	struct sk_skb_cb *scb = (struct sk_skb_cb *)skb->cb;
15358790314SJakub Kicinski 
15458790314SJakub Kicinski 	return &scb->tls;
15558790314SJakub Kicinski }
15658790314SJakub Kicinski 
157541cc48bSJakub Kicinski static inline struct sk_buff *tls_strp_msg(struct tls_sw_context_rx *ctx)
158541cc48bSJakub Kicinski {
159*84c61fe1SJakub Kicinski 	DEBUG_NET_WARN_ON_ONCE(!ctx->strp.msg_ready || !ctx->strp.anchor->len);
160*84c61fe1SJakub Kicinski 	return ctx->strp.anchor;
161541cc48bSJakub Kicinski }
162541cc48bSJakub Kicinski 
163b92a13d4SJakub Kicinski static inline bool tls_strp_msg_ready(struct tls_sw_context_rx *ctx)
164b92a13d4SJakub Kicinski {
165*84c61fe1SJakub Kicinski 	return ctx->strp.msg_ready;
166b92a13d4SJakub Kicinski }
167b92a13d4SJakub Kicinski 
16858790314SJakub Kicinski #ifdef CONFIG_TLS_DEVICE
169816cd168SJakub Kicinski int tls_device_init(void);
17058790314SJakub Kicinski void tls_device_cleanup(void);
17158790314SJakub Kicinski int tls_set_device_offload(struct sock *sk, struct tls_context *ctx);
17258790314SJakub Kicinski void tls_device_free_resources_tx(struct sock *sk);
17358790314SJakub Kicinski int tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx);
17458790314SJakub Kicinski void tls_device_offload_cleanup_rx(struct sock *sk);
17558790314SJakub Kicinski void tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq);
176541cc48bSJakub Kicinski int tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx);
17758790314SJakub Kicinski #else
178816cd168SJakub Kicinski static inline int tls_device_init(void) { return 0; }
17958790314SJakub Kicinski static inline void tls_device_cleanup(void) {}
18058790314SJakub Kicinski 
18158790314SJakub Kicinski static inline int
18258790314SJakub Kicinski tls_set_device_offload(struct sock *sk, struct tls_context *ctx)
18358790314SJakub Kicinski {
18458790314SJakub Kicinski 	return -EOPNOTSUPP;
18558790314SJakub Kicinski }
18658790314SJakub Kicinski 
18758790314SJakub Kicinski static inline void tls_device_free_resources_tx(struct sock *sk) {}
18858790314SJakub Kicinski 
18958790314SJakub Kicinski static inline int
19058790314SJakub Kicinski tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx)
19158790314SJakub Kicinski {
19258790314SJakub Kicinski 	return -EOPNOTSUPP;
19358790314SJakub Kicinski }
19458790314SJakub Kicinski 
19558790314SJakub Kicinski static inline void tls_device_offload_cleanup_rx(struct sock *sk) {}
19658790314SJakub Kicinski static inline void
19758790314SJakub Kicinski tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq) {}
19858790314SJakub Kicinski 
19958790314SJakub Kicinski static inline int
200541cc48bSJakub Kicinski tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx)
20158790314SJakub Kicinski {
20258790314SJakub Kicinski 	return 0;
20358790314SJakub Kicinski }
20458790314SJakub Kicinski #endif
20558790314SJakub Kicinski 
20658790314SJakub Kicinski int tls_push_sg(struct sock *sk, struct tls_context *ctx,
20758790314SJakub Kicinski 		struct scatterlist *sg, u16 first_offset,
20858790314SJakub Kicinski 		int flags);
20958790314SJakub Kicinski int tls_push_partial_record(struct sock *sk, struct tls_context *ctx,
21058790314SJakub Kicinski 			    int flags);
21158790314SJakub Kicinski void tls_free_partial_record(struct sock *sk, struct tls_context *ctx);
21258790314SJakub Kicinski 
21358790314SJakub Kicinski static inline bool tls_is_partially_sent_record(struct tls_context *ctx)
21458790314SJakub Kicinski {
21558790314SJakub Kicinski 	return !!ctx->partially_sent_record;
21658790314SJakub Kicinski }
21758790314SJakub Kicinski 
21858790314SJakub Kicinski static inline bool tls_is_pending_open_record(struct tls_context *tls_ctx)
21958790314SJakub Kicinski {
22058790314SJakub Kicinski 	return tls_ctx->pending_open_record_frags;
22158790314SJakub Kicinski }
22258790314SJakub Kicinski 
22358790314SJakub Kicinski static inline bool tls_bigint_increment(unsigned char *seq, int len)
22458790314SJakub Kicinski {
22558790314SJakub Kicinski 	int i;
22658790314SJakub Kicinski 
22758790314SJakub Kicinski 	for (i = len - 1; i >= 0; i--) {
22858790314SJakub Kicinski 		++seq[i];
22958790314SJakub Kicinski 		if (seq[i] != 0)
23058790314SJakub Kicinski 			break;
23158790314SJakub Kicinski 	}
23258790314SJakub Kicinski 
23358790314SJakub Kicinski 	return (i == -1);
23458790314SJakub Kicinski }
23558790314SJakub Kicinski 
23658790314SJakub Kicinski static inline void tls_bigint_subtract(unsigned char *seq, int  n)
23758790314SJakub Kicinski {
23858790314SJakub Kicinski 	u64 rcd_sn;
23958790314SJakub Kicinski 	__be64 *p;
24058790314SJakub Kicinski 
24158790314SJakub Kicinski 	BUILD_BUG_ON(TLS_MAX_REC_SEQ_SIZE != 8);
24258790314SJakub Kicinski 
24358790314SJakub Kicinski 	p = (__be64 *)seq;
24458790314SJakub Kicinski 	rcd_sn = be64_to_cpu(*p);
24558790314SJakub Kicinski 	*p = cpu_to_be64(rcd_sn - n);
24658790314SJakub Kicinski }
24758790314SJakub Kicinski 
24858790314SJakub Kicinski static inline void
24958790314SJakub Kicinski tls_advance_record_sn(struct sock *sk, struct tls_prot_info *prot,
25058790314SJakub Kicinski 		      struct cipher_context *ctx)
25158790314SJakub Kicinski {
25258790314SJakub Kicinski 	if (tls_bigint_increment(ctx->rec_seq, prot->rec_seq_size))
25358790314SJakub Kicinski 		tls_err_abort(sk, -EBADMSG);
25458790314SJakub Kicinski 
25558790314SJakub Kicinski 	if (prot->version != TLS_1_3_VERSION &&
25658790314SJakub Kicinski 	    prot->cipher_type != TLS_CIPHER_CHACHA20_POLY1305)
25758790314SJakub Kicinski 		tls_bigint_increment(ctx->iv + prot->salt_size,
25858790314SJakub Kicinski 				     prot->iv_size);
25958790314SJakub Kicinski }
26058790314SJakub Kicinski 
26158790314SJakub Kicinski static inline void
26258790314SJakub Kicinski tls_xor_iv_with_seq(struct tls_prot_info *prot, char *iv, char *seq)
26358790314SJakub Kicinski {
26458790314SJakub Kicinski 	int i;
26558790314SJakub Kicinski 
26658790314SJakub Kicinski 	if (prot->version == TLS_1_3_VERSION ||
26758790314SJakub Kicinski 	    prot->cipher_type == TLS_CIPHER_CHACHA20_POLY1305) {
26858790314SJakub Kicinski 		for (i = 0; i < 8; i++)
26958790314SJakub Kicinski 			iv[i + 4] ^= seq[i];
27058790314SJakub Kicinski 	}
27158790314SJakub Kicinski }
27258790314SJakub Kicinski 
27358790314SJakub Kicinski static inline void
27458790314SJakub Kicinski tls_fill_prepend(struct tls_context *ctx, char *buf, size_t plaintext_len,
27558790314SJakub Kicinski 		 unsigned char record_type)
27658790314SJakub Kicinski {
27758790314SJakub Kicinski 	struct tls_prot_info *prot = &ctx->prot_info;
27858790314SJakub Kicinski 	size_t pkt_len, iv_size = prot->iv_size;
27958790314SJakub Kicinski 
28058790314SJakub Kicinski 	pkt_len = plaintext_len + prot->tag_size;
28158790314SJakub Kicinski 	if (prot->version != TLS_1_3_VERSION &&
28258790314SJakub Kicinski 	    prot->cipher_type != TLS_CIPHER_CHACHA20_POLY1305) {
28358790314SJakub Kicinski 		pkt_len += iv_size;
28458790314SJakub Kicinski 
28558790314SJakub Kicinski 		memcpy(buf + TLS_NONCE_OFFSET,
28658790314SJakub Kicinski 		       ctx->tx.iv + prot->salt_size, iv_size);
28758790314SJakub Kicinski 	}
28858790314SJakub Kicinski 
28958790314SJakub Kicinski 	/* we cover nonce explicit here as well, so buf should be of
29058790314SJakub Kicinski 	 * size KTLS_DTLS_HEADER_SIZE + KTLS_DTLS_NONCE_EXPLICIT_SIZE
29158790314SJakub Kicinski 	 */
29258790314SJakub Kicinski 	buf[0] = prot->version == TLS_1_3_VERSION ?
29358790314SJakub Kicinski 		   TLS_RECORD_TYPE_DATA : record_type;
29458790314SJakub Kicinski 	/* Note that VERSION must be TLS_1_2 for both TLS1.2 and TLS1.3 */
29558790314SJakub Kicinski 	buf[1] = TLS_1_2_VERSION_MINOR;
29658790314SJakub Kicinski 	buf[2] = TLS_1_2_VERSION_MAJOR;
29758790314SJakub Kicinski 	/* we can use IV for nonce explicit according to spec */
29858790314SJakub Kicinski 	buf[3] = pkt_len >> 8;
29958790314SJakub Kicinski 	buf[4] = pkt_len & 0xFF;
30058790314SJakub Kicinski }
30158790314SJakub Kicinski 
30258790314SJakub Kicinski static inline
30358790314SJakub Kicinski void tls_make_aad(char *buf, size_t size, char *record_sequence,
30458790314SJakub Kicinski 		  unsigned char record_type, struct tls_prot_info *prot)
30558790314SJakub Kicinski {
30658790314SJakub Kicinski 	if (prot->version != TLS_1_3_VERSION) {
30758790314SJakub Kicinski 		memcpy(buf, record_sequence, prot->rec_seq_size);
30858790314SJakub Kicinski 		buf += 8;
30958790314SJakub Kicinski 	} else {
31058790314SJakub Kicinski 		size += prot->tag_size;
31158790314SJakub Kicinski 	}
31258790314SJakub Kicinski 
31358790314SJakub Kicinski 	buf[0] = prot->version == TLS_1_3_VERSION ?
31458790314SJakub Kicinski 		  TLS_RECORD_TYPE_DATA : record_type;
31558790314SJakub Kicinski 	buf[1] = TLS_1_2_VERSION_MAJOR;
31658790314SJakub Kicinski 	buf[2] = TLS_1_2_VERSION_MINOR;
31758790314SJakub Kicinski 	buf[3] = size >> 8;
31858790314SJakub Kicinski 	buf[4] = size & 0xFF;
31958790314SJakub Kicinski }
32058790314SJakub Kicinski 
32158790314SJakub Kicinski #endif
322