2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2019 Netflix Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/counter.h>
35 #include <sys/endian.h>
36 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/sysctl.h>
44 #include <opencrypto/cryptodev.h>
51 struct ocf_operation {
52 struct ocf_session *os;
57 static MALLOC_DEFINE(M_KTLS_OCF, "ktls_ocf", "OCF KTLS");
59 SYSCTL_DECL(_kern_ipc_tls);
60 SYSCTL_DECL(_kern_ipc_tls_stats);
62 static SYSCTL_NODE(_kern_ipc_tls_stats, OID_AUTO, ocf,
63 CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
64 "Kernel TLS offload via OCF stats");
66 static counter_u64_t ocf_tls12_gcm_crypts;
67 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, tls12_gcm_crypts,
68 CTLFLAG_RD, &ocf_tls12_gcm_crypts,
69 "Total number of OCF TLS 1.2 GCM encryption operations");
71 static counter_u64_t ocf_tls13_gcm_crypts;
72 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, tls13_gcm_crypts,
73 CTLFLAG_RD, &ocf_tls13_gcm_crypts,
74 "Total number of OCF TLS 1.3 GCM encryption operations");
76 static counter_u64_t ocf_inplace;
77 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, inplace,
78 CTLFLAG_RD, &ocf_inplace,
79 "Total number of OCF in-place operations");
81 static counter_u64_t ocf_separate_output;
82 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, separate_output,
83 CTLFLAG_RD, &ocf_separate_output,
84 "Total number of OCF operations with a separate output buffer");
86 static counter_u64_t ocf_retries;
87 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, retries, CTLFLAG_RD,
89 "Number of OCF encryption operation retries");
92 ktls_ocf_callback(struct cryptop *crp)
94 struct ocf_operation *oo;
97 mtx_lock(&oo->os->lock);
99 mtx_unlock(&oo->os->lock);
105 ktls_ocf_tls12_gcm_encrypt(struct ktls_session *tls,
106 const struct tls_record_layer *hdr, uint8_t *trailer, struct iovec *iniov,
107 struct iovec *outiov, int iovcnt, uint64_t seqno,
108 uint8_t record_type __unused)
110 struct uio uio, out_uio, *tag_uio;
111 struct tls_aead_data ad;
113 struct ocf_session *os;
114 struct ocf_operation *oo;
116 uint16_t tls_comp_len;
121 oo = malloc(sizeof(*oo) + (iovcnt + 1) * sizeof(struct iovec),
122 M_KTLS_OCF, M_WAITOK | M_ZERO);
126 uio.uio_iovcnt = iovcnt;
128 uio.uio_segflg = UIO_SYSSPACE;
129 uio.uio_td = curthread;
131 out_uio.uio_iov = outiov;
132 out_uio.uio_iovcnt = iovcnt;
133 out_uio.uio_offset = 0;
134 out_uio.uio_segflg = UIO_SYSSPACE;
135 out_uio.uio_td = curthread;
137 crp = crypto_getreq(os->sid, M_WAITOK);
140 memcpy(crp->crp_iv, tls->params.iv, TLS_AEAD_GCM_LEN);
141 memcpy(crp->crp_iv + TLS_AEAD_GCM_LEN, hdr + 1, sizeof(uint64_t));
144 tls_comp_len = ntohs(hdr->tls_length) -
145 (AES_GMAC_HASH_LEN + sizeof(uint64_t));
146 ad.seq = htobe64(seqno);
147 ad.type = hdr->tls_type;
148 ad.tls_vmajor = hdr->tls_vmajor;
149 ad.tls_vminor = hdr->tls_vminor;
150 ad.tls_length = htons(tls_comp_len);
152 crp->crp_aad_length = sizeof(ad);
154 /* Compute payload length and determine if encryption is in place. */
156 crp->crp_payload_start = 0;
157 for (i = 0; i < iovcnt; i++) {
158 if (iniov[i].iov_base != outiov[i].iov_base)
160 crp->crp_payload_length += iniov[i].iov_len;
162 uio.uio_resid = crp->crp_payload_length;
163 out_uio.uio_resid = crp->crp_payload_length;
170 /* Duplicate iovec and append vector for tag. */
171 memcpy(oo->iov, tag_uio->uio_iov, iovcnt * sizeof(struct iovec));
172 tag_uio->uio_iov = oo->iov;
173 tag_uio->uio_iov[iovcnt].iov_base = trailer;
174 tag_uio->uio_iov[iovcnt].iov_len = AES_GMAC_HASH_LEN;
175 tag_uio->uio_iovcnt++;
176 crp->crp_digest_start = tag_uio->uio_resid;
177 tag_uio->uio_resid += AES_GMAC_HASH_LEN;
179 crp->crp_op = CRYPTO_OP_ENCRYPT | CRYPTO_OP_COMPUTE_DIGEST;
180 crp->crp_flags = CRYPTO_F_CBIMM | CRYPTO_F_IV_SEPARATE;
181 crypto_use_uio(crp, &uio);
183 crypto_use_output_uio(crp, &out_uio);
184 crp->crp_opaque = oo;
185 crp->crp_callback = ktls_ocf_callback;
187 counter_u64_add(ocf_tls12_gcm_crypts, 1);
189 counter_u64_add(ocf_inplace, 1);
191 counter_u64_add(ocf_separate_output, 1);
193 error = crypto_dispatch(crp);
199 mtx_sleep(oo, &os->lock, 0, "ocfktls", 0);
200 mtx_unlock(&os->lock);
202 if (crp->crp_etype != EAGAIN) {
203 error = crp->crp_etype;
208 crp->crp_flags &= ~CRYPTO_F_DONE;
210 counter_u64_add(ocf_retries, 1);
214 free(oo, M_KTLS_OCF);
219 ktls_ocf_tls13_gcm_encrypt(struct ktls_session *tls,
220 const struct tls_record_layer *hdr, uint8_t *trailer, struct iovec *iniov,
221 struct iovec *outiov, int iovcnt, uint64_t seqno, uint8_t record_type)
223 struct uio uio, out_uio;
224 struct tls_aead_data_13 ad;
227 struct ocf_session *os;
228 struct ocf_operation *oo;
229 struct iovec *iov, *out_iov;
235 oo = malloc(sizeof(*oo) + (iovcnt + 1) * sizeof(*iov) * 2, M_KTLS_OCF,
239 out_iov = iov + iovcnt + 2;
241 crp = crypto_getreq(os->sid, M_WAITOK);
243 /* Setup the nonce. */
244 memcpy(nonce, tls->params.iv, tls->params.iv_len);
245 *(uint64_t *)(nonce + 4) ^= htobe64(seqno);
248 ad.type = hdr->tls_type;
249 ad.tls_vmajor = hdr->tls_vmajor;
250 ad.tls_vminor = hdr->tls_vminor;
251 ad.tls_length = hdr->tls_length;
253 crp->crp_aad_length = sizeof(ad);
255 /* Compute payload length and determine if encryption is in place. */
257 crp->crp_payload_start = 0;
258 for (i = 0; i < iovcnt; i++) {
259 if (iniov[i].iov_base != outiov[i].iov_base)
261 crp->crp_payload_length += iniov[i].iov_len;
264 /* Store the record type as the first byte of the trailer. */
265 trailer[0] = record_type;
266 crp->crp_payload_length++;
267 crp->crp_digest_start = crp->crp_payload_length;
270 * Duplicate the input iov to append the trailer. Always
271 * include the full trailer as input to get the record_type
272 * even if only the first byte is used.
274 memcpy(iov, iniov, iovcnt * sizeof(*iov));
275 iov[iovcnt].iov_base = trailer;
276 iov[iovcnt].iov_len = AES_GMAC_HASH_LEN + 1;
278 uio.uio_iovcnt = iovcnt + 1;
280 uio.uio_resid = crp->crp_payload_length + AES_GMAC_HASH_LEN;
281 uio.uio_segflg = UIO_SYSSPACE;
282 uio.uio_td = curthread;
283 crypto_use_uio(crp, &uio);
286 /* Duplicate the output iov to append the trailer. */
287 memcpy(out_iov, outiov, iovcnt * sizeof(*out_iov));
288 out_iov[iovcnt] = iov[iovcnt];
290 out_uio.uio_iov = out_iov;
291 out_uio.uio_iovcnt = iovcnt + 1;
292 out_uio.uio_offset = 0;
293 out_uio.uio_resid = crp->crp_payload_length +
295 out_uio.uio_segflg = UIO_SYSSPACE;
296 out_uio.uio_td = curthread;
297 crypto_use_output_uio(crp, &out_uio);
300 crp->crp_op = CRYPTO_OP_ENCRYPT | CRYPTO_OP_COMPUTE_DIGEST;
301 crp->crp_flags = CRYPTO_F_CBIMM | CRYPTO_F_IV_SEPARATE;
302 crp->crp_opaque = oo;
303 crp->crp_callback = ktls_ocf_callback;
305 memcpy(crp->crp_iv, nonce, sizeof(nonce));
307 counter_u64_add(ocf_tls13_gcm_crypts, 1);
309 counter_u64_add(ocf_inplace, 1);
311 counter_u64_add(ocf_separate_output, 1);
313 error = crypto_dispatch(crp);
319 mtx_sleep(oo, &os->lock, 0, "ocfktls", 0);
320 mtx_unlock(&os->lock);
322 if (crp->crp_etype != EAGAIN) {
323 error = crp->crp_etype;
328 crp->crp_flags &= ~CRYPTO_F_DONE;
330 counter_u64_add(ocf_retries, 1);
334 free(oo, M_KTLS_OCF);
339 ktls_ocf_free(struct ktls_session *tls)
341 struct ocf_session *os;
344 crypto_freesession(os->sid);
345 mtx_destroy(&os->lock);
346 zfree(os, M_KTLS_OCF);
350 ktls_ocf_try(struct socket *so, struct ktls_session *tls)
352 struct crypto_session_params csp;
353 struct ocf_session *os;
356 memset(&csp, 0, sizeof(csp));
357 csp.csp_flags |= CSP_F_SEPARATE_OUTPUT | CSP_F_SEPARATE_AAD;
359 switch (tls->params.cipher_algorithm) {
360 case CRYPTO_AES_NIST_GCM_16:
361 switch (tls->params.cipher_key_len) {
368 csp.csp_mode = CSP_MODE_AEAD;
369 csp.csp_cipher_alg = CRYPTO_AES_NIST_GCM_16;
370 csp.csp_cipher_key = tls->params.cipher_key;
371 csp.csp_cipher_klen = tls->params.cipher_key_len;
372 csp.csp_ivlen = AES_GCM_IV_LEN;
375 return (EPROTONOSUPPORT);
378 /* Only TLS 1.2 and 1.3 are supported. */
379 if (tls->params.tls_vmajor != TLS_MAJOR_VER_ONE ||
380 tls->params.tls_vminor < TLS_MINOR_VER_TWO ||
381 tls->params.tls_vminor > TLS_MINOR_VER_THREE)
382 return (EPROTONOSUPPORT);
384 os = malloc(sizeof(*os), M_KTLS_OCF, M_NOWAIT | M_ZERO);
388 error = crypto_newsession(&os->sid, &csp,
389 CRYPTO_FLAG_HARDWARE | CRYPTO_FLAG_SOFTWARE);
391 free(os, M_KTLS_OCF);
395 mtx_init(&os->lock, "ktls_ocf", NULL, MTX_DEF);
397 if (tls->params.tls_vminor == TLS_MINOR_VER_THREE)
398 tls->sw_encrypt = ktls_ocf_tls13_gcm_encrypt;
400 tls->sw_encrypt = ktls_ocf_tls12_gcm_encrypt;
401 tls->free = ktls_ocf_free;
405 struct ktls_crypto_backend ocf_backend = {
408 .api_version = KTLS_API_VERSION,
413 ktls_ocf_modevent(module_t mod, int what, void *arg)
419 ocf_tls12_gcm_crypts = counter_u64_alloc(M_WAITOK);
420 ocf_tls13_gcm_crypts = counter_u64_alloc(M_WAITOK);
421 ocf_inplace = counter_u64_alloc(M_WAITOK);
422 ocf_separate_output = counter_u64_alloc(M_WAITOK);
423 ocf_retries = counter_u64_alloc(M_WAITOK);
424 return (ktls_crypto_backend_register(&ocf_backend));
426 error = ktls_crypto_backend_deregister(&ocf_backend);
429 counter_u64_free(ocf_tls12_gcm_crypts);
430 counter_u64_free(ocf_tls13_gcm_crypts);
431 counter_u64_free(ocf_inplace);
432 counter_u64_free(ocf_separate_output);
433 counter_u64_free(ocf_retries);
440 static moduledata_t ktls_ocf_moduledata = {
446 DECLARE_MODULE(ktls_ocf, ktls_ocf_moduledata, SI_SUB_PROTO_END, SI_ORDER_ANY);