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>
50 #include <sys/sysctl.h>
52 #include <sys/filedesc.h>
53 #include <sys/errno.h>
54 #include <sys/random.h>
56 #include <sys/kernel.h>
57 #include <sys/module.h>
58 #include <sys/fcntl.h>
63 #include <opencrypto/cryptodev.h>
64 #include <opencrypto/xform.h>
66 SDT_PROVIDER_DECLARE(opencrypto);
68 SDT_PROBE_DEFINE1(opencrypto, dev, ioctl, error, "int"/*line number*/);
70 #ifdef COMPAT_FREEBSD32
71 #include <sys/mount.h>
72 #include <compat/freebsd32/freebsd32.h>
84 struct session2_op32 {
117 struct crparam32 crk_param[CRK_MAXPARAM];
120 struct cryptotstat32 {
121 struct timespec32 acc;
122 struct timespec32 min;
123 struct timespec32 max;
127 struct cryptostats32 {
135 u_int32_t cs_kblocks;
136 struct cryptotstat32 cs_invoke;
137 struct cryptotstat32 cs_done;
138 struct cryptotstat32 cs_cb;
139 struct cryptotstat32 cs_finis;
142 #define CIOCGSESSION32 _IOWR('c', 101, struct session_op32)
143 #define CIOCCRYPT32 _IOWR('c', 103, struct crypt_op32)
144 #define CIOCKEY32 _IOWR('c', 104, struct crypt_kop32)
145 #define CIOCGSESSION232 _IOWR('c', 106, struct session2_op32)
146 #define CIOCKEY232 _IOWR('c', 107, struct crypt_kop32)
149 session_op_from_32(const struct session_op32 *from, struct session_op *to)
152 CP(*from, *to, cipher);
154 CP(*from, *to, keylen);
155 PTRIN_CP(*from, *to, key);
156 CP(*from, *to, mackeylen);
157 PTRIN_CP(*from, *to, mackey);
162 session2_op_from_32(const struct session2_op32 *from, struct session2_op *to)
165 session_op_from_32((const struct session_op32 *)from,
166 (struct session_op *)to);
167 CP(*from, *to, crid);
171 session_op_to_32(const struct session_op *from, struct session_op32 *to)
174 CP(*from, *to, cipher);
176 CP(*from, *to, keylen);
177 PTROUT_CP(*from, *to, key);
178 CP(*from, *to, mackeylen);
179 PTROUT_CP(*from, *to, mackey);
184 session2_op_to_32(const struct session2_op *from, struct session2_op32 *to)
187 session_op_to_32((const struct session_op *)from,
188 (struct session_op32 *)to);
189 CP(*from, *to, crid);
193 crypt_op_from_32(const struct crypt_op32 *from, struct crypt_op *to)
198 CP(*from, *to, flags);
200 PTRIN_CP(*from, *to, src);
201 PTRIN_CP(*from, *to, dst);
202 PTRIN_CP(*from, *to, mac);
203 PTRIN_CP(*from, *to, iv);
207 crypt_op_to_32(const struct crypt_op *from, struct crypt_op32 *to)
212 CP(*from, *to, flags);
214 PTROUT_CP(*from, *to, src);
215 PTROUT_CP(*from, *to, dst);
216 PTROUT_CP(*from, *to, mac);
217 PTROUT_CP(*from, *to, iv);
221 crparam_from_32(const struct crparam32 *from, struct crparam *to)
224 PTRIN_CP(*from, *to, crp_p);
225 CP(*from, *to, crp_nbits);
229 crparam_to_32(const struct crparam *from, struct crparam32 *to)
232 PTROUT_CP(*from, *to, crp_p);
233 CP(*from, *to, crp_nbits);
237 crypt_kop_from_32(const struct crypt_kop32 *from, struct crypt_kop *to)
241 CP(*from, *to, crk_op);
242 CP(*from, *to, crk_status);
243 CP(*from, *to, crk_iparams);
244 CP(*from, *to, crk_oparams);
245 CP(*from, *to, crk_crid);
246 for (i = 0; i < CRK_MAXPARAM; i++)
247 crparam_from_32(&from->crk_param[i], &to->crk_param[i]);
251 crypt_kop_to_32(const struct crypt_kop *from, struct crypt_kop32 *to)
255 CP(*from, *to, crk_op);
256 CP(*from, *to, crk_status);
257 CP(*from, *to, crk_iparams);
258 CP(*from, *to, crk_oparams);
259 CP(*from, *to, crk_crid);
260 for (i = 0; i < CRK_MAXPARAM; i++)
261 crparam_to_32(&from->crk_param[i], &to->crk_param[i]);
266 TAILQ_ENTRY(csession) next;
267 crypto_session_t cses;
270 struct mtx lock; /* for op submission */
272 struct enc_xform *txform;
281 struct cryptop_data {
282 struct csession *cse;
289 TAILQ_HEAD(csessionlist, csession) csessions;
294 static int cryptof_ioctl(struct file *, u_long, void *,
295 struct ucred *, struct thread *);
296 static int cryptof_stat(struct file *, struct stat *,
297 struct ucred *, struct thread *);
298 static int cryptof_close(struct file *, struct thread *);
299 static int cryptof_fill_kinfo(struct file *, struct kinfo_file *,
302 static struct fileops cryptofops = {
303 .fo_read = invfo_rdwr,
304 .fo_write = invfo_rdwr,
305 .fo_truncate = invfo_truncate,
306 .fo_ioctl = cryptof_ioctl,
307 .fo_poll = invfo_poll,
308 .fo_kqfilter = invfo_kqfilter,
309 .fo_stat = cryptof_stat,
310 .fo_close = cryptof_close,
311 .fo_chmod = invfo_chmod,
312 .fo_chown = invfo_chown,
313 .fo_sendfile = invfo_sendfile,
314 .fo_fill_kinfo = cryptof_fill_kinfo,
317 static struct csession *csefind(struct fcrypt *, u_int);
318 static bool csedelete(struct fcrypt *, u_int);
319 static struct csession *csecreate(struct fcrypt *, crypto_session_t,
320 struct crypto_session_params *, struct enc_xform *, void *,
321 struct auth_hash *, void *);
322 static void csefree(struct csession *);
324 static int cryptodev_op(struct csession *, struct crypt_op *,
325 struct ucred *, struct thread *td);
326 static int cryptodev_aead(struct csession *, struct crypt_aead *,
327 struct ucred *, struct thread *);
328 static int cryptodev_key(struct crypt_kop *);
329 static int cryptodev_find(struct crypt_find_op *);
332 * Check a crypto identifier to see if it requested
333 * a software device/driver. This can be done either
334 * by device name/class or through search constraints.
337 checkforsoftware(int *cridp)
343 if (!crypto_devallowsoft) {
344 if (crid & CRYPTOCAP_F_SOFTWARE) {
345 if (crid & CRYPTOCAP_F_HARDWARE) {
346 *cridp = CRYPTOCAP_F_HARDWARE;
351 if ((crid & CRYPTOCAP_F_HARDWARE) == 0 &&
352 (crypto_getcaps(crid) & CRYPTOCAP_F_HARDWARE) == 0)
364 struct ucred *active_cred,
367 #define SES2(p) ((struct session2_op *)p)
368 struct crypto_session_params csp;
369 struct fcrypt *fcr = fp->f_data;
370 struct csession *cse;
371 struct session_op *sop;
372 struct crypt_op *cop;
373 struct crypt_aead *caead;
374 struct enc_xform *txform = NULL;
375 struct auth_hash *thash = NULL;
378 struct crypt_kop *kop;
379 crypto_session_t cses;
382 #ifdef COMPAT_FREEBSD32
383 struct session2_op sopc;
384 struct crypt_op copc;
385 struct crypt_kop kopc;
391 #ifdef COMPAT_FREEBSD32
393 case CIOCGSESSION232:
394 if (cmd == CIOCGSESSION32) {
395 session_op_from_32(data, (struct session_op *)&sopc);
396 sop = (struct session_op *)&sopc;
397 } else if (cmd == CIOCGSESSION232) {
398 session2_op_from_32(data, &sopc);
399 sop = (struct session_op *)&sopc;
402 sop = (struct session_op *)data;
403 switch (sop->cipher) {
407 txform = &enc_xform_rijndael128;
410 txform = &enc_xform_aes_xts;
412 case CRYPTO_NULL_CBC:
413 txform = &enc_xform_null;
415 case CRYPTO_CAMELLIA_CBC:
416 txform = &enc_xform_camellia;
419 txform = &enc_xform_aes_icm;
421 case CRYPTO_AES_NIST_GCM_16:
422 txform = &enc_xform_aes_nist_gcm;
424 case CRYPTO_CHACHA20:
425 txform = &enc_xform_chacha20;
427 case CRYPTO_AES_CCM_16:
428 txform = &enc_xform_ccm;
432 CRYPTDEB("invalid cipher");
433 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
440 case CRYPTO_POLY1305:
441 thash = &auth_hash_poly1305;
443 case CRYPTO_SHA1_HMAC:
444 thash = &auth_hash_hmac_sha1;
446 case CRYPTO_SHA2_224_HMAC:
447 thash = &auth_hash_hmac_sha2_224;
449 case CRYPTO_SHA2_256_HMAC:
450 thash = &auth_hash_hmac_sha2_256;
452 case CRYPTO_SHA2_384_HMAC:
453 thash = &auth_hash_hmac_sha2_384;
455 case CRYPTO_SHA2_512_HMAC:
456 thash = &auth_hash_hmac_sha2_512;
458 case CRYPTO_RIPEMD160_HMAC:
459 thash = &auth_hash_hmac_ripemd_160;
461 #ifdef COMPAT_FREEBSD12
462 case CRYPTO_AES_128_NIST_GMAC:
463 case CRYPTO_AES_192_NIST_GMAC:
464 case CRYPTO_AES_256_NIST_GMAC:
465 /* Should always be paired with GCM. */
466 if (sop->cipher != CRYPTO_AES_NIST_GCM_16) {
467 CRYPTDEB("GMAC without GCM");
468 SDT_PROBE1(opencrypto, dev, ioctl, error,
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,
492 case CRYPTO_AES_CCM_CBC_MAC:
493 switch (sop->mackeylen) {
495 thash = &auth_hash_ccm_cbc_mac_128;
498 thash = &auth_hash_ccm_cbc_mac_192;
501 thash = &auth_hash_ccm_cbc_mac_256;
504 CRYPTDEB("Invalid CBC MAC key size %d",
506 SDT_PROBE1(opencrypto, dev, ioctl,
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));
551 if (sop->cipher == CRYPTO_AES_NIST_GCM_16) {
553 #ifdef COMPAT_FREEBSD12
554 case CRYPTO_AES_128_NIST_GMAC:
555 case CRYPTO_AES_192_NIST_GMAC:
556 case CRYPTO_AES_256_NIST_GMAC:
557 if (sop->keylen != sop->mackeylen) {
558 SDT_PROBE1(opencrypto, dev, ioctl,
567 SDT_PROBE1(opencrypto, dev, ioctl, error,
571 csp.csp_mode = CSP_MODE_AEAD;
572 } else if (sop->cipher == CRYPTO_AES_CCM_16) {
574 #ifdef COMPAT_FREEBSD12
575 case CRYPTO_AES_CCM_CBC_MAC:
576 if (sop->keylen != sop->mackeylen) {
577 SDT_PROBE1(opencrypto, dev, ioctl,
587 SDT_PROBE1(opencrypto, dev, ioctl, error,
591 csp.csp_mode = CSP_MODE_AEAD;
592 } else if (txform && thash)
593 csp.csp_mode = CSP_MODE_ETA;
595 csp.csp_mode = CSP_MODE_CIPHER;
597 csp.csp_mode = CSP_MODE_DIGEST;
600 csp.csp_cipher_alg = txform->type;
601 csp.csp_cipher_klen = sop->keylen;
602 if (sop->keylen > txform->maxkey ||
603 sop->keylen < txform->minkey) {
604 CRYPTDEB("invalid cipher parameters");
606 SDT_PROBE1(opencrypto, dev, ioctl, error,
611 key = malloc(csp.csp_cipher_klen, M_XDATA, M_WAITOK);
612 error = copyin(sop->key, key, csp.csp_cipher_klen);
614 CRYPTDEB("invalid key");
615 SDT_PROBE1(opencrypto, dev, ioctl, error,
619 csp.csp_cipher_key = key;
620 csp.csp_ivlen = txform->ivsize;
624 csp.csp_auth_alg = thash->type;
625 csp.csp_auth_klen = sop->mackeylen;
626 if (sop->mackeylen > thash->keysize ||
627 sop->mackeylen < 0) {
628 CRYPTDEB("invalid mac key length");
630 SDT_PROBE1(opencrypto, dev, ioctl, error,
635 if (csp.csp_auth_klen) {
636 mackey = malloc(csp.csp_auth_klen, M_XDATA,
638 error = copyin(sop->mackey, mackey,
641 CRYPTDEB("invalid mac key");
642 SDT_PROBE1(opencrypto, dev, ioctl,
646 csp.csp_auth_key = mackey;
649 if (csp.csp_auth_alg == CRYPTO_AES_NIST_GMAC)
650 csp.csp_ivlen = AES_GCM_IV_LEN;
651 if (csp.csp_auth_alg == CRYPTO_AES_CCM_CBC_MAC)
652 csp.csp_ivlen = AES_CCM_IV_LEN;
655 /* NB: CIOCGSESSION2 has the crid */
656 if (cmd == CIOCGSESSION2
657 #ifdef COMPAT_FREEBSD32
658 || cmd == CIOCGSESSION232
661 crid = SES2(sop)->crid;
662 error = checkforsoftware(&crid);
664 CRYPTDEB("checkforsoftware");
665 SDT_PROBE1(opencrypto, dev, ioctl, error,
670 crid = CRYPTOCAP_F_HARDWARE;
671 error = crypto_newsession(&cses, &csp, crid);
673 CRYPTDEB("crypto_newsession");
674 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
678 cse = csecreate(fcr, cses, &csp, txform, key, thash, mackey);
681 crypto_freesession(cses);
683 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
684 CRYPTDEB("csecreate");
688 if (cmd == CIOCGSESSION2
689 #ifdef COMPAT_FREEBSD32
690 || cmd == CIOCGSESSION232
693 /* return hardware/driver id */
694 SES2(sop)->crid = crypto_ses2hid(cse->cses);
699 free(mackey, M_XDATA);
701 #ifdef COMPAT_FREEBSD32
703 if (cmd == CIOCGSESSION32)
704 session_op_to_32(sop, data);
705 else if (cmd == CIOCGSESSION232)
706 session2_op_to_32((struct session2_op *)sop,
712 ses = *(u_int32_t *)data;
713 if (!csedelete(fcr, ses)) {
714 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
719 #ifdef COMPAT_FREEBSD32
721 if (cmd == CIOCCRYPT32) {
723 crypt_op_from_32(data, cop);
726 cop = (struct crypt_op *)data;
727 cse = csefind(fcr, cop->ses);
729 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
732 error = cryptodev_op(cse, cop, active_cred, td);
734 #ifdef COMPAT_FREEBSD32
735 if (error == 0 && cmd == CIOCCRYPT32)
736 crypt_op_to_32(cop, data);
741 #ifdef COMPAT_FREEBSD32
745 if (!crypto_userasymcrypto) {
746 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
747 return (EPERM); /* XXX compat? */
749 #ifdef COMPAT_FREEBSD32
750 if (cmd == CIOCKEY32 || cmd == CIOCKEY232) {
752 crypt_kop_from_32(data, kop);
755 kop = (struct crypt_kop *)data;
757 #ifdef COMPAT_FREEBSD32
761 /* NB: crypto core enforces s/w driver use */
763 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
766 error = cryptodev_key(kop);
768 #ifdef COMPAT_FREEBSD32
769 if (cmd == CIOCKEY32 || cmd == CIOCKEY232)
770 crypt_kop_to_32(kop, data);
774 if (!crypto_userasymcrypto) {
776 * NB: if user asym crypto operations are
777 * not permitted return "no algorithms"
778 * so well-behaved applications will just
779 * fallback to doing them in software.
783 error = crypto_getfeat((int *)data);
785 SDT_PROBE1(opencrypto, dev, ioctl, error,
790 error = cryptodev_find((struct crypt_find_op *)data);
793 caead = (struct crypt_aead *)data;
794 cse = csefind(fcr, caead->ses);
796 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
799 error = cryptodev_aead(cse, caead, active_cred, td);
804 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
811 static int cryptodev_cb(struct cryptop *);
813 static struct cryptop_data *
814 cod_alloc(struct csession *cse, size_t len, struct thread *td)
816 struct cryptop_data *cod;
818 cod = malloc(sizeof(struct cryptop_data), M_XDATA, M_WAITOK | M_ZERO);
821 cod->buf = malloc(len, M_XDATA, M_WAITOK);
826 cod_free(struct cryptop_data *cod)
829 free(cod->buf, M_XDATA);
835 struct csession *cse,
836 struct crypt_op *cop,
837 struct ucred *active_cred,
840 struct cryptop_data *cod = NULL;
841 struct cryptop *crp = NULL;
844 if (cop->len > 256*1024-4) {
845 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
850 if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0) {
851 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
856 if (cop->mac && cse->hashsize == 0) {
857 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
862 * The COP_F_CIPHER_FIRST flag predates explicit session
863 * modes, but the only way it was used was for EtA so allow it
864 * as long as it is consistent with EtA.
866 if (cop->flags & COP_F_CIPHER_FIRST) {
867 if (cop->op != COP_ENCRYPT) {
868 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
873 cod = cod_alloc(cse, cop->len + cse->hashsize, td);
875 crp = crypto_getreq(cse->cses, M_WAITOK);
877 error = copyin(cop->src, cod->buf, cop->len);
879 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
882 crp->crp_payload_start = 0;
883 crp->crp_payload_length = cop->len;
885 crp->crp_digest_start = cop->len;
888 case CSP_MODE_COMPRESS:
891 crp->crp_op = CRYPTO_OP_COMPRESS;
894 crp->crp_op = CRYPTO_OP_DECOMPRESS;
897 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
902 case CSP_MODE_CIPHER:
905 crp->crp_op = CRYPTO_OP_ENCRYPT;
908 crp->crp_op = CRYPTO_OP_DECRYPT;
911 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
916 case CSP_MODE_DIGEST:
921 crp->crp_op = CRYPTO_OP_COMPUTE_DIGEST;
924 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
932 crp->crp_op = CRYPTO_OP_ENCRYPT |
933 CRYPTO_OP_COMPUTE_DIGEST;
936 crp->crp_op = CRYPTO_OP_DECRYPT |
937 CRYPTO_OP_VERIFY_DIGEST;
940 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
946 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
951 crp->crp_flags = CRYPTO_F_CBIMM | (cop->flags & COP_F_BATCH);
952 crypto_use_buf(crp, cod->buf, cop->len + cse->hashsize);
953 crp->crp_callback = cryptodev_cb;
954 crp->crp_opaque = cod;
957 if (cse->ivsize == 0) {
958 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
962 error = copyin(cop->iv, crp->crp_iv, cse->ivsize);
964 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
967 crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
968 } else if (cse->ivsize != 0) {
969 crp->crp_iv_start = 0;
970 crp->crp_payload_start += cse->ivsize;
971 crp->crp_payload_length -= cse->ivsize;
974 if (cop->mac != NULL) {
975 error = copyin(cop->mac, cod->buf + cop->len, cse->hashsize);
977 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
983 * Let the dispatch run unlocked, then, interlock against the
984 * callback before checking if the operation completed and going
985 * to sleep. This insures drivers don't inherit our lock which
986 * results in a lock order reversal between crypto_dispatch forced
987 * entry and the crypto_done callback into us.
989 error = crypto_dispatch(crp);
991 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
995 mtx_lock(&cse->lock);
997 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0);
998 mtx_unlock(&cse->lock);
1000 if (crp->crp_etype == EAGAIN) {
1002 crp->crp_flags &= ~CRYPTO_F_DONE;
1007 if (crp->crp_etype != 0) {
1008 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1009 error = crp->crp_etype;
1013 if (cop->dst != NULL) {
1014 error = copyout(cod->buf, cop->dst, cop->len);
1016 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1021 if (cop->mac != NULL) {
1022 error = copyout(cod->buf + cop->len, cop->mac, cse->hashsize);
1024 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1030 crypto_freereq(crp);
1038 struct csession *cse,
1039 struct crypt_aead *caead,
1040 struct ucred *active_cred,
1043 struct cryptop_data *cod = NULL;
1044 struct cryptop *crp = NULL;
1047 if (caead->len > 256*1024-4 || caead->aadlen > 256*1024-4) {
1048 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1052 if (cse->txform == NULL || cse->hashsize == 0 || caead->tag == NULL ||
1053 (caead->len % cse->txform->blocksize) != 0) {
1054 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1059 * The COP_F_CIPHER_FIRST flag predates explicit session
1060 * modes, but the only way it was used was for EtA so allow it
1061 * as long as it is consistent with EtA.
1063 if (caead->flags & COP_F_CIPHER_FIRST) {
1064 if (caead->op != COP_ENCRYPT) {
1065 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1070 cod = cod_alloc(cse, caead->aadlen + caead->len + cse->hashsize, td);
1072 crp = crypto_getreq(cse->cses, M_WAITOK);
1074 error = copyin(caead->aad, cod->buf, caead->aadlen);
1076 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1079 crp->crp_aad_start = 0;
1080 crp->crp_aad_length = caead->aadlen;
1082 error = copyin(caead->src, cod->buf + caead->aadlen, caead->len);
1084 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1087 crp->crp_payload_start = caead->aadlen;
1088 crp->crp_payload_length = caead->len;
1089 crp->crp_digest_start = caead->aadlen + caead->len;
1091 switch (cse->mode) {
1093 switch (caead->op) {
1095 crp->crp_op = CRYPTO_OP_ENCRYPT |
1096 CRYPTO_OP_COMPUTE_DIGEST;
1099 crp->crp_op = CRYPTO_OP_DECRYPT |
1100 CRYPTO_OP_VERIFY_DIGEST;
1103 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1109 switch (caead->op) {
1111 crp->crp_op = CRYPTO_OP_ENCRYPT |
1112 CRYPTO_OP_COMPUTE_DIGEST;
1115 crp->crp_op = CRYPTO_OP_DECRYPT |
1116 CRYPTO_OP_VERIFY_DIGEST;
1119 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1125 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1130 crp->crp_flags = CRYPTO_F_CBIMM | (caead->flags & COP_F_BATCH);
1131 crypto_use_buf(crp, cod->buf, caead->aadlen + caead->len +
1133 crp->crp_callback = cryptodev_cb;
1134 crp->crp_opaque = cod;
1138 * Permit a 16-byte IV for AES-XTS, but only use the
1139 * first 8 bytes as a block number.
1141 if (cse->mode == CSP_MODE_ETA &&
1142 caead->ivlen == AES_BLOCK_LEN &&
1143 cse->ivsize == AES_XTS_IV_LEN)
1144 caead->ivlen = AES_XTS_IV_LEN;
1146 if (caead->ivlen != cse->ivsize) {
1148 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1152 error = copyin(caead->iv, crp->crp_iv, cse->ivsize);
1154 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1157 crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
1159 crp->crp_iv_start = crp->crp_payload_start;
1160 crp->crp_payload_start += cse->ivsize;
1161 crp->crp_payload_length -= cse->ivsize;
1164 error = copyin(caead->tag, cod->buf + caead->len + caead->aadlen,
1167 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1172 * Let the dispatch run unlocked, then, interlock against the
1173 * callback before checking if the operation completed and going
1174 * to sleep. This insures drivers don't inherit our lock which
1175 * results in a lock order reversal between crypto_dispatch forced
1176 * entry and the crypto_done callback into us.
1178 error = crypto_dispatch(crp);
1180 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1184 mtx_lock(&cse->lock);
1186 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0);
1187 mtx_unlock(&cse->lock);
1189 if (crp->crp_etype == EAGAIN) {
1191 crp->crp_flags &= ~CRYPTO_F_DONE;
1196 if (crp->crp_etype != 0) {
1197 error = crp->crp_etype;
1198 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1202 if (caead->dst != NULL) {
1203 error = copyout(cod->buf + caead->aadlen, caead->dst,
1206 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1211 error = copyout(cod->buf + caead->aadlen + caead->len, caead->tag,
1214 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1219 crypto_freereq(crp);
1226 cryptodev_cb(struct cryptop *crp)
1228 struct cryptop_data *cod = crp->crp_opaque;
1231 * Lock to ensure the wakeup() is not missed by the loops
1232 * waiting on cod->done in cryptodev_op() and
1235 mtx_lock(&cod->cse->lock);
1237 mtx_unlock(&cod->cse->lock);
1243 cryptodevkey_cb(struct cryptkop *krp)
1250 cryptodev_key(struct crypt_kop *kop)
1252 struct cryptkop *krp = NULL;
1254 int in, out, size, i;
1256 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
1257 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1261 in = kop->crk_iparams;
1262 out = kop->crk_oparams;
1263 switch (kop->crk_op) {
1265 if (in == 3 && out == 1)
1267 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1269 case CRK_MOD_EXP_CRT:
1270 if (in == 6 && out == 1)
1272 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1275 if (in == 5 && out == 2)
1277 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1279 case CRK_DSA_VERIFY:
1280 if (in == 7 && out == 0)
1282 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1284 case CRK_DH_COMPUTE_KEY:
1285 if (in == 3 && out == 1)
1287 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1290 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1294 krp = (struct cryptkop *)malloc(sizeof *krp, M_XDATA, M_WAITOK|M_ZERO);
1296 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1299 krp->krp_op = kop->crk_op;
1300 krp->krp_status = kop->crk_status;
1301 krp->krp_iparams = kop->crk_iparams;
1302 krp->krp_oparams = kop->crk_oparams;
1303 krp->krp_crid = kop->crk_crid;
1304 krp->krp_status = 0;
1305 krp->krp_callback = cryptodevkey_cb;
1307 for (i = 0; i < CRK_MAXPARAM; i++) {
1308 if (kop->crk_param[i].crp_nbits > 65536) {
1309 /* Limit is the same as in OpenBSD */
1310 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1313 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
1315 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
1316 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1319 krp->krp_param[i].crp_p = malloc(size, M_XDATA, M_WAITOK);
1320 if (i >= krp->krp_iparams)
1322 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
1324 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1329 error = crypto_kdispatch(krp);
1331 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1334 error = tsleep(krp, PSOCK, "crydev", 0);
1336 /* XXX can this happen? if so, how do we recover? */
1337 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1341 kop->crk_crid = krp->krp_hid; /* device that did the work */
1342 if (krp->krp_status != 0) {
1343 error = krp->krp_status;
1344 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1348 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
1349 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1352 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
1354 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1361 kop->crk_status = krp->krp_status;
1362 for (i = 0; i < CRK_MAXPARAM; i++) {
1363 if (krp->krp_param[i].crp_p)
1364 free(krp->krp_param[i].crp_p, M_XDATA);
1372 cryptodev_find(struct crypt_find_op *find)
1375 size_t fnlen = sizeof find->name;
1377 if (find->crid != -1) {
1378 dev = crypto_find_device_byhid(find->crid);
1381 strncpy(find->name, device_get_nameunit(dev), fnlen);
1382 find->name[fnlen - 1] = '\x0';
1384 find->name[fnlen - 1] = '\x0';
1385 find->crid = crypto_find_driver(find->name);
1386 if (find->crid == -1)
1397 struct ucred *active_cred,
1401 return (EOPNOTSUPP);
1406 cryptof_close(struct file *fp, struct thread *td)
1408 struct fcrypt *fcr = fp->f_data;
1409 struct csession *cse;
1411 while ((cse = TAILQ_FIRST(&fcr->csessions))) {
1412 TAILQ_REMOVE(&fcr->csessions, cse, next);
1413 KASSERT(cse->refs == 1,
1414 ("%s: crypto session %p with %d refs", __func__, cse,
1424 cryptof_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
1427 kif->kf_type = KF_TYPE_CRYPTO;
1431 static struct csession *
1432 csefind(struct fcrypt *fcr, u_int ses)
1434 struct csession *cse;
1436 mtx_lock(&fcr->lock);
1437 TAILQ_FOREACH(cse, &fcr->csessions, next) {
1438 if (cse->ses == ses) {
1439 refcount_acquire(&cse->refs);
1440 mtx_unlock(&fcr->lock);
1444 mtx_unlock(&fcr->lock);
1449 csedelete(struct fcrypt *fcr, u_int ses)
1451 struct csession *cse;
1453 mtx_lock(&fcr->lock);
1454 TAILQ_FOREACH(cse, &fcr->csessions, next) {
1455 if (cse->ses == ses) {
1456 TAILQ_REMOVE(&fcr->csessions, cse, next);
1457 mtx_unlock(&fcr->lock);
1462 mtx_unlock(&fcr->lock);
1467 csecreate(struct fcrypt *fcr, crypto_session_t cses,
1468 struct crypto_session_params *csp, struct enc_xform *txform,
1469 void *key, struct auth_hash *thash, void *mackey)
1471 struct csession *cse;
1473 cse = malloc(sizeof(struct csession), M_XDATA, M_NOWAIT | M_ZERO);
1476 mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF);
1477 refcount_init(&cse->refs, 1);
1479 cse->mackey = mackey;
1480 cse->mode = csp->csp_mode;
1482 cse->txform = txform;
1484 cse->hashsize = thash->hashsize;
1485 else if (csp->csp_cipher_alg == CRYPTO_AES_NIST_GCM_16)
1486 cse->hashsize = AES_GMAC_HASH_LEN;
1487 else if (csp->csp_cipher_alg == CRYPTO_AES_CCM_16)
1488 cse->hashsize = AES_CBC_MAC_HASH_LEN;
1489 cse->ivsize = csp->csp_ivlen;
1490 mtx_lock(&fcr->lock);
1491 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
1492 cse->ses = fcr->sesn++;
1493 mtx_unlock(&fcr->lock);
1498 csefree(struct csession *cse)
1501 if (!refcount_release(&cse->refs))
1503 crypto_freesession(cse->cses);
1504 mtx_destroy(&cse->lock);
1506 free(cse->key, M_XDATA);
1508 free(cse->mackey, M_XDATA);
1513 cryptoioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1521 error = falloc_noinstall(td, &f);
1525 fcr = malloc(sizeof(struct fcrypt), M_XDATA, M_WAITOK | M_ZERO);
1526 TAILQ_INIT(&fcr->csessions);
1527 mtx_init(&fcr->lock, "fcrypt", NULL, MTX_DEF);
1529 finit(f, FREAD | FWRITE, DTYPE_CRYPTO, fcr, &cryptofops);
1530 error = finstall(td, f, &fd, 0, NULL);
1532 mtx_destroy(&fcr->lock);
1535 *(uint32_t *)data = fd;
1539 error = cryptodev_find((struct crypt_find_op *)data);
1542 error = crypto_getfeat((int *)data);
1551 static struct cdevsw crypto_cdevsw = {
1552 .d_version = D_VERSION,
1553 .d_ioctl = cryptoioctl,
1556 static struct cdev *crypto_dev;
1559 * Initialization code, both for static and dynamic loading.
1562 cryptodev_modevent(module_t mod, int type, void *unused)
1567 printf("crypto: <crypto device>\n");
1568 crypto_dev = make_dev(&crypto_cdevsw, 0,
1569 UID_ROOT, GID_WHEEL, 0666,
1573 /*XXX disallow if active sessions */
1574 destroy_dev(crypto_dev);
1580 static moduledata_t cryptodev_mod = {
1585 MODULE_VERSION(cryptodev, 1);
1586 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1587 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
1588 MODULE_DEPEND(cryptodev, zlib, 1, 1, 1);