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;
291 TAILQ_HEAD(csessionlist, csession) csessions;
296 static bool use_outputbuffers;
297 SYSCTL_BOOL(_kern_crypto, OID_AUTO, cryptodev_use_output, CTLFLAG_RW,
298 &use_outputbuffers, 0,
299 "Use separate output buffers for /dev/crypto requests.");
301 static bool use_separate_aad;
302 SYSCTL_BOOL(_kern_crypto, OID_AUTO, cryptodev_separate_aad, CTLFLAG_RW,
303 &use_separate_aad, 0,
304 "Use separate AAD buffer for /dev/crypto requests.");
306 static int cryptof_ioctl(struct file *, u_long, void *,
307 struct ucred *, struct thread *);
308 static int cryptof_stat(struct file *, struct stat *,
309 struct ucred *, struct thread *);
310 static int cryptof_close(struct file *, struct thread *);
311 static int cryptof_fill_kinfo(struct file *, struct kinfo_file *,
314 static struct fileops cryptofops = {
315 .fo_read = invfo_rdwr,
316 .fo_write = invfo_rdwr,
317 .fo_truncate = invfo_truncate,
318 .fo_ioctl = cryptof_ioctl,
319 .fo_poll = invfo_poll,
320 .fo_kqfilter = invfo_kqfilter,
321 .fo_stat = cryptof_stat,
322 .fo_close = cryptof_close,
323 .fo_chmod = invfo_chmod,
324 .fo_chown = invfo_chown,
325 .fo_sendfile = invfo_sendfile,
326 .fo_fill_kinfo = cryptof_fill_kinfo,
329 static struct csession *csefind(struct fcrypt *, u_int);
330 static bool csedelete(struct fcrypt *, u_int);
331 static struct csession *csecreate(struct fcrypt *, crypto_session_t,
332 struct crypto_session_params *, struct enc_xform *, void *,
333 struct auth_hash *, void *);
334 static void csefree(struct csession *);
336 static int cryptodev_op(struct csession *, struct crypt_op *,
337 struct ucred *, struct thread *td);
338 static int cryptodev_aead(struct csession *, struct crypt_aead *,
339 struct ucred *, struct thread *);
340 static int cryptodev_key(struct crypt_kop *);
341 static int cryptodev_find(struct crypt_find_op *);
344 * Check a crypto identifier to see if it requested
345 * a software device/driver. This can be done either
346 * by device name/class or through search constraints.
349 checkforsoftware(int *cridp)
355 if (!crypto_devallowsoft) {
356 if (crid & CRYPTOCAP_F_SOFTWARE) {
357 if (crid & CRYPTOCAP_F_HARDWARE) {
358 *cridp = CRYPTOCAP_F_HARDWARE;
363 if ((crid & CRYPTOCAP_F_HARDWARE) == 0 &&
364 (crypto_getcaps(crid) & CRYPTOCAP_F_HARDWARE) == 0)
376 struct ucred *active_cred,
379 #define SES2(p) ((struct session2_op *)p)
380 struct crypto_session_params csp;
381 struct fcrypt *fcr = fp->f_data;
382 struct csession *cse;
383 struct session_op *sop;
384 struct crypt_op *cop;
385 struct crypt_aead *caead;
386 struct enc_xform *txform = NULL;
387 struct auth_hash *thash = NULL;
390 struct crypt_kop *kop;
391 crypto_session_t cses;
394 #ifdef COMPAT_FREEBSD32
395 struct session2_op sopc;
396 struct crypt_op copc;
397 struct crypt_kop kopc;
403 #ifdef COMPAT_FREEBSD32
405 case CIOCGSESSION232:
406 if (cmd == CIOCGSESSION32) {
407 session_op_from_32(data, (struct session_op *)&sopc);
408 sop = (struct session_op *)&sopc;
409 } else if (cmd == CIOCGSESSION232) {
410 session2_op_from_32(data, &sopc);
411 sop = (struct session_op *)&sopc;
414 sop = (struct session_op *)data;
415 switch (sop->cipher) {
419 txform = &enc_xform_rijndael128;
422 txform = &enc_xform_aes_xts;
424 case CRYPTO_NULL_CBC:
425 txform = &enc_xform_null;
427 case CRYPTO_CAMELLIA_CBC:
428 txform = &enc_xform_camellia;
431 txform = &enc_xform_aes_icm;
433 case CRYPTO_AES_NIST_GCM_16:
434 txform = &enc_xform_aes_nist_gcm;
436 case CRYPTO_CHACHA20:
437 txform = &enc_xform_chacha20;
439 case CRYPTO_AES_CCM_16:
440 txform = &enc_xform_ccm;
444 CRYPTDEB("invalid cipher");
445 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
452 case CRYPTO_POLY1305:
453 thash = &auth_hash_poly1305;
455 case CRYPTO_SHA1_HMAC:
456 thash = &auth_hash_hmac_sha1;
458 case CRYPTO_SHA2_224_HMAC:
459 thash = &auth_hash_hmac_sha2_224;
461 case CRYPTO_SHA2_256_HMAC:
462 thash = &auth_hash_hmac_sha2_256;
464 case CRYPTO_SHA2_384_HMAC:
465 thash = &auth_hash_hmac_sha2_384;
467 case CRYPTO_SHA2_512_HMAC:
468 thash = &auth_hash_hmac_sha2_512;
470 case CRYPTO_RIPEMD160_HMAC:
471 thash = &auth_hash_hmac_ripemd_160;
473 #ifdef COMPAT_FREEBSD12
474 case CRYPTO_AES_128_NIST_GMAC:
475 case CRYPTO_AES_192_NIST_GMAC:
476 case CRYPTO_AES_256_NIST_GMAC:
477 /* Should always be paired with GCM. */
478 if (sop->cipher != CRYPTO_AES_NIST_GCM_16) {
479 CRYPTDEB("GMAC without GCM");
480 SDT_PROBE1(opencrypto, dev, ioctl, error,
486 case CRYPTO_AES_NIST_GMAC:
487 switch (sop->mackeylen * 8) {
489 thash = &auth_hash_nist_gmac_aes_128;
492 thash = &auth_hash_nist_gmac_aes_192;
495 thash = &auth_hash_nist_gmac_aes_256;
498 CRYPTDEB("invalid GMAC key length");
499 SDT_PROBE1(opencrypto, dev, ioctl, error,
504 case CRYPTO_AES_CCM_CBC_MAC:
505 switch (sop->mackeylen) {
507 thash = &auth_hash_ccm_cbc_mac_128;
510 thash = &auth_hash_ccm_cbc_mac_192;
513 thash = &auth_hash_ccm_cbc_mac_256;
516 CRYPTDEB("Invalid CBC MAC key size %d",
518 SDT_PROBE1(opencrypto, dev, ioctl,
524 thash = &auth_hash_sha1;
526 case CRYPTO_SHA2_224:
527 thash = &auth_hash_sha2_224;
529 case CRYPTO_SHA2_256:
530 thash = &auth_hash_sha2_256;
532 case CRYPTO_SHA2_384:
533 thash = &auth_hash_sha2_384;
535 case CRYPTO_SHA2_512:
536 thash = &auth_hash_sha2_512;
539 case CRYPTO_NULL_HMAC:
540 thash = &auth_hash_null;
544 thash = &auth_hash_blake2b;
547 thash = &auth_hash_blake2s;
551 CRYPTDEB("invalid mac");
552 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
556 if (txform == NULL && thash == NULL) {
557 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
561 memset(&csp, 0, sizeof(csp));
562 if (use_outputbuffers)
563 csp.csp_flags |= CSP_F_SEPARATE_OUTPUT;
565 if (sop->cipher == CRYPTO_AES_NIST_GCM_16) {
567 #ifdef COMPAT_FREEBSD12
568 case CRYPTO_AES_128_NIST_GMAC:
569 case CRYPTO_AES_192_NIST_GMAC:
570 case CRYPTO_AES_256_NIST_GMAC:
571 if (sop->keylen != sop->mackeylen) {
572 SDT_PROBE1(opencrypto, dev, ioctl,
581 SDT_PROBE1(opencrypto, dev, ioctl, error,
585 csp.csp_mode = CSP_MODE_AEAD;
586 } else if (sop->cipher == CRYPTO_AES_CCM_16) {
588 #ifdef COMPAT_FREEBSD12
589 case CRYPTO_AES_CCM_CBC_MAC:
590 if (sop->keylen != sop->mackeylen) {
591 SDT_PROBE1(opencrypto, dev, ioctl,
601 SDT_PROBE1(opencrypto, dev, ioctl, error,
605 csp.csp_mode = CSP_MODE_AEAD;
606 } else if (txform && thash)
607 csp.csp_mode = CSP_MODE_ETA;
609 csp.csp_mode = CSP_MODE_CIPHER;
611 csp.csp_mode = CSP_MODE_DIGEST;
613 switch (csp.csp_mode) {
616 if (use_separate_aad)
617 csp.csp_flags |= CSP_F_SEPARATE_AAD;
622 csp.csp_cipher_alg = txform->type;
623 csp.csp_cipher_klen = sop->keylen;
624 if (sop->keylen > txform->maxkey ||
625 sop->keylen < txform->minkey) {
626 CRYPTDEB("invalid cipher parameters");
628 SDT_PROBE1(opencrypto, dev, ioctl, error,
633 key = malloc(csp.csp_cipher_klen, M_XDATA, M_WAITOK);
634 error = copyin(sop->key, key, csp.csp_cipher_klen);
636 CRYPTDEB("invalid key");
637 SDT_PROBE1(opencrypto, dev, ioctl, error,
641 csp.csp_cipher_key = key;
642 csp.csp_ivlen = txform->ivsize;
646 csp.csp_auth_alg = thash->type;
647 csp.csp_auth_klen = sop->mackeylen;
648 if (sop->mackeylen > thash->keysize ||
649 sop->mackeylen < 0) {
650 CRYPTDEB("invalid mac key length");
652 SDT_PROBE1(opencrypto, dev, ioctl, error,
657 if (csp.csp_auth_klen) {
658 mackey = malloc(csp.csp_auth_klen, M_XDATA,
660 error = copyin(sop->mackey, mackey,
663 CRYPTDEB("invalid mac key");
664 SDT_PROBE1(opencrypto, dev, ioctl,
668 csp.csp_auth_key = mackey;
671 if (csp.csp_auth_alg == CRYPTO_AES_NIST_GMAC)
672 csp.csp_ivlen = AES_GCM_IV_LEN;
673 if (csp.csp_auth_alg == CRYPTO_AES_CCM_CBC_MAC)
674 csp.csp_ivlen = AES_CCM_IV_LEN;
677 /* NB: CIOCGSESSION2 has the crid */
678 if (cmd == CIOCGSESSION2
679 #ifdef COMPAT_FREEBSD32
680 || cmd == CIOCGSESSION232
683 crid = SES2(sop)->crid;
684 error = checkforsoftware(&crid);
686 CRYPTDEB("checkforsoftware");
687 SDT_PROBE1(opencrypto, dev, ioctl, error,
692 crid = CRYPTOCAP_F_HARDWARE;
693 error = crypto_newsession(&cses, &csp, crid);
695 CRYPTDEB("crypto_newsession");
696 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
700 cse = csecreate(fcr, cses, &csp, txform, key, thash, mackey);
703 crypto_freesession(cses);
705 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
706 CRYPTDEB("csecreate");
710 if (cmd == CIOCGSESSION2
711 #ifdef COMPAT_FREEBSD32
712 || cmd == CIOCGSESSION232
715 /* return hardware/driver id */
716 SES2(sop)->crid = crypto_ses2hid(cse->cses);
721 free(mackey, M_XDATA);
723 #ifdef COMPAT_FREEBSD32
725 if (cmd == CIOCGSESSION32)
726 session_op_to_32(sop, data);
727 else if (cmd == CIOCGSESSION232)
728 session2_op_to_32((struct session2_op *)sop,
734 ses = *(u_int32_t *)data;
735 if (!csedelete(fcr, ses)) {
736 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
741 #ifdef COMPAT_FREEBSD32
743 if (cmd == CIOCCRYPT32) {
745 crypt_op_from_32(data, cop);
748 cop = (struct crypt_op *)data;
749 cse = csefind(fcr, cop->ses);
751 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
754 error = cryptodev_op(cse, cop, active_cred, td);
756 #ifdef COMPAT_FREEBSD32
757 if (error == 0 && cmd == CIOCCRYPT32)
758 crypt_op_to_32(cop, data);
763 #ifdef COMPAT_FREEBSD32
767 if (!crypto_userasymcrypto) {
768 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
769 return (EPERM); /* XXX compat? */
771 #ifdef COMPAT_FREEBSD32
772 if (cmd == CIOCKEY32 || cmd == CIOCKEY232) {
774 crypt_kop_from_32(data, kop);
777 kop = (struct crypt_kop *)data;
779 #ifdef COMPAT_FREEBSD32
783 /* NB: crypto core enforces s/w driver use */
785 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
788 error = cryptodev_key(kop);
790 #ifdef COMPAT_FREEBSD32
791 if (cmd == CIOCKEY32 || cmd == CIOCKEY232)
792 crypt_kop_to_32(kop, data);
796 if (!crypto_userasymcrypto) {
798 * NB: if user asym crypto operations are
799 * not permitted return "no algorithms"
800 * so well-behaved applications will just
801 * fallback to doing them in software.
805 error = crypto_getfeat((int *)data);
807 SDT_PROBE1(opencrypto, dev, ioctl, error,
812 error = cryptodev_find((struct crypt_find_op *)data);
815 caead = (struct crypt_aead *)data;
816 cse = csefind(fcr, caead->ses);
818 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
821 error = cryptodev_aead(cse, caead, active_cred, td);
826 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
833 static int cryptodev_cb(struct cryptop *);
835 static struct cryptop_data *
836 cod_alloc(struct csession *cse, size_t aad_len, size_t len, struct thread *td)
838 struct cryptop_data *cod;
840 cod = malloc(sizeof(struct cryptop_data), M_XDATA, M_WAITOK | M_ZERO);
843 if (crypto_get_params(cse->cses)->csp_flags & CSP_F_SEPARATE_AAD) {
845 cod->aad = malloc(aad_len, M_XDATA, M_WAITOK);
846 cod->buf = malloc(len, M_XDATA, M_WAITOK);
848 cod->buf = malloc(aad_len + len, M_XDATA, M_WAITOK);
849 if (crypto_get_params(cse->cses)->csp_flags & CSP_F_SEPARATE_OUTPUT)
850 cod->obuf = malloc(len, M_XDATA, M_WAITOK);
855 cod_free(struct cryptop_data *cod)
858 free(cod->aad, M_XDATA);
859 free(cod->obuf, M_XDATA);
860 free(cod->buf, M_XDATA);
866 struct csession *cse,
867 struct crypt_op *cop,
868 struct ucred *active_cred,
871 struct cryptop_data *cod = NULL;
872 struct cryptop *crp = NULL;
875 if (cop->len > 256*1024-4) {
876 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
881 if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0) {
882 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
887 if (cop->mac && cse->hashsize == 0) {
888 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
893 * The COP_F_CIPHER_FIRST flag predates explicit session
894 * modes, but the only way it was used was for EtA so allow it
895 * as long as it is consistent with EtA.
897 if (cop->flags & COP_F_CIPHER_FIRST) {
898 if (cop->op != COP_ENCRYPT) {
899 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
904 cod = cod_alloc(cse, 0, cop->len + cse->hashsize, td);
906 crp = crypto_getreq(cse->cses, M_WAITOK);
908 error = copyin(cop->src, cod->buf, cop->len);
910 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
913 crp->crp_payload_start = 0;
914 crp->crp_payload_length = cop->len;
916 crp->crp_digest_start = cop->len;
919 case CSP_MODE_COMPRESS:
922 crp->crp_op = CRYPTO_OP_COMPRESS;
925 crp->crp_op = CRYPTO_OP_DECOMPRESS;
928 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
933 case CSP_MODE_CIPHER:
936 crp->crp_op = CRYPTO_OP_ENCRYPT;
939 crp->crp_op = CRYPTO_OP_DECRYPT;
942 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
947 case CSP_MODE_DIGEST:
952 crp->crp_op = CRYPTO_OP_COMPUTE_DIGEST;
953 if (cod->obuf != NULL)
954 crp->crp_digest_start = 0;
957 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
965 crp->crp_op = CRYPTO_OP_ENCRYPT |
966 CRYPTO_OP_COMPUTE_DIGEST;
969 crp->crp_op = CRYPTO_OP_DECRYPT |
970 CRYPTO_OP_VERIFY_DIGEST;
973 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
979 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
984 crp->crp_flags = CRYPTO_F_CBIMM | (cop->flags & COP_F_BATCH);
985 crypto_use_buf(crp, cod->buf, cop->len + cse->hashsize);
987 crypto_use_output_buf(crp, cod->obuf, cop->len + cse->hashsize);
988 crp->crp_callback = cryptodev_cb;
989 crp->crp_opaque = cod;
992 if (cse->ivsize == 0) {
993 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
997 error = copyin(cop->iv, crp->crp_iv, cse->ivsize);
999 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1002 crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
1003 } else if (cse->ivsize != 0) {
1004 crp->crp_iv_start = 0;
1005 crp->crp_payload_start += cse->ivsize;
1006 crp->crp_payload_length -= cse->ivsize;
1007 cop->dst += cse->ivsize;
1010 if (cop->mac != NULL && crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
1011 error = copyin(cop->mac, cod->buf + crp->crp_digest_start,
1014 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1020 * Let the dispatch run unlocked, then, interlock against the
1021 * callback before checking if the operation completed and going
1022 * to sleep. This insures drivers don't inherit our lock which
1023 * results in a lock order reversal between crypto_dispatch forced
1024 * entry and the crypto_done callback into us.
1026 error = crypto_dispatch(crp);
1028 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1032 mtx_lock(&cse->lock);
1034 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0);
1035 mtx_unlock(&cse->lock);
1037 if (crp->crp_etype == EAGAIN) {
1039 crp->crp_flags &= ~CRYPTO_F_DONE;
1044 if (crp->crp_etype != 0) {
1045 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1046 error = crp->crp_etype;
1050 if (cop->dst != NULL) {
1051 error = copyout(cod->obuf != NULL ? cod->obuf :
1052 cod->buf + crp->crp_payload_start, cop->dst,
1053 crp->crp_payload_length);
1055 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1060 if (cop->mac != NULL && (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) == 0) {
1061 error = copyout((cod->obuf != NULL ? cod->obuf : cod->buf) +
1062 crp->crp_digest_start, cop->mac, cse->hashsize);
1064 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1070 crypto_freereq(crp);
1078 struct csession *cse,
1079 struct crypt_aead *caead,
1080 struct ucred *active_cred,
1083 struct cryptop_data *cod = NULL;
1084 struct cryptop *crp = NULL;
1087 if (caead->len > 256*1024-4 || caead->aadlen > 256*1024-4) {
1088 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1092 if (cse->txform == NULL || cse->hashsize == 0 || caead->tag == NULL ||
1093 (caead->len % cse->txform->blocksize) != 0) {
1094 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1099 * The COP_F_CIPHER_FIRST flag predates explicit session
1100 * modes, but the only way it was used was for EtA so allow it
1101 * as long as it is consistent with EtA.
1103 if (caead->flags & COP_F_CIPHER_FIRST) {
1104 if (caead->op != COP_ENCRYPT) {
1105 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1110 cod = cod_alloc(cse, caead->aadlen, caead->len + cse->hashsize, td);
1112 crp = crypto_getreq(cse->cses, M_WAITOK);
1114 if (cod->aad != NULL)
1115 error = copyin(caead->aad, cod->aad, caead->aadlen);
1117 error = copyin(caead->aad, cod->buf, caead->aadlen);
1119 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1122 crp->crp_aad = cod->aad;
1123 crp->crp_aad_start = 0;
1124 crp->crp_aad_length = caead->aadlen;
1126 if (cod->aad != NULL)
1127 crp->crp_payload_start = 0;
1129 crp->crp_payload_start = caead->aadlen;
1130 error = copyin(caead->src, cod->buf + crp->crp_payload_start,
1133 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1136 crp->crp_payload_length = caead->len;
1137 if (caead->op == COP_ENCRYPT && cod->obuf != NULL)
1138 crp->crp_digest_start = crp->crp_payload_output_start +
1141 crp->crp_digest_start = crp->crp_payload_start + caead->len;
1143 switch (cse->mode) {
1146 switch (caead->op) {
1148 crp->crp_op = CRYPTO_OP_ENCRYPT |
1149 CRYPTO_OP_COMPUTE_DIGEST;
1152 crp->crp_op = CRYPTO_OP_DECRYPT |
1153 CRYPTO_OP_VERIFY_DIGEST;
1156 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1162 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1167 crp->crp_flags = CRYPTO_F_CBIMM | (caead->flags & COP_F_BATCH);
1168 crypto_use_buf(crp, cod->buf, crp->crp_payload_start + caead->len +
1170 if (cod->obuf != NULL)
1171 crypto_use_output_buf(crp, cod->obuf, caead->len +
1173 crp->crp_callback = cryptodev_cb;
1174 crp->crp_opaque = cod;
1178 * Permit a 16-byte IV for AES-XTS, but only use the
1179 * first 8 bytes as a block number.
1181 if (cse->mode == CSP_MODE_ETA &&
1182 caead->ivlen == AES_BLOCK_LEN &&
1183 cse->ivsize == AES_XTS_IV_LEN)
1184 caead->ivlen = AES_XTS_IV_LEN;
1186 if (caead->ivlen != cse->ivsize) {
1188 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1192 error = copyin(caead->iv, crp->crp_iv, cse->ivsize);
1194 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1197 crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
1199 crp->crp_iv_start = crp->crp_payload_start;
1200 crp->crp_payload_start += cse->ivsize;
1201 crp->crp_payload_length -= cse->ivsize;
1202 caead->dst += cse->ivsize;
1205 if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
1206 error = copyin(caead->tag, cod->buf + crp->crp_digest_start,
1209 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1215 * Let the dispatch run unlocked, then, interlock against the
1216 * callback before checking if the operation completed and going
1217 * to sleep. This insures drivers don't inherit our lock which
1218 * results in a lock order reversal between crypto_dispatch forced
1219 * entry and the crypto_done callback into us.
1221 error = crypto_dispatch(crp);
1223 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1227 mtx_lock(&cse->lock);
1229 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0);
1230 mtx_unlock(&cse->lock);
1232 if (crp->crp_etype == EAGAIN) {
1234 crp->crp_flags &= ~CRYPTO_F_DONE;
1239 if (crp->crp_etype != 0) {
1240 error = crp->crp_etype;
1241 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1245 if (caead->dst != NULL) {
1246 error = copyout(cod->obuf != NULL ? cod->obuf :
1247 cod->buf + crp->crp_payload_start, caead->dst,
1248 crp->crp_payload_length);
1250 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1255 if ((crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) == 0) {
1256 error = copyout((cod->obuf != NULL ? cod->obuf : cod->buf) +
1257 crp->crp_digest_start, caead->tag, cse->hashsize);
1259 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1265 crypto_freereq(crp);
1272 cryptodev_cb(struct cryptop *crp)
1274 struct cryptop_data *cod = crp->crp_opaque;
1277 * Lock to ensure the wakeup() is not missed by the loops
1278 * waiting on cod->done in cryptodev_op() and
1281 mtx_lock(&cod->cse->lock);
1283 mtx_unlock(&cod->cse->lock);
1289 cryptodevkey_cb(struct cryptkop *krp)
1296 cryptodev_key(struct crypt_kop *kop)
1298 struct cryptkop *krp = NULL;
1300 int in, out, size, i;
1302 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
1303 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1307 in = kop->crk_iparams;
1308 out = kop->crk_oparams;
1309 switch (kop->crk_op) {
1311 if (in == 3 && out == 1)
1313 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1315 case CRK_MOD_EXP_CRT:
1316 if (in == 6 && out == 1)
1318 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1321 if (in == 5 && out == 2)
1323 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1325 case CRK_DSA_VERIFY:
1326 if (in == 7 && out == 0)
1328 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1330 case CRK_DH_COMPUTE_KEY:
1331 if (in == 3 && out == 1)
1333 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1336 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1340 krp = (struct cryptkop *)malloc(sizeof *krp, M_XDATA, M_WAITOK|M_ZERO);
1342 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1345 krp->krp_op = kop->crk_op;
1346 krp->krp_status = kop->crk_status;
1347 krp->krp_iparams = kop->crk_iparams;
1348 krp->krp_oparams = kop->crk_oparams;
1349 krp->krp_crid = kop->crk_crid;
1350 krp->krp_status = 0;
1351 krp->krp_callback = cryptodevkey_cb;
1353 for (i = 0; i < CRK_MAXPARAM; i++) {
1354 if (kop->crk_param[i].crp_nbits > 65536) {
1355 /* Limit is the same as in OpenBSD */
1356 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1359 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
1361 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
1362 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1365 krp->krp_param[i].crp_p = malloc(size, M_XDATA, M_WAITOK);
1366 if (i >= krp->krp_iparams)
1368 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
1370 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1375 error = crypto_kdispatch(krp);
1377 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1380 error = tsleep(krp, PSOCK, "crydev", 0);
1382 /* XXX can this happen? if so, how do we recover? */
1383 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1387 kop->crk_crid = krp->krp_hid; /* device that did the work */
1388 if (krp->krp_status != 0) {
1389 error = krp->krp_status;
1390 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1394 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
1395 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1398 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
1400 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1407 kop->crk_status = krp->krp_status;
1408 for (i = 0; i < CRK_MAXPARAM; i++) {
1409 if (krp->krp_param[i].crp_p)
1410 free(krp->krp_param[i].crp_p, M_XDATA);
1418 cryptodev_find(struct crypt_find_op *find)
1421 size_t fnlen = sizeof find->name;
1423 if (find->crid != -1) {
1424 dev = crypto_find_device_byhid(find->crid);
1427 strncpy(find->name, device_get_nameunit(dev), fnlen);
1428 find->name[fnlen - 1] = '\x0';
1430 find->name[fnlen - 1] = '\x0';
1431 find->crid = crypto_find_driver(find->name);
1432 if (find->crid == -1)
1443 struct ucred *active_cred,
1447 return (EOPNOTSUPP);
1452 cryptof_close(struct file *fp, struct thread *td)
1454 struct fcrypt *fcr = fp->f_data;
1455 struct csession *cse;
1457 while ((cse = TAILQ_FIRST(&fcr->csessions))) {
1458 TAILQ_REMOVE(&fcr->csessions, cse, next);
1459 KASSERT(cse->refs == 1,
1460 ("%s: crypto session %p with %d refs", __func__, cse,
1470 cryptof_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
1473 kif->kf_type = KF_TYPE_CRYPTO;
1477 static struct csession *
1478 csefind(struct fcrypt *fcr, u_int ses)
1480 struct csession *cse;
1482 mtx_lock(&fcr->lock);
1483 TAILQ_FOREACH(cse, &fcr->csessions, next) {
1484 if (cse->ses == ses) {
1485 refcount_acquire(&cse->refs);
1486 mtx_unlock(&fcr->lock);
1490 mtx_unlock(&fcr->lock);
1495 csedelete(struct fcrypt *fcr, u_int ses)
1497 struct csession *cse;
1499 mtx_lock(&fcr->lock);
1500 TAILQ_FOREACH(cse, &fcr->csessions, next) {
1501 if (cse->ses == ses) {
1502 TAILQ_REMOVE(&fcr->csessions, cse, next);
1503 mtx_unlock(&fcr->lock);
1508 mtx_unlock(&fcr->lock);
1513 csecreate(struct fcrypt *fcr, crypto_session_t cses,
1514 struct crypto_session_params *csp, struct enc_xform *txform,
1515 void *key, struct auth_hash *thash, void *mackey)
1517 struct csession *cse;
1519 cse = malloc(sizeof(struct csession), M_XDATA, M_NOWAIT | M_ZERO);
1522 mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF);
1523 refcount_init(&cse->refs, 1);
1525 cse->mackey = mackey;
1526 cse->mode = csp->csp_mode;
1528 cse->txform = txform;
1530 cse->hashsize = thash->hashsize;
1531 else if (csp->csp_cipher_alg == CRYPTO_AES_NIST_GCM_16)
1532 cse->hashsize = AES_GMAC_HASH_LEN;
1533 else if (csp->csp_cipher_alg == CRYPTO_AES_CCM_16)
1534 cse->hashsize = AES_CBC_MAC_HASH_LEN;
1535 cse->ivsize = csp->csp_ivlen;
1536 mtx_lock(&fcr->lock);
1537 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
1538 cse->ses = fcr->sesn++;
1539 mtx_unlock(&fcr->lock);
1544 csefree(struct csession *cse)
1547 if (!refcount_release(&cse->refs))
1549 crypto_freesession(cse->cses);
1550 mtx_destroy(&cse->lock);
1552 free(cse->key, M_XDATA);
1554 free(cse->mackey, M_XDATA);
1559 cryptoioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1567 error = falloc_noinstall(td, &f);
1571 fcr = malloc(sizeof(struct fcrypt), M_XDATA, M_WAITOK | M_ZERO);
1572 TAILQ_INIT(&fcr->csessions);
1573 mtx_init(&fcr->lock, "fcrypt", NULL, MTX_DEF);
1575 finit(f, FREAD | FWRITE, DTYPE_CRYPTO, fcr, &cryptofops);
1576 error = finstall(td, f, &fd, 0, NULL);
1578 mtx_destroy(&fcr->lock);
1581 *(uint32_t *)data = fd;
1585 error = cryptodev_find((struct crypt_find_op *)data);
1588 error = crypto_getfeat((int *)data);
1597 static struct cdevsw crypto_cdevsw = {
1598 .d_version = D_VERSION,
1599 .d_ioctl = cryptoioctl,
1602 static struct cdev *crypto_dev;
1605 * Initialization code, both for static and dynamic loading.
1608 cryptodev_modevent(module_t mod, int type, void *unused)
1613 printf("crypto: <crypto device>\n");
1614 crypto_dev = make_dev(&crypto_cdevsw, 0,
1615 UID_ROOT, GID_WHEEL, 0666,
1619 /*XXX disallow if active sessions */
1620 destroy_dev(crypto_dev);
1626 static moduledata_t cryptodev_mod = {
1631 MODULE_VERSION(cryptodev, 1);
1632 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1633 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
1634 MODULE_DEPEND(cryptodev, zlib, 1, 1, 1);