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 if (crp->crp_payload_length < cse->ivsize) {
936 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
940 crp->crp_iv_start = 0;
941 crp->crp_payload_start += cse->ivsize;
942 crp->crp_payload_length -= cse->ivsize;
946 if (cop->mac != NULL && crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
947 error = copyin(cop->mac, cod->buf + crp->crp_digest_start,
950 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
956 * Let the dispatch run unlocked, then, interlock against the
957 * callback before checking if the operation completed and going
958 * to sleep. This insures drivers don't inherit our lock which
959 * results in a lock order reversal between crypto_dispatch forced
960 * entry and the crypto_done callback into us.
962 error = crypto_dispatch(crp);
964 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
968 mtx_lock(&cse->lock);
970 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0);
971 mtx_unlock(&cse->lock);
973 if (crp->crp_etype == EAGAIN) {
975 crp->crp_flags &= ~CRYPTO_F_DONE;
980 if (crp->crp_etype != 0) {
981 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
982 error = crp->crp_etype;
986 if (cop->dst != NULL) {
987 error = copyout(cod->obuf != NULL ? cod->obuf :
988 cod->buf + crp->crp_payload_start, dst,
989 crp->crp_payload_length);
991 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
996 if (cop->mac != NULL && (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) == 0) {
997 error = copyout((cod->obuf != NULL ? cod->obuf : cod->buf) +
998 crp->crp_digest_start, cop->mac, cse->hashsize);
1000 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1006 crypto_freereq(crp);
1013 cryptodev_aead(struct csession *cse, struct crypt_aead *caead)
1015 struct cryptop_data *cod = NULL;
1016 struct cryptop *crp = NULL;
1020 if (caead->len > 256*1024-4 || caead->aadlen > 256*1024-4) {
1021 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1025 if (cse->txform == NULL || cse->hashsize == 0 || caead->tag == NULL ||
1026 (caead->len % cse->txform->blocksize) != 0) {
1027 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1032 * The COP_F_CIPHER_FIRST flag predates explicit session
1033 * modes, but the only way it was used was for EtA so allow it
1034 * as long as it is consistent with EtA.
1036 if (caead->flags & COP_F_CIPHER_FIRST) {
1037 if (caead->op != COP_ENCRYPT) {
1038 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1043 cod = cod_alloc(cse, caead->aadlen, caead->len + cse->hashsize);
1046 crp = crypto_getreq(cse->cses, M_WAITOK);
1048 if (cod->aad != NULL)
1049 error = copyin(caead->aad, cod->aad, caead->aadlen);
1051 error = copyin(caead->aad, cod->buf, caead->aadlen);
1053 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1056 crp->crp_aad = cod->aad;
1057 crp->crp_aad_start = 0;
1058 crp->crp_aad_length = caead->aadlen;
1060 if (cod->aad != NULL)
1061 crp->crp_payload_start = 0;
1063 crp->crp_payload_start = caead->aadlen;
1064 error = copyin(caead->src, cod->buf + crp->crp_payload_start,
1067 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1070 crp->crp_payload_length = caead->len;
1071 if (caead->op == COP_ENCRYPT && cod->obuf != NULL)
1072 crp->crp_digest_start = crp->crp_payload_output_start +
1075 crp->crp_digest_start = crp->crp_payload_start + caead->len;
1077 switch (cse->mode) {
1080 switch (caead->op) {
1082 crp->crp_op = CRYPTO_OP_ENCRYPT |
1083 CRYPTO_OP_COMPUTE_DIGEST;
1086 crp->crp_op = CRYPTO_OP_DECRYPT |
1087 CRYPTO_OP_VERIFY_DIGEST;
1090 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1096 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1101 crp->crp_flags = CRYPTO_F_CBIMM | (caead->flags & COP_F_BATCH);
1102 crypto_use_buf(crp, cod->buf, crp->crp_payload_start + caead->len +
1104 if (cod->obuf != NULL)
1105 crypto_use_output_buf(crp, cod->obuf, caead->len +
1107 crp->crp_callback = cryptodev_cb;
1108 crp->crp_opaque = cod;
1112 * Permit a 16-byte IV for AES-XTS, but only use the
1113 * first 8 bytes as a block number.
1115 if (cse->mode == CSP_MODE_ETA &&
1116 caead->ivlen == AES_BLOCK_LEN &&
1117 cse->ivsize == AES_XTS_IV_LEN)
1118 caead->ivlen = AES_XTS_IV_LEN;
1120 if (cse->ivsize == 0) {
1121 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1125 if (caead->ivlen != cse->ivsize) {
1127 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1131 error = copyin(caead->iv, crp->crp_iv, cse->ivsize);
1133 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1136 crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
1139 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1143 if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
1144 error = copyin(caead->tag, cod->buf + crp->crp_digest_start,
1147 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1153 * Let the dispatch run unlocked, then, interlock against the
1154 * callback before checking if the operation completed and going
1155 * to sleep. This insures drivers don't inherit our lock which
1156 * results in a lock order reversal between crypto_dispatch forced
1157 * entry and the crypto_done callback into us.
1159 error = crypto_dispatch(crp);
1161 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1165 mtx_lock(&cse->lock);
1167 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0);
1168 mtx_unlock(&cse->lock);
1170 if (crp->crp_etype == EAGAIN) {
1172 crp->crp_flags &= ~CRYPTO_F_DONE;
1177 if (crp->crp_etype != 0) {
1178 error = crp->crp_etype;
1179 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1183 if (caead->dst != NULL) {
1184 error = copyout(cod->obuf != NULL ? cod->obuf :
1185 cod->buf + crp->crp_payload_start, dst,
1186 crp->crp_payload_length);
1188 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1193 if ((crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) == 0) {
1194 error = copyout((cod->obuf != NULL ? cod->obuf : cod->buf) +
1195 crp->crp_digest_start, caead->tag, cse->hashsize);
1197 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1203 crypto_freereq(crp);
1210 cryptodevkey_cb(struct cryptkop *krp)
1217 cryptodev_key(struct crypt_kop *kop)
1219 struct cryptkop *krp = NULL;
1221 int in, out, size, i;
1223 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
1224 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1228 in = kop->crk_iparams;
1229 out = kop->crk_oparams;
1230 switch (kop->crk_op) {
1232 if (in == 3 && out == 1)
1234 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1236 case CRK_MOD_EXP_CRT:
1237 if (in == 6 && out == 1)
1239 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1242 if (in == 5 && out == 2)
1244 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1246 case CRK_DSA_VERIFY:
1247 if (in == 7 && out == 0)
1249 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1251 case CRK_DH_COMPUTE_KEY:
1252 if (in == 3 && out == 1)
1254 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1257 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1261 krp = malloc(sizeof(*krp), M_XDATA, M_WAITOK | M_ZERO);
1262 krp->krp_op = kop->crk_op;
1263 krp->krp_status = kop->crk_status;
1264 krp->krp_iparams = kop->crk_iparams;
1265 krp->krp_oparams = kop->crk_oparams;
1266 krp->krp_crid = kop->crk_crid;
1267 krp->krp_status = 0;
1268 krp->krp_callback = cryptodevkey_cb;
1270 for (i = 0; i < CRK_MAXPARAM; i++) {
1271 if (kop->crk_param[i].crp_nbits > 65536) {
1272 /* Limit is the same as in OpenBSD */
1273 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1276 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
1278 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
1279 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1282 krp->krp_param[i].crp_p = malloc(size, M_XDATA, M_WAITOK);
1283 if (i >= krp->krp_iparams)
1285 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
1287 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1292 error = crypto_kdispatch(krp);
1294 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1297 error = tsleep(krp, PSOCK, "crydev", 0);
1299 /* XXX can this happen? if so, how do we recover? */
1300 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1304 kop->crk_crid = krp->krp_hid; /* device that did the work */
1305 if (krp->krp_status != 0) {
1306 error = krp->krp_status;
1307 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1311 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
1312 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1315 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
1317 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1324 kop->crk_status = krp->krp_status;
1325 for (i = 0; i < CRK_MAXPARAM; i++) {
1326 if (krp->krp_param[i].crp_p)
1327 free(krp->krp_param[i].crp_p, M_XDATA);
1335 cryptodev_find(struct crypt_find_op *find)
1338 size_t fnlen = sizeof find->name;
1340 if (find->crid != -1) {
1341 dev = crypto_find_device_byhid(find->crid);
1344 strncpy(find->name, device_get_nameunit(dev), fnlen);
1345 find->name[fnlen - 1] = '\x0';
1347 find->name[fnlen - 1] = '\x0';
1348 find->crid = crypto_find_driver(find->name);
1349 if (find->crid == -1)
1356 fcrypt_dtor(void *data)
1358 struct fcrypt *fcr = data;
1359 struct csession *cse;
1361 while ((cse = TAILQ_FIRST(&fcr->csessions))) {
1362 TAILQ_REMOVE(&fcr->csessions, cse, next);
1363 KASSERT(refcount_load(&cse->refs) == 1,
1364 ("%s: crypto session %p with %d refs", __func__, cse,
1365 refcount_load(&cse->refs)));
1368 mtx_destroy(&fcr->lock);
1373 crypto_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
1378 fcr = malloc(sizeof(struct fcrypt), M_XDATA, M_WAITOK | M_ZERO);
1379 TAILQ_INIT(&fcr->csessions);
1380 mtx_init(&fcr->lock, "fcrypt", NULL, MTX_DEF);
1381 error = devfs_set_cdevpriv(fcr, fcrypt_dtor);
1388 crypto_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
1391 static struct timeval keywarn, featwarn;
1393 struct csession *cse;
1394 struct session2_op *sop;
1395 struct crypt_op *cop;
1396 struct crypt_aead *caead;
1397 struct crypt_kop *kop;
1401 struct session2_op sopc;
1402 #ifdef COMPAT_FREEBSD32
1403 struct crypt_op copc;
1404 struct crypt_aead aeadc;
1405 struct crypt_kop kopc;
1408 #ifdef COMPAT_FREEBSD32
1415 case CIOCGSESSION32:
1419 data = (void *)&thunk.sopc;
1420 session_op_from_32((struct session_op32 *)data32, &thunk.sopc);
1422 case CIOCGSESSION232:
1425 cmd = CIOCGSESSION2;
1426 data = (void *)&thunk.sopc;
1427 session2_op_from_32((struct session2_op32 *)data32,
1434 data = (void *)&thunk.copc;
1435 crypt_op_from_32((struct crypt_op32 *)data32, &thunk.copc);
1437 case CIOCCRYPTAEAD32:
1440 cmd = CIOCCRYPTAEAD;
1441 data = (void *)&thunk.aeadc;
1442 crypt_aead_from_32((struct crypt_aead32 *)data32, &thunk.aeadc);
1448 if (cmd == CIOCKEY32)
1452 data = (void *)&thunk.kopc;
1453 crypt_kop_from_32((struct crypt_kop32 *)data32, &thunk.kopc);
1458 devfs_get_cdevpriv((void **)&fcr);
1461 #ifdef COMPAT_FREEBSD12
1464 * NB: This may fail in cases that the old
1465 * implementation did not if the current process has
1466 * restricted filesystem access (e.g. running in a
1467 * jail that does not expose /dev/crypto or in
1470 error = kern_openat(td, AT_FDCWD, "/dev/crypto", UIO_SYSSPACE,
1473 *(uint32_t *)data = td->td_retval[0];
1478 if (cmd == CIOCGSESSION) {
1479 session2_op_from_op((void *)data, &thunk.sopc);
1482 sop = (struct session2_op *)data;
1484 error = cse_create(fcr, sop);
1485 if (cmd == CIOCGSESSION && error == 0)
1486 session2_op_to_op(sop, (void *)data);
1489 ses = *(uint32_t *)data;
1490 if (!cse_delete(fcr, ses)) {
1491 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1496 cop = (struct crypt_op *)data;
1497 cse = cse_find(fcr, cop->ses);
1499 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1502 error = cryptodev_op(cse, cop);
1507 if (ratecheck(&keywarn, &warninterval))
1509 "Asymmetric crypto operations via /dev/crypto");
1511 if (!crypto_userasymcrypto) {
1512 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1513 return (EPERM); /* XXX compat? */
1515 kop = (struct crypt_kop *)data;
1516 if (cmd == CIOCKEY) {
1517 /* NB: crypto core enforces s/w driver use */
1519 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
1522 error = cryptodev_key(kop);
1526 if (ratecheck(&featwarn, &warninterval))
1528 "Asymmetric crypto features via /dev/crypto");
1530 if (!crypto_userasymcrypto) {
1532 * NB: if user asym crypto operations are
1533 * not permitted return "no algorithms"
1534 * so well-behaved applications will just
1535 * fallback to doing them in software.
1539 error = crypto_getfeat((int *)data);
1541 SDT_PROBE1(opencrypto, dev, ioctl, error,
1546 error = cryptodev_find((struct crypt_find_op *)data);
1549 caead = (struct crypt_aead *)data;
1550 cse = cse_find(fcr, caead->ses);
1552 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1555 error = cryptodev_aead(cse, caead);
1560 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1564 #ifdef COMPAT_FREEBSD32
1566 case CIOCGSESSION32:
1568 session_op_to_32((void *)data, data32);
1570 case CIOCGSESSION232:
1572 session2_op_to_32((void *)data, data32);
1576 crypt_op_to_32((void *)data, data32);
1578 case CIOCCRYPTAEAD32:
1580 crypt_aead_to_32((void *)data, data32);
1584 crypt_kop_to_32((void *)data, data32);
1591 static struct cdevsw crypto_cdevsw = {
1592 .d_version = D_VERSION,
1593 .d_open = crypto_open,
1594 .d_ioctl = crypto_ioctl,
1597 static struct cdev *crypto_dev;
1600 * Initialization code, both for static and dynamic loading.
1603 cryptodev_modevent(module_t mod, int type, void *unused)
1608 printf("crypto: <crypto device>\n");
1609 crypto_dev = make_dev(&crypto_cdevsw, 0,
1610 UID_ROOT, GID_WHEEL, 0666,
1614 /*XXX disallow if active sessions */
1615 destroy_dev(crypto_dev);
1621 static moduledata_t cryptodev_mod = {
1626 MODULE_VERSION(cryptodev, 1);
1627 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1628 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
1629 MODULE_DEPEND(cryptodev, zlib, 1, 1, 1);