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;
432 CRYPTDEB("invalid cipher");
433 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
441 case CRYPTO_POLY1305:
442 thash = &auth_hash_poly1305;
444 case CRYPTO_SHA1_HMAC:
445 thash = &auth_hash_hmac_sha1;
447 case CRYPTO_SHA2_224_HMAC:
448 thash = &auth_hash_hmac_sha2_224;
450 case CRYPTO_SHA2_256_HMAC:
451 thash = &auth_hash_hmac_sha2_256;
453 case CRYPTO_SHA2_384_HMAC:
454 thash = &auth_hash_hmac_sha2_384;
456 case CRYPTO_SHA2_512_HMAC:
457 thash = &auth_hash_hmac_sha2_512;
459 case CRYPTO_RIPEMD160_HMAC:
460 thash = &auth_hash_hmac_ripemd_160;
462 #ifdef COMPAT_FREEBSD12
463 case CRYPTO_AES_128_NIST_GMAC:
464 case CRYPTO_AES_192_NIST_GMAC:
465 case CRYPTO_AES_256_NIST_GMAC:
466 /* Should always be paired with GCM. */
467 if (sop->cipher != CRYPTO_AES_NIST_GCM_16) {
468 CRYPTDEB("GMAC without GCM");
469 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
474 case CRYPTO_AES_NIST_GMAC:
475 switch (sop->mackeylen * 8) {
477 thash = &auth_hash_nist_gmac_aes_128;
480 thash = &auth_hash_nist_gmac_aes_192;
483 thash = &auth_hash_nist_gmac_aes_256;
486 CRYPTDEB("invalid GMAC key length");
487 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
491 case CRYPTO_AES_CCM_CBC_MAC:
492 switch (sop->mackeylen) {
494 thash = &auth_hash_ccm_cbc_mac_128;
497 thash = &auth_hash_ccm_cbc_mac_192;
500 thash = &auth_hash_ccm_cbc_mac_256;
503 CRYPTDEB("Invalid CBC MAC key size %d", sop->keylen);
504 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
509 thash = &auth_hash_sha1;
511 case CRYPTO_SHA2_224:
512 thash = &auth_hash_sha2_224;
514 case CRYPTO_SHA2_256:
515 thash = &auth_hash_sha2_256;
517 case CRYPTO_SHA2_384:
518 thash = &auth_hash_sha2_384;
520 case CRYPTO_SHA2_512:
521 thash = &auth_hash_sha2_512;
524 case CRYPTO_NULL_HMAC:
525 thash = &auth_hash_null;
529 thash = &auth_hash_blake2b;
532 thash = &auth_hash_blake2s;
536 CRYPTDEB("invalid mac");
537 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
541 if (txform == NULL && thash == NULL) {
542 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
546 memset(&csp, 0, sizeof(csp));
547 if (use_outputbuffers)
548 csp.csp_flags |= CSP_F_SEPARATE_OUTPUT;
550 if (sop->cipher == CRYPTO_AES_NIST_GCM_16) {
552 #ifdef COMPAT_FREEBSD12
553 case CRYPTO_AES_128_NIST_GMAC:
554 case CRYPTO_AES_192_NIST_GMAC:
555 case CRYPTO_AES_256_NIST_GMAC:
556 if (sop->keylen != sop->mackeylen) {
557 SDT_PROBE1(opencrypto, dev, ioctl, error,
566 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
569 csp.csp_mode = CSP_MODE_AEAD;
570 } else if (sop->cipher == CRYPTO_AES_CCM_16) {
572 #ifdef COMPAT_FREEBSD12
573 case CRYPTO_AES_CCM_CBC_MAC:
574 if (sop->keylen != sop->mackeylen) {
575 SDT_PROBE1(opencrypto, dev, ioctl, error,
585 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
588 csp.csp_mode = CSP_MODE_AEAD;
589 } else if (txform != NULL && thash != NULL)
590 csp.csp_mode = CSP_MODE_ETA;
591 else if (txform != NULL)
592 csp.csp_mode = CSP_MODE_CIPHER;
594 csp.csp_mode = CSP_MODE_DIGEST;
596 switch (csp.csp_mode) {
599 if (use_separate_aad)
600 csp.csp_flags |= CSP_F_SEPARATE_AAD;
604 if (txform != NULL) {
605 csp.csp_cipher_alg = txform->type;
606 csp.csp_cipher_klen = sop->keylen;
607 if (sop->keylen > txform->maxkey ||
608 sop->keylen < txform->minkey) {
609 CRYPTDEB("invalid cipher parameters");
611 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
615 key = malloc(csp.csp_cipher_klen, M_XDATA, M_WAITOK);
616 error = copyin(sop->key, key, csp.csp_cipher_klen);
618 CRYPTDEB("invalid key");
619 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
622 csp.csp_cipher_key = key;
623 csp.csp_ivlen = txform->ivsize;
627 csp.csp_auth_alg = thash->type;
628 csp.csp_auth_klen = sop->mackeylen;
629 if (sop->mackeylen > thash->keysize || sop->mackeylen < 0) {
630 CRYPTDEB("invalid mac key length");
632 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
636 if (csp.csp_auth_klen != 0) {
637 mackey = malloc(csp.csp_auth_klen, M_XDATA, M_WAITOK);
638 error = copyin(sop->mackey, mackey, csp.csp_auth_klen);
640 CRYPTDEB("invalid mac key");
641 SDT_PROBE1(opencrypto, dev, ioctl, error,
645 csp.csp_auth_key = mackey;
648 if (csp.csp_auth_alg == CRYPTO_AES_NIST_GMAC)
649 csp.csp_ivlen = AES_GCM_IV_LEN;
650 if (csp.csp_auth_alg == CRYPTO_AES_CCM_CBC_MAC)
651 csp.csp_ivlen = AES_CCM_IV_LEN;
655 error = checkforsoftware(&crid);
657 CRYPTDEB("checkforsoftware");
658 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
661 error = crypto_newsession(&cses, &csp, crid);
663 CRYPTDEB("crypto_newsession");
664 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
668 cse = malloc(sizeof(struct csession), M_XDATA, M_WAITOK | M_ZERO);
669 mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF);
670 refcount_init(&cse->refs, 1);
672 cse->mackey = mackey;
673 cse->mode = csp.csp_mode;
675 cse->txform = txform;
677 cse->hashsize = thash->hashsize;
678 else if (csp.csp_cipher_alg == CRYPTO_AES_NIST_GCM_16)
679 cse->hashsize = AES_GMAC_HASH_LEN;
680 else if (csp.csp_cipher_alg == CRYPTO_AES_CCM_16)
681 cse->hashsize = AES_CBC_MAC_HASH_LEN;
682 cse->ivsize = csp.csp_ivlen;
684 mtx_lock(&fcr->lock);
685 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
686 cse->ses = fcr->sesn++;
687 mtx_unlock(&fcr->lock);
691 /* return hardware/driver id */
692 sop->crid = crypto_ses2hid(cse->cses);
696 free(mackey, M_XDATA);
701 static struct csession *
702 cse_find(struct fcrypt *fcr, u_int ses)
704 struct csession *cse;
706 mtx_lock(&fcr->lock);
707 TAILQ_FOREACH(cse, &fcr->csessions, next) {
708 if (cse->ses == ses) {
709 refcount_acquire(&cse->refs);
710 mtx_unlock(&fcr->lock);
714 mtx_unlock(&fcr->lock);
719 cse_free(struct csession *cse)
722 if (!refcount_release(&cse->refs))
724 crypto_freesession(cse->cses);
725 mtx_destroy(&cse->lock);
727 free(cse->key, M_XDATA);
729 free(cse->mackey, M_XDATA);
734 cse_delete(struct fcrypt *fcr, u_int ses)
736 struct csession *cse;
738 mtx_lock(&fcr->lock);
739 TAILQ_FOREACH(cse, &fcr->csessions, next) {
740 if (cse->ses == ses) {
741 TAILQ_REMOVE(&fcr->csessions, cse, next);
742 mtx_unlock(&fcr->lock);
747 mtx_unlock(&fcr->lock);
751 static struct cryptop_data *
752 cod_alloc(struct csession *cse, size_t aad_len, size_t len)
754 struct cryptop_data *cod;
756 cod = malloc(sizeof(struct cryptop_data), M_XDATA, M_WAITOK | M_ZERO);
759 if (crypto_get_params(cse->cses)->csp_flags & CSP_F_SEPARATE_AAD) {
761 cod->aad = malloc(aad_len, M_XDATA, M_WAITOK);
762 cod->buf = malloc(len, M_XDATA, M_WAITOK);
764 cod->buf = malloc(aad_len + len, M_XDATA, M_WAITOK);
765 if (crypto_get_params(cse->cses)->csp_flags & CSP_F_SEPARATE_OUTPUT)
766 cod->obuf = malloc(len, M_XDATA, M_WAITOK);
771 cod_free(struct cryptop_data *cod)
774 free(cod->aad, M_XDATA);
775 free(cod->obuf, M_XDATA);
776 free(cod->buf, M_XDATA);
781 cryptodev_cb(struct cryptop *crp)
783 struct cryptop_data *cod = crp->crp_opaque;
786 * Lock to ensure the wakeup() is not missed by the loops
787 * waiting on cod->done in cryptodev_op() and
790 mtx_lock(&cod->cse->lock);
792 mtx_unlock(&cod->cse->lock);
798 cryptodev_op(struct csession *cse, const struct crypt_op *cop)
800 struct cryptop_data *cod = NULL;
801 struct cryptop *crp = NULL;
805 if (cop->len > 256*1024-4) {
806 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
811 if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0) {
812 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
817 if (cop->mac && cse->hashsize == 0) {
818 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
823 * The COP_F_CIPHER_FIRST flag predates explicit session
824 * modes, but the only way it was used was for EtA so allow it
825 * as long as it is consistent with EtA.
827 if (cop->flags & COP_F_CIPHER_FIRST) {
828 if (cop->op != COP_ENCRYPT) {
829 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
834 cod = cod_alloc(cse, 0, cop->len + cse->hashsize);
837 crp = crypto_getreq(cse->cses, M_WAITOK);
839 error = copyin(cop->src, cod->buf, cop->len);
841 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
844 crp->crp_payload_start = 0;
845 crp->crp_payload_length = cop->len;
847 crp->crp_digest_start = cop->len;
850 case CSP_MODE_COMPRESS:
853 crp->crp_op = CRYPTO_OP_COMPRESS;
856 crp->crp_op = CRYPTO_OP_DECOMPRESS;
859 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
864 case CSP_MODE_CIPHER:
867 crp->crp_op = CRYPTO_OP_ENCRYPT;
870 crp->crp_op = CRYPTO_OP_DECRYPT;
873 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
878 case CSP_MODE_DIGEST:
883 crp->crp_op = CRYPTO_OP_COMPUTE_DIGEST;
884 if (cod->obuf != NULL)
885 crp->crp_digest_start = 0;
888 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
896 crp->crp_op = CRYPTO_OP_ENCRYPT |
897 CRYPTO_OP_COMPUTE_DIGEST;
900 crp->crp_op = CRYPTO_OP_DECRYPT |
901 CRYPTO_OP_VERIFY_DIGEST;
904 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
910 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
915 crp->crp_flags = CRYPTO_F_CBIMM | (cop->flags & COP_F_BATCH);
916 crypto_use_buf(crp, cod->buf, cop->len + cse->hashsize);
918 crypto_use_output_buf(crp, cod->obuf, cop->len + cse->hashsize);
919 crp->crp_callback = cryptodev_cb;
920 crp->crp_opaque = cod;
923 if (cse->ivsize == 0) {
924 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
928 error = copyin(cop->iv, crp->crp_iv, cse->ivsize);
930 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
933 crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
934 } else if (cse->ivsize != 0) {
935 crp->crp_iv_start = 0;
936 crp->crp_payload_start += cse->ivsize;
937 crp->crp_payload_length -= cse->ivsize;
941 if (cop->mac != NULL && crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
942 error = copyin(cop->mac, cod->buf + crp->crp_digest_start,
945 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
951 * Let the dispatch run unlocked, then, interlock against the
952 * callback before checking if the operation completed and going
953 * to sleep. This insures drivers don't inherit our lock which
954 * results in a lock order reversal between crypto_dispatch forced
955 * entry and the crypto_done callback into us.
957 error = crypto_dispatch(crp);
959 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
963 mtx_lock(&cse->lock);
965 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0);
966 mtx_unlock(&cse->lock);
968 if (crp->crp_etype == EAGAIN) {
970 crp->crp_flags &= ~CRYPTO_F_DONE;
975 if (crp->crp_etype != 0) {
976 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
977 error = crp->crp_etype;
981 if (cop->dst != NULL) {
982 error = copyout(cod->obuf != NULL ? cod->obuf :
983 cod->buf + crp->crp_payload_start, dst,
984 crp->crp_payload_length);
986 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
991 if (cop->mac != NULL && (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) == 0) {
992 error = copyout((cod->obuf != NULL ? cod->obuf : cod->buf) +
993 crp->crp_digest_start, cop->mac, cse->hashsize);
995 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1001 crypto_freereq(crp);
1008 cryptodev_aead(struct csession *cse, struct crypt_aead *caead)
1010 struct cryptop_data *cod = NULL;
1011 struct cryptop *crp = NULL;
1015 if (caead->len > 256*1024-4 || caead->aadlen > 256*1024-4) {
1016 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1020 if (cse->txform == NULL || cse->hashsize == 0 || caead->tag == NULL ||
1021 (caead->len % cse->txform->blocksize) != 0) {
1022 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1027 * The COP_F_CIPHER_FIRST flag predates explicit session
1028 * modes, but the only way it was used was for EtA so allow it
1029 * as long as it is consistent with EtA.
1031 if (caead->flags & COP_F_CIPHER_FIRST) {
1032 if (caead->op != COP_ENCRYPT) {
1033 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1038 cod = cod_alloc(cse, caead->aadlen, caead->len + cse->hashsize);
1041 crp = crypto_getreq(cse->cses, M_WAITOK);
1043 if (cod->aad != NULL)
1044 error = copyin(caead->aad, cod->aad, caead->aadlen);
1046 error = copyin(caead->aad, cod->buf, caead->aadlen);
1048 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1051 crp->crp_aad = cod->aad;
1052 crp->crp_aad_start = 0;
1053 crp->crp_aad_length = caead->aadlen;
1055 if (cod->aad != NULL)
1056 crp->crp_payload_start = 0;
1058 crp->crp_payload_start = caead->aadlen;
1059 error = copyin(caead->src, cod->buf + crp->crp_payload_start,
1062 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1065 crp->crp_payload_length = caead->len;
1066 if (caead->op == COP_ENCRYPT && cod->obuf != NULL)
1067 crp->crp_digest_start = crp->crp_payload_output_start +
1070 crp->crp_digest_start = crp->crp_payload_start + caead->len;
1072 switch (cse->mode) {
1075 switch (caead->op) {
1077 crp->crp_op = CRYPTO_OP_ENCRYPT |
1078 CRYPTO_OP_COMPUTE_DIGEST;
1081 crp->crp_op = CRYPTO_OP_DECRYPT |
1082 CRYPTO_OP_VERIFY_DIGEST;
1085 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1091 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1096 crp->crp_flags = CRYPTO_F_CBIMM | (caead->flags & COP_F_BATCH);
1097 crypto_use_buf(crp, cod->buf, crp->crp_payload_start + caead->len +
1099 if (cod->obuf != NULL)
1100 crypto_use_output_buf(crp, cod->obuf, caead->len +
1102 crp->crp_callback = cryptodev_cb;
1103 crp->crp_opaque = cod;
1107 * Permit a 16-byte IV for AES-XTS, but only use the
1108 * first 8 bytes as a block number.
1110 if (cse->mode == CSP_MODE_ETA &&
1111 caead->ivlen == AES_BLOCK_LEN &&
1112 cse->ivsize == AES_XTS_IV_LEN)
1113 caead->ivlen = AES_XTS_IV_LEN;
1115 if (caead->ivlen != cse->ivsize) {
1117 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1121 error = copyin(caead->iv, crp->crp_iv, cse->ivsize);
1123 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1126 crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
1128 crp->crp_iv_start = crp->crp_payload_start;
1129 crp->crp_payload_start += cse->ivsize;
1130 crp->crp_payload_length -= cse->ivsize;
1134 if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
1135 error = copyin(caead->tag, cod->buf + crp->crp_digest_start,
1138 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1144 * Let the dispatch run unlocked, then, interlock against the
1145 * callback before checking if the operation completed and going
1146 * to sleep. This insures drivers don't inherit our lock which
1147 * results in a lock order reversal between crypto_dispatch forced
1148 * entry and the crypto_done callback into us.
1150 error = crypto_dispatch(crp);
1152 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1156 mtx_lock(&cse->lock);
1158 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0);
1159 mtx_unlock(&cse->lock);
1161 if (crp->crp_etype == EAGAIN) {
1163 crp->crp_flags &= ~CRYPTO_F_DONE;
1168 if (crp->crp_etype != 0) {
1169 error = crp->crp_etype;
1170 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1174 if (caead->dst != NULL) {
1175 error = copyout(cod->obuf != NULL ? cod->obuf :
1176 cod->buf + crp->crp_payload_start, dst,
1177 crp->crp_payload_length);
1179 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1184 if ((crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) == 0) {
1185 error = copyout((cod->obuf != NULL ? cod->obuf : cod->buf) +
1186 crp->crp_digest_start, caead->tag, cse->hashsize);
1188 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1194 crypto_freereq(crp);
1201 cryptodevkey_cb(struct cryptkop *krp)
1208 cryptodev_key(struct crypt_kop *kop)
1210 struct cryptkop *krp = NULL;
1212 int in, out, size, i;
1214 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
1215 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1219 in = kop->crk_iparams;
1220 out = kop->crk_oparams;
1221 switch (kop->crk_op) {
1223 if (in == 3 && out == 1)
1225 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1227 case CRK_MOD_EXP_CRT:
1228 if (in == 6 && out == 1)
1230 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1233 if (in == 5 && out == 2)
1235 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1237 case CRK_DSA_VERIFY:
1238 if (in == 7 && out == 0)
1240 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1242 case CRK_DH_COMPUTE_KEY:
1243 if (in == 3 && out == 1)
1245 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1248 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1252 krp = malloc(sizeof(*krp), M_XDATA, M_WAITOK | M_ZERO);
1253 krp->krp_op = kop->crk_op;
1254 krp->krp_status = kop->crk_status;
1255 krp->krp_iparams = kop->crk_iparams;
1256 krp->krp_oparams = kop->crk_oparams;
1257 krp->krp_crid = kop->crk_crid;
1258 krp->krp_status = 0;
1259 krp->krp_callback = cryptodevkey_cb;
1261 for (i = 0; i < CRK_MAXPARAM; i++) {
1262 if (kop->crk_param[i].crp_nbits > 65536) {
1263 /* Limit is the same as in OpenBSD */
1264 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1267 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
1269 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
1270 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1273 krp->krp_param[i].crp_p = malloc(size, M_XDATA, M_WAITOK);
1274 if (i >= krp->krp_iparams)
1276 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
1278 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1283 error = crypto_kdispatch(krp);
1285 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1288 error = tsleep(krp, PSOCK, "crydev", 0);
1290 /* XXX can this happen? if so, how do we recover? */
1291 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1295 kop->crk_crid = krp->krp_hid; /* device that did the work */
1296 if (krp->krp_status != 0) {
1297 error = krp->krp_status;
1298 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1302 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
1303 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1306 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
1308 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1315 kop->crk_status = krp->krp_status;
1316 for (i = 0; i < CRK_MAXPARAM; i++) {
1317 if (krp->krp_param[i].crp_p)
1318 free(krp->krp_param[i].crp_p, M_XDATA);
1326 cryptodev_find(struct crypt_find_op *find)
1329 size_t fnlen = sizeof find->name;
1331 if (find->crid != -1) {
1332 dev = crypto_find_device_byhid(find->crid);
1335 strncpy(find->name, device_get_nameunit(dev), fnlen);
1336 find->name[fnlen - 1] = '\x0';
1338 find->name[fnlen - 1] = '\x0';
1339 find->crid = crypto_find_driver(find->name);
1340 if (find->crid == -1)
1347 fcrypt_dtor(void *data)
1349 struct fcrypt *fcr = data;
1350 struct csession *cse;
1352 while ((cse = TAILQ_FIRST(&fcr->csessions))) {
1353 TAILQ_REMOVE(&fcr->csessions, cse, next);
1354 KASSERT(refcount_load(&cse->refs) == 1,
1355 ("%s: crypto session %p with %d refs", __func__, cse,
1356 refcount_load(&cse->refs)));
1359 mtx_destroy(&fcr->lock);
1364 crypto_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
1369 fcr = malloc(sizeof(struct fcrypt), M_XDATA, M_WAITOK | M_ZERO);
1370 TAILQ_INIT(&fcr->csessions);
1371 mtx_init(&fcr->lock, "fcrypt", NULL, MTX_DEF);
1372 error = devfs_set_cdevpriv(fcr, fcrypt_dtor);
1379 crypto_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
1382 static struct timeval keywarn, featwarn;
1384 struct csession *cse;
1385 struct session2_op *sop;
1386 struct crypt_op *cop;
1387 struct crypt_aead *caead;
1388 struct crypt_kop *kop;
1392 struct session2_op sopc;
1393 #ifdef COMPAT_FREEBSD32
1394 struct crypt_op copc;
1395 struct crypt_aead aeadc;
1396 struct crypt_kop kopc;
1399 #ifdef COMPAT_FREEBSD32
1406 case CIOCGSESSION32:
1410 data = (void *)&thunk.sopc;
1411 session_op_from_32((struct session_op32 *)data32, &thunk.sopc);
1413 case CIOCGSESSION232:
1416 cmd = CIOCGSESSION2;
1417 data = (void *)&thunk.sopc;
1418 session2_op_from_32((struct session2_op32 *)data32,
1425 data = (void *)&thunk.copc;
1426 crypt_op_from_32((struct crypt_op32 *)data32, &thunk.copc);
1428 case CIOCCRYPTAEAD32:
1431 cmd = CIOCCRYPTAEAD;
1432 data = (void *)&thunk.aeadc;
1433 crypt_aead_from_32((struct crypt_aead32 *)data32, &thunk.aeadc);
1439 if (cmd == CIOCKEY32)
1443 data = (void *)&thunk.kopc;
1444 crypt_kop_from_32((struct crypt_kop32 *)data32, &thunk.kopc);
1449 devfs_get_cdevpriv((void **)&fcr);
1452 #ifdef COMPAT_FREEBSD12
1455 * NB: This may fail in cases that the old
1456 * implementation did not if the current process has
1457 * restricted filesystem access (e.g. running in a
1458 * jail that does not expose /dev/crypto or in
1461 error = kern_openat(td, AT_FDCWD, "/dev/crypto", UIO_SYSSPACE,
1464 *(uint32_t *)data = td->td_retval[0];
1469 if (cmd == CIOCGSESSION) {
1470 session2_op_from_op((void *)data, &thunk.sopc);
1473 sop = (struct session2_op *)data;
1475 error = cse_create(fcr, sop);
1476 if (cmd == CIOCGSESSION && error == 0)
1477 session2_op_to_op(sop, (void *)data);
1480 ses = *(uint32_t *)data;
1481 if (!cse_delete(fcr, ses)) {
1482 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1487 cop = (struct crypt_op *)data;
1488 cse = cse_find(fcr, cop->ses);
1490 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1493 error = cryptodev_op(cse, cop);
1498 if (ratecheck(&keywarn, &warninterval))
1500 "Asymmetric crypto operations via /dev/crypto");
1502 if (!crypto_userasymcrypto) {
1503 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1504 return (EPERM); /* XXX compat? */
1506 kop = (struct crypt_kop *)data;
1507 if (cmd == CIOCKEY) {
1508 /* NB: crypto core enforces s/w driver use */
1510 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
1513 error = cryptodev_key(kop);
1517 if (ratecheck(&featwarn, &warninterval))
1519 "Asymmetric crypto features via /dev/crypto");
1521 if (!crypto_userasymcrypto) {
1523 * NB: if user asym crypto operations are
1524 * not permitted return "no algorithms"
1525 * so well-behaved applications will just
1526 * fallback to doing them in software.
1530 error = crypto_getfeat((int *)data);
1532 SDT_PROBE1(opencrypto, dev, ioctl, error,
1537 error = cryptodev_find((struct crypt_find_op *)data);
1540 caead = (struct crypt_aead *)data;
1541 cse = cse_find(fcr, caead->ses);
1543 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1546 error = cryptodev_aead(cse, caead);
1551 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1555 #ifdef COMPAT_FREEBSD32
1557 case CIOCGSESSION32:
1559 session_op_to_32((void *)data, data32);
1561 case CIOCGSESSION232:
1563 session2_op_to_32((void *)data, data32);
1567 crypt_op_to_32((void *)data, data32);
1569 case CIOCCRYPTAEAD32:
1571 crypt_aead_to_32((void *)data, data32);
1575 crypt_kop_to_32((void *)data, data32);
1582 static struct cdevsw crypto_cdevsw = {
1583 .d_version = D_VERSION,
1584 .d_open = crypto_open,
1585 .d_ioctl = crypto_ioctl,
1588 static struct cdev *crypto_dev;
1591 * Initialization code, both for static and dynamic loading.
1594 cryptodev_modevent(module_t mod, int type, void *unused)
1599 printf("crypto: <crypto device>\n");
1600 crypto_dev = make_dev(&crypto_cdevsw, 0,
1601 UID_ROOT, GID_WHEEL, 0666,
1605 /*XXX disallow if active sessions */
1606 destroy_dev(crypto_dev);
1612 static moduledata_t cryptodev_mod = {
1617 MODULE_VERSION(cryptodev, 1);
1618 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1619 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
1620 MODULE_DEPEND(cryptodev, zlib, 1, 1, 1);