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 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/malloc.h>
50 #include <sys/mutex.h>
52 #include <sys/sysctl.h>
53 #include <sys/errno.h>
54 #include <sys/random.h>
56 #include <sys/kernel.h>
57 #include <sys/module.h>
58 #include <sys/fcntl.h>
61 #include <sys/syscallsubr.h>
63 #include <opencrypto/cryptodev.h>
64 #include <opencrypto/xform.h>
66 SDT_PROVIDER_DECLARE(opencrypto);
68 SDT_PROBE_DEFINE1(opencrypto, dev, ioctl, error, "int"/*line number*/);
70 #ifdef COMPAT_FREEBSD12
72 * Previously, most ioctls were performed against a cloned descriptor
73 * of /dev/crypto obtained via CRIOGET. Now all ioctls are performed
74 * against /dev/crypto directly.
76 #define CRIOGET _IOWR('c', 100, uint32_t)
79 /* the following are done against the cloned descriptor */
81 #ifdef COMPAT_FREEBSD32
82 #include <sys/mount.h>
83 #include <compat/freebsd32/freebsd32.h>
95 struct session2_op32 {
119 struct crypt_aead32 {
133 #define CIOCGSESSION32 _IOWR('c', 101, struct session_op32)
134 #define CIOCCRYPT32 _IOWR('c', 103, struct crypt_op32)
135 #define CIOCGSESSION232 _IOWR('c', 106, struct session2_op32)
136 #define CIOCCRYPTAEAD32 _IOWR('c', 109, struct crypt_aead32)
139 session_op_from_32(const struct session_op32 *from, struct session2_op *to)
142 memset(to, 0, sizeof(*to));
143 CP(*from, *to, cipher);
145 CP(*from, *to, keylen);
146 PTRIN_CP(*from, *to, key);
147 CP(*from, *to, mackeylen);
148 PTRIN_CP(*from, *to, mackey);
150 to->crid = CRYPTOCAP_F_HARDWARE;
154 session2_op_from_32(const struct session2_op32 *from, struct session2_op *to)
157 session_op_from_32((const struct session_op32 *)from, to);
158 CP(*from, *to, crid);
159 CP(*from, *to, ivlen);
160 CP(*from, *to, maclen);
164 session_op_to_32(const struct session2_op *from, struct session_op32 *to)
167 CP(*from, *to, cipher);
169 CP(*from, *to, keylen);
170 PTROUT_CP(*from, *to, key);
171 CP(*from, *to, mackeylen);
172 PTROUT_CP(*from, *to, mackey);
177 session2_op_to_32(const struct session2_op *from, struct session2_op32 *to)
180 session_op_to_32(from, (struct session_op32 *)to);
181 CP(*from, *to, crid);
185 crypt_op_from_32(const struct crypt_op32 *from, struct crypt_op *to)
190 CP(*from, *to, flags);
192 PTRIN_CP(*from, *to, src);
193 PTRIN_CP(*from, *to, dst);
194 PTRIN_CP(*from, *to, mac);
195 PTRIN_CP(*from, *to, iv);
199 crypt_op_to_32(const struct crypt_op *from, struct crypt_op32 *to)
204 CP(*from, *to, flags);
206 PTROUT_CP(*from, *to, src);
207 PTROUT_CP(*from, *to, dst);
208 PTROUT_CP(*from, *to, mac);
209 PTROUT_CP(*from, *to, iv);
213 crypt_aead_from_32(const struct crypt_aead32 *from, struct crypt_aead *to)
218 CP(*from, *to, flags);
220 CP(*from, *to, aadlen);
221 CP(*from, *to, ivlen);
222 PTRIN_CP(*from, *to, src);
223 PTRIN_CP(*from, *to, dst);
224 PTRIN_CP(*from, *to, aad);
225 PTRIN_CP(*from, *to, tag);
226 PTRIN_CP(*from, *to, iv);
230 crypt_aead_to_32(const struct crypt_aead *from, struct crypt_aead32 *to)
235 CP(*from, *to, flags);
237 CP(*from, *to, aadlen);
238 CP(*from, *to, ivlen);
239 PTROUT_CP(*from, *to, src);
240 PTROUT_CP(*from, *to, dst);
241 PTROUT_CP(*from, *to, aad);
242 PTROUT_CP(*from, *to, tag);
243 PTROUT_CP(*from, *to, iv);
248 session2_op_from_op(const struct session_op *from, struct session2_op *to)
251 memset(to, 0, sizeof(*to));
252 memcpy(to, from, sizeof(*from));
253 to->crid = CRYPTOCAP_F_HARDWARE;
257 session2_op_to_op(const struct session2_op *from, struct session_op *to)
260 memcpy(to, from, sizeof(*to));
264 TAILQ_ENTRY(csession) next;
265 crypto_session_t cses;
268 struct mtx lock; /* for op submission */
278 struct cryptop_data {
279 struct csession *cse;
288 TAILQ_HEAD(csessionlist, csession) csessions;
293 static bool use_outputbuffers;
294 SYSCTL_BOOL(_kern_crypto, OID_AUTO, cryptodev_use_output, CTLFLAG_RW,
295 &use_outputbuffers, 0,
296 "Use separate output buffers for /dev/crypto requests.");
298 static bool use_separate_aad;
299 SYSCTL_BOOL(_kern_crypto, OID_AUTO, cryptodev_separate_aad, CTLFLAG_RW,
300 &use_separate_aad, 0,
301 "Use separate AAD buffer for /dev/crypto requests.");
303 static MALLOC_DEFINE(M_CRYPTODEV, "cryptodev", "/dev/crypto data buffers");
306 * Check a crypto identifier to see if it requested
307 * a software device/driver. This can be done either
308 * by device name/class or through search constraints.
311 checkforsoftware(int *cridp)
317 if (!crypto_devallowsoft) {
318 if (crid & CRYPTOCAP_F_SOFTWARE) {
319 if (crid & CRYPTOCAP_F_HARDWARE) {
320 *cridp = CRYPTOCAP_F_HARDWARE;
325 if ((crid & CRYPTOCAP_F_HARDWARE) == 0 &&
326 (crypto_getcaps(crid) & CRYPTOCAP_F_HARDWARE) == 0)
333 cse_create(struct fcrypt *fcr, struct session2_op *sop)
335 struct crypto_session_params csp;
336 struct csession *cse;
337 const struct enc_xform *txform;
338 const struct auth_hash *thash;
341 crypto_session_t cses;
342 int crid, error, mac;
345 #ifdef COMPAT_FREEBSD12
347 case CRYPTO_AES_128_NIST_GMAC:
348 case CRYPTO_AES_192_NIST_GMAC:
349 case CRYPTO_AES_256_NIST_GMAC:
350 /* Should always be paired with GCM. */
351 if (sop->cipher != CRYPTO_AES_NIST_GCM_16) {
352 CRYPTDEB("GMAC without GCM");
353 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
356 if (sop->keylen != sop->mackeylen) {
357 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
362 case CRYPTO_AES_CCM_CBC_MAC:
363 /* Should always be paired with CCM. */
364 if (sop->cipher != CRYPTO_AES_CCM_16) {
365 CRYPTDEB("CBC-MAC without CCM");
366 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
369 if (sop->keylen != sop->mackeylen) {
370 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
378 memset(&csp, 0, sizeof(csp));
379 if (use_outputbuffers)
380 csp.csp_flags |= CSP_F_SEPARATE_OUTPUT;
382 csp.csp_auth_alg = mac;
383 csp.csp_auth_klen = sop->mackeylen;
385 if (sop->cipher != 0) {
386 csp.csp_cipher_alg = sop->cipher;
387 csp.csp_cipher_klen = sop->keylen;
389 thash = crypto_auth_hash(&csp);
390 txform = crypto_cipher(&csp);
392 if (txform != NULL && txform->macsize != 0) {
394 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
397 csp.csp_mode = CSP_MODE_AEAD;
398 } else if (txform != NULL && thash != NULL) {
399 csp.csp_mode = CSP_MODE_ETA;
400 } else if (txform != NULL) {
401 csp.csp_mode = CSP_MODE_CIPHER;
402 } else if (thash != NULL) {
403 csp.csp_mode = CSP_MODE_DIGEST;
405 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
409 switch (csp.csp_mode) {
412 if (use_separate_aad)
413 csp.csp_flags |= CSP_F_SEPARATE_AAD;
417 if (txform != NULL) {
418 if (sop->keylen > txform->maxkey ||
419 sop->keylen < txform->minkey) {
420 CRYPTDEB("invalid cipher parameters");
422 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
426 key = malloc(csp.csp_cipher_klen, M_CRYPTODEV, M_WAITOK);
427 error = copyin(sop->key, key, csp.csp_cipher_klen);
429 CRYPTDEB("invalid key");
430 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
433 csp.csp_cipher_key = key;
434 csp.csp_ivlen = txform->ivsize;
438 if (sop->mackeylen > thash->keysize || sop->mackeylen < 0) {
439 CRYPTDEB("invalid mac key length");
441 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
445 if (csp.csp_auth_klen != 0) {
446 mackey = malloc(csp.csp_auth_klen, M_CRYPTODEV,
448 error = copyin(sop->mackey, mackey, csp.csp_auth_klen);
450 CRYPTDEB("invalid mac key");
451 SDT_PROBE1(opencrypto, dev, ioctl, error,
455 csp.csp_auth_key = mackey;
458 if (csp.csp_auth_alg == CRYPTO_AES_NIST_GMAC)
459 csp.csp_ivlen = AES_GCM_IV_LEN;
460 if (csp.csp_auth_alg == CRYPTO_AES_CCM_CBC_MAC)
461 csp.csp_ivlen = AES_CCM_IV_LEN;
464 if (sop->ivlen != 0) {
465 if (csp.csp_ivlen == 0) {
466 CRYPTDEB("does not support an IV");
468 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
471 csp.csp_ivlen = sop->ivlen;
473 if (sop->maclen != 0) {
474 if (!(thash != NULL || csp.csp_mode == CSP_MODE_AEAD)) {
475 CRYPTDEB("does not support a MAC");
477 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
480 csp.csp_auth_mlen = sop->maclen;
484 error = checkforsoftware(&crid);
486 CRYPTDEB("checkforsoftware");
487 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
490 error = crypto_newsession(&cses, &csp, crid);
492 CRYPTDEB("crypto_newsession");
493 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
497 cse = malloc(sizeof(struct csession), M_CRYPTODEV, M_WAITOK | M_ZERO);
498 mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF);
499 refcount_init(&cse->refs, 1);
501 cse->mackey = mackey;
503 if (sop->maclen != 0)
504 cse->hashsize = sop->maclen;
505 else if (thash != NULL)
506 cse->hashsize = thash->hashsize;
507 else if (csp.csp_mode == CSP_MODE_AEAD)
508 cse->hashsize = txform->macsize;
509 cse->ivsize = csp.csp_ivlen;
512 * NB: This isn't necessarily the block size of the underlying
513 * MAC or cipher but is instead a restriction on valid input
517 cse->blocksize = txform->blocksize;
521 mtx_lock(&fcr->lock);
522 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
523 cse->ses = fcr->sesn++;
524 mtx_unlock(&fcr->lock);
528 /* return hardware/driver id */
529 sop->crid = crypto_ses2hid(cse->cses);
532 free(key, M_CRYPTODEV);
533 free(mackey, M_CRYPTODEV);
538 static struct csession *
539 cse_find(struct fcrypt *fcr, u_int ses)
541 struct csession *cse;
543 mtx_lock(&fcr->lock);
544 TAILQ_FOREACH(cse, &fcr->csessions, next) {
545 if (cse->ses == ses) {
546 refcount_acquire(&cse->refs);
547 mtx_unlock(&fcr->lock);
551 mtx_unlock(&fcr->lock);
556 cse_free(struct csession *cse)
559 if (!refcount_release(&cse->refs))
561 crypto_freesession(cse->cses);
562 mtx_destroy(&cse->lock);
564 free(cse->key, M_CRYPTODEV);
566 free(cse->mackey, M_CRYPTODEV);
567 free(cse, M_CRYPTODEV);
571 cse_delete(struct fcrypt *fcr, u_int ses)
573 struct csession *cse;
575 mtx_lock(&fcr->lock);
576 TAILQ_FOREACH(cse, &fcr->csessions, next) {
577 if (cse->ses == ses) {
578 TAILQ_REMOVE(&fcr->csessions, cse, next);
579 mtx_unlock(&fcr->lock);
584 mtx_unlock(&fcr->lock);
588 static struct cryptop_data *
589 cod_alloc(struct csession *cse, size_t aad_len, size_t len)
591 struct cryptop_data *cod;
593 cod = malloc(sizeof(struct cryptop_data), M_CRYPTODEV, M_WAITOK |
597 if (crypto_get_params(cse->cses)->csp_flags & CSP_F_SEPARATE_AAD) {
599 cod->aad = malloc(aad_len, M_CRYPTODEV, M_WAITOK);
600 cod->buf = malloc(len, M_CRYPTODEV, M_WAITOK);
602 cod->buf = malloc(aad_len + len, M_CRYPTODEV, M_WAITOK);
603 if (crypto_get_params(cse->cses)->csp_flags & CSP_F_SEPARATE_OUTPUT)
604 cod->obuf = malloc(len, M_CRYPTODEV, M_WAITOK);
609 cod_free(struct cryptop_data *cod)
612 free(cod->aad, M_CRYPTODEV);
613 free(cod->obuf, M_CRYPTODEV);
614 free(cod->buf, M_CRYPTODEV);
615 free(cod, M_CRYPTODEV);
619 cryptodev_cb(struct cryptop *crp)
621 struct cryptop_data *cod = crp->crp_opaque;
624 * Lock to ensure the wakeup() is not missed by the loops
625 * waiting on cod->done in cryptodev_op() and
628 mtx_lock(&cod->cse->lock);
630 mtx_unlock(&cod->cse->lock);
636 cryptodev_op(struct csession *cse, const struct crypt_op *cop)
638 const struct crypto_session_params *csp;
639 struct cryptop_data *cod = NULL;
640 struct cryptop *crp = NULL;
644 if (cop->len > 256*1024-4) {
645 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
649 if ((cop->len % cse->blocksize) != 0) {
650 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
654 if (cop->mac && cse->hashsize == 0) {
655 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
660 * The COP_F_CIPHER_FIRST flag predates explicit session
661 * modes, but the only way it was used was for EtA so allow it
662 * as long as it is consistent with EtA.
664 if (cop->flags & COP_F_CIPHER_FIRST) {
665 if (cop->op != COP_ENCRYPT) {
666 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
671 cod = cod_alloc(cse, 0, cop->len + cse->hashsize);
674 crp = crypto_getreq(cse->cses, M_WAITOK);
676 error = copyin(cop->src, cod->buf, cop->len);
678 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
681 crp->crp_payload_start = 0;
682 crp->crp_payload_length = cop->len;
684 crp->crp_digest_start = cop->len;
686 csp = crypto_get_params(cse->cses);
687 switch (csp->csp_mode) {
688 case CSP_MODE_COMPRESS:
691 crp->crp_op = CRYPTO_OP_COMPRESS;
694 crp->crp_op = CRYPTO_OP_DECOMPRESS;
697 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
702 case CSP_MODE_CIPHER:
704 (cop->iv == NULL && cop->len == cse->ivsize)) {
705 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
711 crp->crp_op = CRYPTO_OP_ENCRYPT;
714 crp->crp_op = CRYPTO_OP_DECRYPT;
717 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
722 case CSP_MODE_DIGEST:
727 crp->crp_op = CRYPTO_OP_COMPUTE_DIGEST;
728 if (cod->obuf != NULL)
729 crp->crp_digest_start = 0;
732 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
738 if (cse->ivsize != 0 && cop->iv == NULL) {
739 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
747 crp->crp_op = CRYPTO_OP_ENCRYPT |
748 CRYPTO_OP_COMPUTE_DIGEST;
751 crp->crp_op = CRYPTO_OP_DECRYPT |
752 CRYPTO_OP_VERIFY_DIGEST;
755 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
761 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
766 crp->crp_flags = CRYPTO_F_CBIMM | (cop->flags & COP_F_BATCH);
767 crypto_use_buf(crp, cod->buf, cop->len + cse->hashsize);
769 crypto_use_output_buf(crp, cod->obuf, cop->len + cse->hashsize);
770 crp->crp_callback = cryptodev_cb;
771 crp->crp_opaque = cod;
774 if (cse->ivsize == 0) {
775 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
779 error = copyin(cop->iv, crp->crp_iv, cse->ivsize);
781 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
784 crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
785 } else if (cse->ivsize != 0) {
786 if (crp->crp_payload_length < cse->ivsize) {
787 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
791 crp->crp_iv_start = 0;
792 crp->crp_payload_length -= cse->ivsize;
793 if (crp->crp_payload_length != 0)
794 crp->crp_payload_start = cse->ivsize;
798 if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
799 error = copyin(cop->mac, cod->buf + crp->crp_digest_start,
802 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
808 * Let the dispatch run unlocked, then, interlock against the
809 * callback before checking if the operation completed and going
810 * to sleep. This insures drivers don't inherit our lock which
811 * results in a lock order reversal between crypto_dispatch forced
812 * entry and the crypto_done callback into us.
814 error = crypto_dispatch(crp);
816 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
820 mtx_lock(&cse->lock);
822 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0);
823 mtx_unlock(&cse->lock);
825 if (crp->crp_etype == EAGAIN) {
827 crp->crp_flags &= ~CRYPTO_F_DONE;
832 if (crp->crp_etype != 0) {
833 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
834 error = crp->crp_etype;
838 if (cop->dst != NULL) {
839 error = copyout(cod->obuf != NULL ? cod->obuf :
840 cod->buf + crp->crp_payload_start, dst,
841 crp->crp_payload_length);
843 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
848 if (cop->mac != NULL && (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) == 0) {
849 error = copyout((cod->obuf != NULL ? cod->obuf : cod->buf) +
850 crp->crp_digest_start, cop->mac, cse->hashsize);
852 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
865 cryptodev_aead(struct csession *cse, struct crypt_aead *caead)
867 const struct crypto_session_params *csp;
868 struct cryptop_data *cod = NULL;
869 struct cryptop *crp = NULL;
873 if (caead->len > 256*1024-4 || caead->aadlen > 256*1024-4) {
874 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
878 if ((caead->len % cse->blocksize) != 0) {
879 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
883 if (cse->hashsize == 0 || caead->tag == NULL) {
884 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
889 * The COP_F_CIPHER_FIRST flag predates explicit session
890 * modes, but the only way it was used was for EtA so allow it
891 * as long as it is consistent with EtA.
893 if (caead->flags & COP_F_CIPHER_FIRST) {
894 if (caead->op != COP_ENCRYPT) {
895 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
900 cod = cod_alloc(cse, caead->aadlen, caead->len + cse->hashsize);
903 crp = crypto_getreq(cse->cses, M_WAITOK);
905 if (cod->aad != NULL)
906 error = copyin(caead->aad, cod->aad, caead->aadlen);
908 error = copyin(caead->aad, cod->buf, caead->aadlen);
910 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
913 crp->crp_aad = cod->aad;
914 crp->crp_aad_start = 0;
915 crp->crp_aad_length = caead->aadlen;
917 if (cod->aad != NULL)
918 crp->crp_payload_start = 0;
920 crp->crp_payload_start = caead->aadlen;
921 error = copyin(caead->src, cod->buf + crp->crp_payload_start,
924 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
927 crp->crp_payload_length = caead->len;
928 if (caead->op == COP_ENCRYPT && cod->obuf != NULL)
929 crp->crp_digest_start = crp->crp_payload_output_start +
932 crp->crp_digest_start = crp->crp_payload_start + caead->len;
934 csp = crypto_get_params(cse->cses);
935 switch (csp->csp_mode) {
940 crp->crp_op = CRYPTO_OP_ENCRYPT |
941 CRYPTO_OP_COMPUTE_DIGEST;
944 crp->crp_op = CRYPTO_OP_DECRYPT |
945 CRYPTO_OP_VERIFY_DIGEST;
948 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
954 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
959 crp->crp_flags = CRYPTO_F_CBIMM | (caead->flags & COP_F_BATCH);
960 crypto_use_buf(crp, cod->buf, crp->crp_payload_start + caead->len +
962 if (cod->obuf != NULL)
963 crypto_use_output_buf(crp, cod->obuf, caead->len +
965 crp->crp_callback = cryptodev_cb;
966 crp->crp_opaque = cod;
970 * Permit a 16-byte IV for AES-XTS, but only use the
971 * first 8 bytes as a block number.
973 if (csp->csp_mode == CSP_MODE_ETA &&
974 csp->csp_cipher_alg == CRYPTO_AES_XTS &&
975 caead->ivlen == AES_BLOCK_LEN)
976 caead->ivlen = AES_XTS_IV_LEN;
978 if (cse->ivsize == 0) {
979 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
983 if (caead->ivlen != cse->ivsize) {
985 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
989 error = copyin(caead->iv, crp->crp_iv, cse->ivsize);
991 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
994 crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
997 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1001 if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
1002 error = copyin(caead->tag, cod->buf + crp->crp_digest_start,
1005 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1011 * Let the dispatch run unlocked, then, interlock against the
1012 * callback before checking if the operation completed and going
1013 * to sleep. This insures drivers don't inherit our lock which
1014 * results in a lock order reversal between crypto_dispatch forced
1015 * entry and the crypto_done callback into us.
1017 error = crypto_dispatch(crp);
1019 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1023 mtx_lock(&cse->lock);
1025 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0);
1026 mtx_unlock(&cse->lock);
1028 if (crp->crp_etype == EAGAIN) {
1030 crp->crp_flags &= ~CRYPTO_F_DONE;
1035 if (crp->crp_etype != 0) {
1036 error = crp->crp_etype;
1037 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1041 if (caead->dst != NULL) {
1042 error = copyout(cod->obuf != NULL ? cod->obuf :
1043 cod->buf + crp->crp_payload_start, dst,
1044 crp->crp_payload_length);
1046 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1051 if ((crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) == 0) {
1052 error = copyout((cod->obuf != NULL ? cod->obuf : cod->buf) +
1053 crp->crp_digest_start, caead->tag, cse->hashsize);
1055 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1061 crypto_freereq(crp);
1068 cryptodev_find(struct crypt_find_op *find)
1071 size_t fnlen = sizeof find->name;
1073 if (find->crid != -1) {
1074 dev = crypto_find_device_byhid(find->crid);
1077 strncpy(find->name, device_get_nameunit(dev), fnlen);
1078 find->name[fnlen - 1] = '\x0';
1080 find->name[fnlen - 1] = '\x0';
1081 find->crid = crypto_find_driver(find->name);
1082 if (find->crid == -1)
1089 fcrypt_dtor(void *data)
1091 struct fcrypt *fcr = data;
1092 struct csession *cse;
1094 while ((cse = TAILQ_FIRST(&fcr->csessions))) {
1095 TAILQ_REMOVE(&fcr->csessions, cse, next);
1096 KASSERT(refcount_load(&cse->refs) == 1,
1097 ("%s: crypto session %p with %d refs", __func__, cse,
1098 refcount_load(&cse->refs)));
1101 mtx_destroy(&fcr->lock);
1102 free(fcr, M_CRYPTODEV);
1106 crypto_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
1111 fcr = malloc(sizeof(struct fcrypt), M_CRYPTODEV, M_WAITOK | M_ZERO);
1112 TAILQ_INIT(&fcr->csessions);
1113 mtx_init(&fcr->lock, "fcrypt", NULL, MTX_DEF);
1114 error = devfs_set_cdevpriv(fcr, fcrypt_dtor);
1121 crypto_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
1125 struct csession *cse;
1126 struct session2_op *sop;
1127 struct crypt_op *cop;
1128 struct crypt_aead *caead;
1132 struct session2_op sopc;
1133 #ifdef COMPAT_FREEBSD32
1134 struct crypt_op copc;
1135 struct crypt_aead aeadc;
1138 #ifdef COMPAT_FREEBSD32
1145 case CIOCGSESSION32:
1149 data = (void *)&thunk.sopc;
1150 session_op_from_32((struct session_op32 *)data32, &thunk.sopc);
1152 case CIOCGSESSION232:
1155 cmd = CIOCGSESSION2;
1156 data = (void *)&thunk.sopc;
1157 session2_op_from_32((struct session2_op32 *)data32,
1164 data = (void *)&thunk.copc;
1165 crypt_op_from_32((struct crypt_op32 *)data32, &thunk.copc);
1167 case CIOCCRYPTAEAD32:
1170 cmd = CIOCCRYPTAEAD;
1171 data = (void *)&thunk.aeadc;
1172 crypt_aead_from_32((struct crypt_aead32 *)data32, &thunk.aeadc);
1177 devfs_get_cdevpriv((void **)&fcr);
1180 #ifdef COMPAT_FREEBSD12
1183 * NB: This may fail in cases that the old
1184 * implementation did not if the current process has
1185 * restricted filesystem access (e.g. running in a
1186 * jail that does not expose /dev/crypto or in
1189 error = kern_openat(td, AT_FDCWD, "/dev/crypto", UIO_SYSSPACE,
1192 *(uint32_t *)data = td->td_retval[0];
1197 if (cmd == CIOCGSESSION) {
1198 session2_op_from_op((void *)data, &thunk.sopc);
1201 sop = (struct session2_op *)data;
1203 error = cse_create(fcr, sop);
1204 if (cmd == CIOCGSESSION && error == 0)
1205 session2_op_to_op(sop, (void *)data);
1208 ses = *(uint32_t *)data;
1209 if (!cse_delete(fcr, ses)) {
1210 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1215 cop = (struct crypt_op *)data;
1216 cse = cse_find(fcr, cop->ses);
1218 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1221 error = cryptodev_op(cse, cop);
1225 error = cryptodev_find((struct crypt_find_op *)data);
1228 caead = (struct crypt_aead *)data;
1229 cse = cse_find(fcr, caead->ses);
1231 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1234 error = cryptodev_aead(cse, caead);
1239 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1243 #ifdef COMPAT_FREEBSD32
1245 case CIOCGSESSION32:
1247 session_op_to_32((void *)data, data32);
1249 case CIOCGSESSION232:
1251 session2_op_to_32((void *)data, data32);
1255 crypt_op_to_32((void *)data, data32);
1257 case CIOCCRYPTAEAD32:
1259 crypt_aead_to_32((void *)data, data32);
1266 static struct cdevsw crypto_cdevsw = {
1267 .d_version = D_VERSION,
1268 .d_open = crypto_open,
1269 .d_ioctl = crypto_ioctl,
1272 static struct cdev *crypto_dev;
1275 * Initialization code, both for static and dynamic loading.
1278 cryptodev_modevent(module_t mod, int type, void *unused)
1283 printf("crypto: <crypto device>\n");
1284 crypto_dev = make_dev(&crypto_cdevsw, 0,
1285 UID_ROOT, GID_WHEEL, 0666,
1289 /*XXX disallow if active sessions */
1290 destroy_dev(crypto_dev);
1296 static moduledata_t cryptodev_mod = {
1301 MODULE_VERSION(cryptodev, 1);
1302 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1303 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
1304 MODULE_DEPEND(cryptodev, zlib, 1, 1, 1);