2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 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 AUTHOR 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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/counter.h>
34 #include <sys/endian.h>
35 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/sysctl.h>
43 #include <opencrypto/cryptodev.h>
47 crypto_session_t mac_sid;
52 /* Only used for TLS 1.0 with the implicit IV. */
57 char iv[AES_BLOCK_LEN];
60 struct ocf_operation {
61 struct ocf_session *os;
65 static MALLOC_DEFINE(M_KTLS_OCF, "ktls_ocf", "OCF KTLS");
67 SYSCTL_DECL(_kern_ipc_tls);
68 SYSCTL_DECL(_kern_ipc_tls_stats);
70 static SYSCTL_NODE(_kern_ipc_tls_stats, OID_AUTO, ocf,
71 CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
72 "Kernel TLS offload via OCF stats");
74 static COUNTER_U64_DEFINE_EARLY(ocf_tls10_cbc_encrypts);
75 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, tls10_cbc_encrypts,
76 CTLFLAG_RD, &ocf_tls10_cbc_encrypts,
77 "Total number of OCF TLS 1.0 CBC encryption operations");
79 static COUNTER_U64_DEFINE_EARLY(ocf_tls11_cbc_encrypts);
80 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, tls11_cbc_encrypts,
81 CTLFLAG_RD, &ocf_tls11_cbc_encrypts,
82 "Total number of OCF TLS 1.1/1.2 CBC encryption operations");
84 static COUNTER_U64_DEFINE_EARLY(ocf_tls12_gcm_decrypts);
85 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, tls12_gcm_decrypts,
86 CTLFLAG_RD, &ocf_tls12_gcm_decrypts,
87 "Total number of OCF TLS 1.2 GCM decryption operations");
89 static COUNTER_U64_DEFINE_EARLY(ocf_tls12_gcm_encrypts);
90 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, tls12_gcm_encrypts,
91 CTLFLAG_RD, &ocf_tls12_gcm_encrypts,
92 "Total number of OCF TLS 1.2 GCM encryption operations");
94 static COUNTER_U64_DEFINE_EARLY(ocf_tls12_chacha20_decrypts);
95 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, tls12_chacha20_decrypts,
96 CTLFLAG_RD, &ocf_tls12_chacha20_decrypts,
97 "Total number of OCF TLS 1.2 Chacha20-Poly1305 decryption operations");
99 static COUNTER_U64_DEFINE_EARLY(ocf_tls12_chacha20_encrypts);
100 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, tls12_chacha20_encrypts,
101 CTLFLAG_RD, &ocf_tls12_chacha20_encrypts,
102 "Total number of OCF TLS 1.2 Chacha20-Poly1305 encryption operations");
104 static COUNTER_U64_DEFINE_EARLY(ocf_tls13_gcm_decrypts);
105 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, tls13_gcm_decrypts,
106 CTLFLAG_RD, &ocf_tls13_gcm_decrypts,
107 "Total number of OCF TLS 1.3 GCM decryption operations");
109 static COUNTER_U64_DEFINE_EARLY(ocf_tls13_gcm_encrypts);
110 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, tls13_gcm_encrypts,
111 CTLFLAG_RD, &ocf_tls13_gcm_encrypts,
112 "Total number of OCF TLS 1.3 GCM encryption operations");
114 static COUNTER_U64_DEFINE_EARLY(ocf_tls13_chacha20_decrypts);
115 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, tls13_chacha20_decrypts,
116 CTLFLAG_RD, &ocf_tls13_chacha20_decrypts,
117 "Total number of OCF TLS 1.3 Chacha20-Poly1305 decryption operations");
119 static COUNTER_U64_DEFINE_EARLY(ocf_tls13_chacha20_encrypts);
120 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, tls13_chacha20_encrypts,
121 CTLFLAG_RD, &ocf_tls13_chacha20_encrypts,
122 "Total number of OCF TLS 1.3 Chacha20-Poly1305 encryption operations");
124 static COUNTER_U64_DEFINE_EARLY(ocf_inplace);
125 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, inplace,
126 CTLFLAG_RD, &ocf_inplace,
127 "Total number of OCF in-place operations");
129 static COUNTER_U64_DEFINE_EARLY(ocf_separate_output);
130 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, separate_output,
131 CTLFLAG_RD, &ocf_separate_output,
132 "Total number of OCF operations with a separate output buffer");
134 static COUNTER_U64_DEFINE_EARLY(ocf_retries);
135 SYSCTL_COUNTER_U64(_kern_ipc_tls_stats_ocf, OID_AUTO, retries, CTLFLAG_RD,
137 "Number of OCF encryption operation retries");
140 ktls_ocf_callback(struct cryptop *crp)
142 struct ocf_operation *oo;
144 oo = crp->crp_opaque;
145 mtx_lock(&oo->os->lock);
147 mtx_unlock(&oo->os->lock);
153 ktls_ocf_dispatch(struct ocf_session *os, struct cryptop *crp)
155 struct ocf_operation oo;
161 crp->crp_opaque = &oo;
162 crp->crp_callback = ktls_ocf_callback;
164 error = crypto_dispatch(crp);
170 mtx_sleep(&oo, &os->lock, 0, "ocfktls", 0);
171 mtx_unlock(&os->lock);
173 if (crp->crp_etype != EAGAIN) {
174 error = crp->crp_etype;
179 crp->crp_flags &= ~CRYPTO_F_DONE;
181 counter_u64_add(ocf_retries, 1);
187 ktls_ocf_tls_cbc_encrypt(struct ktls_session *tls,
188 const struct tls_record_layer *hdr, uint8_t *trailer, struct iovec *iniov,
189 struct iovec *outiov, int iovcnt, uint64_t seqno,
190 uint8_t record_type __unused)
192 struct uio uio, out_uio;
193 struct tls_mac_data ad;
195 struct ocf_session *os;
196 struct iovec iov[iovcnt + 2];
197 struct iovec out_iov[iovcnt + 1];
199 uint16_t tls_comp_len;
206 if (os->implicit_iv) {
208 KASSERT(!os->in_progress,
209 ("concurrent implicit IV encryptions"));
210 if (os->next_seqno != seqno) {
211 printf("KTLS CBC: TLS records out of order. "
212 "Expected %ju, got %ju\n",
213 (uintmax_t)os->next_seqno, (uintmax_t)seqno);
214 mtx_unlock(&os->lock);
217 os->in_progress = true;
218 mtx_unlock(&os->lock);
223 * Compute the payload length.
225 * XXX: This could be easily computed O(1) from the mbuf
226 * fields, but we don't have those accessible here. Can
227 * at least compute inplace as well while we are here.
231 for (i = 0; i < iovcnt; i++) {
232 tls_comp_len += iniov[i].iov_len;
233 if (iniov[i].iov_base != outiov[i].iov_base)
237 /* Initialize the AAD. */
238 ad.seq = htobe64(seqno);
239 ad.type = hdr->tls_type;
240 ad.tls_vmajor = hdr->tls_vmajor;
241 ad.tls_vminor = hdr->tls_vminor;
242 ad.tls_length = htons(tls_comp_len);
244 /* First, compute the MAC. */
245 iov[0].iov_base = &ad;
246 iov[0].iov_len = sizeof(ad);
247 memcpy(&iov[1], iniov, sizeof(*iniov) * iovcnt);
248 iov[iovcnt + 1].iov_base = trailer;
249 iov[iovcnt + 1].iov_len = os->mac_len;
251 uio.uio_iovcnt = iovcnt + 2;
253 uio.uio_segflg = UIO_SYSSPACE;
254 uio.uio_td = curthread;
255 uio.uio_resid = sizeof(ad) + tls_comp_len + os->mac_len;
257 crypto_initreq(&crp, os->mac_sid);
258 crp.crp_payload_start = 0;
259 crp.crp_payload_length = sizeof(ad) + tls_comp_len;
260 crp.crp_digest_start = crp.crp_payload_length;
261 crp.crp_op = CRYPTO_OP_COMPUTE_DIGEST;
262 crp.crp_flags = CRYPTO_F_CBIMM;
263 crypto_use_uio(&crp, &uio);
264 error = ktls_ocf_dispatch(os, &crp);
266 crypto_destroyreq(&crp);
269 if (os->implicit_iv) {
271 os->in_progress = false;
272 mtx_unlock(&os->lock);
278 /* Second, add the padding. */
279 pad = (unsigned)(AES_BLOCK_LEN - (tls_comp_len + os->mac_len + 1)) %
281 for (i = 0; i < pad + 1; i++)
282 trailer[os->mac_len + i] = pad;
284 /* Finally, encrypt the record. */
287 * Don't recopy the input iovec, instead just adjust the
288 * trailer length and skip over the AAD vector in the uio.
290 iov[iovcnt + 1].iov_len += pad + 1;
291 uio.uio_iov = iov + 1;
292 uio.uio_iovcnt = iovcnt + 1;
293 uio.uio_resid = tls_comp_len + iov[iovcnt + 1].iov_len;
294 KASSERT(uio.uio_resid % AES_BLOCK_LEN == 0,
295 ("invalid encryption size"));
297 crypto_initreq(&crp, os->sid);
298 crp.crp_payload_start = 0;
299 crp.crp_payload_length = uio.uio_resid;
300 crp.crp_op = CRYPTO_OP_ENCRYPT;
301 crp.crp_flags = CRYPTO_F_CBIMM | CRYPTO_F_IV_SEPARATE;
303 memcpy(crp.crp_iv, os->iv, AES_BLOCK_LEN);
305 memcpy(crp.crp_iv, hdr + 1, AES_BLOCK_LEN);
306 crypto_use_uio(&crp, &uio);
308 memcpy(out_iov, outiov, sizeof(*iniov) * iovcnt);
309 out_iov[iovcnt] = iov[iovcnt + 1];
310 out_uio.uio_iov = out_iov;
311 out_uio.uio_iovcnt = iovcnt + 1;
312 out_uio.uio_offset = 0;
313 out_uio.uio_segflg = UIO_SYSSPACE;
314 out_uio.uio_td = curthread;
315 out_uio.uio_resid = uio.uio_resid;
316 crypto_use_output_uio(&crp, &out_uio);
320 counter_u64_add(ocf_tls10_cbc_encrypts, 1);
322 counter_u64_add(ocf_tls11_cbc_encrypts, 1);
324 counter_u64_add(ocf_inplace, 1);
326 counter_u64_add(ocf_separate_output, 1);
327 error = ktls_ocf_dispatch(os, &crp);
329 crypto_destroyreq(&crp);
331 if (os->implicit_iv) {
332 KASSERT(os->mac_len + pad + 1 >= AES_BLOCK_LEN,
333 ("trailer too short to read IV"));
334 memcpy(os->iv, trailer + os->mac_len + pad + 1 - AES_BLOCK_LEN,
338 os->next_seqno = seqno + 1;
339 os->in_progress = false;
340 mtx_unlock(&os->lock);
347 ktls_ocf_tls12_aead_encrypt(struct ktls_session *tls,
348 const struct tls_record_layer *hdr, uint8_t *trailer, struct iovec *iniov,
349 struct iovec *outiov, int iovcnt, uint64_t seqno,
350 uint8_t record_type __unused)
352 struct uio uio, out_uio, *tag_uio;
353 struct tls_aead_data ad;
355 struct ocf_session *os;
356 struct iovec iov[iovcnt + 1];
358 uint16_t tls_comp_len;
364 uio.uio_iovcnt = iovcnt;
366 uio.uio_segflg = UIO_SYSSPACE;
367 uio.uio_td = curthread;
369 out_uio.uio_iov = outiov;
370 out_uio.uio_iovcnt = iovcnt;
371 out_uio.uio_offset = 0;
372 out_uio.uio_segflg = UIO_SYSSPACE;
373 out_uio.uio_td = curthread;
375 crypto_initreq(&crp, os->sid);
378 if (tls->params.cipher_algorithm == CRYPTO_AES_NIST_GCM_16) {
379 memcpy(crp.crp_iv, tls->params.iv, TLS_AEAD_GCM_LEN);
380 memcpy(crp.crp_iv + TLS_AEAD_GCM_LEN, hdr + 1,
384 * Chacha20-Poly1305 constructs the IV for TLS 1.2
385 * identically to constructing the IV for AEAD in TLS
388 memcpy(crp.crp_iv, tls->params.iv, tls->params.iv_len);
389 *(uint64_t *)(crp.crp_iv + 4) ^= htobe64(seqno);
393 if (tls->params.cipher_algorithm == CRYPTO_AES_NIST_GCM_16)
394 tls_comp_len = ntohs(hdr->tls_length) -
395 (AES_GMAC_HASH_LEN + sizeof(uint64_t));
397 tls_comp_len = ntohs(hdr->tls_length) - POLY1305_HASH_LEN;
398 ad.seq = htobe64(seqno);
399 ad.type = hdr->tls_type;
400 ad.tls_vmajor = hdr->tls_vmajor;
401 ad.tls_vminor = hdr->tls_vminor;
402 ad.tls_length = htons(tls_comp_len);
404 crp.crp_aad_length = sizeof(ad);
406 /* Compute payload length and determine if encryption is in place. */
408 crp.crp_payload_start = 0;
409 for (i = 0; i < iovcnt; i++) {
410 if (iniov[i].iov_base != outiov[i].iov_base)
412 crp.crp_payload_length += iniov[i].iov_len;
414 uio.uio_resid = crp.crp_payload_length;
415 out_uio.uio_resid = crp.crp_payload_length;
422 /* Duplicate iovec and append vector for tag. */
423 memcpy(iov, tag_uio->uio_iov, iovcnt * sizeof(struct iovec));
424 iov[iovcnt].iov_base = trailer;
425 iov[iovcnt].iov_len = tls->params.tls_tlen;
426 tag_uio->uio_iov = iov;
427 tag_uio->uio_iovcnt++;
428 crp.crp_digest_start = tag_uio->uio_resid;
429 tag_uio->uio_resid += tls->params.tls_tlen;
431 crp.crp_op = CRYPTO_OP_ENCRYPT | CRYPTO_OP_COMPUTE_DIGEST;
432 crp.crp_flags = CRYPTO_F_CBIMM | CRYPTO_F_IV_SEPARATE;
433 crypto_use_uio(&crp, &uio);
435 crypto_use_output_uio(&crp, &out_uio);
437 if (tls->params.cipher_algorithm == CRYPTO_AES_NIST_GCM_16)
438 counter_u64_add(ocf_tls12_gcm_encrypts, 1);
440 counter_u64_add(ocf_tls12_chacha20_encrypts, 1);
442 counter_u64_add(ocf_inplace, 1);
444 counter_u64_add(ocf_separate_output, 1);
445 error = ktls_ocf_dispatch(os, &crp);
447 crypto_destroyreq(&crp);
452 ktls_ocf_tls12_aead_decrypt(struct ktls_session *tls,
453 const struct tls_record_layer *hdr, struct mbuf *m, uint64_t seqno,
456 struct tls_aead_data ad;
458 struct ocf_session *os;
459 struct ocf_operation oo;
461 uint16_t tls_comp_len;
468 crypto_initreq(&crp, os->sid);
471 if (tls->params.cipher_algorithm == CRYPTO_AES_NIST_GCM_16) {
472 memcpy(crp.crp_iv, tls->params.iv, TLS_AEAD_GCM_LEN);
473 memcpy(crp.crp_iv + TLS_AEAD_GCM_LEN, hdr + 1,
477 * Chacha20-Poly1305 constructs the IV for TLS 1.2
478 * identically to constructing the IV for AEAD in TLS
481 memcpy(crp.crp_iv, tls->params.iv, tls->params.iv_len);
482 *(uint64_t *)(crp.crp_iv + 4) ^= htobe64(seqno);
486 if (tls->params.cipher_algorithm == CRYPTO_AES_NIST_GCM_16)
487 tls_comp_len = ntohs(hdr->tls_length) -
488 (AES_GMAC_HASH_LEN + sizeof(uint64_t));
490 tls_comp_len = ntohs(hdr->tls_length) - POLY1305_HASH_LEN;
491 ad.seq = htobe64(seqno);
492 ad.type = hdr->tls_type;
493 ad.tls_vmajor = hdr->tls_vmajor;
494 ad.tls_vminor = hdr->tls_vminor;
495 ad.tls_length = htons(tls_comp_len);
497 crp.crp_aad_length = sizeof(ad);
499 crp.crp_payload_start = tls->params.tls_hlen;
500 crp.crp_payload_length = tls_comp_len;
501 crp.crp_digest_start = crp.crp_payload_start + crp.crp_payload_length;
503 crp.crp_op = CRYPTO_OP_DECRYPT | CRYPTO_OP_VERIFY_DIGEST;
504 crp.crp_flags = CRYPTO_F_CBIMM | CRYPTO_F_IV_SEPARATE;
505 crypto_use_mbuf(&crp, m);
507 if (tls->params.cipher_algorithm == CRYPTO_AES_NIST_GCM_16)
508 counter_u64_add(ocf_tls12_gcm_decrypts, 1);
510 counter_u64_add(ocf_tls12_chacha20_decrypts, 1);
511 error = ktls_ocf_dispatch(os, &crp);
513 crypto_destroyreq(&crp);
514 *trailer_len = tls->params.tls_tlen;
519 ktls_ocf_tls13_aead_encrypt(struct ktls_session *tls,
520 const struct tls_record_layer *hdr, uint8_t *trailer, struct iovec *iniov,
521 struct iovec *outiov, int iovcnt, uint64_t seqno, uint8_t record_type)
523 struct uio uio, out_uio;
524 struct tls_aead_data_13 ad;
527 struct ocf_session *os;
528 struct iovec iov[iovcnt + 1], out_iov[iovcnt + 1];
534 crypto_initreq(&crp, os->sid);
536 /* Setup the nonce. */
537 memcpy(nonce, tls->params.iv, tls->params.iv_len);
538 *(uint64_t *)(nonce + 4) ^= htobe64(seqno);
541 ad.type = hdr->tls_type;
542 ad.tls_vmajor = hdr->tls_vmajor;
543 ad.tls_vminor = hdr->tls_vminor;
544 ad.tls_length = hdr->tls_length;
546 crp.crp_aad_length = sizeof(ad);
548 /* Compute payload length and determine if encryption is in place. */
550 crp.crp_payload_start = 0;
551 for (i = 0; i < iovcnt; i++) {
552 if (iniov[i].iov_base != outiov[i].iov_base)
554 crp.crp_payload_length += iniov[i].iov_len;
557 /* Store the record type as the first byte of the trailer. */
558 trailer[0] = record_type;
559 crp.crp_payload_length++;
560 crp.crp_digest_start = crp.crp_payload_length;
563 * Duplicate the input iov to append the trailer. Always
564 * include the full trailer as input to get the record_type
565 * even if only the first byte is used.
567 memcpy(iov, iniov, iovcnt * sizeof(*iov));
568 iov[iovcnt].iov_base = trailer;
569 iov[iovcnt].iov_len = tls->params.tls_tlen;
571 uio.uio_iovcnt = iovcnt + 1;
573 uio.uio_resid = crp.crp_payload_length + tls->params.tls_tlen - 1;
574 uio.uio_segflg = UIO_SYSSPACE;
575 uio.uio_td = curthread;
576 crypto_use_uio(&crp, &uio);
579 /* Duplicate the output iov to append the trailer. */
580 memcpy(out_iov, outiov, iovcnt * sizeof(*out_iov));
581 out_iov[iovcnt] = iov[iovcnt];
583 out_uio.uio_iov = out_iov;
584 out_uio.uio_iovcnt = iovcnt + 1;
585 out_uio.uio_offset = 0;
586 out_uio.uio_resid = crp.crp_payload_length +
587 tls->params.tls_tlen - 1;
588 out_uio.uio_segflg = UIO_SYSSPACE;
589 out_uio.uio_td = curthread;
590 crypto_use_output_uio(&crp, &out_uio);
593 crp.crp_op = CRYPTO_OP_ENCRYPT | CRYPTO_OP_COMPUTE_DIGEST;
594 crp.crp_flags = CRYPTO_F_CBIMM | CRYPTO_F_IV_SEPARATE;
596 memcpy(crp.crp_iv, nonce, sizeof(nonce));
598 if (tls->params.cipher_algorithm == CRYPTO_AES_NIST_GCM_16)
599 counter_u64_add(ocf_tls13_gcm_encrypts, 1);
601 counter_u64_add(ocf_tls13_chacha20_encrypts, 1);
603 counter_u64_add(ocf_inplace, 1);
605 counter_u64_add(ocf_separate_output, 1);
606 error = ktls_ocf_dispatch(os, &crp);
608 crypto_destroyreq(&crp);
613 ktls_ocf_tls13_aead_decrypt(struct ktls_session *tls,
614 const struct tls_record_layer *hdr, struct mbuf *m, uint64_t seqno,
617 struct tls_aead_data_13 ad;
619 struct ocf_session *os;
625 tag_len = tls->params.tls_tlen - 1;
627 /* Payload must contain at least one byte for the record type. */
628 if (ntohs(hdr->tls_length) < tag_len + 1)
631 crypto_initreq(&crp, os->sid);
633 /* Setup the nonce. */
634 memcpy(crp.crp_iv, tls->params.iv, tls->params.iv_len);
635 *(uint64_t *)(crp.crp_iv + 4) ^= htobe64(seqno);
638 ad.type = hdr->tls_type;
639 ad.tls_vmajor = hdr->tls_vmajor;
640 ad.tls_vminor = hdr->tls_vminor;
641 ad.tls_length = hdr->tls_length;
643 crp.crp_aad_length = sizeof(ad);
645 crp.crp_payload_start = tls->params.tls_hlen;
646 crp.crp_payload_length = ntohs(hdr->tls_length) - tag_len;
647 crp.crp_digest_start = crp.crp_payload_start + crp.crp_payload_length;
649 crp.crp_op = CRYPTO_OP_DECRYPT | CRYPTO_OP_VERIFY_DIGEST;
650 crp.crp_flags = CRYPTO_F_CBIMM | CRYPTO_F_IV_SEPARATE;
651 crypto_use_mbuf(&crp, m);
653 if (tls->params.cipher_algorithm == CRYPTO_AES_NIST_GCM_16)
654 counter_u64_add(ocf_tls13_gcm_decrypts, 1);
656 counter_u64_add(ocf_tls13_chacha20_decrypts, 1);
657 error = ktls_ocf_dispatch(os, &crp);
659 crypto_destroyreq(&crp);
660 *trailer_len = tag_len;
665 ktls_ocf_free(struct ktls_session *tls)
667 struct ocf_session *os;
670 crypto_freesession(os->sid);
671 crypto_freesession(os->mac_sid);
672 mtx_destroy(&os->lock);
673 zfree(os, M_KTLS_OCF);
677 ktls_ocf_try(struct socket *so, struct ktls_session *tls, int direction)
679 struct crypto_session_params csp, mac_csp;
680 struct ocf_session *os;
683 memset(&csp, 0, sizeof(csp));
684 memset(&mac_csp, 0, sizeof(mac_csp));
685 mac_csp.csp_mode = CSP_MODE_NONE;
688 switch (tls->params.cipher_algorithm) {
689 case CRYPTO_AES_NIST_GCM_16:
690 switch (tls->params.cipher_key_len) {
698 /* Only TLS 1.2 and 1.3 are supported. */
699 if (tls->params.tls_vmajor != TLS_MAJOR_VER_ONE ||
700 tls->params.tls_vminor < TLS_MINOR_VER_TWO ||
701 tls->params.tls_vminor > TLS_MINOR_VER_THREE)
702 return (EPROTONOSUPPORT);
704 csp.csp_flags |= CSP_F_SEPARATE_OUTPUT | CSP_F_SEPARATE_AAD;
705 csp.csp_mode = CSP_MODE_AEAD;
706 csp.csp_cipher_alg = CRYPTO_AES_NIST_GCM_16;
707 csp.csp_cipher_key = tls->params.cipher_key;
708 csp.csp_cipher_klen = tls->params.cipher_key_len;
709 csp.csp_ivlen = AES_GCM_IV_LEN;
712 switch (tls->params.cipher_key_len) {
720 switch (tls->params.auth_algorithm) {
721 case CRYPTO_SHA1_HMAC:
722 mac_len = SHA1_HASH_LEN;
724 case CRYPTO_SHA2_256_HMAC:
725 mac_len = SHA2_256_HASH_LEN;
727 case CRYPTO_SHA2_384_HMAC:
728 mac_len = SHA2_384_HASH_LEN;
734 /* Only TLS 1.0-1.2 are supported. */
735 if (tls->params.tls_vmajor != TLS_MAJOR_VER_ONE ||
736 tls->params.tls_vminor < TLS_MINOR_VER_ZERO ||
737 tls->params.tls_vminor > TLS_MINOR_VER_TWO)
738 return (EPROTONOSUPPORT);
740 /* AES-CBC is not supported for receive. */
741 if (direction == KTLS_RX)
742 return (EPROTONOSUPPORT);
744 csp.csp_flags |= CSP_F_SEPARATE_OUTPUT;
745 csp.csp_mode = CSP_MODE_CIPHER;
746 csp.csp_cipher_alg = CRYPTO_AES_CBC;
747 csp.csp_cipher_key = tls->params.cipher_key;
748 csp.csp_cipher_klen = tls->params.cipher_key_len;
749 csp.csp_ivlen = AES_BLOCK_LEN;
751 mac_csp.csp_flags |= CSP_F_SEPARATE_OUTPUT;
752 mac_csp.csp_mode = CSP_MODE_DIGEST;
753 mac_csp.csp_auth_alg = tls->params.auth_algorithm;
754 mac_csp.csp_auth_key = tls->params.auth_key;
755 mac_csp.csp_auth_klen = tls->params.auth_key_len;
757 case CRYPTO_CHACHA20_POLY1305:
758 switch (tls->params.cipher_key_len) {
765 /* Only TLS 1.2 and 1.3 are supported. */
766 if (tls->params.tls_vmajor != TLS_MAJOR_VER_ONE ||
767 tls->params.tls_vminor < TLS_MINOR_VER_TWO ||
768 tls->params.tls_vminor > TLS_MINOR_VER_THREE)
769 return (EPROTONOSUPPORT);
771 csp.csp_flags |= CSP_F_SEPARATE_OUTPUT | CSP_F_SEPARATE_AAD;
772 csp.csp_mode = CSP_MODE_AEAD;
773 csp.csp_cipher_alg = CRYPTO_CHACHA20_POLY1305;
774 csp.csp_cipher_key = tls->params.cipher_key;
775 csp.csp_cipher_klen = tls->params.cipher_key_len;
776 csp.csp_ivlen = CHACHA20_POLY1305_IV_LEN;
779 return (EPROTONOSUPPORT);
782 os = malloc(sizeof(*os), M_KTLS_OCF, M_NOWAIT | M_ZERO);
786 error = crypto_newsession(&os->sid, &csp,
787 CRYPTO_FLAG_HARDWARE | CRYPTO_FLAG_SOFTWARE);
789 free(os, M_KTLS_OCF);
793 if (mac_csp.csp_mode != CSP_MODE_NONE) {
794 error = crypto_newsession(&os->mac_sid, &mac_csp,
795 CRYPTO_FLAG_HARDWARE | CRYPTO_FLAG_SOFTWARE);
797 crypto_freesession(os->sid);
798 free(os, M_KTLS_OCF);
801 os->mac_len = mac_len;
804 mtx_init(&os->lock, "ktls_ocf", NULL, MTX_DEF);
806 if (tls->params.cipher_algorithm == CRYPTO_AES_NIST_GCM_16 ||
807 tls->params.cipher_algorithm == CRYPTO_CHACHA20_POLY1305) {
808 if (direction == KTLS_TX) {
809 if (tls->params.tls_vminor == TLS_MINOR_VER_THREE)
810 tls->sw_encrypt = ktls_ocf_tls13_aead_encrypt;
812 tls->sw_encrypt = ktls_ocf_tls12_aead_encrypt;
814 if (tls->params.tls_vminor == TLS_MINOR_VER_THREE)
815 tls->sw_decrypt = ktls_ocf_tls13_aead_decrypt;
817 tls->sw_decrypt = ktls_ocf_tls12_aead_decrypt;
820 tls->sw_encrypt = ktls_ocf_tls_cbc_encrypt;
821 if (tls->params.tls_vminor == TLS_MINOR_VER_ZERO) {
822 os->implicit_iv = true;
823 memcpy(os->iv, tls->params.iv, AES_BLOCK_LEN);
825 os->next_seqno = tls->next_seqno;
829 tls->free = ktls_ocf_free;
833 struct ktls_crypto_backend ocf_backend = {
836 .api_version = KTLS_API_VERSION,
841 ktls_ocf_modevent(module_t mod, int what, void *arg)
845 return (ktls_crypto_backend_register(&ocf_backend));
847 return (ktls_crypto_backend_deregister(&ocf_backend));
853 static moduledata_t ktls_ocf_moduledata = {
859 DECLARE_MODULE(ktls_ocf, ktls_ocf_moduledata, SI_SUB_PROTO_END, SI_ORDER_ANY);