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 {
146 struct crparam32 crk_param[CRK_MAXPARAM];
149 #define CIOCGSESSION32 _IOWR('c', 101, struct session_op32)
150 #define CIOCCRYPT32 _IOWR('c', 103, struct crypt_op32)
151 #define CIOCKEY32 _IOWR('c', 104, struct crypt_kop32)
152 #define CIOCGSESSION232 _IOWR('c', 106, struct session2_op32)
153 #define CIOCKEY232 _IOWR('c', 107, struct crypt_kop32)
154 #define CIOCCRYPTAEAD32 _IOWR('c', 109, struct crypt_aead32)
157 session_op_from_32(const struct session_op32 *from, struct session2_op *to)
160 memset(to, 0, sizeof(*to));
161 CP(*from, *to, cipher);
163 CP(*from, *to, keylen);
164 PTRIN_CP(*from, *to, key);
165 CP(*from, *to, mackeylen);
166 PTRIN_CP(*from, *to, mackey);
168 to->crid = CRYPTOCAP_F_HARDWARE;
172 session2_op_from_32(const struct session2_op32 *from, struct session2_op *to)
175 session_op_from_32((const struct session_op32 *)from, to);
176 CP(*from, *to, crid);
177 CP(*from, *to, ivlen);
178 CP(*from, *to, maclen);
182 session_op_to_32(const struct session2_op *from, struct session_op32 *to)
185 CP(*from, *to, cipher);
187 CP(*from, *to, keylen);
188 PTROUT_CP(*from, *to, key);
189 CP(*from, *to, mackeylen);
190 PTROUT_CP(*from, *to, mackey);
195 session2_op_to_32(const struct session2_op *from, struct session2_op32 *to)
198 session_op_to_32(from, (struct session_op32 *)to);
199 CP(*from, *to, crid);
203 crypt_op_from_32(const struct crypt_op32 *from, struct crypt_op *to)
208 CP(*from, *to, flags);
210 PTRIN_CP(*from, *to, src);
211 PTRIN_CP(*from, *to, dst);
212 PTRIN_CP(*from, *to, mac);
213 PTRIN_CP(*from, *to, iv);
217 crypt_op_to_32(const struct crypt_op *from, struct crypt_op32 *to)
222 CP(*from, *to, flags);
224 PTROUT_CP(*from, *to, src);
225 PTROUT_CP(*from, *to, dst);
226 PTROUT_CP(*from, *to, mac);
227 PTROUT_CP(*from, *to, iv);
231 crypt_aead_from_32(const struct crypt_aead32 *from, struct crypt_aead *to)
236 CP(*from, *to, flags);
238 CP(*from, *to, aadlen);
239 CP(*from, *to, ivlen);
240 PTRIN_CP(*from, *to, src);
241 PTRIN_CP(*from, *to, dst);
242 PTRIN_CP(*from, *to, aad);
243 PTRIN_CP(*from, *to, tag);
244 PTRIN_CP(*from, *to, iv);
248 crypt_aead_to_32(const struct crypt_aead *from, struct crypt_aead32 *to)
253 CP(*from, *to, flags);
255 CP(*from, *to, aadlen);
256 CP(*from, *to, ivlen);
257 PTROUT_CP(*from, *to, src);
258 PTROUT_CP(*from, *to, dst);
259 PTROUT_CP(*from, *to, aad);
260 PTROUT_CP(*from, *to, tag);
261 PTROUT_CP(*from, *to, iv);
265 crparam_from_32(const struct crparam32 *from, struct crparam *to)
268 PTRIN_CP(*from, *to, crp_p);
269 CP(*from, *to, crp_nbits);
273 crparam_to_32(const struct crparam *from, struct crparam32 *to)
276 PTROUT_CP(*from, *to, crp_p);
277 CP(*from, *to, crp_nbits);
281 crypt_kop_from_32(const struct crypt_kop32 *from, struct crypt_kop *to)
285 CP(*from, *to, crk_op);
286 CP(*from, *to, crk_status);
287 CP(*from, *to, crk_iparams);
288 CP(*from, *to, crk_oparams);
289 CP(*from, *to, crk_crid);
290 for (i = 0; i < CRK_MAXPARAM; i++)
291 crparam_from_32(&from->crk_param[i], &to->crk_param[i]);
295 crypt_kop_to_32(const struct crypt_kop *from, struct crypt_kop32 *to)
299 CP(*from, *to, crk_op);
300 CP(*from, *to, crk_status);
301 CP(*from, *to, crk_iparams);
302 CP(*from, *to, crk_oparams);
303 CP(*from, *to, crk_crid);
304 for (i = 0; i < CRK_MAXPARAM; i++)
305 crparam_to_32(&from->crk_param[i], &to->crk_param[i]);
310 session2_op_from_op(const struct session_op *from, struct session2_op *to)
313 memset(to, 0, sizeof(*to));
314 memcpy(to, from, sizeof(*from));
315 to->crid = CRYPTOCAP_F_HARDWARE;
319 session2_op_to_op(const struct session2_op *from, struct session_op *to)
322 memcpy(to, from, sizeof(*to));
326 TAILQ_ENTRY(csession) next;
327 crypto_session_t cses;
330 struct mtx lock; /* for op submission */
332 struct enc_xform *txform;
340 struct cryptop_data {
341 struct csession *cse;
350 TAILQ_HEAD(csessionlist, csession) csessions;
355 static bool use_outputbuffers;
356 SYSCTL_BOOL(_kern_crypto, OID_AUTO, cryptodev_use_output, CTLFLAG_RW,
357 &use_outputbuffers, 0,
358 "Use separate output buffers for /dev/crypto requests.");
360 static bool use_separate_aad;
361 SYSCTL_BOOL(_kern_crypto, OID_AUTO, cryptodev_separate_aad, CTLFLAG_RW,
362 &use_separate_aad, 0,
363 "Use separate AAD buffer for /dev/crypto requests.");
365 static struct timeval warninterval = { .tv_sec = 60, .tv_usec = 0 };
366 SYSCTL_TIMEVAL_SEC(_kern, OID_AUTO, cryptodev_warn_interval, CTLFLAG_RW,
368 "Delay in seconds between warnings of deprecated /dev/crypto algorithms");
370 static MALLOC_DEFINE(M_CRYPTODEV, "cryptodev", "/dev/crypto data buffers");
373 * Check a crypto identifier to see if it requested
374 * a software device/driver. This can be done either
375 * by device name/class or through search constraints.
378 checkforsoftware(int *cridp)
384 if (!crypto_devallowsoft) {
385 if (crid & CRYPTOCAP_F_SOFTWARE) {
386 if (crid & CRYPTOCAP_F_HARDWARE) {
387 *cridp = CRYPTOCAP_F_HARDWARE;
392 if ((crid & CRYPTOCAP_F_HARDWARE) == 0 &&
393 (crypto_getcaps(crid) & CRYPTOCAP_F_HARDWARE) == 0)
400 cse_create(struct fcrypt *fcr, struct session2_op *sop)
402 struct crypto_session_params csp;
403 struct csession *cse;
404 struct enc_xform *txform;
405 struct auth_hash *thash;
408 crypto_session_t cses;
411 switch (sop->cipher) {
416 txform = &enc_xform_rijndael128;
419 txform = &enc_xform_aes_xts;
421 case CRYPTO_NULL_CBC:
422 txform = &enc_xform_null;
424 case CRYPTO_CAMELLIA_CBC:
425 txform = &enc_xform_camellia;
428 txform = &enc_xform_aes_icm;
430 case CRYPTO_AES_NIST_GCM_16:
431 txform = &enc_xform_aes_nist_gcm;
433 case CRYPTO_CHACHA20:
434 txform = &enc_xform_chacha20;
436 case CRYPTO_AES_CCM_16:
437 txform = &enc_xform_ccm;
439 case CRYPTO_CHACHA20_POLY1305:
440 txform = &enc_xform_chacha20_poly1305;
443 CRYPTDEB("invalid cipher");
444 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
452 case CRYPTO_POLY1305:
453 thash = &auth_hash_poly1305;
455 case CRYPTO_SHA1_HMAC:
456 thash = &auth_hash_hmac_sha1;
458 case CRYPTO_SHA2_224_HMAC:
459 thash = &auth_hash_hmac_sha2_224;
461 case CRYPTO_SHA2_256_HMAC:
462 thash = &auth_hash_hmac_sha2_256;
464 case CRYPTO_SHA2_384_HMAC:
465 thash = &auth_hash_hmac_sha2_384;
467 case CRYPTO_SHA2_512_HMAC:
468 thash = &auth_hash_hmac_sha2_512;
470 case CRYPTO_RIPEMD160_HMAC:
471 thash = &auth_hash_hmac_ripemd_160;
473 #ifdef COMPAT_FREEBSD12
474 case CRYPTO_AES_128_NIST_GMAC:
475 case CRYPTO_AES_192_NIST_GMAC:
476 case CRYPTO_AES_256_NIST_GMAC:
477 /* Should always be paired with GCM. */
478 if (sop->cipher != CRYPTO_AES_NIST_GCM_16) {
479 CRYPTDEB("GMAC without GCM");
480 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
485 case CRYPTO_AES_NIST_GMAC:
486 switch (sop->mackeylen * 8) {
488 thash = &auth_hash_nist_gmac_aes_128;
491 thash = &auth_hash_nist_gmac_aes_192;
494 thash = &auth_hash_nist_gmac_aes_256;
497 CRYPTDEB("invalid GMAC key length");
498 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
502 case CRYPTO_AES_CCM_CBC_MAC:
503 switch (sop->mackeylen) {
505 thash = &auth_hash_ccm_cbc_mac_128;
508 thash = &auth_hash_ccm_cbc_mac_192;
511 thash = &auth_hash_ccm_cbc_mac_256;
514 CRYPTDEB("Invalid CBC MAC key size %d", sop->keylen);
515 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
520 thash = &auth_hash_sha1;
522 case CRYPTO_SHA2_224:
523 thash = &auth_hash_sha2_224;
525 case CRYPTO_SHA2_256:
526 thash = &auth_hash_sha2_256;
528 case CRYPTO_SHA2_384:
529 thash = &auth_hash_sha2_384;
531 case CRYPTO_SHA2_512:
532 thash = &auth_hash_sha2_512;
535 case CRYPTO_NULL_HMAC:
536 thash = &auth_hash_null;
540 thash = &auth_hash_blake2b;
543 thash = &auth_hash_blake2s;
547 CRYPTDEB("invalid mac");
548 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
552 if (txform == NULL && thash == NULL) {
553 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
557 memset(&csp, 0, sizeof(csp));
558 if (use_outputbuffers)
559 csp.csp_flags |= CSP_F_SEPARATE_OUTPUT;
561 if (sop->cipher == CRYPTO_AES_NIST_GCM_16) {
563 #ifdef COMPAT_FREEBSD12
564 case CRYPTO_AES_128_NIST_GMAC:
565 case CRYPTO_AES_192_NIST_GMAC:
566 case CRYPTO_AES_256_NIST_GMAC:
567 if (sop->keylen != sop->mackeylen) {
568 SDT_PROBE1(opencrypto, dev, ioctl, error,
577 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
580 csp.csp_mode = CSP_MODE_AEAD;
581 } else if (sop->cipher == CRYPTO_AES_CCM_16) {
583 #ifdef COMPAT_FREEBSD12
584 case CRYPTO_AES_CCM_CBC_MAC:
585 if (sop->keylen != sop->mackeylen) {
586 SDT_PROBE1(opencrypto, dev, ioctl, error,
596 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
599 csp.csp_mode = CSP_MODE_AEAD;
600 } else if (sop->cipher == CRYPTO_CHACHA20_POLY1305) {
602 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
605 csp.csp_mode = CSP_MODE_AEAD;
606 } else if (txform != NULL && thash != NULL)
607 csp.csp_mode = CSP_MODE_ETA;
608 else if (txform != NULL)
609 csp.csp_mode = CSP_MODE_CIPHER;
611 csp.csp_mode = CSP_MODE_DIGEST;
613 switch (csp.csp_mode) {
616 if (use_separate_aad)
617 csp.csp_flags |= CSP_F_SEPARATE_AAD;
621 if (txform != NULL) {
622 csp.csp_cipher_alg = txform->type;
623 csp.csp_cipher_klen = sop->keylen;
624 if (sop->keylen > txform->maxkey ||
625 sop->keylen < txform->minkey) {
626 CRYPTDEB("invalid cipher parameters");
628 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
632 key = malloc(csp.csp_cipher_klen, M_CRYPTODEV, M_WAITOK);
633 error = copyin(sop->key, key, csp.csp_cipher_klen);
635 CRYPTDEB("invalid key");
636 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
639 csp.csp_cipher_key = key;
640 csp.csp_ivlen = txform->ivsize;
644 csp.csp_auth_alg = thash->type;
645 csp.csp_auth_klen = sop->mackeylen;
646 if (sop->mackeylen > thash->keysize || sop->mackeylen < 0) {
647 CRYPTDEB("invalid mac key length");
649 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
653 if (csp.csp_auth_klen != 0) {
654 mackey = malloc(csp.csp_auth_klen, M_CRYPTODEV,
656 error = copyin(sop->mackey, mackey, csp.csp_auth_klen);
658 CRYPTDEB("invalid mac key");
659 SDT_PROBE1(opencrypto, dev, ioctl, error,
663 csp.csp_auth_key = mackey;
666 if (csp.csp_auth_alg == CRYPTO_AES_NIST_GMAC)
667 csp.csp_ivlen = AES_GCM_IV_LEN;
668 if (csp.csp_auth_alg == CRYPTO_AES_CCM_CBC_MAC)
669 csp.csp_ivlen = AES_CCM_IV_LEN;
672 if (sop->ivlen != 0) {
673 if (csp.csp_ivlen == 0) {
674 CRYPTDEB("does not support an IV");
676 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
679 csp.csp_ivlen = sop->ivlen;
681 if (sop->maclen != 0) {
682 if (!(thash != NULL || csp.csp_mode == CSP_MODE_AEAD)) {
683 CRYPTDEB("does not support a MAC");
685 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
688 csp.csp_auth_mlen = sop->maclen;
692 error = checkforsoftware(&crid);
694 CRYPTDEB("checkforsoftware");
695 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
698 error = crypto_newsession(&cses, &csp, crid);
700 CRYPTDEB("crypto_newsession");
701 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
705 cse = malloc(sizeof(struct csession), M_CRYPTODEV, M_WAITOK | M_ZERO);
706 mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF);
707 refcount_init(&cse->refs, 1);
709 cse->mackey = mackey;
711 cse->txform = txform;
712 if (sop->maclen != 0)
713 cse->hashsize = sop->maclen;
714 else if (thash != NULL)
715 cse->hashsize = thash->hashsize;
716 else if (csp.csp_cipher_alg == CRYPTO_AES_NIST_GCM_16)
717 cse->hashsize = AES_GMAC_HASH_LEN;
718 else if (csp.csp_cipher_alg == CRYPTO_AES_CCM_16)
719 cse->hashsize = AES_CBC_MAC_HASH_LEN;
720 else if (csp.csp_cipher_alg == CRYPTO_CHACHA20_POLY1305)
721 cse->hashsize = POLY1305_HASH_LEN;
722 cse->ivsize = csp.csp_ivlen;
724 mtx_lock(&fcr->lock);
725 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
726 cse->ses = fcr->sesn++;
727 mtx_unlock(&fcr->lock);
731 /* return hardware/driver id */
732 sop->crid = crypto_ses2hid(cse->cses);
735 free(key, M_CRYPTODEV);
736 free(mackey, M_CRYPTODEV);
741 static struct csession *
742 cse_find(struct fcrypt *fcr, u_int ses)
744 struct csession *cse;
746 mtx_lock(&fcr->lock);
747 TAILQ_FOREACH(cse, &fcr->csessions, next) {
748 if (cse->ses == ses) {
749 refcount_acquire(&cse->refs);
750 mtx_unlock(&fcr->lock);
754 mtx_unlock(&fcr->lock);
759 cse_free(struct csession *cse)
762 if (!refcount_release(&cse->refs))
764 crypto_freesession(cse->cses);
765 mtx_destroy(&cse->lock);
767 free(cse->key, M_CRYPTODEV);
769 free(cse->mackey, M_CRYPTODEV);
770 free(cse, M_CRYPTODEV);
774 cse_delete(struct fcrypt *fcr, u_int ses)
776 struct csession *cse;
778 mtx_lock(&fcr->lock);
779 TAILQ_FOREACH(cse, &fcr->csessions, next) {
780 if (cse->ses == ses) {
781 TAILQ_REMOVE(&fcr->csessions, cse, next);
782 mtx_unlock(&fcr->lock);
787 mtx_unlock(&fcr->lock);
791 static struct cryptop_data *
792 cod_alloc(struct csession *cse, size_t aad_len, size_t len)
794 struct cryptop_data *cod;
796 cod = malloc(sizeof(struct cryptop_data), M_CRYPTODEV, M_WAITOK |
800 if (crypto_get_params(cse->cses)->csp_flags & CSP_F_SEPARATE_AAD) {
802 cod->aad = malloc(aad_len, M_CRYPTODEV, M_WAITOK);
803 cod->buf = malloc(len, M_CRYPTODEV, M_WAITOK);
805 cod->buf = malloc(aad_len + len, M_CRYPTODEV, M_WAITOK);
806 if (crypto_get_params(cse->cses)->csp_flags & CSP_F_SEPARATE_OUTPUT)
807 cod->obuf = malloc(len, M_CRYPTODEV, M_WAITOK);
812 cod_free(struct cryptop_data *cod)
815 free(cod->aad, M_CRYPTODEV);
816 free(cod->obuf, M_CRYPTODEV);
817 free(cod->buf, M_CRYPTODEV);
818 free(cod, M_CRYPTODEV);
822 cryptodev_cb(struct cryptop *crp)
824 struct cryptop_data *cod = crp->crp_opaque;
827 * Lock to ensure the wakeup() is not missed by the loops
828 * waiting on cod->done in cryptodev_op() and
831 mtx_lock(&cod->cse->lock);
833 mtx_unlock(&cod->cse->lock);
839 cryptodev_op(struct csession *cse, const struct crypt_op *cop)
841 const struct crypto_session_params *csp;
842 struct cryptop_data *cod = NULL;
843 struct cryptop *crp = NULL;
847 if (cop->len > 256*1024-4) {
848 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
853 if ((cop->len % cse->txform->blocksize) != 0) {
854 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
859 if (cop->mac && cse->hashsize == 0) {
860 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
865 * The COP_F_CIPHER_FIRST flag predates explicit session
866 * modes, but the only way it was used was for EtA so allow it
867 * as long as it is consistent with EtA.
869 if (cop->flags & COP_F_CIPHER_FIRST) {
870 if (cop->op != COP_ENCRYPT) {
871 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
876 cod = cod_alloc(cse, 0, cop->len + cse->hashsize);
879 crp = crypto_getreq(cse->cses, M_WAITOK);
881 error = copyin(cop->src, cod->buf, cop->len);
883 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
886 crp->crp_payload_start = 0;
887 crp->crp_payload_length = cop->len;
889 crp->crp_digest_start = cop->len;
891 csp = crypto_get_params(cse->cses);
892 switch (csp->csp_mode) {
893 case CSP_MODE_COMPRESS:
896 crp->crp_op = CRYPTO_OP_COMPRESS;
899 crp->crp_op = CRYPTO_OP_DECOMPRESS;
902 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
907 case CSP_MODE_CIPHER:
909 (cop->iv == NULL && cop->len == cse->ivsize)) {
910 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
916 crp->crp_op = CRYPTO_OP_ENCRYPT;
919 crp->crp_op = CRYPTO_OP_DECRYPT;
922 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
927 case CSP_MODE_DIGEST:
932 crp->crp_op = CRYPTO_OP_COMPUTE_DIGEST;
933 if (cod->obuf != NULL)
934 crp->crp_digest_start = 0;
937 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
943 if (cse->ivsize != 0 && cop->iv == NULL) {
944 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
952 crp->crp_op = CRYPTO_OP_ENCRYPT |
953 CRYPTO_OP_COMPUTE_DIGEST;
956 crp->crp_op = CRYPTO_OP_DECRYPT |
957 CRYPTO_OP_VERIFY_DIGEST;
960 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
966 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
971 crp->crp_flags = CRYPTO_F_CBIMM | (cop->flags & COP_F_BATCH);
972 crypto_use_buf(crp, cod->buf, cop->len + cse->hashsize);
974 crypto_use_output_buf(crp, cod->obuf, cop->len + cse->hashsize);
975 crp->crp_callback = cryptodev_cb;
976 crp->crp_opaque = cod;
979 if (cse->ivsize == 0) {
980 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
984 error = copyin(cop->iv, crp->crp_iv, cse->ivsize);
986 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
989 crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
990 } else if (cse->ivsize != 0) {
991 if (crp->crp_payload_length < cse->ivsize) {
992 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
996 crp->crp_iv_start = 0;
997 crp->crp_payload_length -= cse->ivsize;
998 if (crp->crp_payload_length != 0)
999 crp->crp_payload_start = cse->ivsize;
1003 if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
1004 error = copyin(cop->mac, 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 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1039 error = crp->crp_etype;
1043 if (cop->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 (cop->mac != NULL && (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) == 0) {
1054 error = copyout((cod->obuf != NULL ? cod->obuf : cod->buf) +
1055 crp->crp_digest_start, cop->mac, cse->hashsize);
1057 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1063 crypto_freereq(crp);
1070 cryptodev_aead(struct csession *cse, struct crypt_aead *caead)
1072 const struct crypto_session_params *csp;
1073 struct cryptop_data *cod = NULL;
1074 struct cryptop *crp = NULL;
1078 if (caead->len > 256*1024-4 || caead->aadlen > 256*1024-4) {
1079 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1083 if (cse->txform == NULL || cse->hashsize == 0 || caead->tag == NULL ||
1084 (caead->len % cse->txform->blocksize) != 0) {
1085 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1090 * The COP_F_CIPHER_FIRST flag predates explicit session
1091 * modes, but the only way it was used was for EtA so allow it
1092 * as long as it is consistent with EtA.
1094 if (caead->flags & COP_F_CIPHER_FIRST) {
1095 if (caead->op != COP_ENCRYPT) {
1096 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1101 cod = cod_alloc(cse, caead->aadlen, caead->len + cse->hashsize);
1104 crp = crypto_getreq(cse->cses, M_WAITOK);
1106 if (cod->aad != NULL)
1107 error = copyin(caead->aad, cod->aad, caead->aadlen);
1109 error = copyin(caead->aad, cod->buf, caead->aadlen);
1111 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1114 crp->crp_aad = cod->aad;
1115 crp->crp_aad_start = 0;
1116 crp->crp_aad_length = caead->aadlen;
1118 if (cod->aad != NULL)
1119 crp->crp_payload_start = 0;
1121 crp->crp_payload_start = caead->aadlen;
1122 error = copyin(caead->src, cod->buf + crp->crp_payload_start,
1125 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1128 crp->crp_payload_length = caead->len;
1129 if (caead->op == COP_ENCRYPT && cod->obuf != NULL)
1130 crp->crp_digest_start = crp->crp_payload_output_start +
1133 crp->crp_digest_start = crp->crp_payload_start + caead->len;
1135 csp = crypto_get_params(cse->cses);
1136 switch (csp->csp_mode) {
1139 switch (caead->op) {
1141 crp->crp_op = CRYPTO_OP_ENCRYPT |
1142 CRYPTO_OP_COMPUTE_DIGEST;
1145 crp->crp_op = CRYPTO_OP_DECRYPT |
1146 CRYPTO_OP_VERIFY_DIGEST;
1149 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1155 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1160 crp->crp_flags = CRYPTO_F_CBIMM | (caead->flags & COP_F_BATCH);
1161 crypto_use_buf(crp, cod->buf, crp->crp_payload_start + caead->len +
1163 if (cod->obuf != NULL)
1164 crypto_use_output_buf(crp, cod->obuf, caead->len +
1166 crp->crp_callback = cryptodev_cb;
1167 crp->crp_opaque = cod;
1171 * Permit a 16-byte IV for AES-XTS, but only use the
1172 * first 8 bytes as a block number.
1174 if (csp->csp_mode == CSP_MODE_ETA &&
1175 csp->csp_cipher_alg == CRYPTO_AES_XTS &&
1176 caead->ivlen == AES_BLOCK_LEN)
1177 caead->ivlen = AES_XTS_IV_LEN;
1179 if (cse->ivsize == 0) {
1180 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1184 if (caead->ivlen != cse->ivsize) {
1186 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1190 error = copyin(caead->iv, crp->crp_iv, cse->ivsize);
1192 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1195 crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
1198 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1202 if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
1203 error = copyin(caead->tag, cod->buf + crp->crp_digest_start,
1206 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1212 * Let the dispatch run unlocked, then, interlock against the
1213 * callback before checking if the operation completed and going
1214 * to sleep. This insures drivers don't inherit our lock which
1215 * results in a lock order reversal between crypto_dispatch forced
1216 * entry and the crypto_done callback into us.
1218 error = crypto_dispatch(crp);
1220 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1224 mtx_lock(&cse->lock);
1226 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0);
1227 mtx_unlock(&cse->lock);
1229 if (crp->crp_etype == EAGAIN) {
1231 crp->crp_flags &= ~CRYPTO_F_DONE;
1236 if (crp->crp_etype != 0) {
1237 error = crp->crp_etype;
1238 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1242 if (caead->dst != NULL) {
1243 error = copyout(cod->obuf != NULL ? cod->obuf :
1244 cod->buf + crp->crp_payload_start, dst,
1245 crp->crp_payload_length);
1247 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1252 if ((crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) == 0) {
1253 error = copyout((cod->obuf != NULL ? cod->obuf : cod->buf) +
1254 crp->crp_digest_start, caead->tag, cse->hashsize);
1256 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1262 crypto_freereq(crp);
1269 cryptodevkey_cb(struct cryptkop *krp)
1276 cryptodev_key(struct crypt_kop *kop)
1278 struct cryptkop *krp = NULL;
1280 int in, out, size, i;
1282 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
1283 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1287 in = kop->crk_iparams;
1288 out = kop->crk_oparams;
1289 switch (kop->crk_op) {
1291 if (in == 3 && out == 1)
1293 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1295 case CRK_MOD_EXP_CRT:
1296 if (in == 6 && out == 1)
1298 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1301 if (in == 5 && out == 2)
1303 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1305 case CRK_DSA_VERIFY:
1306 if (in == 7 && out == 0)
1308 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1310 case CRK_DH_COMPUTE_KEY:
1311 if (in == 3 && out == 1)
1313 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1316 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1320 krp = malloc(sizeof(*krp), M_CRYPTODEV, M_WAITOK | M_ZERO);
1321 krp->krp_op = kop->crk_op;
1322 krp->krp_status = kop->crk_status;
1323 krp->krp_iparams = kop->crk_iparams;
1324 krp->krp_oparams = kop->crk_oparams;
1325 krp->krp_crid = kop->crk_crid;
1326 krp->krp_status = 0;
1327 krp->krp_callback = cryptodevkey_cb;
1329 for (i = 0; i < CRK_MAXPARAM; i++) {
1330 if (kop->crk_param[i].crp_nbits > 65536) {
1331 /* Limit is the same as in OpenBSD */
1332 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1335 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
1337 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
1338 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1341 krp->krp_param[i].crp_p = malloc(size, M_CRYPTODEV, M_WAITOK);
1342 if (i >= krp->krp_iparams)
1344 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
1346 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1351 error = crypto_kdispatch(krp);
1353 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1356 error = tsleep(krp, PSOCK, "crydev", 0);
1358 /* XXX can this happen? if so, how do we recover? */
1359 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1363 kop->crk_crid = krp->krp_hid; /* device that did the work */
1364 if (krp->krp_status != 0) {
1365 error = krp->krp_status;
1366 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1370 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
1371 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1374 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
1376 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1383 kop->crk_status = krp->krp_status;
1384 for (i = 0; i < CRK_MAXPARAM; i++) {
1385 if (krp->krp_param[i].crp_p)
1386 free(krp->krp_param[i].crp_p, M_CRYPTODEV);
1388 free(krp, M_CRYPTODEV);
1394 cryptodev_find(struct crypt_find_op *find)
1397 size_t fnlen = sizeof find->name;
1399 if (find->crid != -1) {
1400 dev = crypto_find_device_byhid(find->crid);
1403 strncpy(find->name, device_get_nameunit(dev), fnlen);
1404 find->name[fnlen - 1] = '\x0';
1406 find->name[fnlen - 1] = '\x0';
1407 find->crid = crypto_find_driver(find->name);
1408 if (find->crid == -1)
1415 fcrypt_dtor(void *data)
1417 struct fcrypt *fcr = data;
1418 struct csession *cse;
1420 while ((cse = TAILQ_FIRST(&fcr->csessions))) {
1421 TAILQ_REMOVE(&fcr->csessions, cse, next);
1422 KASSERT(refcount_load(&cse->refs) == 1,
1423 ("%s: crypto session %p with %d refs", __func__, cse,
1424 refcount_load(&cse->refs)));
1427 mtx_destroy(&fcr->lock);
1428 free(fcr, M_CRYPTODEV);
1432 crypto_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
1437 fcr = malloc(sizeof(struct fcrypt), M_CRYPTODEV, M_WAITOK | M_ZERO);
1438 TAILQ_INIT(&fcr->csessions);
1439 mtx_init(&fcr->lock, "fcrypt", NULL, MTX_DEF);
1440 error = devfs_set_cdevpriv(fcr, fcrypt_dtor);
1447 crypto_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
1450 static struct timeval keywarn, featwarn;
1452 struct csession *cse;
1453 struct session2_op *sop;
1454 struct crypt_op *cop;
1455 struct crypt_aead *caead;
1456 struct crypt_kop *kop;
1460 struct session2_op sopc;
1461 #ifdef COMPAT_FREEBSD32
1462 struct crypt_op copc;
1463 struct crypt_aead aeadc;
1464 struct crypt_kop kopc;
1467 #ifdef COMPAT_FREEBSD32
1474 case CIOCGSESSION32:
1478 data = (void *)&thunk.sopc;
1479 session_op_from_32((struct session_op32 *)data32, &thunk.sopc);
1481 case CIOCGSESSION232:
1484 cmd = CIOCGSESSION2;
1485 data = (void *)&thunk.sopc;
1486 session2_op_from_32((struct session2_op32 *)data32,
1493 data = (void *)&thunk.copc;
1494 crypt_op_from_32((struct crypt_op32 *)data32, &thunk.copc);
1496 case CIOCCRYPTAEAD32:
1499 cmd = CIOCCRYPTAEAD;
1500 data = (void *)&thunk.aeadc;
1501 crypt_aead_from_32((struct crypt_aead32 *)data32, &thunk.aeadc);
1507 if (cmd == CIOCKEY32)
1511 data = (void *)&thunk.kopc;
1512 crypt_kop_from_32((struct crypt_kop32 *)data32, &thunk.kopc);
1517 devfs_get_cdevpriv((void **)&fcr);
1520 #ifdef COMPAT_FREEBSD12
1523 * NB: This may fail in cases that the old
1524 * implementation did not if the current process has
1525 * restricted filesystem access (e.g. running in a
1526 * jail that does not expose /dev/crypto or in
1529 error = kern_openat(td, AT_FDCWD, "/dev/crypto", UIO_SYSSPACE,
1532 *(uint32_t *)data = td->td_retval[0];
1537 if (cmd == CIOCGSESSION) {
1538 session2_op_from_op((void *)data, &thunk.sopc);
1541 sop = (struct session2_op *)data;
1543 error = cse_create(fcr, sop);
1544 if (cmd == CIOCGSESSION && error == 0)
1545 session2_op_to_op(sop, (void *)data);
1548 ses = *(uint32_t *)data;
1549 if (!cse_delete(fcr, ses)) {
1550 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1555 cop = (struct crypt_op *)data;
1556 cse = cse_find(fcr, cop->ses);
1558 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1561 error = cryptodev_op(cse, cop);
1566 if (ratecheck(&keywarn, &warninterval))
1568 "Asymmetric crypto operations via /dev/crypto");
1570 if (!crypto_userasymcrypto) {
1571 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1572 return (EPERM); /* XXX compat? */
1574 kop = (struct crypt_kop *)data;
1575 if (cmd == CIOCKEY) {
1576 /* NB: crypto core enforces s/w driver use */
1578 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
1581 error = cryptodev_key(kop);
1585 if (ratecheck(&featwarn, &warninterval))
1587 "Asymmetric crypto features via /dev/crypto");
1589 if (!crypto_userasymcrypto) {
1591 * NB: if user asym crypto operations are
1592 * not permitted return "no algorithms"
1593 * so well-behaved applications will just
1594 * fallback to doing them in software.
1598 error = crypto_getfeat((int *)data);
1600 SDT_PROBE1(opencrypto, dev, ioctl, error,
1605 error = cryptodev_find((struct crypt_find_op *)data);
1608 caead = (struct crypt_aead *)data;
1609 cse = cse_find(fcr, caead->ses);
1611 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1614 error = cryptodev_aead(cse, caead);
1619 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1623 #ifdef COMPAT_FREEBSD32
1625 case CIOCGSESSION32:
1627 session_op_to_32((void *)data, data32);
1629 case CIOCGSESSION232:
1631 session2_op_to_32((void *)data, data32);
1635 crypt_op_to_32((void *)data, data32);
1637 case CIOCCRYPTAEAD32:
1639 crypt_aead_to_32((void *)data, data32);
1643 crypt_kop_to_32((void *)data, data32);
1650 static struct cdevsw crypto_cdevsw = {
1651 .d_version = D_VERSION,
1652 .d_open = crypto_open,
1653 .d_ioctl = crypto_ioctl,
1656 static struct cdev *crypto_dev;
1659 * Initialization code, both for static and dynamic loading.
1662 cryptodev_modevent(module_t mod, int type, void *unused)
1667 printf("crypto: <crypto device>\n");
1668 crypto_dev = make_dev(&crypto_cdevsw, 0,
1669 UID_ROOT, GID_WHEEL, 0666,
1673 /*XXX disallow if active sessions */
1674 destroy_dev(crypto_dev);
1680 static moduledata_t cryptodev_mod = {
1685 MODULE_VERSION(cryptodev, 1);
1686 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1687 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
1688 MODULE_DEPEND(cryptodev, zlib, 1, 1, 1);