1 /* $OpenBSD: cryptodev.c,v 1.52 2002/06/19 07:22:46 deraadt Exp $ */
4 * Copyright (c) 2001 Theo de Raadt
5 * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
6 * Copyright (c) 2014-2021 The FreeBSD Foundation
9 * Portions of this software were developed by John-Mark Gurney
10 * under sponsorship of the FreeBSD Foundation and
11 * Rubicon Communications, LLC (Netgate).
13 * Portions of this software were developed by Ararat River
14 * Consulting, LLC under sponsorship of the FreeBSD Foundation.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. The name of the author may not be used to endorse or promote products
26 * derived from this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * Effort sponsored in part by the Defense Advanced Research Projects
40 * Agency (DARPA) and Air Force Research Laboratory, Air Force
41 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/malloc.h>
52 #include <sys/mutex.h>
54 #include <sys/sysctl.h>
55 #include <sys/errno.h>
56 #include <sys/random.h>
58 #include <sys/kernel.h>
59 #include <sys/module.h>
60 #include <sys/fcntl.h>
63 #include <sys/syscallsubr.h>
65 #include <opencrypto/cryptodev.h>
66 #include <opencrypto/xform.h>
68 SDT_PROVIDER_DECLARE(opencrypto);
70 SDT_PROBE_DEFINE1(opencrypto, dev, ioctl, error, "int"/*line number*/);
72 #ifdef COMPAT_FREEBSD12
74 * Previously, most ioctls were performed against a cloned descriptor
75 * of /dev/crypto obtained via CRIOGET. Now all ioctls are performed
76 * against /dev/crypto directly.
78 #define CRIOGET _IOWR('c', 100, uint32_t)
81 /* the following are done against the cloned descriptor */
83 #ifdef COMPAT_FREEBSD32
84 #include <sys/mount.h>
85 #include <compat/freebsd32/freebsd32.h>
97 struct session2_op32 {
121 struct crypt_aead32 {
135 #define CIOCGSESSION32 _IOWR('c', 101, struct session_op32)
136 #define CIOCCRYPT32 _IOWR('c', 103, struct crypt_op32)
137 #define CIOCGSESSION232 _IOWR('c', 106, struct session2_op32)
138 #define CIOCCRYPTAEAD32 _IOWR('c', 109, struct crypt_aead32)
141 session_op_from_32(const struct session_op32 *from, struct session2_op *to)
144 memset(to, 0, sizeof(*to));
145 CP(*from, *to, cipher);
147 CP(*from, *to, keylen);
148 PTRIN_CP(*from, *to, key);
149 CP(*from, *to, mackeylen);
150 PTRIN_CP(*from, *to, mackey);
152 to->crid = CRYPTOCAP_F_HARDWARE;
156 session2_op_from_32(const struct session2_op32 *from, struct session2_op *to)
159 session_op_from_32((const struct session_op32 *)from, to);
160 CP(*from, *to, crid);
161 CP(*from, *to, ivlen);
162 CP(*from, *to, maclen);
166 session_op_to_32(const struct session2_op *from, struct session_op32 *to)
169 CP(*from, *to, cipher);
171 CP(*from, *to, keylen);
172 PTROUT_CP(*from, *to, key);
173 CP(*from, *to, mackeylen);
174 PTROUT_CP(*from, *to, mackey);
179 session2_op_to_32(const struct session2_op *from, struct session2_op32 *to)
182 session_op_to_32(from, (struct session_op32 *)to);
183 CP(*from, *to, crid);
187 crypt_op_from_32(const struct crypt_op32 *from, struct crypt_op *to)
192 CP(*from, *to, flags);
194 PTRIN_CP(*from, *to, src);
195 PTRIN_CP(*from, *to, dst);
196 PTRIN_CP(*from, *to, mac);
197 PTRIN_CP(*from, *to, iv);
201 crypt_op_to_32(const struct crypt_op *from, struct crypt_op32 *to)
206 CP(*from, *to, flags);
208 PTROUT_CP(*from, *to, src);
209 PTROUT_CP(*from, *to, dst);
210 PTROUT_CP(*from, *to, mac);
211 PTROUT_CP(*from, *to, iv);
215 crypt_aead_from_32(const struct crypt_aead32 *from, struct crypt_aead *to)
220 CP(*from, *to, flags);
222 CP(*from, *to, aadlen);
223 CP(*from, *to, ivlen);
224 PTRIN_CP(*from, *to, src);
225 PTRIN_CP(*from, *to, dst);
226 PTRIN_CP(*from, *to, aad);
227 PTRIN_CP(*from, *to, tag);
228 PTRIN_CP(*from, *to, iv);
232 crypt_aead_to_32(const struct crypt_aead *from, struct crypt_aead32 *to)
237 CP(*from, *to, flags);
239 CP(*from, *to, aadlen);
240 CP(*from, *to, ivlen);
241 PTROUT_CP(*from, *to, src);
242 PTROUT_CP(*from, *to, dst);
243 PTROUT_CP(*from, *to, aad);
244 PTROUT_CP(*from, *to, tag);
245 PTROUT_CP(*from, *to, iv);
250 session2_op_from_op(const struct session_op *from, struct session2_op *to)
253 memset(to, 0, sizeof(*to));
254 memcpy(to, from, sizeof(*from));
255 to->crid = CRYPTOCAP_F_HARDWARE;
259 session2_op_to_op(const struct session2_op *from, struct session_op *to)
262 memcpy(to, from, sizeof(*to));
266 TAILQ_ENTRY(csession) next;
267 crypto_session_t cses;
270 struct mtx lock; /* for op submission */
280 struct cryptop_data {
281 struct csession *cse;
290 TAILQ_HEAD(csessionlist, csession) csessions;
295 static bool use_outputbuffers;
296 SYSCTL_BOOL(_kern_crypto, OID_AUTO, cryptodev_use_output, CTLFLAG_RW,
297 &use_outputbuffers, 0,
298 "Use separate output buffers for /dev/crypto requests.");
300 static bool use_separate_aad;
301 SYSCTL_BOOL(_kern_crypto, OID_AUTO, cryptodev_separate_aad, CTLFLAG_RW,
302 &use_separate_aad, 0,
303 "Use separate AAD buffer for /dev/crypto requests.");
305 static MALLOC_DEFINE(M_CRYPTODEV, "cryptodev", "/dev/crypto data buffers");
308 * Check a crypto identifier to see if it requested
309 * a software device/driver. This can be done either
310 * by device name/class or through search constraints.
313 checkforsoftware(int *cridp)
319 if (!crypto_devallowsoft) {
320 if (crid & CRYPTOCAP_F_SOFTWARE) {
321 if (crid & CRYPTOCAP_F_HARDWARE) {
322 *cridp = CRYPTOCAP_F_HARDWARE;
327 if ((crid & CRYPTOCAP_F_HARDWARE) == 0 &&
328 (crypto_getcaps(crid) & CRYPTOCAP_F_HARDWARE) == 0)
335 cse_create(struct fcrypt *fcr, struct session2_op *sop)
337 struct crypto_session_params csp;
338 struct csession *cse;
339 const struct enc_xform *txform;
340 const struct auth_hash *thash;
343 crypto_session_t cses;
344 int crid, error, mac;
347 #ifdef COMPAT_FREEBSD12
349 case CRYPTO_AES_128_NIST_GMAC:
350 case CRYPTO_AES_192_NIST_GMAC:
351 case CRYPTO_AES_256_NIST_GMAC:
352 /* Should always be paired with GCM. */
353 if (sop->cipher != CRYPTO_AES_NIST_GCM_16) {
354 CRYPTDEB("GMAC without GCM");
355 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
358 if (sop->keylen != sop->mackeylen) {
359 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
364 case CRYPTO_AES_CCM_CBC_MAC:
365 /* Should always be paired with CCM. */
366 if (sop->cipher != CRYPTO_AES_CCM_16) {
367 CRYPTDEB("CBC-MAC without CCM");
368 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
371 if (sop->keylen != sop->mackeylen) {
372 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
380 memset(&csp, 0, sizeof(csp));
381 if (use_outputbuffers)
382 csp.csp_flags |= CSP_F_SEPARATE_OUTPUT;
384 csp.csp_auth_alg = mac;
385 csp.csp_auth_klen = sop->mackeylen;
387 if (sop->cipher != 0) {
388 csp.csp_cipher_alg = sop->cipher;
389 csp.csp_cipher_klen = sop->keylen;
391 thash = crypto_auth_hash(&csp);
392 txform = crypto_cipher(&csp);
394 if (txform != NULL && txform->macsize != 0) {
396 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
399 csp.csp_mode = CSP_MODE_AEAD;
400 } else if (txform != NULL && thash != NULL) {
401 csp.csp_mode = CSP_MODE_ETA;
402 } else if (txform != NULL) {
403 csp.csp_mode = CSP_MODE_CIPHER;
404 } else if (thash != NULL) {
405 csp.csp_mode = CSP_MODE_DIGEST;
407 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
411 switch (csp.csp_mode) {
414 if (use_separate_aad)
415 csp.csp_flags |= CSP_F_SEPARATE_AAD;
419 if (txform != NULL) {
420 if (sop->keylen > txform->maxkey ||
421 sop->keylen < txform->minkey) {
422 CRYPTDEB("invalid cipher parameters");
424 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
428 key = malloc(csp.csp_cipher_klen, M_CRYPTODEV, M_WAITOK);
429 error = copyin(sop->key, key, csp.csp_cipher_klen);
431 CRYPTDEB("invalid key");
432 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
435 csp.csp_cipher_key = key;
436 csp.csp_ivlen = txform->ivsize;
440 if (sop->mackeylen > thash->keysize || sop->mackeylen < 0) {
441 CRYPTDEB("invalid mac key length");
443 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
447 if (csp.csp_auth_klen != 0) {
448 mackey = malloc(csp.csp_auth_klen, M_CRYPTODEV,
450 error = copyin(sop->mackey, mackey, csp.csp_auth_klen);
452 CRYPTDEB("invalid mac key");
453 SDT_PROBE1(opencrypto, dev, ioctl, error,
457 csp.csp_auth_key = mackey;
460 if (csp.csp_auth_alg == CRYPTO_AES_NIST_GMAC)
461 csp.csp_ivlen = AES_GCM_IV_LEN;
462 if (csp.csp_auth_alg == CRYPTO_AES_CCM_CBC_MAC)
463 csp.csp_ivlen = AES_CCM_IV_LEN;
466 if (sop->ivlen != 0) {
467 if (csp.csp_ivlen == 0) {
468 CRYPTDEB("does not support an IV");
470 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
473 csp.csp_ivlen = sop->ivlen;
475 if (sop->maclen != 0) {
476 if (!(thash != NULL || csp.csp_mode == CSP_MODE_AEAD)) {
477 CRYPTDEB("does not support a MAC");
479 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
482 csp.csp_auth_mlen = sop->maclen;
486 error = checkforsoftware(&crid);
488 CRYPTDEB("checkforsoftware");
489 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
492 error = crypto_newsession(&cses, &csp, crid);
494 CRYPTDEB("crypto_newsession");
495 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
499 cse = malloc(sizeof(struct csession), M_CRYPTODEV, M_WAITOK | M_ZERO);
500 mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF);
501 refcount_init(&cse->refs, 1);
503 cse->mackey = mackey;
505 if (sop->maclen != 0)
506 cse->hashsize = sop->maclen;
507 else if (thash != NULL)
508 cse->hashsize = thash->hashsize;
509 else if (csp.csp_mode == CSP_MODE_AEAD)
510 cse->hashsize = txform->macsize;
511 cse->ivsize = csp.csp_ivlen;
514 * NB: This isn't necessarily the block size of the underlying
515 * MAC or cipher but is instead a restriction on valid input
519 cse->blocksize = txform->blocksize;
523 mtx_lock(&fcr->lock);
524 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
525 cse->ses = fcr->sesn++;
526 mtx_unlock(&fcr->lock);
530 /* return hardware/driver id */
531 sop->crid = crypto_ses2hid(cse->cses);
534 free(key, M_CRYPTODEV);
535 free(mackey, M_CRYPTODEV);
540 static struct csession *
541 cse_find(struct fcrypt *fcr, u_int ses)
543 struct csession *cse;
545 mtx_lock(&fcr->lock);
546 TAILQ_FOREACH(cse, &fcr->csessions, next) {
547 if (cse->ses == ses) {
548 refcount_acquire(&cse->refs);
549 mtx_unlock(&fcr->lock);
553 mtx_unlock(&fcr->lock);
558 cse_free(struct csession *cse)
561 if (!refcount_release(&cse->refs))
563 crypto_freesession(cse->cses);
564 mtx_destroy(&cse->lock);
566 free(cse->key, M_CRYPTODEV);
568 free(cse->mackey, M_CRYPTODEV);
569 free(cse, M_CRYPTODEV);
573 cse_delete(struct fcrypt *fcr, u_int ses)
575 struct csession *cse;
577 mtx_lock(&fcr->lock);
578 TAILQ_FOREACH(cse, &fcr->csessions, next) {
579 if (cse->ses == ses) {
580 TAILQ_REMOVE(&fcr->csessions, cse, next);
581 mtx_unlock(&fcr->lock);
586 mtx_unlock(&fcr->lock);
590 static struct cryptop_data *
591 cod_alloc(struct csession *cse, size_t aad_len, size_t len)
593 struct cryptop_data *cod;
595 cod = malloc(sizeof(struct cryptop_data), M_CRYPTODEV, M_WAITOK |
599 if (crypto_get_params(cse->cses)->csp_flags & CSP_F_SEPARATE_AAD) {
601 cod->aad = malloc(aad_len, M_CRYPTODEV, M_WAITOK);
602 cod->buf = malloc(len, M_CRYPTODEV, M_WAITOK);
604 cod->buf = malloc(aad_len + len, M_CRYPTODEV, M_WAITOK);
605 if (crypto_get_params(cse->cses)->csp_flags & CSP_F_SEPARATE_OUTPUT)
606 cod->obuf = malloc(len, M_CRYPTODEV, M_WAITOK);
611 cod_free(struct cryptop_data *cod)
614 free(cod->aad, M_CRYPTODEV);
615 free(cod->obuf, M_CRYPTODEV);
616 free(cod->buf, M_CRYPTODEV);
617 free(cod, M_CRYPTODEV);
621 cryptodev_cb(struct cryptop *crp)
623 struct cryptop_data *cod = crp->crp_opaque;
626 * Lock to ensure the wakeup() is not missed by the loops
627 * waiting on cod->done in cryptodev_op() and
630 mtx_lock(&cod->cse->lock);
632 mtx_unlock(&cod->cse->lock);
638 cryptodev_op(struct csession *cse, const struct crypt_op *cop)
640 const struct crypto_session_params *csp;
641 struct cryptop_data *cod = NULL;
642 struct cryptop *crp = NULL;
646 if (cop->len > 256*1024-4) {
647 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
651 if ((cop->len % cse->blocksize) != 0) {
652 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
656 if (cop->mac && cse->hashsize == 0) {
657 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
662 * The COP_F_CIPHER_FIRST flag predates explicit session
663 * modes, but the only way it was used was for EtA so allow it
664 * as long as it is consistent with EtA.
666 if (cop->flags & COP_F_CIPHER_FIRST) {
667 if (cop->op != COP_ENCRYPT) {
668 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
673 cod = cod_alloc(cse, 0, cop->len + cse->hashsize);
676 crp = crypto_getreq(cse->cses, M_WAITOK);
678 error = copyin(cop->src, cod->buf, cop->len);
680 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
683 crp->crp_payload_start = 0;
684 crp->crp_payload_length = cop->len;
686 crp->crp_digest_start = cop->len;
688 csp = crypto_get_params(cse->cses);
689 switch (csp->csp_mode) {
690 case CSP_MODE_COMPRESS:
693 crp->crp_op = CRYPTO_OP_COMPRESS;
696 crp->crp_op = CRYPTO_OP_DECOMPRESS;
699 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
704 case CSP_MODE_CIPHER:
706 (cop->iv == NULL && cop->len == cse->ivsize)) {
707 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
713 crp->crp_op = CRYPTO_OP_ENCRYPT;
716 crp->crp_op = CRYPTO_OP_DECRYPT;
719 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
724 case CSP_MODE_DIGEST:
729 crp->crp_op = CRYPTO_OP_COMPUTE_DIGEST;
730 if (cod->obuf != NULL)
731 crp->crp_digest_start = 0;
734 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
740 if (cse->ivsize != 0 && cop->iv == NULL) {
741 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
749 crp->crp_op = CRYPTO_OP_ENCRYPT |
750 CRYPTO_OP_COMPUTE_DIGEST;
753 crp->crp_op = CRYPTO_OP_DECRYPT |
754 CRYPTO_OP_VERIFY_DIGEST;
757 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
763 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
768 crp->crp_flags = CRYPTO_F_CBIMM | (cop->flags & COP_F_BATCH);
769 crypto_use_buf(crp, cod->buf, cop->len + cse->hashsize);
771 crypto_use_output_buf(crp, cod->obuf, cop->len + cse->hashsize);
772 crp->crp_callback = cryptodev_cb;
773 crp->crp_opaque = cod;
776 if (cse->ivsize == 0) {
777 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
781 error = copyin(cop->iv, crp->crp_iv, cse->ivsize);
783 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
786 crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
787 } else if (cse->ivsize != 0) {
788 if (crp->crp_payload_length < cse->ivsize) {
789 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
793 crp->crp_iv_start = 0;
794 crp->crp_payload_length -= cse->ivsize;
795 if (crp->crp_payload_length != 0)
796 crp->crp_payload_start = cse->ivsize;
800 if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
801 error = copyin(cop->mac, cod->buf + crp->crp_digest_start,
804 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
810 * Let the dispatch run unlocked, then, interlock against the
811 * callback before checking if the operation completed and going
812 * to sleep. This insures drivers don't inherit our lock which
813 * results in a lock order reversal between crypto_dispatch forced
814 * entry and the crypto_done callback into us.
816 error = crypto_dispatch(crp);
818 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
822 mtx_lock(&cse->lock);
824 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0);
825 mtx_unlock(&cse->lock);
827 if (crp->crp_etype == EAGAIN) {
829 crp->crp_flags &= ~CRYPTO_F_DONE;
834 if (crp->crp_etype != 0) {
835 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
836 error = crp->crp_etype;
840 if (cop->dst != NULL) {
841 error = copyout(cod->obuf != NULL ? cod->obuf :
842 cod->buf + crp->crp_payload_start, dst,
843 crp->crp_payload_length);
845 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
850 if (cop->mac != NULL && (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) == 0) {
851 error = copyout((cod->obuf != NULL ? cod->obuf : cod->buf) +
852 crp->crp_digest_start, cop->mac, cse->hashsize);
854 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
867 cryptodev_aead(struct csession *cse, struct crypt_aead *caead)
869 const struct crypto_session_params *csp;
870 struct cryptop_data *cod = NULL;
871 struct cryptop *crp = NULL;
875 if (caead->len > 256*1024-4 || caead->aadlen > 256*1024-4) {
876 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
880 if ((caead->len % cse->blocksize) != 0) {
881 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
885 if (cse->hashsize == 0 || caead->tag == NULL) {
886 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
891 * The COP_F_CIPHER_FIRST flag predates explicit session
892 * modes, but the only way it was used was for EtA so allow it
893 * as long as it is consistent with EtA.
895 if (caead->flags & COP_F_CIPHER_FIRST) {
896 if (caead->op != COP_ENCRYPT) {
897 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
902 cod = cod_alloc(cse, caead->aadlen, caead->len + cse->hashsize);
905 crp = crypto_getreq(cse->cses, M_WAITOK);
907 if (cod->aad != NULL)
908 error = copyin(caead->aad, cod->aad, caead->aadlen);
910 error = copyin(caead->aad, cod->buf, caead->aadlen);
912 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
915 crp->crp_aad = cod->aad;
916 crp->crp_aad_start = 0;
917 crp->crp_aad_length = caead->aadlen;
919 if (cod->aad != NULL)
920 crp->crp_payload_start = 0;
922 crp->crp_payload_start = caead->aadlen;
923 error = copyin(caead->src, cod->buf + crp->crp_payload_start,
926 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
929 crp->crp_payload_length = caead->len;
930 if (caead->op == COP_ENCRYPT && cod->obuf != NULL)
931 crp->crp_digest_start = crp->crp_payload_output_start +
934 crp->crp_digest_start = crp->crp_payload_start + caead->len;
936 csp = crypto_get_params(cse->cses);
937 switch (csp->csp_mode) {
942 crp->crp_op = CRYPTO_OP_ENCRYPT |
943 CRYPTO_OP_COMPUTE_DIGEST;
946 crp->crp_op = CRYPTO_OP_DECRYPT |
947 CRYPTO_OP_VERIFY_DIGEST;
950 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
956 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
961 crp->crp_flags = CRYPTO_F_CBIMM | (caead->flags & COP_F_BATCH);
962 crypto_use_buf(crp, cod->buf, crp->crp_payload_start + caead->len +
964 if (cod->obuf != NULL)
965 crypto_use_output_buf(crp, cod->obuf, caead->len +
967 crp->crp_callback = cryptodev_cb;
968 crp->crp_opaque = cod;
972 * Permit a 16-byte IV for AES-XTS, but only use the
973 * first 8 bytes as a block number.
975 if (csp->csp_mode == CSP_MODE_ETA &&
976 csp->csp_cipher_alg == CRYPTO_AES_XTS &&
977 caead->ivlen == AES_BLOCK_LEN)
978 caead->ivlen = AES_XTS_IV_LEN;
980 if (cse->ivsize == 0) {
981 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
985 if (caead->ivlen != cse->ivsize) {
987 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
991 error = copyin(caead->iv, crp->crp_iv, cse->ivsize);
993 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
996 crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
999 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1003 if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
1004 error = copyin(caead->tag, cod->buf + crp->crp_digest_start,
1007 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1013 * Let the dispatch run unlocked, then, interlock against the
1014 * callback before checking if the operation completed and going
1015 * to sleep. This insures drivers don't inherit our lock which
1016 * results in a lock order reversal between crypto_dispatch forced
1017 * entry and the crypto_done callback into us.
1019 error = crypto_dispatch(crp);
1021 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1025 mtx_lock(&cse->lock);
1027 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0);
1028 mtx_unlock(&cse->lock);
1030 if (crp->crp_etype == EAGAIN) {
1032 crp->crp_flags &= ~CRYPTO_F_DONE;
1037 if (crp->crp_etype != 0) {
1038 error = crp->crp_etype;
1039 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1043 if (caead->dst != NULL) {
1044 error = copyout(cod->obuf != NULL ? cod->obuf :
1045 cod->buf + crp->crp_payload_start, dst,
1046 crp->crp_payload_length);
1048 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1053 if ((crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) == 0) {
1054 error = copyout((cod->obuf != NULL ? cod->obuf : cod->buf) +
1055 crp->crp_digest_start, caead->tag, cse->hashsize);
1057 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1063 crypto_freereq(crp);
1070 cryptodev_find(struct crypt_find_op *find)
1073 size_t fnlen = sizeof find->name;
1075 if (find->crid != -1) {
1076 dev = crypto_find_device_byhid(find->crid);
1079 strncpy(find->name, device_get_nameunit(dev), fnlen);
1080 find->name[fnlen - 1] = '\x0';
1082 find->name[fnlen - 1] = '\x0';
1083 find->crid = crypto_find_driver(find->name);
1084 if (find->crid == -1)
1091 fcrypt_dtor(void *data)
1093 struct fcrypt *fcr = data;
1094 struct csession *cse;
1096 while ((cse = TAILQ_FIRST(&fcr->csessions))) {
1097 TAILQ_REMOVE(&fcr->csessions, cse, next);
1098 KASSERT(refcount_load(&cse->refs) == 1,
1099 ("%s: crypto session %p with %d refs", __func__, cse,
1100 refcount_load(&cse->refs)));
1103 mtx_destroy(&fcr->lock);
1104 free(fcr, M_CRYPTODEV);
1108 crypto_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
1113 fcr = malloc(sizeof(struct fcrypt), M_CRYPTODEV, M_WAITOK | M_ZERO);
1114 TAILQ_INIT(&fcr->csessions);
1115 mtx_init(&fcr->lock, "fcrypt", NULL, MTX_DEF);
1116 error = devfs_set_cdevpriv(fcr, fcrypt_dtor);
1123 crypto_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
1127 struct csession *cse;
1128 struct session2_op *sop;
1129 struct crypt_op *cop;
1130 struct crypt_aead *caead;
1134 struct session2_op sopc;
1135 #ifdef COMPAT_FREEBSD32
1136 struct crypt_op copc;
1137 struct crypt_aead aeadc;
1140 #ifdef COMPAT_FREEBSD32
1147 case CIOCGSESSION32:
1151 data = (void *)&thunk.sopc;
1152 session_op_from_32((struct session_op32 *)data32, &thunk.sopc);
1154 case CIOCGSESSION232:
1157 cmd = CIOCGSESSION2;
1158 data = (void *)&thunk.sopc;
1159 session2_op_from_32((struct session2_op32 *)data32,
1166 data = (void *)&thunk.copc;
1167 crypt_op_from_32((struct crypt_op32 *)data32, &thunk.copc);
1169 case CIOCCRYPTAEAD32:
1172 cmd = CIOCCRYPTAEAD;
1173 data = (void *)&thunk.aeadc;
1174 crypt_aead_from_32((struct crypt_aead32 *)data32, &thunk.aeadc);
1179 devfs_get_cdevpriv((void **)&fcr);
1182 #ifdef COMPAT_FREEBSD12
1185 * NB: This may fail in cases that the old
1186 * implementation did not if the current process has
1187 * restricted filesystem access (e.g. running in a
1188 * jail that does not expose /dev/crypto or in
1191 error = kern_openat(td, AT_FDCWD, "/dev/crypto", UIO_SYSSPACE,
1194 *(uint32_t *)data = td->td_retval[0];
1199 if (cmd == CIOCGSESSION) {
1200 session2_op_from_op((void *)data, &thunk.sopc);
1203 sop = (struct session2_op *)data;
1205 error = cse_create(fcr, sop);
1206 if (cmd == CIOCGSESSION && error == 0)
1207 session2_op_to_op(sop, (void *)data);
1210 ses = *(uint32_t *)data;
1211 if (!cse_delete(fcr, ses)) {
1212 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1217 cop = (struct crypt_op *)data;
1218 cse = cse_find(fcr, cop->ses);
1220 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1223 error = cryptodev_op(cse, cop);
1227 error = cryptodev_find((struct crypt_find_op *)data);
1230 caead = (struct crypt_aead *)data;
1231 cse = cse_find(fcr, caead->ses);
1233 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1236 error = cryptodev_aead(cse, caead);
1241 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1245 #ifdef COMPAT_FREEBSD32
1247 case CIOCGSESSION32:
1249 session_op_to_32((void *)data, data32);
1251 case CIOCGSESSION232:
1253 session2_op_to_32((void *)data, data32);
1257 crypt_op_to_32((void *)data, data32);
1259 case CIOCCRYPTAEAD32:
1261 crypt_aead_to_32((void *)data, data32);
1268 static struct cdevsw crypto_cdevsw = {
1269 .d_version = D_VERSION,
1270 .d_open = crypto_open,
1271 .d_ioctl = crypto_ioctl,
1274 static struct cdev *crypto_dev;
1277 * Initialization code, both for static and dynamic loading.
1280 cryptodev_modevent(module_t mod, int type, void *unused)
1285 printf("crypto: <crypto device>\n");
1286 crypto_dev = make_dev(&crypto_cdevsw, 0,
1287 UID_ROOT, GID_WHEEL, 0666,
1291 /*XXX disallow if active sessions */
1292 destroy_dev(crypto_dev);
1298 static moduledata_t cryptodev_mod = {
1303 MODULE_VERSION(cryptodev, 1);
1304 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1305 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
1306 MODULE_DEPEND(cryptodev, zlib, 1, 1, 1);