2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2014-2019 Netflix Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
15 * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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
32 #include <sys/refcount.h>
33 #include <sys/_task.h>
35 struct tls_record_layer {
41 } __attribute__ ((packed));
43 #define TLS_MAX_MSG_SIZE_V10_2 16384
44 #define TLS_MAX_PARAM_SIZE 1024 /* Max key/mac/iv in sockopt */
45 #define TLS_AEAD_GCM_LEN 4
46 #define TLS_1_3_GCM_IV_LEN 12
47 #define TLS_CBC_IMPLICIT_IV_LEN 16
49 /* Type values for the record layer */
50 #define TLS_RLTYPE_APP 23
53 * Nonce for GCM for TLS 1.2 per RFC 5288.
55 struct tls_nonce_data {
56 uint8_t fixed[TLS_AEAD_GCM_LEN];
61 * AEAD additional data format for TLS 1.2 per RFC 5246.
63 struct tls_aead_data {
64 uint64_t seq; /* In network order */
72 * AEAD additional data format for TLS 1.3 per RFC 8446.
74 struct tls_aead_data_13 {
82 * Stream Cipher MAC additional data input. This does not match the
83 * exact data on the wire (the sequence number is not placed on the
84 * wire, and any explicit IV after the record header is not covered by
95 #define TLS_MAJOR_VER_ONE 3
96 #define TLS_MINOR_VER_ZERO 1 /* 3, 1 */
97 #define TLS_MINOR_VER_ONE 2 /* 3, 2 */
98 #define TLS_MINOR_VER_TWO 3 /* 3, 3 */
99 #define TLS_MINOR_VER_THREE 4 /* 3, 4 */
101 /* For TCP_TXTLS_ENABLE */
103 const uint8_t *cipher_key;
104 const uint8_t *iv; /* Implicit IV. */
105 const uint8_t *auth_key;
106 int cipher_algorithm; /* e.g. CRYPTO_AES_CBC */
109 int auth_algorithm; /* e.g. CRYPTO_SHA2_256_HMAC */
116 struct tls_session_params {
119 uint8_t iv[TLS_CBC_IMPLICIT_IV_LEN];
120 int cipher_algorithm;
122 uint16_t cipher_key_len;
124 uint16_t auth_key_len;
125 uint16_t max_frame_len;
136 #define KTLS_API_VERSION 6
146 struct ktls_crypto_backend {
147 LIST_ENTRY(ktls_crypto_backend) next;
148 int (*try)(struct socket *so, struct ktls_session *tls);
155 struct ktls_session {
156 int (*sw_encrypt)(struct ktls_session *tls,
157 const struct tls_record_layer *hdr, uint8_t *trailer,
158 struct iovec *src, struct iovec *dst, int iovcnt,
159 uint64_t seqno, uint8_t record_type);
162 struct m_snd_tag *snd_tag;
164 struct ktls_crypto_backend *be;
165 void (*free)(struct ktls_session *tls);
166 struct tls_session_params params;
168 volatile u_int refcount;
171 struct task reset_tag_task;
174 } __aligned(CACHE_LINE_SIZE);
176 int ktls_crypto_backend_register(struct ktls_crypto_backend *be);
177 int ktls_crypto_backend_deregister(struct ktls_crypto_backend *be);
178 int ktls_enable_tx(struct socket *so, struct tls_enable *en);
179 void ktls_destroy(struct ktls_session *tls);
180 void ktls_frame(struct mbuf *m, struct ktls_session *tls, int *enqueue_cnt,
181 uint8_t record_type);
182 void ktls_seq(struct sockbuf *sb, struct mbuf *m);
183 void ktls_enqueue(struct mbuf *m, struct socket *so, int page_count);
184 void ktls_enqueue_to_free(struct mbuf_ext_pgs *pgs);
185 int ktls_set_tx_mode(struct socket *so, int mode);
186 int ktls_get_tx_mode(struct socket *so);
187 int ktls_output_eagain(struct inpcb *inp, struct ktls_session *tls);
189 static inline struct ktls_session *
190 ktls_hold(struct ktls_session *tls)
194 refcount_acquire(&tls->refcount);
199 ktls_free(struct ktls_session *tls)
202 if (refcount_release(&tls->refcount))
206 #endif /* !_KERNEL */
207 #endif /* !_SYS_KTLS_H_ */