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 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 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. The name of the author may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Effort sponsored in part by the Defense Advanced Research Projects
37 * Agency (DARPA) and Air Force Research Laboratory, Air Force
38 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/malloc.h>
49 #include <sys/mutex.h>
51 #include <sys/sysctl.h>
52 #include <sys/errno.h>
53 #include <sys/random.h>
55 #include <sys/kernel.h>
56 #include <sys/module.h>
57 #include <sys/fcntl.h>
60 #include <sys/syscallsubr.h>
62 #include <opencrypto/cryptodev.h>
63 #include <opencrypto/xform.h>
65 SDT_PROVIDER_DECLARE(opencrypto);
67 SDT_PROBE_DEFINE1(opencrypto, dev, ioctl, error, "int"/*line number*/);
69 #ifdef COMPAT_FREEBSD12
71 * Previously, most ioctls were performed against a cloned descriptor
72 * of /dev/crypto obtained via CRIOGET. Now all ioctls are performed
73 * against /dev/crypto directly.
75 #define CRIOGET _IOWR('c', 100, uint32_t)
78 /* the following are done against the cloned descriptor */
80 #ifdef COMPAT_FREEBSD32
81 #include <sys/mount.h>
82 #include <compat/freebsd32/freebsd32.h>
94 struct session2_op32 {
116 struct crypt_aead32 {
141 struct crparam32 crk_param[CRK_MAXPARAM];
144 #define CIOCGSESSION32 _IOWR('c', 101, struct session_op32)
145 #define CIOCCRYPT32 _IOWR('c', 103, struct crypt_op32)
146 #define CIOCKEY32 _IOWR('c', 104, struct crypt_kop32)
147 #define CIOCGSESSION232 _IOWR('c', 106, struct session2_op32)
148 #define CIOCKEY232 _IOWR('c', 107, struct crypt_kop32)
149 #define CIOCCRYPTAEAD32 _IOWR('c', 109, struct crypt_aead32)
152 session_op_from_32(const struct session_op32 *from, struct session2_op *to)
155 memset(to, 0, sizeof(*to));
156 CP(*from, *to, cipher);
158 CP(*from, *to, keylen);
159 PTRIN_CP(*from, *to, key);
160 CP(*from, *to, mackeylen);
161 PTRIN_CP(*from, *to, mackey);
163 to->crid = CRYPTOCAP_F_HARDWARE;
167 session2_op_from_32(const struct session2_op32 *from, struct session2_op *to)
170 session_op_from_32((const struct session_op32 *)from, to);
171 CP(*from, *to, crid);
175 session_op_to_32(const struct session2_op *from, struct session_op32 *to)
178 CP(*from, *to, cipher);
180 CP(*from, *to, keylen);
181 PTROUT_CP(*from, *to, key);
182 CP(*from, *to, mackeylen);
183 PTROUT_CP(*from, *to, mackey);
188 session2_op_to_32(const struct session2_op *from, struct session2_op32 *to)
191 session_op_to_32(from, (struct session_op32 *)to);
192 CP(*from, *to, crid);
196 crypt_op_from_32(const struct crypt_op32 *from, struct crypt_op *to)
201 CP(*from, *to, flags);
203 PTRIN_CP(*from, *to, src);
204 PTRIN_CP(*from, *to, dst);
205 PTRIN_CP(*from, *to, mac);
206 PTRIN_CP(*from, *to, iv);
210 crypt_op_to_32(const struct crypt_op *from, struct crypt_op32 *to)
215 CP(*from, *to, flags);
217 PTROUT_CP(*from, *to, src);
218 PTROUT_CP(*from, *to, dst);
219 PTROUT_CP(*from, *to, mac);
220 PTROUT_CP(*from, *to, iv);
224 crypt_aead_from_32(const struct crypt_aead32 *from, struct crypt_aead *to)
229 CP(*from, *to, flags);
231 CP(*from, *to, aadlen);
232 CP(*from, *to, ivlen);
233 PTRIN_CP(*from, *to, src);
234 PTRIN_CP(*from, *to, dst);
235 PTRIN_CP(*from, *to, aad);
236 PTRIN_CP(*from, *to, tag);
237 PTRIN_CP(*from, *to, iv);
241 crypt_aead_to_32(const struct crypt_aead *from, struct crypt_aead32 *to)
246 CP(*from, *to, flags);
248 CP(*from, *to, aadlen);
249 CP(*from, *to, ivlen);
250 PTROUT_CP(*from, *to, src);
251 PTROUT_CP(*from, *to, dst);
252 PTROUT_CP(*from, *to, aad);
253 PTROUT_CP(*from, *to, tag);
254 PTROUT_CP(*from, *to, iv);
258 crparam_from_32(const struct crparam32 *from, struct crparam *to)
261 PTRIN_CP(*from, *to, crp_p);
262 CP(*from, *to, crp_nbits);
266 crparam_to_32(const struct crparam *from, struct crparam32 *to)
269 PTROUT_CP(*from, *to, crp_p);
270 CP(*from, *to, crp_nbits);
274 crypt_kop_from_32(const struct crypt_kop32 *from, struct crypt_kop *to)
278 CP(*from, *to, crk_op);
279 CP(*from, *to, crk_status);
280 CP(*from, *to, crk_iparams);
281 CP(*from, *to, crk_oparams);
282 CP(*from, *to, crk_crid);
283 for (i = 0; i < CRK_MAXPARAM; i++)
284 crparam_from_32(&from->crk_param[i], &to->crk_param[i]);
288 crypt_kop_to_32(const struct crypt_kop *from, struct crypt_kop32 *to)
292 CP(*from, *to, crk_op);
293 CP(*from, *to, crk_status);
294 CP(*from, *to, crk_iparams);
295 CP(*from, *to, crk_oparams);
296 CP(*from, *to, crk_crid);
297 for (i = 0; i < CRK_MAXPARAM; i++)
298 crparam_to_32(&from->crk_param[i], &to->crk_param[i]);
303 session2_op_from_op(const struct session_op *from, struct session2_op *to)
306 memset(to, 0, sizeof(*to));
307 memcpy(to, from, sizeof(*from));
308 to->crid = CRYPTOCAP_F_HARDWARE;
312 session2_op_to_op(const struct session2_op *from, struct session_op *to)
315 memcpy(to, from, sizeof(*to));
319 TAILQ_ENTRY(csession) next;
320 crypto_session_t cses;
323 struct mtx lock; /* for op submission */
325 struct enc_xform *txform;
334 struct cryptop_data {
335 struct csession *cse;
344 TAILQ_HEAD(csessionlist, csession) csessions;
349 static bool use_outputbuffers;
350 SYSCTL_BOOL(_kern_crypto, OID_AUTO, cryptodev_use_output, CTLFLAG_RW,
351 &use_outputbuffers, 0,
352 "Use separate output buffers for /dev/crypto requests.");
354 static bool use_separate_aad;
355 SYSCTL_BOOL(_kern_crypto, OID_AUTO, cryptodev_separate_aad, CTLFLAG_RW,
356 &use_separate_aad, 0,
357 "Use separate AAD buffer for /dev/crypto requests.");
359 static struct timeval warninterval = { .tv_sec = 60, .tv_usec = 0 };
360 SYSCTL_TIMEVAL_SEC(_kern, OID_AUTO, cryptodev_warn_interval, CTLFLAG_RW,
362 "Delay in seconds between warnings of deprecated /dev/crypto algorithms");
365 * Check a crypto identifier to see if it requested
366 * a software device/driver. This can be done either
367 * by device name/class or through search constraints.
370 checkforsoftware(int *cridp)
376 if (!crypto_devallowsoft) {
377 if (crid & CRYPTOCAP_F_SOFTWARE) {
378 if (crid & CRYPTOCAP_F_HARDWARE) {
379 *cridp = CRYPTOCAP_F_HARDWARE;
384 if ((crid & CRYPTOCAP_F_HARDWARE) == 0 &&
385 (crypto_getcaps(crid) & CRYPTOCAP_F_HARDWARE) == 0)
392 cse_create(struct fcrypt *fcr, struct session2_op *sop)
394 struct crypto_session_params csp;
395 struct csession *cse;
396 struct enc_xform *txform;
397 struct auth_hash *thash;
400 crypto_session_t cses;
403 switch (sop->cipher) {
408 txform = &enc_xform_rijndael128;
411 txform = &enc_xform_aes_xts;
413 case CRYPTO_NULL_CBC:
414 txform = &enc_xform_null;
416 case CRYPTO_CAMELLIA_CBC:
417 txform = &enc_xform_camellia;
420 txform = &enc_xform_aes_icm;
422 case CRYPTO_AES_NIST_GCM_16:
423 txform = &enc_xform_aes_nist_gcm;
425 case CRYPTO_CHACHA20:
426 txform = &enc_xform_chacha20;
428 case CRYPTO_AES_CCM_16:
429 txform = &enc_xform_ccm;
431 case CRYPTO_CHACHA20_POLY1305:
432 txform = &enc_xform_chacha20_poly1305;
435 CRYPTDEB("invalid cipher");
436 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
444 case CRYPTO_POLY1305:
445 thash = &auth_hash_poly1305;
447 case CRYPTO_SHA1_HMAC:
448 thash = &auth_hash_hmac_sha1;
450 case CRYPTO_SHA2_224_HMAC:
451 thash = &auth_hash_hmac_sha2_224;
453 case CRYPTO_SHA2_256_HMAC:
454 thash = &auth_hash_hmac_sha2_256;
456 case CRYPTO_SHA2_384_HMAC:
457 thash = &auth_hash_hmac_sha2_384;
459 case CRYPTO_SHA2_512_HMAC:
460 thash = &auth_hash_hmac_sha2_512;
462 case CRYPTO_RIPEMD160_HMAC:
463 thash = &auth_hash_hmac_ripemd_160;
465 #ifdef COMPAT_FREEBSD12
466 case CRYPTO_AES_128_NIST_GMAC:
467 case CRYPTO_AES_192_NIST_GMAC:
468 case CRYPTO_AES_256_NIST_GMAC:
469 /* Should always be paired with GCM. */
470 if (sop->cipher != CRYPTO_AES_NIST_GCM_16) {
471 CRYPTDEB("GMAC without GCM");
472 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
477 case CRYPTO_AES_NIST_GMAC:
478 switch (sop->mackeylen * 8) {
480 thash = &auth_hash_nist_gmac_aes_128;
483 thash = &auth_hash_nist_gmac_aes_192;
486 thash = &auth_hash_nist_gmac_aes_256;
489 CRYPTDEB("invalid GMAC key length");
490 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
494 case CRYPTO_AES_CCM_CBC_MAC:
495 switch (sop->mackeylen) {
497 thash = &auth_hash_ccm_cbc_mac_128;
500 thash = &auth_hash_ccm_cbc_mac_192;
503 thash = &auth_hash_ccm_cbc_mac_256;
506 CRYPTDEB("Invalid CBC MAC key size %d", sop->keylen);
507 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
512 thash = &auth_hash_sha1;
514 case CRYPTO_SHA2_224:
515 thash = &auth_hash_sha2_224;
517 case CRYPTO_SHA2_256:
518 thash = &auth_hash_sha2_256;
520 case CRYPTO_SHA2_384:
521 thash = &auth_hash_sha2_384;
523 case CRYPTO_SHA2_512:
524 thash = &auth_hash_sha2_512;
527 case CRYPTO_NULL_HMAC:
528 thash = &auth_hash_null;
532 thash = &auth_hash_blake2b;
535 thash = &auth_hash_blake2s;
539 CRYPTDEB("invalid mac");
540 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
544 if (txform == NULL && thash == NULL) {
545 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
549 memset(&csp, 0, sizeof(csp));
550 if (use_outputbuffers)
551 csp.csp_flags |= CSP_F_SEPARATE_OUTPUT;
553 if (sop->cipher == CRYPTO_AES_NIST_GCM_16) {
555 #ifdef COMPAT_FREEBSD12
556 case CRYPTO_AES_128_NIST_GMAC:
557 case CRYPTO_AES_192_NIST_GMAC:
558 case CRYPTO_AES_256_NIST_GMAC:
559 if (sop->keylen != sop->mackeylen) {
560 SDT_PROBE1(opencrypto, dev, ioctl, error,
569 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
572 csp.csp_mode = CSP_MODE_AEAD;
573 } else if (sop->cipher == CRYPTO_AES_CCM_16) {
575 #ifdef COMPAT_FREEBSD12
576 case CRYPTO_AES_CCM_CBC_MAC:
577 if (sop->keylen != sop->mackeylen) {
578 SDT_PROBE1(opencrypto, dev, ioctl, error,
588 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
591 csp.csp_mode = CSP_MODE_AEAD;
592 } else if (sop->cipher == CRYPTO_CHACHA20_POLY1305) {
594 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
597 csp.csp_mode = CSP_MODE_AEAD;
598 } else if (txform != NULL && thash != NULL)
599 csp.csp_mode = CSP_MODE_ETA;
600 else if (txform != NULL)
601 csp.csp_mode = CSP_MODE_CIPHER;
603 csp.csp_mode = CSP_MODE_DIGEST;
605 switch (csp.csp_mode) {
608 if (use_separate_aad)
609 csp.csp_flags |= CSP_F_SEPARATE_AAD;
613 if (txform != NULL) {
614 csp.csp_cipher_alg = txform->type;
615 csp.csp_cipher_klen = sop->keylen;
616 if (sop->keylen > txform->maxkey ||
617 sop->keylen < txform->minkey) {
618 CRYPTDEB("invalid cipher parameters");
620 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
624 key = malloc(csp.csp_cipher_klen, M_XDATA, M_WAITOK);
625 error = copyin(sop->key, key, csp.csp_cipher_klen);
627 CRYPTDEB("invalid key");
628 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
631 csp.csp_cipher_key = key;
632 csp.csp_ivlen = txform->ivsize;
636 csp.csp_auth_alg = thash->type;
637 csp.csp_auth_klen = sop->mackeylen;
638 if (sop->mackeylen > thash->keysize || sop->mackeylen < 0) {
639 CRYPTDEB("invalid mac key length");
641 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
645 if (csp.csp_auth_klen != 0) {
646 mackey = malloc(csp.csp_auth_klen, M_XDATA, M_WAITOK);
647 error = copyin(sop->mackey, mackey, csp.csp_auth_klen);
649 CRYPTDEB("invalid mac key");
650 SDT_PROBE1(opencrypto, dev, ioctl, error,
654 csp.csp_auth_key = mackey;
657 if (csp.csp_auth_alg == CRYPTO_AES_NIST_GMAC)
658 csp.csp_ivlen = AES_GCM_IV_LEN;
659 if (csp.csp_auth_alg == CRYPTO_AES_CCM_CBC_MAC)
660 csp.csp_ivlen = AES_CCM_IV_LEN;
664 error = checkforsoftware(&crid);
666 CRYPTDEB("checkforsoftware");
667 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
670 error = crypto_newsession(&cses, &csp, crid);
672 CRYPTDEB("crypto_newsession");
673 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
677 cse = malloc(sizeof(struct csession), M_XDATA, M_WAITOK | M_ZERO);
678 mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF);
679 refcount_init(&cse->refs, 1);
681 cse->mackey = mackey;
682 cse->mode = csp.csp_mode;
684 cse->txform = txform;
686 cse->hashsize = thash->hashsize;
687 else if (csp.csp_cipher_alg == CRYPTO_AES_NIST_GCM_16)
688 cse->hashsize = AES_GMAC_HASH_LEN;
689 else if (csp.csp_cipher_alg == CRYPTO_AES_CCM_16)
690 cse->hashsize = AES_CBC_MAC_HASH_LEN;
691 else if (csp.csp_cipher_alg == CRYPTO_CHACHA20_POLY1305)
692 cse->hashsize = POLY1305_HASH_LEN;
693 cse->ivsize = csp.csp_ivlen;
695 mtx_lock(&fcr->lock);
696 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
697 cse->ses = fcr->sesn++;
698 mtx_unlock(&fcr->lock);
702 /* return hardware/driver id */
703 sop->crid = crypto_ses2hid(cse->cses);
707 free(mackey, M_XDATA);
712 static struct csession *
713 cse_find(struct fcrypt *fcr, u_int ses)
715 struct csession *cse;
717 mtx_lock(&fcr->lock);
718 TAILQ_FOREACH(cse, &fcr->csessions, next) {
719 if (cse->ses == ses) {
720 refcount_acquire(&cse->refs);
721 mtx_unlock(&fcr->lock);
725 mtx_unlock(&fcr->lock);
730 cse_free(struct csession *cse)
733 if (!refcount_release(&cse->refs))
735 crypto_freesession(cse->cses);
736 mtx_destroy(&cse->lock);
738 free(cse->key, M_XDATA);
740 free(cse->mackey, M_XDATA);
745 cse_delete(struct fcrypt *fcr, u_int ses)
747 struct csession *cse;
749 mtx_lock(&fcr->lock);
750 TAILQ_FOREACH(cse, &fcr->csessions, next) {
751 if (cse->ses == ses) {
752 TAILQ_REMOVE(&fcr->csessions, cse, next);
753 mtx_unlock(&fcr->lock);
758 mtx_unlock(&fcr->lock);
762 static struct cryptop_data *
763 cod_alloc(struct csession *cse, size_t aad_len, size_t len)
765 struct cryptop_data *cod;
767 cod = malloc(sizeof(struct cryptop_data), M_XDATA, M_WAITOK | M_ZERO);
770 if (crypto_get_params(cse->cses)->csp_flags & CSP_F_SEPARATE_AAD) {
772 cod->aad = malloc(aad_len, M_XDATA, M_WAITOK);
773 cod->buf = malloc(len, M_XDATA, M_WAITOK);
775 cod->buf = malloc(aad_len + len, M_XDATA, M_WAITOK);
776 if (crypto_get_params(cse->cses)->csp_flags & CSP_F_SEPARATE_OUTPUT)
777 cod->obuf = malloc(len, M_XDATA, M_WAITOK);
782 cod_free(struct cryptop_data *cod)
785 free(cod->aad, M_XDATA);
786 free(cod->obuf, M_XDATA);
787 free(cod->buf, M_XDATA);
792 cryptodev_cb(struct cryptop *crp)
794 struct cryptop_data *cod = crp->crp_opaque;
797 * Lock to ensure the wakeup() is not missed by the loops
798 * waiting on cod->done in cryptodev_op() and
801 mtx_lock(&cod->cse->lock);
803 mtx_unlock(&cod->cse->lock);
809 cryptodev_op(struct csession *cse, const struct crypt_op *cop)
811 struct cryptop_data *cod = NULL;
812 struct cryptop *crp = NULL;
816 if (cop->len > 256*1024-4) {
817 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
822 if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0) {
823 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
828 if (cop->mac && cse->hashsize == 0) {
829 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
834 * The COP_F_CIPHER_FIRST flag predates explicit session
835 * modes, but the only way it was used was for EtA so allow it
836 * as long as it is consistent with EtA.
838 if (cop->flags & COP_F_CIPHER_FIRST) {
839 if (cop->op != COP_ENCRYPT) {
840 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
845 cod = cod_alloc(cse, 0, cop->len + cse->hashsize);
848 crp = crypto_getreq(cse->cses, M_WAITOK);
850 error = copyin(cop->src, cod->buf, cop->len);
852 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
855 crp->crp_payload_start = 0;
856 crp->crp_payload_length = cop->len;
858 crp->crp_digest_start = cop->len;
861 case CSP_MODE_COMPRESS:
864 crp->crp_op = CRYPTO_OP_COMPRESS;
867 crp->crp_op = CRYPTO_OP_DECOMPRESS;
870 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
875 case CSP_MODE_CIPHER:
878 crp->crp_op = CRYPTO_OP_ENCRYPT;
881 crp->crp_op = CRYPTO_OP_DECRYPT;
884 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
889 case CSP_MODE_DIGEST:
894 crp->crp_op = CRYPTO_OP_COMPUTE_DIGEST;
895 if (cod->obuf != NULL)
896 crp->crp_digest_start = 0;
899 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
907 crp->crp_op = CRYPTO_OP_ENCRYPT |
908 CRYPTO_OP_COMPUTE_DIGEST;
911 crp->crp_op = CRYPTO_OP_DECRYPT |
912 CRYPTO_OP_VERIFY_DIGEST;
915 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
921 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
926 crp->crp_flags = CRYPTO_F_CBIMM | (cop->flags & COP_F_BATCH);
927 crypto_use_buf(crp, cod->buf, cop->len + cse->hashsize);
929 crypto_use_output_buf(crp, cod->obuf, cop->len + cse->hashsize);
930 crp->crp_callback = cryptodev_cb;
931 crp->crp_opaque = cod;
934 if (cse->ivsize == 0) {
935 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
939 error = copyin(cop->iv, crp->crp_iv, cse->ivsize);
941 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
944 crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
945 } else if (cse->ivsize != 0) {
946 crp->crp_iv_start = 0;
947 crp->crp_payload_start += cse->ivsize;
948 crp->crp_payload_length -= cse->ivsize;
952 if (cop->mac != NULL && crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
953 error = copyin(cop->mac, cod->buf + crp->crp_digest_start,
956 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
962 * Let the dispatch run unlocked, then, interlock against the
963 * callback before checking if the operation completed and going
964 * to sleep. This insures drivers don't inherit our lock which
965 * results in a lock order reversal between crypto_dispatch forced
966 * entry and the crypto_done callback into us.
968 error = crypto_dispatch(crp);
970 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
974 mtx_lock(&cse->lock);
976 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0);
977 mtx_unlock(&cse->lock);
979 if (crp->crp_etype == EAGAIN) {
981 crp->crp_flags &= ~CRYPTO_F_DONE;
986 if (crp->crp_etype != 0) {
987 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
988 error = crp->crp_etype;
992 if (cop->dst != NULL) {
993 error = copyout(cod->obuf != NULL ? cod->obuf :
994 cod->buf + crp->crp_payload_start, dst,
995 crp->crp_payload_length);
997 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1002 if (cop->mac != NULL && (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) == 0) {
1003 error = copyout((cod->obuf != NULL ? cod->obuf : cod->buf) +
1004 crp->crp_digest_start, cop->mac, cse->hashsize);
1006 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1012 crypto_freereq(crp);
1019 cryptodev_aead(struct csession *cse, struct crypt_aead *caead)
1021 struct cryptop_data *cod = NULL;
1022 struct cryptop *crp = NULL;
1026 if (caead->len > 256*1024-4 || caead->aadlen > 256*1024-4) {
1027 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1031 if (cse->txform == NULL || cse->hashsize == 0 || caead->tag == NULL ||
1032 (caead->len % cse->txform->blocksize) != 0) {
1033 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1038 * The COP_F_CIPHER_FIRST flag predates explicit session
1039 * modes, but the only way it was used was for EtA so allow it
1040 * as long as it is consistent with EtA.
1042 if (caead->flags & COP_F_CIPHER_FIRST) {
1043 if (caead->op != COP_ENCRYPT) {
1044 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1049 cod = cod_alloc(cse, caead->aadlen, caead->len + cse->hashsize);
1052 crp = crypto_getreq(cse->cses, M_WAITOK);
1054 if (cod->aad != NULL)
1055 error = copyin(caead->aad, cod->aad, caead->aadlen);
1057 error = copyin(caead->aad, cod->buf, caead->aadlen);
1059 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1062 crp->crp_aad = cod->aad;
1063 crp->crp_aad_start = 0;
1064 crp->crp_aad_length = caead->aadlen;
1066 if (cod->aad != NULL)
1067 crp->crp_payload_start = 0;
1069 crp->crp_payload_start = caead->aadlen;
1070 error = copyin(caead->src, cod->buf + crp->crp_payload_start,
1073 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1076 crp->crp_payload_length = caead->len;
1077 if (caead->op == COP_ENCRYPT && cod->obuf != NULL)
1078 crp->crp_digest_start = crp->crp_payload_output_start +
1081 crp->crp_digest_start = crp->crp_payload_start + caead->len;
1083 switch (cse->mode) {
1086 switch (caead->op) {
1088 crp->crp_op = CRYPTO_OP_ENCRYPT |
1089 CRYPTO_OP_COMPUTE_DIGEST;
1092 crp->crp_op = CRYPTO_OP_DECRYPT |
1093 CRYPTO_OP_VERIFY_DIGEST;
1096 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1102 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1107 crp->crp_flags = CRYPTO_F_CBIMM | (caead->flags & COP_F_BATCH);
1108 crypto_use_buf(crp, cod->buf, crp->crp_payload_start + caead->len +
1110 if (cod->obuf != NULL)
1111 crypto_use_output_buf(crp, cod->obuf, caead->len +
1113 crp->crp_callback = cryptodev_cb;
1114 crp->crp_opaque = cod;
1118 * Permit a 16-byte IV for AES-XTS, but only use the
1119 * first 8 bytes as a block number.
1121 if (cse->mode == CSP_MODE_ETA &&
1122 caead->ivlen == AES_BLOCK_LEN &&
1123 cse->ivsize == AES_XTS_IV_LEN)
1124 caead->ivlen = AES_XTS_IV_LEN;
1126 if (caead->ivlen != cse->ivsize) {
1128 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1132 error = copyin(caead->iv, crp->crp_iv, cse->ivsize);
1134 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1137 crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
1139 crp->crp_iv_start = crp->crp_payload_start;
1140 crp->crp_payload_start += cse->ivsize;
1141 crp->crp_payload_length -= cse->ivsize;
1145 if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
1146 error = copyin(caead->tag, cod->buf + crp->crp_digest_start,
1149 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1155 * Let the dispatch run unlocked, then, interlock against the
1156 * callback before checking if the operation completed and going
1157 * to sleep. This insures drivers don't inherit our lock which
1158 * results in a lock order reversal between crypto_dispatch forced
1159 * entry and the crypto_done callback into us.
1161 error = crypto_dispatch(crp);
1163 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1167 mtx_lock(&cse->lock);
1169 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0);
1170 mtx_unlock(&cse->lock);
1172 if (crp->crp_etype == EAGAIN) {
1174 crp->crp_flags &= ~CRYPTO_F_DONE;
1179 if (crp->crp_etype != 0) {
1180 error = crp->crp_etype;
1181 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1185 if (caead->dst != NULL) {
1186 error = copyout(cod->obuf != NULL ? cod->obuf :
1187 cod->buf + crp->crp_payload_start, dst,
1188 crp->crp_payload_length);
1190 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1195 if ((crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) == 0) {
1196 error = copyout((cod->obuf != NULL ? cod->obuf : cod->buf) +
1197 crp->crp_digest_start, caead->tag, cse->hashsize);
1199 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1205 crypto_freereq(crp);
1212 cryptodevkey_cb(struct cryptkop *krp)
1219 cryptodev_key(struct crypt_kop *kop)
1221 struct cryptkop *krp = NULL;
1223 int in, out, size, i;
1225 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
1226 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1230 in = kop->crk_iparams;
1231 out = kop->crk_oparams;
1232 switch (kop->crk_op) {
1234 if (in == 3 && out == 1)
1236 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1238 case CRK_MOD_EXP_CRT:
1239 if (in == 6 && out == 1)
1241 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1244 if (in == 5 && out == 2)
1246 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1248 case CRK_DSA_VERIFY:
1249 if (in == 7 && out == 0)
1251 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1253 case CRK_DH_COMPUTE_KEY:
1254 if (in == 3 && out == 1)
1256 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1259 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1263 krp = malloc(sizeof(*krp), M_XDATA, M_WAITOK | M_ZERO);
1264 krp->krp_op = kop->crk_op;
1265 krp->krp_status = kop->crk_status;
1266 krp->krp_iparams = kop->crk_iparams;
1267 krp->krp_oparams = kop->crk_oparams;
1268 krp->krp_crid = kop->crk_crid;
1269 krp->krp_status = 0;
1270 krp->krp_callback = cryptodevkey_cb;
1272 for (i = 0; i < CRK_MAXPARAM; i++) {
1273 if (kop->crk_param[i].crp_nbits > 65536) {
1274 /* Limit is the same as in OpenBSD */
1275 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1278 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
1280 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
1281 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1284 krp->krp_param[i].crp_p = malloc(size, M_XDATA, M_WAITOK);
1285 if (i >= krp->krp_iparams)
1287 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
1289 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1294 error = crypto_kdispatch(krp);
1296 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1299 error = tsleep(krp, PSOCK, "crydev", 0);
1301 /* XXX can this happen? if so, how do we recover? */
1302 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1306 kop->crk_crid = krp->krp_hid; /* device that did the work */
1307 if (krp->krp_status != 0) {
1308 error = krp->krp_status;
1309 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1313 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
1314 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1317 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
1319 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1326 kop->crk_status = krp->krp_status;
1327 for (i = 0; i < CRK_MAXPARAM; i++) {
1328 if (krp->krp_param[i].crp_p)
1329 free(krp->krp_param[i].crp_p, M_XDATA);
1337 cryptodev_find(struct crypt_find_op *find)
1340 size_t fnlen = sizeof find->name;
1342 if (find->crid != -1) {
1343 dev = crypto_find_device_byhid(find->crid);
1346 strncpy(find->name, device_get_nameunit(dev), fnlen);
1347 find->name[fnlen - 1] = '\x0';
1349 find->name[fnlen - 1] = '\x0';
1350 find->crid = crypto_find_driver(find->name);
1351 if (find->crid == -1)
1358 fcrypt_dtor(void *data)
1360 struct fcrypt *fcr = data;
1361 struct csession *cse;
1363 while ((cse = TAILQ_FIRST(&fcr->csessions))) {
1364 TAILQ_REMOVE(&fcr->csessions, cse, next);
1365 KASSERT(refcount_load(&cse->refs) == 1,
1366 ("%s: crypto session %p with %d refs", __func__, cse,
1367 refcount_load(&cse->refs)));
1370 mtx_destroy(&fcr->lock);
1375 crypto_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
1380 fcr = malloc(sizeof(struct fcrypt), M_XDATA, M_WAITOK | M_ZERO);
1381 TAILQ_INIT(&fcr->csessions);
1382 mtx_init(&fcr->lock, "fcrypt", NULL, MTX_DEF);
1383 error = devfs_set_cdevpriv(fcr, fcrypt_dtor);
1390 crypto_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
1393 static struct timeval keywarn, featwarn;
1395 struct csession *cse;
1396 struct session2_op *sop;
1397 struct crypt_op *cop;
1398 struct crypt_aead *caead;
1399 struct crypt_kop *kop;
1403 struct session2_op sopc;
1404 #ifdef COMPAT_FREEBSD32
1405 struct crypt_op copc;
1406 struct crypt_aead aeadc;
1407 struct crypt_kop kopc;
1410 #ifdef COMPAT_FREEBSD32
1417 case CIOCGSESSION32:
1421 data = (void *)&thunk.sopc;
1422 session_op_from_32((struct session_op32 *)data32, &thunk.sopc);
1424 case CIOCGSESSION232:
1427 cmd = CIOCGSESSION2;
1428 data = (void *)&thunk.sopc;
1429 session2_op_from_32((struct session2_op32 *)data32,
1436 data = (void *)&thunk.copc;
1437 crypt_op_from_32((struct crypt_op32 *)data32, &thunk.copc);
1439 case CIOCCRYPTAEAD32:
1442 cmd = CIOCCRYPTAEAD;
1443 data = (void *)&thunk.aeadc;
1444 crypt_aead_from_32((struct crypt_aead32 *)data32, &thunk.aeadc);
1450 if (cmd == CIOCKEY32)
1454 data = (void *)&thunk.kopc;
1455 crypt_kop_from_32((struct crypt_kop32 *)data32, &thunk.kopc);
1460 devfs_get_cdevpriv((void **)&fcr);
1463 #ifdef COMPAT_FREEBSD12
1466 * NB: This may fail in cases that the old
1467 * implementation did not if the current process has
1468 * restricted filesystem access (e.g. running in a
1469 * jail that does not expose /dev/crypto or in
1472 error = kern_openat(td, AT_FDCWD, "/dev/crypto", UIO_SYSSPACE,
1475 *(uint32_t *)data = td->td_retval[0];
1480 if (cmd == CIOCGSESSION) {
1481 session2_op_from_op((void *)data, &thunk.sopc);
1484 sop = (struct session2_op *)data;
1486 error = cse_create(fcr, sop);
1487 if (cmd == CIOCGSESSION && error == 0)
1488 session2_op_to_op(sop, (void *)data);
1491 ses = *(uint32_t *)data;
1492 if (!cse_delete(fcr, ses)) {
1493 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1498 cop = (struct crypt_op *)data;
1499 cse = cse_find(fcr, cop->ses);
1501 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1504 error = cryptodev_op(cse, cop);
1509 if (ratecheck(&keywarn, &warninterval))
1511 "Asymmetric crypto operations via /dev/crypto");
1513 if (!crypto_userasymcrypto) {
1514 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1515 return (EPERM); /* XXX compat? */
1517 kop = (struct crypt_kop *)data;
1518 if (cmd == CIOCKEY) {
1519 /* NB: crypto core enforces s/w driver use */
1521 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
1524 error = cryptodev_key(kop);
1528 if (ratecheck(&featwarn, &warninterval))
1530 "Asymmetric crypto features via /dev/crypto");
1532 if (!crypto_userasymcrypto) {
1534 * NB: if user asym crypto operations are
1535 * not permitted return "no algorithms"
1536 * so well-behaved applications will just
1537 * fallback to doing them in software.
1541 error = crypto_getfeat((int *)data);
1543 SDT_PROBE1(opencrypto, dev, ioctl, error,
1548 error = cryptodev_find((struct crypt_find_op *)data);
1551 caead = (struct crypt_aead *)data;
1552 cse = cse_find(fcr, caead->ses);
1554 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1557 error = cryptodev_aead(cse, caead);
1562 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1566 #ifdef COMPAT_FREEBSD32
1568 case CIOCGSESSION32:
1570 session_op_to_32((void *)data, data32);
1572 case CIOCGSESSION232:
1574 session2_op_to_32((void *)data, data32);
1578 crypt_op_to_32((void *)data, data32);
1580 case CIOCCRYPTAEAD32:
1582 crypt_aead_to_32((void *)data, data32);
1586 crypt_kop_to_32((void *)data, data32);
1593 static struct cdevsw crypto_cdevsw = {
1594 .d_version = D_VERSION,
1595 .d_open = crypto_open,
1596 .d_ioctl = crypto_ioctl,
1599 static struct cdev *crypto_dev;
1602 * Initialization code, both for static and dynamic loading.
1605 cryptodev_modevent(module_t mod, int type, void *unused)
1610 printf("crypto: <crypto device>\n");
1611 crypto_dev = make_dev(&crypto_cdevsw, 0,
1612 UID_ROOT, GID_WHEEL, 0666,
1616 /*XXX disallow if active sessions */
1617 destroy_dev(crypto_dev);
1623 static moduledata_t cryptodev_mod = {
1628 MODULE_VERSION(cryptodev, 1);
1629 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1630 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
1631 MODULE_DEPEND(cryptodev, zlib, 1, 1, 1);