]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/opencrypto/ktls_ocf.c
Don't dynamically allocate data structures for KTLS crypto requests.
[FreeBSD/FreeBSD.git] / sys / opencrypto / ktls_ocf.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2019 Netflix Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
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>
37 #include <sys/ktls.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/sysctl.h>
43 #include <sys/uio.h>
44 #include <opencrypto/cryptodev.h>
45
46 struct ocf_session {
47         crypto_session_t sid;
48         struct mtx lock;
49 };
50
51 struct ocf_operation {
52         struct ocf_session *os;
53         bool done;
54 };
55
56 static MALLOC_DEFINE(M_KTLS_OCF, "ktls_ocf", "OCF KTLS");
57
58 SYSCTL_DECL(_kern_ipc_tls);
59 SYSCTL_DECL(_kern_ipc_tls_stats);
60
61 static SYSCTL_NODE(_kern_ipc_tls_stats, OID_AUTO, ocf,
62     CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
63     "Kernel TLS offload via OCF stats");
64
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");
69
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");
74
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");
79
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");
84
85 static counter_u64_t ocf_retries;
86 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, retries, CTLFLAG_RD,
87     &ocf_retries,
88     "Number of OCF encryption operation retries");
89
90 static int
91 ktls_ocf_callback(struct cryptop *crp)
92 {
93         struct ocf_operation *oo;
94
95         oo = crp->crp_opaque;
96         mtx_lock(&oo->os->lock);
97         oo->done = true;
98         mtx_unlock(&oo->os->lock);
99         wakeup(oo);
100         return (0);
101 }
102
103 static int
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)
108 {
109         struct uio uio, out_uio, *tag_uio;
110         struct tls_aead_data ad;
111         struct cryptop crp;
112         struct ocf_session *os;
113         struct ocf_operation oo;
114         struct iovec iov[iovcnt + 1];
115         int i, error;
116         uint16_t tls_comp_len;
117         bool inplace;
118
119         os = tls->cipher;
120
121         oo.os = os;
122         oo.done = false;
123
124         uio.uio_iov = iniov;
125         uio.uio_iovcnt = iovcnt;
126         uio.uio_offset = 0;
127         uio.uio_segflg = UIO_SYSSPACE;
128         uio.uio_td = curthread;
129
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;
135
136         crypto_initreq(&crp, os->sid);
137
138         /* Setup the IV. */
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));
141
142         /* Setup the AAD. */
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);
150         crp.crp_aad = &ad;
151         crp.crp_aad_length = sizeof(ad);
152
153         /* Compute payload length and determine if encryption is in place. */
154         inplace = true;
155         crp.crp_payload_start = 0;
156         for (i = 0; i < iovcnt; i++) {
157                 if (iniov[i].iov_base != outiov[i].iov_base)
158                         inplace = false;
159                 crp.crp_payload_length += iniov[i].iov_len;
160         }
161         uio.uio_resid = crp.crp_payload_length;
162         out_uio.uio_resid = crp.crp_payload_length;
163
164         if (inplace)
165                 tag_uio = &uio;
166         else
167                 tag_uio = &out_uio;
168
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;
177
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);
181         if (!inplace)
182                 crypto_use_output_uio(&crp, &out_uio);
183         crp.crp_opaque = &oo;
184         crp.crp_callback = ktls_ocf_callback;
185
186         counter_u64_add(ocf_tls12_gcm_crypts, 1);
187         if (inplace)
188                 counter_u64_add(ocf_inplace, 1);
189         else
190                 counter_u64_add(ocf_separate_output, 1);
191         for (;;) {
192                 error = crypto_dispatch(&crp);
193                 if (error)
194                         break;
195
196                 mtx_lock(&os->lock);
197                 while (!oo.done)
198                         mtx_sleep(&oo, &os->lock, 0, "ocfktls", 0);
199                 mtx_unlock(&os->lock);
200
201                 if (crp.crp_etype != EAGAIN) {
202                         error = crp.crp_etype;
203                         break;
204                 }
205
206                 crp.crp_etype = 0;
207                 crp.crp_flags &= ~CRYPTO_F_DONE;
208                 oo.done = false;
209                 counter_u64_add(ocf_retries, 1);
210         }
211
212         crypto_destroyreq(&crp);
213         return (error);
214 }
215
216 static int
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)
220 {
221         struct uio uio, out_uio;
222         struct tls_aead_data_13 ad;
223         char nonce[12];
224         struct cryptop crp;
225         struct ocf_session *os;
226         struct ocf_operation oo;
227         struct iovec iov[iovcnt + 1], out_iov[iovcnt + 1];
228         int i, error;
229         bool inplace;
230
231         os = tls->cipher;
232
233         oo.os = os;
234         oo.done = false;
235
236         crypto_initreq(&crp, os->sid);
237
238         /* Setup the nonce. */
239         memcpy(nonce, tls->params.iv, tls->params.iv_len);
240         *(uint64_t *)(nonce + 4) ^= htobe64(seqno);
241
242         /* Setup the AAD. */
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;
247         crp.crp_aad = &ad;
248         crp.crp_aad_length = sizeof(ad);
249
250         /* Compute payload length and determine if encryption is in place. */
251         inplace = true;
252         crp.crp_payload_start = 0;
253         for (i = 0; i < iovcnt; i++) {
254                 if (iniov[i].iov_base != outiov[i].iov_base)
255                         inplace = false;
256                 crp.crp_payload_length += iniov[i].iov_len;
257         }
258
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;
263
264         /*
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.
268          */
269         memcpy(iov, iniov, iovcnt * sizeof(*iov));
270         iov[iovcnt].iov_base = trailer;
271         iov[iovcnt].iov_len = AES_GMAC_HASH_LEN + 1;
272         uio.uio_iov = iov;
273         uio.uio_iovcnt = iovcnt + 1;
274         uio.uio_offset = 0;
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);
279
280         if (!inplace) {
281                 /* Duplicate the output iov to append the trailer. */
282                 memcpy(out_iov, outiov, iovcnt * sizeof(*out_iov));
283                 out_iov[iovcnt] = iov[iovcnt];
284
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 +
289                     AES_GMAC_HASH_LEN;
290                 out_uio.uio_segflg = UIO_SYSSPACE;
291                 out_uio.uio_td = curthread;
292                 crypto_use_output_uio(&crp, &out_uio);
293         }
294
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;
299
300         memcpy(crp.crp_iv, nonce, sizeof(nonce));
301
302         counter_u64_add(ocf_tls13_gcm_crypts, 1);
303         if (inplace)
304                 counter_u64_add(ocf_inplace, 1);
305         else
306                 counter_u64_add(ocf_separate_output, 1);
307         for (;;) {
308                 error = crypto_dispatch(&crp);
309                 if (error)
310                         break;
311
312                 mtx_lock(&os->lock);
313                 while (!oo.done)
314                         mtx_sleep(&oo, &os->lock, 0, "ocfktls", 0);
315                 mtx_unlock(&os->lock);
316
317                 if (crp.crp_etype != EAGAIN) {
318                         error = crp.crp_etype;
319                         break;
320                 }
321
322                 crp.crp_etype = 0;
323                 crp.crp_flags &= ~CRYPTO_F_DONE;
324                 oo.done = false;
325                 counter_u64_add(ocf_retries, 1);
326         }
327
328         crypto_destroyreq(&crp);
329         return (error);
330 }
331
332 static void
333 ktls_ocf_free(struct ktls_session *tls)
334 {
335         struct ocf_session *os;
336
337         os = tls->cipher;
338         crypto_freesession(os->sid);
339         mtx_destroy(&os->lock);
340         zfree(os, M_KTLS_OCF);
341 }
342
343 static int
344 ktls_ocf_try(struct socket *so, struct ktls_session *tls)
345 {
346         struct crypto_session_params csp;
347         struct ocf_session *os;
348         int error;
349
350         memset(&csp, 0, sizeof(csp));
351         csp.csp_flags |= CSP_F_SEPARATE_OUTPUT | CSP_F_SEPARATE_AAD;
352
353         switch (tls->params.cipher_algorithm) {
354         case CRYPTO_AES_NIST_GCM_16:
355                 switch (tls->params.cipher_key_len) {
356                 case 128 / 8:
357                 case 256 / 8:
358                         break;
359                 default:
360                         return (EINVAL);
361                 }
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;
367                 break;
368         default:
369                 return (EPROTONOSUPPORT);
370         }
371
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);
377
378         os = malloc(sizeof(*os), M_KTLS_OCF, M_NOWAIT | M_ZERO);
379         if (os == NULL)
380                 return (ENOMEM);
381
382         error = crypto_newsession(&os->sid, &csp,
383             CRYPTO_FLAG_HARDWARE | CRYPTO_FLAG_SOFTWARE);
384         if (error) {
385                 free(os, M_KTLS_OCF);
386                 return (error);
387         }
388
389         mtx_init(&os->lock, "ktls_ocf", NULL, MTX_DEF);
390         tls->cipher = os;
391         if (tls->params.tls_vminor == TLS_MINOR_VER_THREE)
392                 tls->sw_encrypt = ktls_ocf_tls13_gcm_encrypt;
393         else
394                 tls->sw_encrypt = ktls_ocf_tls12_gcm_encrypt;
395         tls->free = ktls_ocf_free;
396         return (0);
397 }
398
399 struct ktls_crypto_backend ocf_backend = {
400         .name = "OCF",
401         .prio = 5,
402         .api_version = KTLS_API_VERSION,
403         .try = ktls_ocf_try,
404 };
405
406 static int
407 ktls_ocf_modevent(module_t mod, int what, void *arg)
408 {
409         int error;
410
411         switch (what) {
412         case MOD_LOAD:
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));
419         case MOD_UNLOAD:
420                 error = ktls_crypto_backend_deregister(&ocf_backend);
421                 if (error)
422                         return (error);
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);
428                 return (0);
429         default:
430                 return (EOPNOTSUPP);
431         }
432 }
433
434 static moduledata_t ktls_ocf_moduledata = {
435         "ktls_ocf",
436         ktls_ocf_modevent,
437         NULL
438 };
439
440 DECLARE_MODULE(ktls_ocf, ktls_ocf_moduledata, SI_SUB_PROTO_END, SI_ORDER_ANY);