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 {
106 struct crypt_aead32 {
131 struct crparam32 crk_param[CRK_MAXPARAM];
134 #define CIOCGSESSION32 _IOWR('c', 101, struct session_op32)
135 #define CIOCCRYPT32 _IOWR('c', 103, struct crypt_op32)
136 #define CIOCKEY32 _IOWR('c', 104, struct crypt_kop32)
137 #define CIOCGSESSION232 _IOWR('c', 106, struct session2_op32)
138 #define CIOCKEY232 _IOWR('c', 107, struct crypt_kop32)
139 #define CIOCCRYPTAEAD32 _IOWR('c', 109, struct crypt_aead32)
142 session_op_from_32(const struct session_op32 *from, struct session2_op *to)
145 memset(to, 0, sizeof(*to));
146 CP(*from, *to, cipher);
148 CP(*from, *to, keylen);
149 PTRIN_CP(*from, *to, key);
150 CP(*from, *to, mackeylen);
151 PTRIN_CP(*from, *to, mackey);
153 to->crid = CRYPTOCAP_F_HARDWARE;
157 session2_op_from_32(const struct session2_op32 *from, struct session2_op *to)
160 session_op_from_32((const struct session_op32 *)from, to);
161 CP(*from, *to, crid);
165 session_op_to_32(const struct session2_op *from, struct session_op32 *to)
168 CP(*from, *to, cipher);
170 CP(*from, *to, keylen);
171 PTROUT_CP(*from, *to, key);
172 CP(*from, *to, mackeylen);
173 PTROUT_CP(*from, *to, mackey);
178 session2_op_to_32(const struct session2_op *from, struct session2_op32 *to)
181 session_op_to_32(from, (struct session_op32 *)to);
182 CP(*from, *to, crid);
186 crypt_op_from_32(const struct crypt_op32 *from, struct crypt_op *to)
191 CP(*from, *to, flags);
193 PTRIN_CP(*from, *to, src);
194 PTRIN_CP(*from, *to, dst);
195 PTRIN_CP(*from, *to, mac);
196 PTRIN_CP(*from, *to, iv);
200 crypt_op_to_32(const struct crypt_op *from, struct crypt_op32 *to)
205 CP(*from, *to, flags);
207 PTROUT_CP(*from, *to, src);
208 PTROUT_CP(*from, *to, dst);
209 PTROUT_CP(*from, *to, mac);
210 PTROUT_CP(*from, *to, iv);
214 crypt_aead_from_32(const struct crypt_aead32 *from, struct crypt_aead *to)
219 CP(*from, *to, flags);
221 CP(*from, *to, aadlen);
222 CP(*from, *to, ivlen);
223 PTRIN_CP(*from, *to, src);
224 PTRIN_CP(*from, *to, dst);
225 PTRIN_CP(*from, *to, aad);
226 PTRIN_CP(*from, *to, tag);
227 PTRIN_CP(*from, *to, iv);
231 crypt_aead_to_32(const struct crypt_aead *from, struct crypt_aead32 *to)
236 CP(*from, *to, flags);
238 CP(*from, *to, aadlen);
239 CP(*from, *to, ivlen);
240 PTROUT_CP(*from, *to, src);
241 PTROUT_CP(*from, *to, dst);
242 PTROUT_CP(*from, *to, aad);
243 PTROUT_CP(*from, *to, tag);
244 PTROUT_CP(*from, *to, iv);
248 crparam_from_32(const struct crparam32 *from, struct crparam *to)
251 PTRIN_CP(*from, *to, crp_p);
252 CP(*from, *to, crp_nbits);
256 crparam_to_32(const struct crparam *from, struct crparam32 *to)
259 PTROUT_CP(*from, *to, crp_p);
260 CP(*from, *to, crp_nbits);
264 crypt_kop_from_32(const struct crypt_kop32 *from, struct crypt_kop *to)
268 CP(*from, *to, crk_op);
269 CP(*from, *to, crk_status);
270 CP(*from, *to, crk_iparams);
271 CP(*from, *to, crk_oparams);
272 CP(*from, *to, crk_crid);
273 for (i = 0; i < CRK_MAXPARAM; i++)
274 crparam_from_32(&from->crk_param[i], &to->crk_param[i]);
278 crypt_kop_to_32(const struct crypt_kop *from, struct crypt_kop32 *to)
282 CP(*from, *to, crk_op);
283 CP(*from, *to, crk_status);
284 CP(*from, *to, crk_iparams);
285 CP(*from, *to, crk_oparams);
286 CP(*from, *to, crk_crid);
287 for (i = 0; i < CRK_MAXPARAM; i++)
288 crparam_to_32(&from->crk_param[i], &to->crk_param[i]);
293 session2_op_from_op(const struct session_op *from, struct session2_op *to)
296 memset(to, 0, sizeof(*to));
297 memcpy(to, from, sizeof(*from));
298 to->crid = CRYPTOCAP_F_HARDWARE;
302 session2_op_to_op(const struct session2_op *from, struct session_op *to)
305 memcpy(to, from, sizeof(*to));
309 TAILQ_ENTRY(csession) next;
310 crypto_session_t cses;
313 struct mtx lock; /* for op submission */
315 struct enc_xform *txform;
324 struct cryptop_data {
325 struct csession *cse;
334 TAILQ_HEAD(csessionlist, csession) csessions;
339 static bool use_outputbuffers;
340 SYSCTL_BOOL(_kern_crypto, OID_AUTO, cryptodev_use_output, CTLFLAG_RW,
341 &use_outputbuffers, 0,
342 "Use separate output buffers for /dev/crypto requests.");
344 static bool use_separate_aad;
345 SYSCTL_BOOL(_kern_crypto, OID_AUTO, cryptodev_separate_aad, CTLFLAG_RW,
346 &use_separate_aad, 0,
347 "Use separate AAD buffer for /dev/crypto requests.");
349 static struct timeval warninterval = { .tv_sec = 60, .tv_usec = 0 };
350 SYSCTL_TIMEVAL_SEC(_kern, OID_AUTO, cryptodev_warn_interval, CTLFLAG_RW,
352 "Delay in seconds between warnings of deprecated /dev/crypto algorithms");
354 static int cryptof_ioctl(struct file *, u_long, void *, struct ucred *,
356 static int cryptof_stat(struct file *, struct stat *, struct ucred *,
358 static int cryptof_close(struct file *, struct thread *);
359 static int cryptof_fill_kinfo(struct file *, struct kinfo_file *,
362 static struct fileops cryptofops = {
363 .fo_read = invfo_rdwr,
364 .fo_write = invfo_rdwr,
365 .fo_truncate = invfo_truncate,
366 .fo_ioctl = cryptof_ioctl,
367 .fo_poll = invfo_poll,
368 .fo_kqfilter = invfo_kqfilter,
369 .fo_stat = cryptof_stat,
370 .fo_close = cryptof_close,
371 .fo_chmod = invfo_chmod,
372 .fo_chown = invfo_chown,
373 .fo_sendfile = invfo_sendfile,
374 .fo_fill_kinfo = cryptof_fill_kinfo,
377 static struct csession *csefind(struct fcrypt *, u_int);
378 static bool csedelete(struct fcrypt *, u_int);
379 static struct csession *csecreate(struct fcrypt *, crypto_session_t,
380 struct crypto_session_params *, struct enc_xform *, void *,
381 struct auth_hash *, void *);
382 static void csefree(struct csession *);
384 static int cryptodev_op(struct csession *, const struct crypt_op *,
385 struct ucred *, struct thread *);
386 static int cryptodev_aead(struct csession *, struct crypt_aead *,
387 struct ucred *, struct thread *);
388 static int cryptodev_key(struct crypt_kop *);
389 static int cryptodev_find(struct crypt_find_op *);
392 * Check a crypto identifier to see if it requested
393 * a software device/driver. This can be done either
394 * by device name/class or through search constraints.
397 checkforsoftware(int *cridp)
403 if (!crypto_devallowsoft) {
404 if (crid & CRYPTOCAP_F_SOFTWARE) {
405 if (crid & CRYPTOCAP_F_HARDWARE) {
406 *cridp = CRYPTOCAP_F_HARDWARE;
411 if ((crid & CRYPTOCAP_F_HARDWARE) == 0 &&
412 (crypto_getcaps(crid) & CRYPTOCAP_F_HARDWARE) == 0)
420 cryptof_ioctl(struct file *fp, u_long cmd, void *data,
421 struct ucred *active_cred, struct thread *td)
423 static struct timeval keywarn, featwarn;
424 struct crypto_session_params csp;
425 struct fcrypt *fcr = fp->f_data;
426 struct csession *cse;
427 struct session2_op *sop;
428 struct crypt_op *cop;
429 struct crypt_aead *caead;
430 struct enc_xform *txform = NULL;
431 struct auth_hash *thash = NULL;
434 struct crypt_kop *kop;
435 crypto_session_t cses;
439 struct session2_op sopc;
440 #ifdef COMPAT_FREEBSD32
441 struct crypt_op copc;
442 struct crypt_aead aeadc;
443 struct crypt_kop kopc;
446 #ifdef COMPAT_FREEBSD32
458 session_op_from_32((struct session_op32 *)data32, &thunk.sopc);
460 case CIOCGSESSION232:
465 session2_op_from_32((struct session2_op32 *)data32,
473 crypt_op_from_32((struct crypt_op32 *)data32, &thunk.copc);
475 case CIOCCRYPTAEAD32:
480 crypt_aead_from_32((struct crypt_aead32 *)data32, &thunk.aeadc);
486 if (cmd == CIOCKEY32)
491 crypt_kop_from_32((struct crypt_kop32 *)data32, &thunk.kopc);
499 if (cmd == CIOCGSESSION) {
500 session2_op_from_op(data, &thunk.sopc);
503 sop = (struct session2_op *)data;
505 switch (sop->cipher) {
509 txform = &enc_xform_rijndael128;
512 txform = &enc_xform_aes_xts;
514 case CRYPTO_NULL_CBC:
515 txform = &enc_xform_null;
517 case CRYPTO_CAMELLIA_CBC:
518 txform = &enc_xform_camellia;
521 txform = &enc_xform_aes_icm;
523 case CRYPTO_AES_NIST_GCM_16:
524 txform = &enc_xform_aes_nist_gcm;
526 case CRYPTO_CHACHA20:
527 txform = &enc_xform_chacha20;
529 case CRYPTO_AES_CCM_16:
530 txform = &enc_xform_ccm;
534 CRYPTDEB("invalid cipher");
535 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
542 case CRYPTO_POLY1305:
543 thash = &auth_hash_poly1305;
545 case CRYPTO_SHA1_HMAC:
546 thash = &auth_hash_hmac_sha1;
548 case CRYPTO_SHA2_224_HMAC:
549 thash = &auth_hash_hmac_sha2_224;
551 case CRYPTO_SHA2_256_HMAC:
552 thash = &auth_hash_hmac_sha2_256;
554 case CRYPTO_SHA2_384_HMAC:
555 thash = &auth_hash_hmac_sha2_384;
557 case CRYPTO_SHA2_512_HMAC:
558 thash = &auth_hash_hmac_sha2_512;
560 case CRYPTO_RIPEMD160_HMAC:
561 thash = &auth_hash_hmac_ripemd_160;
563 #ifdef COMPAT_FREEBSD12
564 case CRYPTO_AES_128_NIST_GMAC:
565 case CRYPTO_AES_192_NIST_GMAC:
566 case CRYPTO_AES_256_NIST_GMAC:
567 /* Should always be paired with GCM. */
568 if (sop->cipher != CRYPTO_AES_NIST_GCM_16) {
569 CRYPTDEB("GMAC without GCM");
570 SDT_PROBE1(opencrypto, dev, ioctl, error,
576 case CRYPTO_AES_NIST_GMAC:
577 switch (sop->mackeylen * 8) {
579 thash = &auth_hash_nist_gmac_aes_128;
582 thash = &auth_hash_nist_gmac_aes_192;
585 thash = &auth_hash_nist_gmac_aes_256;
588 CRYPTDEB("invalid GMAC key length");
589 SDT_PROBE1(opencrypto, dev, ioctl, error,
594 case CRYPTO_AES_CCM_CBC_MAC:
595 switch (sop->mackeylen) {
597 thash = &auth_hash_ccm_cbc_mac_128;
600 thash = &auth_hash_ccm_cbc_mac_192;
603 thash = &auth_hash_ccm_cbc_mac_256;
606 CRYPTDEB("Invalid CBC MAC key size %d",
608 SDT_PROBE1(opencrypto, dev, ioctl,
614 thash = &auth_hash_sha1;
616 case CRYPTO_SHA2_224:
617 thash = &auth_hash_sha2_224;
619 case CRYPTO_SHA2_256:
620 thash = &auth_hash_sha2_256;
622 case CRYPTO_SHA2_384:
623 thash = &auth_hash_sha2_384;
625 case CRYPTO_SHA2_512:
626 thash = &auth_hash_sha2_512;
629 case CRYPTO_NULL_HMAC:
630 thash = &auth_hash_null;
634 thash = &auth_hash_blake2b;
637 thash = &auth_hash_blake2s;
641 CRYPTDEB("invalid mac");
642 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
646 if (txform == NULL && thash == NULL) {
647 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
651 memset(&csp, 0, sizeof(csp));
652 if (use_outputbuffers)
653 csp.csp_flags |= CSP_F_SEPARATE_OUTPUT;
655 if (sop->cipher == CRYPTO_AES_NIST_GCM_16) {
657 #ifdef COMPAT_FREEBSD12
658 case CRYPTO_AES_128_NIST_GMAC:
659 case CRYPTO_AES_192_NIST_GMAC:
660 case CRYPTO_AES_256_NIST_GMAC:
661 if (sop->keylen != sop->mackeylen) {
662 SDT_PROBE1(opencrypto, dev, ioctl,
671 SDT_PROBE1(opencrypto, dev, ioctl, error,
675 csp.csp_mode = CSP_MODE_AEAD;
676 } else if (sop->cipher == CRYPTO_AES_CCM_16) {
678 #ifdef COMPAT_FREEBSD12
679 case CRYPTO_AES_CCM_CBC_MAC:
680 if (sop->keylen != sop->mackeylen) {
681 SDT_PROBE1(opencrypto, dev, ioctl,
691 SDT_PROBE1(opencrypto, dev, ioctl, error,
695 csp.csp_mode = CSP_MODE_AEAD;
696 } else if (txform && thash)
697 csp.csp_mode = CSP_MODE_ETA;
699 csp.csp_mode = CSP_MODE_CIPHER;
701 csp.csp_mode = CSP_MODE_DIGEST;
703 switch (csp.csp_mode) {
706 if (use_separate_aad)
707 csp.csp_flags |= CSP_F_SEPARATE_AAD;
712 csp.csp_cipher_alg = txform->type;
713 csp.csp_cipher_klen = sop->keylen;
714 if (sop->keylen > txform->maxkey ||
715 sop->keylen < txform->minkey) {
716 CRYPTDEB("invalid cipher parameters");
718 SDT_PROBE1(opencrypto, dev, ioctl, error,
723 key = malloc(csp.csp_cipher_klen, M_XDATA, M_WAITOK);
724 error = copyin(sop->key, key, csp.csp_cipher_klen);
726 CRYPTDEB("invalid key");
727 SDT_PROBE1(opencrypto, dev, ioctl, error,
731 csp.csp_cipher_key = key;
732 csp.csp_ivlen = txform->ivsize;
736 csp.csp_auth_alg = thash->type;
737 csp.csp_auth_klen = sop->mackeylen;
738 if (sop->mackeylen > thash->keysize ||
739 sop->mackeylen < 0) {
740 CRYPTDEB("invalid mac key length");
742 SDT_PROBE1(opencrypto, dev, ioctl, error,
747 if (csp.csp_auth_klen) {
748 mackey = malloc(csp.csp_auth_klen, M_XDATA,
750 error = copyin(sop->mackey, mackey,
753 CRYPTDEB("invalid mac key");
754 SDT_PROBE1(opencrypto, dev, ioctl,
758 csp.csp_auth_key = mackey;
761 if (csp.csp_auth_alg == CRYPTO_AES_NIST_GMAC)
762 csp.csp_ivlen = AES_GCM_IV_LEN;
763 if (csp.csp_auth_alg == CRYPTO_AES_CCM_CBC_MAC)
764 csp.csp_ivlen = AES_CCM_IV_LEN;
768 error = checkforsoftware(&crid);
770 CRYPTDEB("checkforsoftware");
771 SDT_PROBE1(opencrypto, dev, ioctl, error,
775 error = crypto_newsession(&cses, &csp, crid);
777 CRYPTDEB("crypto_newsession");
778 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
782 cse = csecreate(fcr, cses, &csp, txform, key, thash, mackey);
785 crypto_freesession(cses);
787 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
788 CRYPTDEB("csecreate");
793 /* return hardware/driver id */
794 sop->crid = crypto_ses2hid(cse->cses);
798 free(mackey, M_XDATA);
801 if (cmd == CIOCGSESSION && error == 0)
802 session2_op_to_op(sop, data);
805 ses = *(uint32_t *)data;
806 if (!csedelete(fcr, ses)) {
807 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
812 cop = (struct crypt_op *)data;
813 cse = csefind(fcr, cop->ses);
815 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
818 error = cryptodev_op(cse, cop, active_cred, td);
823 if (ratecheck(&keywarn, &warninterval))
825 "Asymmetric crypto operations via /dev/crypto");
827 if (!crypto_userasymcrypto) {
828 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
829 return (EPERM); /* XXX compat? */
831 kop = (struct crypt_kop *)data;
832 if (cmd == CIOCKEY) {
833 /* NB: crypto core enforces s/w driver use */
835 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
838 error = cryptodev_key(kop);
842 if (ratecheck(&featwarn, &warninterval))
844 "Asymmetric crypto features via /dev/crypto");
846 if (!crypto_userasymcrypto) {
848 * NB: if user asym crypto operations are
849 * not permitted return "no algorithms"
850 * so well-behaved applications will just
851 * fallback to doing them in software.
855 error = crypto_getfeat((int *)data);
857 SDT_PROBE1(opencrypto, dev, ioctl, error,
862 error = cryptodev_find((struct crypt_find_op *)data);
865 caead = (struct crypt_aead *)data;
866 cse = csefind(fcr, caead->ses);
868 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
871 error = cryptodev_aead(cse, caead, active_cred, td);
876 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
880 #ifdef COMPAT_FREEBSD32
884 session_op_to_32(data, data32);
886 case CIOCGSESSION232:
888 session2_op_to_32(data, data32);
892 crypt_op_to_32(data, data32);
894 case CIOCCRYPTAEAD32:
896 crypt_aead_to_32(data, data32);
900 crypt_kop_to_32(data, data32);
907 static struct cryptop_data *
908 cod_alloc(struct csession *cse, size_t aad_len, size_t len, struct thread *td)
910 struct cryptop_data *cod;
912 cod = malloc(sizeof(struct cryptop_data), M_XDATA, M_WAITOK | M_ZERO);
915 if (crypto_get_params(cse->cses)->csp_flags & CSP_F_SEPARATE_AAD) {
917 cod->aad = malloc(aad_len, M_XDATA, M_WAITOK);
918 cod->buf = malloc(len, M_XDATA, M_WAITOK);
920 cod->buf = malloc(aad_len + len, M_XDATA, M_WAITOK);
921 if (crypto_get_params(cse->cses)->csp_flags & CSP_F_SEPARATE_OUTPUT)
922 cod->obuf = malloc(len, M_XDATA, M_WAITOK);
927 cod_free(struct cryptop_data *cod)
930 free(cod->aad, M_XDATA);
931 free(cod->obuf, M_XDATA);
932 free(cod->buf, M_XDATA);
937 cryptodev_cb(struct cryptop *crp)
939 struct cryptop_data *cod = crp->crp_opaque;
942 * Lock to ensure the wakeup() is not missed by the loops
943 * waiting on cod->done in cryptodev_op() and
946 mtx_lock(&cod->cse->lock);
948 mtx_unlock(&cod->cse->lock);
954 cryptodev_op(struct csession *cse, const struct crypt_op *cop,
955 struct ucred *active_cred, struct thread *td)
957 struct cryptop_data *cod = NULL;
958 struct cryptop *crp = NULL;
962 if (cop->len > 256*1024-4) {
963 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
968 if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0) {
969 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
974 if (cop->mac && cse->hashsize == 0) {
975 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
980 * The COP_F_CIPHER_FIRST flag predates explicit session
981 * modes, but the only way it was used was for EtA so allow it
982 * as long as it is consistent with EtA.
984 if (cop->flags & COP_F_CIPHER_FIRST) {
985 if (cop->op != COP_ENCRYPT) {
986 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
991 cod = cod_alloc(cse, 0, cop->len + cse->hashsize, td);
994 crp = crypto_getreq(cse->cses, M_WAITOK);
996 error = copyin(cop->src, cod->buf, cop->len);
998 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1001 crp->crp_payload_start = 0;
1002 crp->crp_payload_length = cop->len;
1004 crp->crp_digest_start = cop->len;
1006 switch (cse->mode) {
1007 case CSP_MODE_COMPRESS:
1010 crp->crp_op = CRYPTO_OP_COMPRESS;
1013 crp->crp_op = CRYPTO_OP_DECOMPRESS;
1016 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1021 case CSP_MODE_CIPHER:
1024 crp->crp_op = CRYPTO_OP_ENCRYPT;
1027 crp->crp_op = CRYPTO_OP_DECRYPT;
1030 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1035 case CSP_MODE_DIGEST:
1040 crp->crp_op = CRYPTO_OP_COMPUTE_DIGEST;
1041 if (cod->obuf != NULL)
1042 crp->crp_digest_start = 0;
1045 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1053 crp->crp_op = CRYPTO_OP_ENCRYPT |
1054 CRYPTO_OP_COMPUTE_DIGEST;
1057 crp->crp_op = CRYPTO_OP_DECRYPT |
1058 CRYPTO_OP_VERIFY_DIGEST;
1061 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1067 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1072 crp->crp_flags = CRYPTO_F_CBIMM | (cop->flags & COP_F_BATCH);
1073 crypto_use_buf(crp, cod->buf, cop->len + cse->hashsize);
1075 crypto_use_output_buf(crp, cod->obuf, cop->len + cse->hashsize);
1076 crp->crp_callback = cryptodev_cb;
1077 crp->crp_opaque = cod;
1080 if (cse->ivsize == 0) {
1081 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1085 error = copyin(cop->iv, crp->crp_iv, cse->ivsize);
1087 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1090 crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
1091 } else if (cse->ivsize != 0) {
1092 crp->crp_iv_start = 0;
1093 crp->crp_payload_start += cse->ivsize;
1094 crp->crp_payload_length -= cse->ivsize;
1098 if (cop->mac != NULL && crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
1099 error = copyin(cop->mac, cod->buf + crp->crp_digest_start,
1102 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1108 * Let the dispatch run unlocked, then, interlock against the
1109 * callback before checking if the operation completed and going
1110 * to sleep. This insures drivers don't inherit our lock which
1111 * results in a lock order reversal between crypto_dispatch forced
1112 * entry and the crypto_done callback into us.
1114 error = crypto_dispatch(crp);
1116 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1120 mtx_lock(&cse->lock);
1122 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0);
1123 mtx_unlock(&cse->lock);
1125 if (crp->crp_etype == EAGAIN) {
1127 crp->crp_flags &= ~CRYPTO_F_DONE;
1132 if (crp->crp_etype != 0) {
1133 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1134 error = crp->crp_etype;
1138 if (cop->dst != NULL) {
1139 error = copyout(cod->obuf != NULL ? cod->obuf :
1140 cod->buf + crp->crp_payload_start, dst,
1141 crp->crp_payload_length);
1143 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1148 if (cop->mac != NULL && (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) == 0) {
1149 error = copyout((cod->obuf != NULL ? cod->obuf : cod->buf) +
1150 crp->crp_digest_start, cop->mac, cse->hashsize);
1152 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1158 crypto_freereq(crp);
1165 cryptodev_aead(struct csession *cse, struct crypt_aead *caead,
1166 struct ucred *active_cred, struct thread *td)
1168 struct cryptop_data *cod = NULL;
1169 struct cryptop *crp = NULL;
1173 if (caead->len > 256*1024-4 || caead->aadlen > 256*1024-4) {
1174 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1178 if (cse->txform == NULL || cse->hashsize == 0 || caead->tag == NULL ||
1179 (caead->len % cse->txform->blocksize) != 0) {
1180 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1185 * The COP_F_CIPHER_FIRST flag predates explicit session
1186 * modes, but the only way it was used was for EtA so allow it
1187 * as long as it is consistent with EtA.
1189 if (caead->flags & COP_F_CIPHER_FIRST) {
1190 if (caead->op != COP_ENCRYPT) {
1191 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1196 cod = cod_alloc(cse, caead->aadlen, caead->len + cse->hashsize, td);
1199 crp = crypto_getreq(cse->cses, M_WAITOK);
1201 if (cod->aad != NULL)
1202 error = copyin(caead->aad, cod->aad, caead->aadlen);
1204 error = copyin(caead->aad, cod->buf, caead->aadlen);
1206 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1209 crp->crp_aad = cod->aad;
1210 crp->crp_aad_start = 0;
1211 crp->crp_aad_length = caead->aadlen;
1213 if (cod->aad != NULL)
1214 crp->crp_payload_start = 0;
1216 crp->crp_payload_start = caead->aadlen;
1217 error = copyin(caead->src, cod->buf + crp->crp_payload_start,
1220 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1223 crp->crp_payload_length = caead->len;
1224 if (caead->op == COP_ENCRYPT && cod->obuf != NULL)
1225 crp->crp_digest_start = crp->crp_payload_output_start +
1228 crp->crp_digest_start = crp->crp_payload_start + caead->len;
1230 switch (cse->mode) {
1233 switch (caead->op) {
1235 crp->crp_op = CRYPTO_OP_ENCRYPT |
1236 CRYPTO_OP_COMPUTE_DIGEST;
1239 crp->crp_op = CRYPTO_OP_DECRYPT |
1240 CRYPTO_OP_VERIFY_DIGEST;
1243 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1249 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1254 crp->crp_flags = CRYPTO_F_CBIMM | (caead->flags & COP_F_BATCH);
1255 crypto_use_buf(crp, cod->buf, crp->crp_payload_start + caead->len +
1257 if (cod->obuf != NULL)
1258 crypto_use_output_buf(crp, cod->obuf, caead->len +
1260 crp->crp_callback = cryptodev_cb;
1261 crp->crp_opaque = cod;
1265 * Permit a 16-byte IV for AES-XTS, but only use the
1266 * first 8 bytes as a block number.
1268 if (cse->mode == CSP_MODE_ETA &&
1269 caead->ivlen == AES_BLOCK_LEN &&
1270 cse->ivsize == AES_XTS_IV_LEN)
1271 caead->ivlen = AES_XTS_IV_LEN;
1273 if (caead->ivlen != cse->ivsize) {
1275 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1279 error = copyin(caead->iv, crp->crp_iv, cse->ivsize);
1281 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1284 crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
1286 crp->crp_iv_start = crp->crp_payload_start;
1287 crp->crp_payload_start += cse->ivsize;
1288 crp->crp_payload_length -= cse->ivsize;
1292 if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
1293 error = copyin(caead->tag, cod->buf + crp->crp_digest_start,
1296 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1302 * Let the dispatch run unlocked, then, interlock against the
1303 * callback before checking if the operation completed and going
1304 * to sleep. This insures drivers don't inherit our lock which
1305 * results in a lock order reversal between crypto_dispatch forced
1306 * entry and the crypto_done callback into us.
1308 error = crypto_dispatch(crp);
1310 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1314 mtx_lock(&cse->lock);
1316 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0);
1317 mtx_unlock(&cse->lock);
1319 if (crp->crp_etype == EAGAIN) {
1321 crp->crp_flags &= ~CRYPTO_F_DONE;
1326 if (crp->crp_etype != 0) {
1327 error = crp->crp_etype;
1328 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1332 if (caead->dst != NULL) {
1333 error = copyout(cod->obuf != NULL ? cod->obuf :
1334 cod->buf + crp->crp_payload_start, dst,
1335 crp->crp_payload_length);
1337 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1342 if ((crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) == 0) {
1343 error = copyout((cod->obuf != NULL ? cod->obuf : cod->buf) +
1344 crp->crp_digest_start, caead->tag, cse->hashsize);
1346 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1352 crypto_freereq(crp);
1359 cryptodevkey_cb(struct cryptkop *krp)
1366 cryptodev_key(struct crypt_kop *kop)
1368 struct cryptkop *krp = NULL;
1370 int in, out, size, i;
1372 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
1373 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1377 in = kop->crk_iparams;
1378 out = kop->crk_oparams;
1379 switch (kop->crk_op) {
1381 if (in == 3 && out == 1)
1383 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1385 case CRK_MOD_EXP_CRT:
1386 if (in == 6 && out == 1)
1388 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1391 if (in == 5 && out == 2)
1393 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1395 case CRK_DSA_VERIFY:
1396 if (in == 7 && out == 0)
1398 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1400 case CRK_DH_COMPUTE_KEY:
1401 if (in == 3 && out == 1)
1403 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1406 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1410 krp = malloc(sizeof(*krp), M_XDATA, M_WAITOK | M_ZERO);
1411 krp->krp_op = kop->crk_op;
1412 krp->krp_status = kop->crk_status;
1413 krp->krp_iparams = kop->crk_iparams;
1414 krp->krp_oparams = kop->crk_oparams;
1415 krp->krp_crid = kop->crk_crid;
1416 krp->krp_status = 0;
1417 krp->krp_callback = cryptodevkey_cb;
1419 for (i = 0; i < CRK_MAXPARAM; i++) {
1420 if (kop->crk_param[i].crp_nbits > 65536) {
1421 /* Limit is the same as in OpenBSD */
1422 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1425 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
1427 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
1428 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1431 krp->krp_param[i].crp_p = malloc(size, M_XDATA, M_WAITOK);
1432 if (i >= krp->krp_iparams)
1434 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
1436 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1441 error = crypto_kdispatch(krp);
1443 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1446 error = tsleep(krp, PSOCK, "crydev", 0);
1448 /* XXX can this happen? if so, how do we recover? */
1449 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1453 kop->crk_crid = krp->krp_hid; /* device that did the work */
1454 if (krp->krp_status != 0) {
1455 error = krp->krp_status;
1456 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1460 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
1461 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1464 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
1466 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1473 kop->crk_status = krp->krp_status;
1474 for (i = 0; i < CRK_MAXPARAM; i++) {
1475 if (krp->krp_param[i].crp_p)
1476 free(krp->krp_param[i].crp_p, M_XDATA);
1484 cryptodev_find(struct crypt_find_op *find)
1487 size_t fnlen = sizeof find->name;
1489 if (find->crid != -1) {
1490 dev = crypto_find_device_byhid(find->crid);
1493 strncpy(find->name, device_get_nameunit(dev), fnlen);
1494 find->name[fnlen - 1] = '\x0';
1496 find->name[fnlen - 1] = '\x0';
1497 find->crid = crypto_find_driver(find->name);
1498 if (find->crid == -1)
1506 cryptof_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
1510 return (EOPNOTSUPP);
1515 cryptof_close(struct file *fp, struct thread *td)
1517 struct fcrypt *fcr = fp->f_data;
1518 struct csession *cse;
1520 while ((cse = TAILQ_FIRST(&fcr->csessions))) {
1521 TAILQ_REMOVE(&fcr->csessions, cse, next);
1522 KASSERT(cse->refs == 1,
1523 ("%s: crypto session %p with %d refs", __func__, cse,
1533 cryptof_fill_kinfo(struct file *fp, struct kinfo_file *kif,
1534 struct filedesc *fdp)
1537 kif->kf_type = KF_TYPE_CRYPTO;
1541 static struct csession *
1542 csefind(struct fcrypt *fcr, u_int ses)
1544 struct csession *cse;
1546 mtx_lock(&fcr->lock);
1547 TAILQ_FOREACH(cse, &fcr->csessions, next) {
1548 if (cse->ses == ses) {
1549 refcount_acquire(&cse->refs);
1550 mtx_unlock(&fcr->lock);
1554 mtx_unlock(&fcr->lock);
1559 csedelete(struct fcrypt *fcr, u_int ses)
1561 struct csession *cse;
1563 mtx_lock(&fcr->lock);
1564 TAILQ_FOREACH(cse, &fcr->csessions, next) {
1565 if (cse->ses == ses) {
1566 TAILQ_REMOVE(&fcr->csessions, cse, next);
1567 mtx_unlock(&fcr->lock);
1572 mtx_unlock(&fcr->lock);
1577 csecreate(struct fcrypt *fcr, crypto_session_t cses,
1578 struct crypto_session_params *csp, struct enc_xform *txform,
1579 void *key, struct auth_hash *thash, void *mackey)
1581 struct csession *cse;
1583 cse = malloc(sizeof(struct csession), M_XDATA, M_NOWAIT | M_ZERO);
1586 mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF);
1587 refcount_init(&cse->refs, 1);
1589 cse->mackey = mackey;
1590 cse->mode = csp->csp_mode;
1592 cse->txform = txform;
1594 cse->hashsize = thash->hashsize;
1595 else if (csp->csp_cipher_alg == CRYPTO_AES_NIST_GCM_16)
1596 cse->hashsize = AES_GMAC_HASH_LEN;
1597 else if (csp->csp_cipher_alg == CRYPTO_AES_CCM_16)
1598 cse->hashsize = AES_CBC_MAC_HASH_LEN;
1599 cse->ivsize = csp->csp_ivlen;
1600 mtx_lock(&fcr->lock);
1601 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
1602 cse->ses = fcr->sesn++;
1603 mtx_unlock(&fcr->lock);
1608 csefree(struct csession *cse)
1611 if (!refcount_release(&cse->refs))
1613 crypto_freesession(cse->cses);
1614 mtx_destroy(&cse->lock);
1616 free(cse->key, M_XDATA);
1618 free(cse->mackey, M_XDATA);
1623 cryptoioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
1632 error = falloc_noinstall(td, &f);
1636 fcr = malloc(sizeof(struct fcrypt), M_XDATA, M_WAITOK | M_ZERO);
1637 TAILQ_INIT(&fcr->csessions);
1638 mtx_init(&fcr->lock, "fcrypt", NULL, MTX_DEF);
1640 finit(f, FREAD | FWRITE, DTYPE_CRYPTO, fcr, &cryptofops);
1641 error = finstall(td, f, &fd, 0, NULL);
1643 mtx_destroy(&fcr->lock);
1646 *(uint32_t *)data = fd;
1650 error = cryptodev_find((struct crypt_find_op *)data);
1653 error = crypto_getfeat((int *)data);
1662 static struct cdevsw crypto_cdevsw = {
1663 .d_version = D_VERSION,
1664 .d_ioctl = cryptoioctl,
1667 static struct cdev *crypto_dev;
1670 * Initialization code, both for static and dynamic loading.
1673 cryptodev_modevent(module_t mod, int type, void *unused)
1678 printf("crypto: <crypto device>\n");
1679 crypto_dev = make_dev(&crypto_cdevsw, 0,
1680 UID_ROOT, GID_WHEEL, 0666,
1684 /*XXX disallow if active sessions */
1685 destroy_dev(crypto_dev);
1691 static moduledata_t cryptodev_mod = {
1696 MODULE_VERSION(cryptodev, 1);
1697 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1698 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
1699 MODULE_DEPEND(cryptodev, zlib, 1, 1, 1);