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;
56 static MALLOC_DEFINE(M_KTLS_OCF, "ktls_ocf", "OCF KTLS");
58 SYSCTL_DECL(_kern_ipc_tls);
59 SYSCTL_DECL(_kern_ipc_tls_stats);
61 static SYSCTL_NODE(_kern_ipc_tls_stats, OID_AUTO, ocf,
62 CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
63 "Kernel TLS offload via OCF stats");
65 static counter_u64_t ocf_tls12_gcm_crypts;
66 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, tls12_gcm_crypts,
67 CTLFLAG_RD, &ocf_tls12_gcm_crypts,
68 "Total number of OCF TLS 1.2 GCM encryption operations");
70 static counter_u64_t ocf_tls13_gcm_crypts;
71 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, tls13_gcm_crypts,
72 CTLFLAG_RD, &ocf_tls13_gcm_crypts,
73 "Total number of OCF TLS 1.3 GCM encryption operations");
75 static counter_u64_t ocf_inplace;
76 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, inplace,
77 CTLFLAG_RD, &ocf_inplace,
78 "Total number of OCF in-place operations");
80 static counter_u64_t ocf_separate_output;
81 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, separate_output,
82 CTLFLAG_RD, &ocf_separate_output,
83 "Total number of OCF operations with a separate output buffer");
85 static counter_u64_t ocf_retries;
86 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, retries, CTLFLAG_RD,
88 "Number of OCF encryption operation retries");
91 ktls_ocf_callback(struct cryptop *crp)
93 struct ocf_operation *oo;
96 mtx_lock(&oo->os->lock);
98 mtx_unlock(&oo->os->lock);
104 ktls_ocf_tls12_gcm_encrypt(struct ktls_session *tls,
105 const struct tls_record_layer *hdr, uint8_t *trailer, struct iovec *iniov,
106 struct iovec *outiov, int iovcnt, uint64_t seqno,
107 uint8_t record_type __unused)
109 struct uio uio, out_uio, *tag_uio;
110 struct tls_aead_data ad;
112 struct ocf_session *os;
113 struct ocf_operation oo;
114 struct iovec iov[iovcnt + 1];
116 uint16_t tls_comp_len;
125 uio.uio_iovcnt = iovcnt;
127 uio.uio_segflg = UIO_SYSSPACE;
128 uio.uio_td = curthread;
130 out_uio.uio_iov = outiov;
131 out_uio.uio_iovcnt = iovcnt;
132 out_uio.uio_offset = 0;
133 out_uio.uio_segflg = UIO_SYSSPACE;
134 out_uio.uio_td = curthread;
136 crypto_initreq(&crp, os->sid);
139 memcpy(crp.crp_iv, tls->params.iv, TLS_AEAD_GCM_LEN);
140 memcpy(crp.crp_iv + TLS_AEAD_GCM_LEN, hdr + 1, sizeof(uint64_t));
143 tls_comp_len = ntohs(hdr->tls_length) -
144 (AES_GMAC_HASH_LEN + sizeof(uint64_t));
145 ad.seq = htobe64(seqno);
146 ad.type = hdr->tls_type;
147 ad.tls_vmajor = hdr->tls_vmajor;
148 ad.tls_vminor = hdr->tls_vminor;
149 ad.tls_length = htons(tls_comp_len);
151 crp.crp_aad_length = sizeof(ad);
153 /* Compute payload length and determine if encryption is in place. */
155 crp.crp_payload_start = 0;
156 for (i = 0; i < iovcnt; i++) {
157 if (iniov[i].iov_base != outiov[i].iov_base)
159 crp.crp_payload_length += iniov[i].iov_len;
161 uio.uio_resid = crp.crp_payload_length;
162 out_uio.uio_resid = crp.crp_payload_length;
169 /* Duplicate iovec and append vector for tag. */
170 memcpy(iov, tag_uio->uio_iov, iovcnt * sizeof(struct iovec));
171 iov[iovcnt].iov_base = trailer;
172 iov[iovcnt].iov_len = AES_GMAC_HASH_LEN;
173 tag_uio->uio_iov = iov;
174 tag_uio->uio_iovcnt++;
175 crp.crp_digest_start = tag_uio->uio_resid;
176 tag_uio->uio_resid += AES_GMAC_HASH_LEN;
178 crp.crp_op = CRYPTO_OP_ENCRYPT | CRYPTO_OP_COMPUTE_DIGEST;
179 crp.crp_flags = CRYPTO_F_CBIMM | CRYPTO_F_IV_SEPARATE;
180 crypto_use_uio(&crp, &uio);
182 crypto_use_output_uio(&crp, &out_uio);
183 crp.crp_opaque = &oo;
184 crp.crp_callback = ktls_ocf_callback;
186 counter_u64_add(ocf_tls12_gcm_crypts, 1);
188 counter_u64_add(ocf_inplace, 1);
190 counter_u64_add(ocf_separate_output, 1);
192 error = crypto_dispatch(&crp);
198 mtx_sleep(&oo, &os->lock, 0, "ocfktls", 0);
199 mtx_unlock(&os->lock);
201 if (crp.crp_etype != EAGAIN) {
202 error = crp.crp_etype;
207 crp.crp_flags &= ~CRYPTO_F_DONE;
209 counter_u64_add(ocf_retries, 1);
212 crypto_destroyreq(&crp);
217 ktls_ocf_tls13_gcm_encrypt(struct ktls_session *tls,
218 const struct tls_record_layer *hdr, uint8_t *trailer, struct iovec *iniov,
219 struct iovec *outiov, int iovcnt, uint64_t seqno, uint8_t record_type)
221 struct uio uio, out_uio;
222 struct tls_aead_data_13 ad;
225 struct ocf_session *os;
226 struct ocf_operation oo;
227 struct iovec iov[iovcnt + 1], out_iov[iovcnt + 1];
236 crypto_initreq(&crp, os->sid);
238 /* Setup the nonce. */
239 memcpy(nonce, tls->params.iv, tls->params.iv_len);
240 *(uint64_t *)(nonce + 4) ^= htobe64(seqno);
243 ad.type = hdr->tls_type;
244 ad.tls_vmajor = hdr->tls_vmajor;
245 ad.tls_vminor = hdr->tls_vminor;
246 ad.tls_length = hdr->tls_length;
248 crp.crp_aad_length = sizeof(ad);
250 /* Compute payload length and determine if encryption is in place. */
252 crp.crp_payload_start = 0;
253 for (i = 0; i < iovcnt; i++) {
254 if (iniov[i].iov_base != outiov[i].iov_base)
256 crp.crp_payload_length += iniov[i].iov_len;
259 /* Store the record type as the first byte of the trailer. */
260 trailer[0] = record_type;
261 crp.crp_payload_length++;
262 crp.crp_digest_start = crp.crp_payload_length;
265 * Duplicate the input iov to append the trailer. Always
266 * include the full trailer as input to get the record_type
267 * even if only the first byte is used.
269 memcpy(iov, iniov, iovcnt * sizeof(*iov));
270 iov[iovcnt].iov_base = trailer;
271 iov[iovcnt].iov_len = AES_GMAC_HASH_LEN + 1;
273 uio.uio_iovcnt = iovcnt + 1;
275 uio.uio_resid = crp.crp_payload_length + AES_GMAC_HASH_LEN;
276 uio.uio_segflg = UIO_SYSSPACE;
277 uio.uio_td = curthread;
278 crypto_use_uio(&crp, &uio);
281 /* Duplicate the output iov to append the trailer. */
282 memcpy(out_iov, outiov, iovcnt * sizeof(*out_iov));
283 out_iov[iovcnt] = iov[iovcnt];
285 out_uio.uio_iov = out_iov;
286 out_uio.uio_iovcnt = iovcnt + 1;
287 out_uio.uio_offset = 0;
288 out_uio.uio_resid = crp.crp_payload_length +
290 out_uio.uio_segflg = UIO_SYSSPACE;
291 out_uio.uio_td = curthread;
292 crypto_use_output_uio(&crp, &out_uio);
295 crp.crp_op = CRYPTO_OP_ENCRYPT | CRYPTO_OP_COMPUTE_DIGEST;
296 crp.crp_flags = CRYPTO_F_CBIMM | CRYPTO_F_IV_SEPARATE;
297 crp.crp_opaque = &oo;
298 crp.crp_callback = ktls_ocf_callback;
300 memcpy(crp.crp_iv, nonce, sizeof(nonce));
302 counter_u64_add(ocf_tls13_gcm_crypts, 1);
304 counter_u64_add(ocf_inplace, 1);
306 counter_u64_add(ocf_separate_output, 1);
308 error = crypto_dispatch(&crp);
314 mtx_sleep(&oo, &os->lock, 0, "ocfktls", 0);
315 mtx_unlock(&os->lock);
317 if (crp.crp_etype != EAGAIN) {
318 error = crp.crp_etype;
323 crp.crp_flags &= ~CRYPTO_F_DONE;
325 counter_u64_add(ocf_retries, 1);
328 crypto_destroyreq(&crp);
333 ktls_ocf_free(struct ktls_session *tls)
335 struct ocf_session *os;
338 crypto_freesession(os->sid);
339 mtx_destroy(&os->lock);
340 zfree(os, M_KTLS_OCF);
344 ktls_ocf_try(struct socket *so, struct ktls_session *tls)
346 struct crypto_session_params csp;
347 struct ocf_session *os;
350 memset(&csp, 0, sizeof(csp));
351 csp.csp_flags |= CSP_F_SEPARATE_OUTPUT | CSP_F_SEPARATE_AAD;
353 switch (tls->params.cipher_algorithm) {
354 case CRYPTO_AES_NIST_GCM_16:
355 switch (tls->params.cipher_key_len) {
362 csp.csp_mode = CSP_MODE_AEAD;
363 csp.csp_cipher_alg = CRYPTO_AES_NIST_GCM_16;
364 csp.csp_cipher_key = tls->params.cipher_key;
365 csp.csp_cipher_klen = tls->params.cipher_key_len;
366 csp.csp_ivlen = AES_GCM_IV_LEN;
369 return (EPROTONOSUPPORT);
372 /* Only TLS 1.2 and 1.3 are supported. */
373 if (tls->params.tls_vmajor != TLS_MAJOR_VER_ONE ||
374 tls->params.tls_vminor < TLS_MINOR_VER_TWO ||
375 tls->params.tls_vminor > TLS_MINOR_VER_THREE)
376 return (EPROTONOSUPPORT);
378 os = malloc(sizeof(*os), M_KTLS_OCF, M_NOWAIT | M_ZERO);
382 error = crypto_newsession(&os->sid, &csp,
383 CRYPTO_FLAG_HARDWARE | CRYPTO_FLAG_SOFTWARE);
385 free(os, M_KTLS_OCF);
389 mtx_init(&os->lock, "ktls_ocf", NULL, MTX_DEF);
391 if (tls->params.tls_vminor == TLS_MINOR_VER_THREE)
392 tls->sw_encrypt = ktls_ocf_tls13_gcm_encrypt;
394 tls->sw_encrypt = ktls_ocf_tls12_gcm_encrypt;
395 tls->free = ktls_ocf_free;
399 struct ktls_crypto_backend ocf_backend = {
402 .api_version = KTLS_API_VERSION,
407 ktls_ocf_modevent(module_t mod, int what, void *arg)
413 ocf_tls12_gcm_crypts = counter_u64_alloc(M_WAITOK);
414 ocf_tls13_gcm_crypts = counter_u64_alloc(M_WAITOK);
415 ocf_inplace = counter_u64_alloc(M_WAITOK);
416 ocf_separate_output = counter_u64_alloc(M_WAITOK);
417 ocf_retries = counter_u64_alloc(M_WAITOK);
418 return (ktls_crypto_backend_register(&ocf_backend));
420 error = ktls_crypto_backend_deregister(&ocf_backend);
423 counter_u64_free(ocf_tls12_gcm_crypts);
424 counter_u64_free(ocf_tls13_gcm_crypts);
425 counter_u64_free(ocf_inplace);
426 counter_u64_free(ocf_separate_output);
427 counter_u64_free(ocf_retries);
434 static moduledata_t ktls_ocf_moduledata = {
440 DECLARE_MODULE(ktls_ocf, ktls_ocf_moduledata, SI_SUB_PROTO_END, SI_ORDER_ANY);