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 int cryptof_ioctl(struct file *, u_long, void *,
350 struct ucred *, struct thread *);
351 static int cryptof_stat(struct file *, struct stat *,
352 struct ucred *, struct thread *);
353 static int cryptof_close(struct file *, struct thread *);
354 static int cryptof_fill_kinfo(struct file *, struct kinfo_file *,
357 static struct fileops cryptofops = {
358 .fo_read = invfo_rdwr,
359 .fo_write = invfo_rdwr,
360 .fo_truncate = invfo_truncate,
361 .fo_ioctl = cryptof_ioctl,
362 .fo_poll = invfo_poll,
363 .fo_kqfilter = invfo_kqfilter,
364 .fo_stat = cryptof_stat,
365 .fo_close = cryptof_close,
366 .fo_chmod = invfo_chmod,
367 .fo_chown = invfo_chown,
368 .fo_sendfile = invfo_sendfile,
369 .fo_fill_kinfo = cryptof_fill_kinfo,
372 static struct csession *csefind(struct fcrypt *, u_int);
373 static bool csedelete(struct fcrypt *, u_int);
374 static struct csession *csecreate(struct fcrypt *, crypto_session_t,
375 struct crypto_session_params *, struct enc_xform *, void *,
376 struct auth_hash *, void *);
377 static void csefree(struct csession *);
379 static int cryptodev_op(struct csession *, struct crypt_op *,
380 struct ucred *, struct thread *td);
381 static int cryptodev_aead(struct csession *, struct crypt_aead *,
382 struct ucred *, struct thread *);
383 static int cryptodev_key(struct crypt_kop *);
384 static int cryptodev_find(struct crypt_find_op *);
387 * Check a crypto identifier to see if it requested
388 * a software device/driver. This can be done either
389 * by device name/class or through search constraints.
392 checkforsoftware(int *cridp)
398 if (!crypto_devallowsoft) {
399 if (crid & CRYPTOCAP_F_SOFTWARE) {
400 if (crid & CRYPTOCAP_F_HARDWARE) {
401 *cridp = CRYPTOCAP_F_HARDWARE;
406 if ((crid & CRYPTOCAP_F_HARDWARE) == 0 &&
407 (crypto_getcaps(crid) & CRYPTOCAP_F_HARDWARE) == 0)
419 struct ucred *active_cred,
422 struct crypto_session_params csp;
423 struct fcrypt *fcr = fp->f_data;
424 struct csession *cse;
425 struct session2_op *sop;
426 struct crypt_op *cop;
427 struct crypt_aead *caead;
428 struct enc_xform *txform = NULL;
429 struct auth_hash *thash = NULL;
432 struct crypt_kop *kop;
433 crypto_session_t cses;
437 struct session2_op sopc;
438 #ifdef COMPAT_FREEBSD32
439 struct crypt_op copc;
440 struct crypt_aead aeadc;
441 struct crypt_kop kopc;
444 #ifdef COMPAT_FREEBSD32
456 session_op_from_32((struct session_op32 *)data32, &thunk.sopc);
458 case CIOCGSESSION232:
463 session2_op_from_32((struct session2_op32 *)data32,
471 crypt_op_from_32((struct crypt_op32 *)data32, &thunk.copc);
473 case CIOCCRYPTAEAD32:
478 crypt_aead_from_32((struct crypt_aead32 *)data32, &thunk.aeadc);
484 if (cmd == CIOCKEY32)
489 crypt_kop_from_32((struct crypt_kop32 *)data32, &thunk.kopc);
497 if (cmd == CIOCGSESSION) {
498 session2_op_from_op(data, &thunk.sopc);
501 sop = (struct session2_op *)data;
503 switch (sop->cipher) {
507 txform = &enc_xform_rijndael128;
510 txform = &enc_xform_aes_xts;
512 case CRYPTO_NULL_CBC:
513 txform = &enc_xform_null;
515 case CRYPTO_CAMELLIA_CBC:
516 txform = &enc_xform_camellia;
519 txform = &enc_xform_aes_icm;
521 case CRYPTO_AES_NIST_GCM_16:
522 txform = &enc_xform_aes_nist_gcm;
524 case CRYPTO_CHACHA20:
525 txform = &enc_xform_chacha20;
527 case CRYPTO_AES_CCM_16:
528 txform = &enc_xform_ccm;
532 CRYPTDEB("invalid cipher");
533 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
540 case CRYPTO_POLY1305:
541 thash = &auth_hash_poly1305;
543 case CRYPTO_SHA1_HMAC:
544 thash = &auth_hash_hmac_sha1;
546 case CRYPTO_SHA2_224_HMAC:
547 thash = &auth_hash_hmac_sha2_224;
549 case CRYPTO_SHA2_256_HMAC:
550 thash = &auth_hash_hmac_sha2_256;
552 case CRYPTO_SHA2_384_HMAC:
553 thash = &auth_hash_hmac_sha2_384;
555 case CRYPTO_SHA2_512_HMAC:
556 thash = &auth_hash_hmac_sha2_512;
558 case CRYPTO_RIPEMD160_HMAC:
559 thash = &auth_hash_hmac_ripemd_160;
561 #ifdef COMPAT_FREEBSD12
562 case CRYPTO_AES_128_NIST_GMAC:
563 case CRYPTO_AES_192_NIST_GMAC:
564 case CRYPTO_AES_256_NIST_GMAC:
565 /* Should always be paired with GCM. */
566 if (sop->cipher != CRYPTO_AES_NIST_GCM_16) {
567 CRYPTDEB("GMAC without GCM");
568 SDT_PROBE1(opencrypto, dev, ioctl, error,
574 case CRYPTO_AES_NIST_GMAC:
575 switch (sop->mackeylen * 8) {
577 thash = &auth_hash_nist_gmac_aes_128;
580 thash = &auth_hash_nist_gmac_aes_192;
583 thash = &auth_hash_nist_gmac_aes_256;
586 CRYPTDEB("invalid GMAC key length");
587 SDT_PROBE1(opencrypto, dev, ioctl, error,
592 case CRYPTO_AES_CCM_CBC_MAC:
593 switch (sop->mackeylen) {
595 thash = &auth_hash_ccm_cbc_mac_128;
598 thash = &auth_hash_ccm_cbc_mac_192;
601 thash = &auth_hash_ccm_cbc_mac_256;
604 CRYPTDEB("Invalid CBC MAC key size %d",
606 SDT_PROBE1(opencrypto, dev, ioctl,
612 thash = &auth_hash_sha1;
614 case CRYPTO_SHA2_224:
615 thash = &auth_hash_sha2_224;
617 case CRYPTO_SHA2_256:
618 thash = &auth_hash_sha2_256;
620 case CRYPTO_SHA2_384:
621 thash = &auth_hash_sha2_384;
623 case CRYPTO_SHA2_512:
624 thash = &auth_hash_sha2_512;
627 case CRYPTO_NULL_HMAC:
628 thash = &auth_hash_null;
632 thash = &auth_hash_blake2b;
635 thash = &auth_hash_blake2s;
639 CRYPTDEB("invalid mac");
640 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
644 if (txform == NULL && thash == NULL) {
645 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
649 memset(&csp, 0, sizeof(csp));
650 if (use_outputbuffers)
651 csp.csp_flags |= CSP_F_SEPARATE_OUTPUT;
653 if (sop->cipher == CRYPTO_AES_NIST_GCM_16) {
655 #ifdef COMPAT_FREEBSD12
656 case CRYPTO_AES_128_NIST_GMAC:
657 case CRYPTO_AES_192_NIST_GMAC:
658 case CRYPTO_AES_256_NIST_GMAC:
659 if (sop->keylen != sop->mackeylen) {
660 SDT_PROBE1(opencrypto, dev, ioctl,
669 SDT_PROBE1(opencrypto, dev, ioctl, error,
673 csp.csp_mode = CSP_MODE_AEAD;
674 } else if (sop->cipher == CRYPTO_AES_CCM_16) {
676 #ifdef COMPAT_FREEBSD12
677 case CRYPTO_AES_CCM_CBC_MAC:
678 if (sop->keylen != sop->mackeylen) {
679 SDT_PROBE1(opencrypto, dev, ioctl,
689 SDT_PROBE1(opencrypto, dev, ioctl, error,
693 csp.csp_mode = CSP_MODE_AEAD;
694 } else if (txform && thash)
695 csp.csp_mode = CSP_MODE_ETA;
697 csp.csp_mode = CSP_MODE_CIPHER;
699 csp.csp_mode = CSP_MODE_DIGEST;
701 switch (csp.csp_mode) {
704 if (use_separate_aad)
705 csp.csp_flags |= CSP_F_SEPARATE_AAD;
710 csp.csp_cipher_alg = txform->type;
711 csp.csp_cipher_klen = sop->keylen;
712 if (sop->keylen > txform->maxkey ||
713 sop->keylen < txform->minkey) {
714 CRYPTDEB("invalid cipher parameters");
716 SDT_PROBE1(opencrypto, dev, ioctl, error,
721 key = malloc(csp.csp_cipher_klen, M_XDATA, M_WAITOK);
722 error = copyin(sop->key, key, csp.csp_cipher_klen);
724 CRYPTDEB("invalid key");
725 SDT_PROBE1(opencrypto, dev, ioctl, error,
729 csp.csp_cipher_key = key;
730 csp.csp_ivlen = txform->ivsize;
734 csp.csp_auth_alg = thash->type;
735 csp.csp_auth_klen = sop->mackeylen;
736 if (sop->mackeylen > thash->keysize ||
737 sop->mackeylen < 0) {
738 CRYPTDEB("invalid mac key length");
740 SDT_PROBE1(opencrypto, dev, ioctl, error,
745 if (csp.csp_auth_klen) {
746 mackey = malloc(csp.csp_auth_klen, M_XDATA,
748 error = copyin(sop->mackey, mackey,
751 CRYPTDEB("invalid mac key");
752 SDT_PROBE1(opencrypto, dev, ioctl,
756 csp.csp_auth_key = mackey;
759 if (csp.csp_auth_alg == CRYPTO_AES_NIST_GMAC)
760 csp.csp_ivlen = AES_GCM_IV_LEN;
761 if (csp.csp_auth_alg == CRYPTO_AES_CCM_CBC_MAC)
762 csp.csp_ivlen = AES_CCM_IV_LEN;
766 error = checkforsoftware(&crid);
768 CRYPTDEB("checkforsoftware");
769 SDT_PROBE1(opencrypto, dev, ioctl, error,
773 error = crypto_newsession(&cses, &csp, crid);
775 CRYPTDEB("crypto_newsession");
776 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
780 cse = csecreate(fcr, cses, &csp, txform, key, thash, mackey);
783 crypto_freesession(cses);
785 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
786 CRYPTDEB("csecreate");
791 /* return hardware/driver id */
792 sop->crid = crypto_ses2hid(cse->cses);
796 free(mackey, M_XDATA);
799 if (cmd == CIOCGSESSION && error == 0)
800 session2_op_to_op(sop, data);
803 ses = *(u_int32_t *)data;
804 if (!csedelete(fcr, ses)) {
805 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
810 cop = (struct crypt_op *)data;
811 cse = csefind(fcr, cop->ses);
813 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
816 error = cryptodev_op(cse, cop, active_cred, td);
821 if (!crypto_userasymcrypto) {
822 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
823 return (EPERM); /* XXX compat? */
825 kop = (struct crypt_kop *)data;
826 if (cmd == CIOCKEY) {
827 /* NB: crypto core enforces s/w driver use */
829 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
832 error = cryptodev_key(kop);
836 if (!crypto_userasymcrypto) {
838 * NB: if user asym crypto operations are
839 * not permitted return "no algorithms"
840 * so well-behaved applications will just
841 * fallback to doing them in software.
845 error = crypto_getfeat((int *)data);
847 SDT_PROBE1(opencrypto, dev, ioctl, error,
852 error = cryptodev_find((struct crypt_find_op *)data);
855 caead = (struct crypt_aead *)data;
856 cse = csefind(fcr, caead->ses);
858 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
861 error = cryptodev_aead(cse, caead, active_cred, td);
866 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
870 #ifdef COMPAT_FREEBSD32
874 session_op_to_32(data, data32);
876 case CIOCGSESSION232:
878 session2_op_to_32(data, data32);
882 crypt_op_to_32(data, data32);
884 case CIOCCRYPTAEAD32:
886 crypt_aead_to_32(data, data32);
890 crypt_kop_to_32(data, data32);
897 static int cryptodev_cb(struct cryptop *);
899 static struct cryptop_data *
900 cod_alloc(struct csession *cse, size_t aad_len, size_t len, struct thread *td)
902 struct cryptop_data *cod;
904 cod = malloc(sizeof(struct cryptop_data), M_XDATA, M_WAITOK | M_ZERO);
907 if (crypto_get_params(cse->cses)->csp_flags & CSP_F_SEPARATE_AAD) {
909 cod->aad = malloc(aad_len, M_XDATA, M_WAITOK);
910 cod->buf = malloc(len, M_XDATA, M_WAITOK);
912 cod->buf = malloc(aad_len + len, M_XDATA, M_WAITOK);
913 if (crypto_get_params(cse->cses)->csp_flags & CSP_F_SEPARATE_OUTPUT)
914 cod->obuf = malloc(len, M_XDATA, M_WAITOK);
919 cod_free(struct cryptop_data *cod)
922 free(cod->aad, M_XDATA);
923 free(cod->obuf, M_XDATA);
924 free(cod->buf, M_XDATA);
930 struct csession *cse,
931 struct crypt_op *cop,
932 struct ucred *active_cred,
935 struct cryptop_data *cod = NULL;
936 struct cryptop *crp = NULL;
939 if (cop->len > 256*1024-4) {
940 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
945 if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0) {
946 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
951 if (cop->mac && cse->hashsize == 0) {
952 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
957 * The COP_F_CIPHER_FIRST flag predates explicit session
958 * modes, but the only way it was used was for EtA so allow it
959 * as long as it is consistent with EtA.
961 if (cop->flags & COP_F_CIPHER_FIRST) {
962 if (cop->op != COP_ENCRYPT) {
963 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
968 cod = cod_alloc(cse, 0, cop->len + cse->hashsize, td);
970 crp = crypto_getreq(cse->cses, M_WAITOK);
972 error = copyin(cop->src, cod->buf, cop->len);
974 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
977 crp->crp_payload_start = 0;
978 crp->crp_payload_length = cop->len;
980 crp->crp_digest_start = cop->len;
983 case CSP_MODE_COMPRESS:
986 crp->crp_op = CRYPTO_OP_COMPRESS;
989 crp->crp_op = CRYPTO_OP_DECOMPRESS;
992 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
997 case CSP_MODE_CIPHER:
1000 crp->crp_op = CRYPTO_OP_ENCRYPT;
1003 crp->crp_op = CRYPTO_OP_DECRYPT;
1006 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1011 case CSP_MODE_DIGEST:
1016 crp->crp_op = CRYPTO_OP_COMPUTE_DIGEST;
1017 if (cod->obuf != NULL)
1018 crp->crp_digest_start = 0;
1021 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1029 crp->crp_op = CRYPTO_OP_ENCRYPT |
1030 CRYPTO_OP_COMPUTE_DIGEST;
1033 crp->crp_op = CRYPTO_OP_DECRYPT |
1034 CRYPTO_OP_VERIFY_DIGEST;
1037 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1043 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1048 crp->crp_flags = CRYPTO_F_CBIMM | (cop->flags & COP_F_BATCH);
1049 crypto_use_buf(crp, cod->buf, cop->len + cse->hashsize);
1051 crypto_use_output_buf(crp, cod->obuf, cop->len + cse->hashsize);
1052 crp->crp_callback = cryptodev_cb;
1053 crp->crp_opaque = cod;
1056 if (cse->ivsize == 0) {
1057 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1061 error = copyin(cop->iv, crp->crp_iv, cse->ivsize);
1063 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1066 crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
1067 } else if (cse->ivsize != 0) {
1068 crp->crp_iv_start = 0;
1069 crp->crp_payload_start += cse->ivsize;
1070 crp->crp_payload_length -= cse->ivsize;
1071 cop->dst += cse->ivsize;
1074 if (cop->mac != NULL && crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
1075 error = copyin(cop->mac, cod->buf + crp->crp_digest_start,
1078 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1084 * Let the dispatch run unlocked, then, interlock against the
1085 * callback before checking if the operation completed and going
1086 * to sleep. This insures drivers don't inherit our lock which
1087 * results in a lock order reversal between crypto_dispatch forced
1088 * entry and the crypto_done callback into us.
1090 error = crypto_dispatch(crp);
1092 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1096 mtx_lock(&cse->lock);
1098 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0);
1099 mtx_unlock(&cse->lock);
1101 if (crp->crp_etype == EAGAIN) {
1103 crp->crp_flags &= ~CRYPTO_F_DONE;
1108 if (crp->crp_etype != 0) {
1109 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1110 error = crp->crp_etype;
1114 if (cop->dst != NULL) {
1115 error = copyout(cod->obuf != NULL ? cod->obuf :
1116 cod->buf + crp->crp_payload_start, cop->dst,
1117 crp->crp_payload_length);
1119 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1124 if (cop->mac != NULL && (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) == 0) {
1125 error = copyout((cod->obuf != NULL ? cod->obuf : cod->buf) +
1126 crp->crp_digest_start, cop->mac, cse->hashsize);
1128 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1134 crypto_freereq(crp);
1142 struct csession *cse,
1143 struct crypt_aead *caead,
1144 struct ucred *active_cred,
1147 struct cryptop_data *cod = NULL;
1148 struct cryptop *crp = NULL;
1151 if (caead->len > 256*1024-4 || caead->aadlen > 256*1024-4) {
1152 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1156 if (cse->txform == NULL || cse->hashsize == 0 || caead->tag == NULL ||
1157 (caead->len % cse->txform->blocksize) != 0) {
1158 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1163 * The COP_F_CIPHER_FIRST flag predates explicit session
1164 * modes, but the only way it was used was for EtA so allow it
1165 * as long as it is consistent with EtA.
1167 if (caead->flags & COP_F_CIPHER_FIRST) {
1168 if (caead->op != COP_ENCRYPT) {
1169 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1174 cod = cod_alloc(cse, caead->aadlen, caead->len + cse->hashsize, td);
1176 crp = crypto_getreq(cse->cses, M_WAITOK);
1178 if (cod->aad != NULL)
1179 error = copyin(caead->aad, cod->aad, caead->aadlen);
1181 error = copyin(caead->aad, cod->buf, caead->aadlen);
1183 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1186 crp->crp_aad = cod->aad;
1187 crp->crp_aad_start = 0;
1188 crp->crp_aad_length = caead->aadlen;
1190 if (cod->aad != NULL)
1191 crp->crp_payload_start = 0;
1193 crp->crp_payload_start = caead->aadlen;
1194 error = copyin(caead->src, cod->buf + crp->crp_payload_start,
1197 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1200 crp->crp_payload_length = caead->len;
1201 if (caead->op == COP_ENCRYPT && cod->obuf != NULL)
1202 crp->crp_digest_start = crp->crp_payload_output_start +
1205 crp->crp_digest_start = crp->crp_payload_start + caead->len;
1207 switch (cse->mode) {
1210 switch (caead->op) {
1212 crp->crp_op = CRYPTO_OP_ENCRYPT |
1213 CRYPTO_OP_COMPUTE_DIGEST;
1216 crp->crp_op = CRYPTO_OP_DECRYPT |
1217 CRYPTO_OP_VERIFY_DIGEST;
1220 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1226 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1231 crp->crp_flags = CRYPTO_F_CBIMM | (caead->flags & COP_F_BATCH);
1232 crypto_use_buf(crp, cod->buf, crp->crp_payload_start + caead->len +
1234 if (cod->obuf != NULL)
1235 crypto_use_output_buf(crp, cod->obuf, caead->len +
1237 crp->crp_callback = cryptodev_cb;
1238 crp->crp_opaque = cod;
1242 * Permit a 16-byte IV for AES-XTS, but only use the
1243 * first 8 bytes as a block number.
1245 if (cse->mode == CSP_MODE_ETA &&
1246 caead->ivlen == AES_BLOCK_LEN &&
1247 cse->ivsize == AES_XTS_IV_LEN)
1248 caead->ivlen = AES_XTS_IV_LEN;
1250 if (caead->ivlen != cse->ivsize) {
1252 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1256 error = copyin(caead->iv, crp->crp_iv, cse->ivsize);
1258 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1261 crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
1263 crp->crp_iv_start = crp->crp_payload_start;
1264 crp->crp_payload_start += cse->ivsize;
1265 crp->crp_payload_length -= cse->ivsize;
1266 caead->dst += cse->ivsize;
1269 if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
1270 error = copyin(caead->tag, cod->buf + crp->crp_digest_start,
1273 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1279 * Let the dispatch run unlocked, then, interlock against the
1280 * callback before checking if the operation completed and going
1281 * to sleep. This insures drivers don't inherit our lock which
1282 * results in a lock order reversal between crypto_dispatch forced
1283 * entry and the crypto_done callback into us.
1285 error = crypto_dispatch(crp);
1287 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1291 mtx_lock(&cse->lock);
1293 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0);
1294 mtx_unlock(&cse->lock);
1296 if (crp->crp_etype == EAGAIN) {
1298 crp->crp_flags &= ~CRYPTO_F_DONE;
1303 if (crp->crp_etype != 0) {
1304 error = crp->crp_etype;
1305 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1309 if (caead->dst != NULL) {
1310 error = copyout(cod->obuf != NULL ? cod->obuf :
1311 cod->buf + crp->crp_payload_start, caead->dst,
1312 crp->crp_payload_length);
1314 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1319 if ((crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) == 0) {
1320 error = copyout((cod->obuf != NULL ? cod->obuf : cod->buf) +
1321 crp->crp_digest_start, caead->tag, cse->hashsize);
1323 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1329 crypto_freereq(crp);
1336 cryptodev_cb(struct cryptop *crp)
1338 struct cryptop_data *cod = crp->crp_opaque;
1341 * Lock to ensure the wakeup() is not missed by the loops
1342 * waiting on cod->done in cryptodev_op() and
1345 mtx_lock(&cod->cse->lock);
1347 mtx_unlock(&cod->cse->lock);
1353 cryptodevkey_cb(struct cryptkop *krp)
1360 cryptodev_key(struct crypt_kop *kop)
1362 struct cryptkop *krp = NULL;
1364 int in, out, size, i;
1366 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
1367 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1371 in = kop->crk_iparams;
1372 out = kop->crk_oparams;
1373 switch (kop->crk_op) {
1375 if (in == 3 && out == 1)
1377 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1379 case CRK_MOD_EXP_CRT:
1380 if (in == 6 && out == 1)
1382 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1385 if (in == 5 && out == 2)
1387 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1389 case CRK_DSA_VERIFY:
1390 if (in == 7 && out == 0)
1392 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1394 case CRK_DH_COMPUTE_KEY:
1395 if (in == 3 && out == 1)
1397 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1400 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1404 krp = malloc(sizeof(*krp), M_XDATA, M_WAITOK | M_ZERO);
1405 krp->krp_op = kop->crk_op;
1406 krp->krp_status = kop->crk_status;
1407 krp->krp_iparams = kop->crk_iparams;
1408 krp->krp_oparams = kop->crk_oparams;
1409 krp->krp_crid = kop->crk_crid;
1410 krp->krp_status = 0;
1411 krp->krp_callback = cryptodevkey_cb;
1413 for (i = 0; i < CRK_MAXPARAM; i++) {
1414 if (kop->crk_param[i].crp_nbits > 65536) {
1415 /* Limit is the same as in OpenBSD */
1416 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1419 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
1421 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
1422 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1425 krp->krp_param[i].crp_p = malloc(size, M_XDATA, M_WAITOK);
1426 if (i >= krp->krp_iparams)
1428 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
1430 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1435 error = crypto_kdispatch(krp);
1437 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1440 error = tsleep(krp, PSOCK, "crydev", 0);
1442 /* XXX can this happen? if so, how do we recover? */
1443 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1447 kop->crk_crid = krp->krp_hid; /* device that did the work */
1448 if (krp->krp_status != 0) {
1449 error = krp->krp_status;
1450 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1454 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
1455 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1458 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
1460 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1467 kop->crk_status = krp->krp_status;
1468 for (i = 0; i < CRK_MAXPARAM; i++) {
1469 if (krp->krp_param[i].crp_p)
1470 free(krp->krp_param[i].crp_p, M_XDATA);
1478 cryptodev_find(struct crypt_find_op *find)
1481 size_t fnlen = sizeof find->name;
1483 if (find->crid != -1) {
1484 dev = crypto_find_device_byhid(find->crid);
1487 strncpy(find->name, device_get_nameunit(dev), fnlen);
1488 find->name[fnlen - 1] = '\x0';
1490 find->name[fnlen - 1] = '\x0';
1491 find->crid = crypto_find_driver(find->name);
1492 if (find->crid == -1)
1503 struct ucred *active_cred,
1507 return (EOPNOTSUPP);
1512 cryptof_close(struct file *fp, struct thread *td)
1514 struct fcrypt *fcr = fp->f_data;
1515 struct csession *cse;
1517 while ((cse = TAILQ_FIRST(&fcr->csessions))) {
1518 TAILQ_REMOVE(&fcr->csessions, cse, next);
1519 KASSERT(cse->refs == 1,
1520 ("%s: crypto session %p with %d refs", __func__, cse,
1530 cryptof_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
1533 kif->kf_type = KF_TYPE_CRYPTO;
1537 static struct csession *
1538 csefind(struct fcrypt *fcr, u_int ses)
1540 struct csession *cse;
1542 mtx_lock(&fcr->lock);
1543 TAILQ_FOREACH(cse, &fcr->csessions, next) {
1544 if (cse->ses == ses) {
1545 refcount_acquire(&cse->refs);
1546 mtx_unlock(&fcr->lock);
1550 mtx_unlock(&fcr->lock);
1555 csedelete(struct fcrypt *fcr, u_int ses)
1557 struct csession *cse;
1559 mtx_lock(&fcr->lock);
1560 TAILQ_FOREACH(cse, &fcr->csessions, next) {
1561 if (cse->ses == ses) {
1562 TAILQ_REMOVE(&fcr->csessions, cse, next);
1563 mtx_unlock(&fcr->lock);
1568 mtx_unlock(&fcr->lock);
1573 csecreate(struct fcrypt *fcr, crypto_session_t cses,
1574 struct crypto_session_params *csp, struct enc_xform *txform,
1575 void *key, struct auth_hash *thash, void *mackey)
1577 struct csession *cse;
1579 cse = malloc(sizeof(struct csession), M_XDATA, M_NOWAIT | M_ZERO);
1582 mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF);
1583 refcount_init(&cse->refs, 1);
1585 cse->mackey = mackey;
1586 cse->mode = csp->csp_mode;
1588 cse->txform = txform;
1590 cse->hashsize = thash->hashsize;
1591 else if (csp->csp_cipher_alg == CRYPTO_AES_NIST_GCM_16)
1592 cse->hashsize = AES_GMAC_HASH_LEN;
1593 else if (csp->csp_cipher_alg == CRYPTO_AES_CCM_16)
1594 cse->hashsize = AES_CBC_MAC_HASH_LEN;
1595 cse->ivsize = csp->csp_ivlen;
1596 mtx_lock(&fcr->lock);
1597 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
1598 cse->ses = fcr->sesn++;
1599 mtx_unlock(&fcr->lock);
1604 csefree(struct csession *cse)
1607 if (!refcount_release(&cse->refs))
1609 crypto_freesession(cse->cses);
1610 mtx_destroy(&cse->lock);
1612 free(cse->key, M_XDATA);
1614 free(cse->mackey, M_XDATA);
1619 cryptoioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1627 error = falloc_noinstall(td, &f);
1631 fcr = malloc(sizeof(struct fcrypt), M_XDATA, M_WAITOK | M_ZERO);
1632 TAILQ_INIT(&fcr->csessions);
1633 mtx_init(&fcr->lock, "fcrypt", NULL, MTX_DEF);
1635 finit(f, FREAD | FWRITE, DTYPE_CRYPTO, fcr, &cryptofops);
1636 error = finstall(td, f, &fd, 0, NULL);
1638 mtx_destroy(&fcr->lock);
1641 *(uint32_t *)data = fd;
1645 error = cryptodev_find((struct crypt_find_op *)data);
1648 error = crypto_getfeat((int *)data);
1657 static struct cdevsw crypto_cdevsw = {
1658 .d_version = D_VERSION,
1659 .d_ioctl = cryptoioctl,
1662 static struct cdev *crypto_dev;
1665 * Initialization code, both for static and dynamic loading.
1668 cryptodev_modevent(module_t mod, int type, void *unused)
1673 printf("crypto: <crypto device>\n");
1674 crypto_dev = make_dev(&crypto_cdevsw, 0,
1675 UID_ROOT, GID_WHEEL, 0666,
1679 /*XXX disallow if active sessions */
1680 destroy_dev(crypto_dev);
1686 static moduledata_t cryptodev_mod = {
1691 MODULE_VERSION(cryptodev, 1);
1692 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1693 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
1694 MODULE_DEPEND(cryptodev, zlib, 1, 1, 1);