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 #define CIOCGSESSION32 _IOWR('c', 101, struct session_op32)
121 #define CIOCCRYPT32 _IOWR('c', 103, struct crypt_op32)
122 #define CIOCKEY32 _IOWR('c', 104, struct crypt_kop32)
123 #define CIOCGSESSION232 _IOWR('c', 106, struct session2_op32)
124 #define CIOCKEY232 _IOWR('c', 107, struct crypt_kop32)
127 session_op_from_32(const struct session_op32 *from, struct session_op *to)
130 CP(*from, *to, cipher);
132 CP(*from, *to, keylen);
133 PTRIN_CP(*from, *to, key);
134 CP(*from, *to, mackeylen);
135 PTRIN_CP(*from, *to, mackey);
140 session2_op_from_32(const struct session2_op32 *from, struct session2_op *to)
143 session_op_from_32((const struct session_op32 *)from,
144 (struct session_op *)to);
145 CP(*from, *to, crid);
149 session_op_to_32(const struct session_op *from, struct session_op32 *to)
152 CP(*from, *to, cipher);
154 CP(*from, *to, keylen);
155 PTROUT_CP(*from, *to, key);
156 CP(*from, *to, mackeylen);
157 PTROUT_CP(*from, *to, mackey);
162 session2_op_to_32(const struct session2_op *from, struct session2_op32 *to)
165 session_op_to_32((const struct session_op *)from,
166 (struct session_op32 *)to);
167 CP(*from, *to, crid);
171 crypt_op_from_32(const struct crypt_op32 *from, struct crypt_op *to)
176 CP(*from, *to, flags);
178 PTRIN_CP(*from, *to, src);
179 PTRIN_CP(*from, *to, dst);
180 PTRIN_CP(*from, *to, mac);
181 PTRIN_CP(*from, *to, iv);
185 crypt_op_to_32(const struct crypt_op *from, struct crypt_op32 *to)
190 CP(*from, *to, flags);
192 PTROUT_CP(*from, *to, src);
193 PTROUT_CP(*from, *to, dst);
194 PTROUT_CP(*from, *to, mac);
195 PTROUT_CP(*from, *to, iv);
199 crparam_from_32(const struct crparam32 *from, struct crparam *to)
202 PTRIN_CP(*from, *to, crp_p);
203 CP(*from, *to, crp_nbits);
207 crparam_to_32(const struct crparam *from, struct crparam32 *to)
210 PTROUT_CP(*from, *to, crp_p);
211 CP(*from, *to, crp_nbits);
215 crypt_kop_from_32(const struct crypt_kop32 *from, struct crypt_kop *to)
219 CP(*from, *to, crk_op);
220 CP(*from, *to, crk_status);
221 CP(*from, *to, crk_iparams);
222 CP(*from, *to, crk_oparams);
223 CP(*from, *to, crk_crid);
224 for (i = 0; i < CRK_MAXPARAM; i++)
225 crparam_from_32(&from->crk_param[i], &to->crk_param[i]);
229 crypt_kop_to_32(const struct crypt_kop *from, struct crypt_kop32 *to)
233 CP(*from, *to, crk_op);
234 CP(*from, *to, crk_status);
235 CP(*from, *to, crk_iparams);
236 CP(*from, *to, crk_oparams);
237 CP(*from, *to, crk_crid);
238 for (i = 0; i < CRK_MAXPARAM; i++)
239 crparam_to_32(&from->crk_param[i], &to->crk_param[i]);
244 TAILQ_ENTRY(csession) next;
245 crypto_session_t cses;
248 struct mtx lock; /* for op submission */
250 struct enc_xform *txform;
259 struct cryptop_data {
260 struct csession *cse;
269 TAILQ_HEAD(csessionlist, csession) csessions;
274 static bool use_outputbuffers;
275 SYSCTL_BOOL(_kern_crypto, OID_AUTO, cryptodev_use_output, CTLFLAG_RW,
276 &use_outputbuffers, 0,
277 "Use separate output buffers for /dev/crypto requests.");
279 static bool use_separate_aad;
280 SYSCTL_BOOL(_kern_crypto, OID_AUTO, cryptodev_separate_aad, CTLFLAG_RW,
281 &use_separate_aad, 0,
282 "Use separate AAD buffer for /dev/crypto requests.");
284 static int cryptof_ioctl(struct file *, u_long, void *,
285 struct ucred *, struct thread *);
286 static int cryptof_stat(struct file *, struct stat *,
287 struct ucred *, struct thread *);
288 static int cryptof_close(struct file *, struct thread *);
289 static int cryptof_fill_kinfo(struct file *, struct kinfo_file *,
292 static struct fileops cryptofops = {
293 .fo_read = invfo_rdwr,
294 .fo_write = invfo_rdwr,
295 .fo_truncate = invfo_truncate,
296 .fo_ioctl = cryptof_ioctl,
297 .fo_poll = invfo_poll,
298 .fo_kqfilter = invfo_kqfilter,
299 .fo_stat = cryptof_stat,
300 .fo_close = cryptof_close,
301 .fo_chmod = invfo_chmod,
302 .fo_chown = invfo_chown,
303 .fo_sendfile = invfo_sendfile,
304 .fo_fill_kinfo = cryptof_fill_kinfo,
307 static struct csession *csefind(struct fcrypt *, u_int);
308 static bool csedelete(struct fcrypt *, u_int);
309 static struct csession *csecreate(struct fcrypt *, crypto_session_t,
310 struct crypto_session_params *, struct enc_xform *, void *,
311 struct auth_hash *, void *);
312 static void csefree(struct csession *);
314 static int cryptodev_op(struct csession *, struct crypt_op *,
315 struct ucred *, struct thread *td);
316 static int cryptodev_aead(struct csession *, struct crypt_aead *,
317 struct ucred *, struct thread *);
318 static int cryptodev_key(struct crypt_kop *);
319 static int cryptodev_find(struct crypt_find_op *);
322 * Check a crypto identifier to see if it requested
323 * a software device/driver. This can be done either
324 * by device name/class or through search constraints.
327 checkforsoftware(int *cridp)
333 if (!crypto_devallowsoft) {
334 if (crid & CRYPTOCAP_F_SOFTWARE) {
335 if (crid & CRYPTOCAP_F_HARDWARE) {
336 *cridp = CRYPTOCAP_F_HARDWARE;
341 if ((crid & CRYPTOCAP_F_HARDWARE) == 0 &&
342 (crypto_getcaps(crid) & CRYPTOCAP_F_HARDWARE) == 0)
354 struct ucred *active_cred,
357 #define SES2(p) ((struct session2_op *)p)
358 struct crypto_session_params csp;
359 struct fcrypt *fcr = fp->f_data;
360 struct csession *cse;
361 struct session_op *sop;
362 struct crypt_op *cop;
363 struct crypt_aead *caead;
364 struct enc_xform *txform = NULL;
365 struct auth_hash *thash = NULL;
368 struct crypt_kop *kop;
369 crypto_session_t cses;
372 #ifdef COMPAT_FREEBSD32
373 struct session2_op sopc;
374 struct crypt_op copc;
375 struct crypt_kop kopc;
381 #ifdef COMPAT_FREEBSD32
383 case CIOCGSESSION232:
384 if (cmd == CIOCGSESSION32) {
385 session_op_from_32(data, (struct session_op *)&sopc);
386 sop = (struct session_op *)&sopc;
387 } else if (cmd == CIOCGSESSION232) {
388 session2_op_from_32(data, &sopc);
389 sop = (struct session_op *)&sopc;
392 sop = (struct session_op *)data;
393 switch (sop->cipher) {
397 txform = &enc_xform_rijndael128;
400 txform = &enc_xform_aes_xts;
402 case CRYPTO_NULL_CBC:
403 txform = &enc_xform_null;
405 case CRYPTO_CAMELLIA_CBC:
406 txform = &enc_xform_camellia;
409 txform = &enc_xform_aes_icm;
411 case CRYPTO_AES_NIST_GCM_16:
412 txform = &enc_xform_aes_nist_gcm;
414 case CRYPTO_CHACHA20:
415 txform = &enc_xform_chacha20;
417 case CRYPTO_AES_CCM_16:
418 txform = &enc_xform_ccm;
422 CRYPTDEB("invalid cipher");
423 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
430 case CRYPTO_POLY1305:
431 thash = &auth_hash_poly1305;
433 case CRYPTO_SHA1_HMAC:
434 thash = &auth_hash_hmac_sha1;
436 case CRYPTO_SHA2_224_HMAC:
437 thash = &auth_hash_hmac_sha2_224;
439 case CRYPTO_SHA2_256_HMAC:
440 thash = &auth_hash_hmac_sha2_256;
442 case CRYPTO_SHA2_384_HMAC:
443 thash = &auth_hash_hmac_sha2_384;
445 case CRYPTO_SHA2_512_HMAC:
446 thash = &auth_hash_hmac_sha2_512;
448 case CRYPTO_RIPEMD160_HMAC:
449 thash = &auth_hash_hmac_ripemd_160;
451 #ifdef COMPAT_FREEBSD12
452 case CRYPTO_AES_128_NIST_GMAC:
453 case CRYPTO_AES_192_NIST_GMAC:
454 case CRYPTO_AES_256_NIST_GMAC:
455 /* Should always be paired with GCM. */
456 if (sop->cipher != CRYPTO_AES_NIST_GCM_16) {
457 CRYPTDEB("GMAC without GCM");
458 SDT_PROBE1(opencrypto, dev, ioctl, error,
464 case CRYPTO_AES_NIST_GMAC:
465 switch (sop->mackeylen * 8) {
467 thash = &auth_hash_nist_gmac_aes_128;
470 thash = &auth_hash_nist_gmac_aes_192;
473 thash = &auth_hash_nist_gmac_aes_256;
476 CRYPTDEB("invalid GMAC key length");
477 SDT_PROBE1(opencrypto, dev, ioctl, error,
482 case CRYPTO_AES_CCM_CBC_MAC:
483 switch (sop->mackeylen) {
485 thash = &auth_hash_ccm_cbc_mac_128;
488 thash = &auth_hash_ccm_cbc_mac_192;
491 thash = &auth_hash_ccm_cbc_mac_256;
494 CRYPTDEB("Invalid CBC MAC key size %d",
496 SDT_PROBE1(opencrypto, dev, ioctl,
502 thash = &auth_hash_sha1;
504 case CRYPTO_SHA2_224:
505 thash = &auth_hash_sha2_224;
507 case CRYPTO_SHA2_256:
508 thash = &auth_hash_sha2_256;
510 case CRYPTO_SHA2_384:
511 thash = &auth_hash_sha2_384;
513 case CRYPTO_SHA2_512:
514 thash = &auth_hash_sha2_512;
517 case CRYPTO_NULL_HMAC:
518 thash = &auth_hash_null;
522 thash = &auth_hash_blake2b;
525 thash = &auth_hash_blake2s;
529 CRYPTDEB("invalid mac");
530 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
534 if (txform == NULL && thash == NULL) {
535 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
539 memset(&csp, 0, sizeof(csp));
540 if (use_outputbuffers)
541 csp.csp_flags |= CSP_F_SEPARATE_OUTPUT;
543 if (sop->cipher == CRYPTO_AES_NIST_GCM_16) {
545 #ifdef COMPAT_FREEBSD12
546 case CRYPTO_AES_128_NIST_GMAC:
547 case CRYPTO_AES_192_NIST_GMAC:
548 case CRYPTO_AES_256_NIST_GMAC:
549 if (sop->keylen != sop->mackeylen) {
550 SDT_PROBE1(opencrypto, dev, ioctl,
559 SDT_PROBE1(opencrypto, dev, ioctl, error,
563 csp.csp_mode = CSP_MODE_AEAD;
564 } else if (sop->cipher == CRYPTO_AES_CCM_16) {
566 #ifdef COMPAT_FREEBSD12
567 case CRYPTO_AES_CCM_CBC_MAC:
568 if (sop->keylen != sop->mackeylen) {
569 SDT_PROBE1(opencrypto, dev, ioctl,
579 SDT_PROBE1(opencrypto, dev, ioctl, error,
583 csp.csp_mode = CSP_MODE_AEAD;
584 } else if (txform && thash)
585 csp.csp_mode = CSP_MODE_ETA;
587 csp.csp_mode = CSP_MODE_CIPHER;
589 csp.csp_mode = CSP_MODE_DIGEST;
591 switch (csp.csp_mode) {
594 if (use_separate_aad)
595 csp.csp_flags |= CSP_F_SEPARATE_AAD;
600 csp.csp_cipher_alg = txform->type;
601 csp.csp_cipher_klen = sop->keylen;
602 if (sop->keylen > txform->maxkey ||
603 sop->keylen < txform->minkey) {
604 CRYPTDEB("invalid cipher parameters");
606 SDT_PROBE1(opencrypto, dev, ioctl, error,
611 key = malloc(csp.csp_cipher_klen, M_XDATA, M_WAITOK);
612 error = copyin(sop->key, key, csp.csp_cipher_klen);
614 CRYPTDEB("invalid key");
615 SDT_PROBE1(opencrypto, dev, ioctl, error,
619 csp.csp_cipher_key = key;
620 csp.csp_ivlen = txform->ivsize;
624 csp.csp_auth_alg = thash->type;
625 csp.csp_auth_klen = sop->mackeylen;
626 if (sop->mackeylen > thash->keysize ||
627 sop->mackeylen < 0) {
628 CRYPTDEB("invalid mac key length");
630 SDT_PROBE1(opencrypto, dev, ioctl, error,
635 if (csp.csp_auth_klen) {
636 mackey = malloc(csp.csp_auth_klen, M_XDATA,
638 error = copyin(sop->mackey, mackey,
641 CRYPTDEB("invalid mac key");
642 SDT_PROBE1(opencrypto, dev, ioctl,
646 csp.csp_auth_key = mackey;
649 if (csp.csp_auth_alg == CRYPTO_AES_NIST_GMAC)
650 csp.csp_ivlen = AES_GCM_IV_LEN;
651 if (csp.csp_auth_alg == CRYPTO_AES_CCM_CBC_MAC)
652 csp.csp_ivlen = AES_CCM_IV_LEN;
655 /* NB: CIOCGSESSION2 has the crid */
656 if (cmd == CIOCGSESSION2
657 #ifdef COMPAT_FREEBSD32
658 || cmd == CIOCGSESSION232
661 crid = SES2(sop)->crid;
662 error = checkforsoftware(&crid);
664 CRYPTDEB("checkforsoftware");
665 SDT_PROBE1(opencrypto, dev, ioctl, error,
670 crid = CRYPTOCAP_F_HARDWARE;
671 error = crypto_newsession(&cses, &csp, crid);
673 CRYPTDEB("crypto_newsession");
674 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
678 cse = csecreate(fcr, cses, &csp, txform, key, thash, mackey);
681 crypto_freesession(cses);
683 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
684 CRYPTDEB("csecreate");
688 if (cmd == CIOCGSESSION2
689 #ifdef COMPAT_FREEBSD32
690 || cmd == CIOCGSESSION232
693 /* return hardware/driver id */
694 SES2(sop)->crid = crypto_ses2hid(cse->cses);
699 free(mackey, M_XDATA);
701 #ifdef COMPAT_FREEBSD32
703 if (cmd == CIOCGSESSION32)
704 session_op_to_32(sop, data);
705 else if (cmd == CIOCGSESSION232)
706 session2_op_to_32((struct session2_op *)sop,
712 ses = *(u_int32_t *)data;
713 if (!csedelete(fcr, ses)) {
714 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
719 #ifdef COMPAT_FREEBSD32
721 if (cmd == CIOCCRYPT32) {
723 crypt_op_from_32(data, cop);
726 cop = (struct crypt_op *)data;
727 cse = csefind(fcr, cop->ses);
729 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
732 error = cryptodev_op(cse, cop, active_cred, td);
734 #ifdef COMPAT_FREEBSD32
735 if (error == 0 && cmd == CIOCCRYPT32)
736 crypt_op_to_32(cop, data);
741 #ifdef COMPAT_FREEBSD32
745 if (!crypto_userasymcrypto) {
746 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
747 return (EPERM); /* XXX compat? */
749 #ifdef COMPAT_FREEBSD32
750 if (cmd == CIOCKEY32 || cmd == CIOCKEY232) {
752 crypt_kop_from_32(data, kop);
755 kop = (struct crypt_kop *)data;
757 #ifdef COMPAT_FREEBSD32
761 /* NB: crypto core enforces s/w driver use */
763 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
766 error = cryptodev_key(kop);
768 #ifdef COMPAT_FREEBSD32
769 if (cmd == CIOCKEY32 || cmd == CIOCKEY232)
770 crypt_kop_to_32(kop, data);
774 if (!crypto_userasymcrypto) {
776 * NB: if user asym crypto operations are
777 * not permitted return "no algorithms"
778 * so well-behaved applications will just
779 * fallback to doing them in software.
783 error = crypto_getfeat((int *)data);
785 SDT_PROBE1(opencrypto, dev, ioctl, error,
790 error = cryptodev_find((struct crypt_find_op *)data);
793 caead = (struct crypt_aead *)data;
794 cse = csefind(fcr, caead->ses);
796 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
799 error = cryptodev_aead(cse, caead, active_cred, td);
804 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
811 static int cryptodev_cb(struct cryptop *);
813 static struct cryptop_data *
814 cod_alloc(struct csession *cse, size_t aad_len, size_t len, struct thread *td)
816 struct cryptop_data *cod;
818 cod = malloc(sizeof(struct cryptop_data), M_XDATA, M_WAITOK | M_ZERO);
821 if (crypto_get_params(cse->cses)->csp_flags & CSP_F_SEPARATE_AAD) {
823 cod->aad = malloc(aad_len, M_XDATA, M_WAITOK);
824 cod->buf = malloc(len, M_XDATA, M_WAITOK);
826 cod->buf = malloc(aad_len + len, M_XDATA, M_WAITOK);
827 if (crypto_get_params(cse->cses)->csp_flags & CSP_F_SEPARATE_OUTPUT)
828 cod->obuf = malloc(len, M_XDATA, M_WAITOK);
833 cod_free(struct cryptop_data *cod)
836 free(cod->aad, M_XDATA);
837 free(cod->obuf, M_XDATA);
838 free(cod->buf, M_XDATA);
844 struct csession *cse,
845 struct crypt_op *cop,
846 struct ucred *active_cred,
849 struct cryptop_data *cod = NULL;
850 struct cryptop *crp = NULL;
853 if (cop->len > 256*1024-4) {
854 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
859 if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0) {
860 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
865 if (cop->mac && cse->hashsize == 0) {
866 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
871 * The COP_F_CIPHER_FIRST flag predates explicit session
872 * modes, but the only way it was used was for EtA so allow it
873 * as long as it is consistent with EtA.
875 if (cop->flags & COP_F_CIPHER_FIRST) {
876 if (cop->op != COP_ENCRYPT) {
877 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
882 cod = cod_alloc(cse, 0, cop->len + cse->hashsize, td);
884 crp = crypto_getreq(cse->cses, M_WAITOK);
886 error = copyin(cop->src, cod->buf, cop->len);
888 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
891 crp->crp_payload_start = 0;
892 crp->crp_payload_length = cop->len;
894 crp->crp_digest_start = cop->len;
897 case CSP_MODE_COMPRESS:
900 crp->crp_op = CRYPTO_OP_COMPRESS;
903 crp->crp_op = CRYPTO_OP_DECOMPRESS;
906 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
911 case CSP_MODE_CIPHER:
914 crp->crp_op = CRYPTO_OP_ENCRYPT;
917 crp->crp_op = CRYPTO_OP_DECRYPT;
920 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
925 case CSP_MODE_DIGEST:
930 crp->crp_op = CRYPTO_OP_COMPUTE_DIGEST;
931 if (cod->obuf != NULL)
932 crp->crp_digest_start = 0;
935 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
943 crp->crp_op = CRYPTO_OP_ENCRYPT |
944 CRYPTO_OP_COMPUTE_DIGEST;
947 crp->crp_op = CRYPTO_OP_DECRYPT |
948 CRYPTO_OP_VERIFY_DIGEST;
951 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
957 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
962 crp->crp_flags = CRYPTO_F_CBIMM | (cop->flags & COP_F_BATCH);
963 crypto_use_buf(crp, cod->buf, cop->len + cse->hashsize);
965 crypto_use_output_buf(crp, cod->obuf, cop->len + cse->hashsize);
966 crp->crp_callback = cryptodev_cb;
967 crp->crp_opaque = cod;
970 if (cse->ivsize == 0) {
971 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
975 error = copyin(cop->iv, crp->crp_iv, cse->ivsize);
977 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
980 crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
981 } else if (cse->ivsize != 0) {
982 crp->crp_iv_start = 0;
983 crp->crp_payload_start += cse->ivsize;
984 crp->crp_payload_length -= cse->ivsize;
985 cop->dst += cse->ivsize;
988 if (cop->mac != NULL && crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
989 error = copyin(cop->mac, cod->buf + crp->crp_digest_start,
992 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
998 * Let the dispatch run unlocked, then, interlock against the
999 * callback before checking if the operation completed and going
1000 * to sleep. This insures drivers don't inherit our lock which
1001 * results in a lock order reversal between crypto_dispatch forced
1002 * entry and the crypto_done callback into us.
1004 error = crypto_dispatch(crp);
1006 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1010 mtx_lock(&cse->lock);
1012 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0);
1013 mtx_unlock(&cse->lock);
1015 if (crp->crp_etype == EAGAIN) {
1017 crp->crp_flags &= ~CRYPTO_F_DONE;
1022 if (crp->crp_etype != 0) {
1023 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1024 error = crp->crp_etype;
1028 if (cop->dst != NULL) {
1029 error = copyout(cod->obuf != NULL ? cod->obuf :
1030 cod->buf + crp->crp_payload_start, cop->dst,
1031 crp->crp_payload_length);
1033 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1038 if (cop->mac != NULL && (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) == 0) {
1039 error = copyout((cod->obuf != NULL ? cod->obuf : cod->buf) +
1040 crp->crp_digest_start, cop->mac, cse->hashsize);
1042 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1048 crypto_freereq(crp);
1056 struct csession *cse,
1057 struct crypt_aead *caead,
1058 struct ucred *active_cred,
1061 struct cryptop_data *cod = NULL;
1062 struct cryptop *crp = NULL;
1065 if (caead->len > 256*1024-4 || caead->aadlen > 256*1024-4) {
1066 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1070 if (cse->txform == NULL || cse->hashsize == 0 || caead->tag == NULL ||
1071 (caead->len % cse->txform->blocksize) != 0) {
1072 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1077 * The COP_F_CIPHER_FIRST flag predates explicit session
1078 * modes, but the only way it was used was for EtA so allow it
1079 * as long as it is consistent with EtA.
1081 if (caead->flags & COP_F_CIPHER_FIRST) {
1082 if (caead->op != COP_ENCRYPT) {
1083 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1088 cod = cod_alloc(cse, caead->aadlen, caead->len + cse->hashsize, td);
1090 crp = crypto_getreq(cse->cses, M_WAITOK);
1092 if (cod->aad != NULL)
1093 error = copyin(caead->aad, cod->aad, caead->aadlen);
1095 error = copyin(caead->aad, cod->buf, caead->aadlen);
1097 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1100 crp->crp_aad = cod->aad;
1101 crp->crp_aad_start = 0;
1102 crp->crp_aad_length = caead->aadlen;
1104 if (cod->aad != NULL)
1105 crp->crp_payload_start = 0;
1107 crp->crp_payload_start = caead->aadlen;
1108 error = copyin(caead->src, cod->buf + crp->crp_payload_start,
1111 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1114 crp->crp_payload_length = caead->len;
1115 if (caead->op == COP_ENCRYPT && cod->obuf != NULL)
1116 crp->crp_digest_start = crp->crp_payload_output_start +
1119 crp->crp_digest_start = crp->crp_payload_start + caead->len;
1121 switch (cse->mode) {
1124 switch (caead->op) {
1126 crp->crp_op = CRYPTO_OP_ENCRYPT |
1127 CRYPTO_OP_COMPUTE_DIGEST;
1130 crp->crp_op = CRYPTO_OP_DECRYPT |
1131 CRYPTO_OP_VERIFY_DIGEST;
1134 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1140 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1145 crp->crp_flags = CRYPTO_F_CBIMM | (caead->flags & COP_F_BATCH);
1146 crypto_use_buf(crp, cod->buf, crp->crp_payload_start + caead->len +
1148 if (cod->obuf != NULL)
1149 crypto_use_output_buf(crp, cod->obuf, caead->len +
1151 crp->crp_callback = cryptodev_cb;
1152 crp->crp_opaque = cod;
1156 * Permit a 16-byte IV for AES-XTS, but only use the
1157 * first 8 bytes as a block number.
1159 if (cse->mode == CSP_MODE_ETA &&
1160 caead->ivlen == AES_BLOCK_LEN &&
1161 cse->ivsize == AES_XTS_IV_LEN)
1162 caead->ivlen = AES_XTS_IV_LEN;
1164 if (caead->ivlen != cse->ivsize) {
1166 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1170 error = copyin(caead->iv, crp->crp_iv, cse->ivsize);
1172 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1175 crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
1177 crp->crp_iv_start = crp->crp_payload_start;
1178 crp->crp_payload_start += cse->ivsize;
1179 crp->crp_payload_length -= cse->ivsize;
1180 caead->dst += cse->ivsize;
1183 if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
1184 error = copyin(caead->tag, cod->buf + crp->crp_digest_start,
1187 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1193 * Let the dispatch run unlocked, then, interlock against the
1194 * callback before checking if the operation completed and going
1195 * to sleep. This insures drivers don't inherit our lock which
1196 * results in a lock order reversal between crypto_dispatch forced
1197 * entry and the crypto_done callback into us.
1199 error = crypto_dispatch(crp);
1201 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1205 mtx_lock(&cse->lock);
1207 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0);
1208 mtx_unlock(&cse->lock);
1210 if (crp->crp_etype == EAGAIN) {
1212 crp->crp_flags &= ~CRYPTO_F_DONE;
1217 if (crp->crp_etype != 0) {
1218 error = crp->crp_etype;
1219 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1223 if (caead->dst != NULL) {
1224 error = copyout(cod->obuf != NULL ? cod->obuf :
1225 cod->buf + crp->crp_payload_start, caead->dst,
1226 crp->crp_payload_length);
1228 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1233 if ((crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) == 0) {
1234 error = copyout((cod->obuf != NULL ? cod->obuf : cod->buf) +
1235 crp->crp_digest_start, caead->tag, cse->hashsize);
1237 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1243 crypto_freereq(crp);
1250 cryptodev_cb(struct cryptop *crp)
1252 struct cryptop_data *cod = crp->crp_opaque;
1255 * Lock to ensure the wakeup() is not missed by the loops
1256 * waiting on cod->done in cryptodev_op() and
1259 mtx_lock(&cod->cse->lock);
1261 mtx_unlock(&cod->cse->lock);
1267 cryptodevkey_cb(struct cryptkop *krp)
1274 cryptodev_key(struct crypt_kop *kop)
1276 struct cryptkop *krp = NULL;
1278 int in, out, size, i;
1280 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
1281 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1285 in = kop->crk_iparams;
1286 out = kop->crk_oparams;
1287 switch (kop->crk_op) {
1289 if (in == 3 && out == 1)
1291 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1293 case CRK_MOD_EXP_CRT:
1294 if (in == 6 && out == 1)
1296 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1299 if (in == 5 && out == 2)
1301 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1303 case CRK_DSA_VERIFY:
1304 if (in == 7 && out == 0)
1306 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1308 case CRK_DH_COMPUTE_KEY:
1309 if (in == 3 && out == 1)
1311 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1314 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1318 krp = (struct cryptkop *)malloc(sizeof *krp, M_XDATA, M_WAITOK|M_ZERO);
1320 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1323 krp->krp_op = kop->crk_op;
1324 krp->krp_status = kop->crk_status;
1325 krp->krp_iparams = kop->crk_iparams;
1326 krp->krp_oparams = kop->crk_oparams;
1327 krp->krp_crid = kop->crk_crid;
1328 krp->krp_status = 0;
1329 krp->krp_callback = cryptodevkey_cb;
1331 for (i = 0; i < CRK_MAXPARAM; i++) {
1332 if (kop->crk_param[i].crp_nbits > 65536) {
1333 /* Limit is the same as in OpenBSD */
1334 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1337 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
1339 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
1340 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1343 krp->krp_param[i].crp_p = malloc(size, M_XDATA, M_WAITOK);
1344 if (i >= krp->krp_iparams)
1346 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
1348 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1353 error = crypto_kdispatch(krp);
1355 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1358 error = tsleep(krp, PSOCK, "crydev", 0);
1360 /* XXX can this happen? if so, how do we recover? */
1361 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1365 kop->crk_crid = krp->krp_hid; /* device that did the work */
1366 if (krp->krp_status != 0) {
1367 error = krp->krp_status;
1368 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1372 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
1373 size = (krp->krp_param[i].crp_nbits + 7) / 8;
1376 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
1378 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__);
1385 kop->crk_status = krp->krp_status;
1386 for (i = 0; i < CRK_MAXPARAM; i++) {
1387 if (krp->krp_param[i].crp_p)
1388 free(krp->krp_param[i].crp_p, M_XDATA);
1396 cryptodev_find(struct crypt_find_op *find)
1399 size_t fnlen = sizeof find->name;
1401 if (find->crid != -1) {
1402 dev = crypto_find_device_byhid(find->crid);
1405 strncpy(find->name, device_get_nameunit(dev), fnlen);
1406 find->name[fnlen - 1] = '\x0';
1408 find->name[fnlen - 1] = '\x0';
1409 find->crid = crypto_find_driver(find->name);
1410 if (find->crid == -1)
1421 struct ucred *active_cred,
1425 return (EOPNOTSUPP);
1430 cryptof_close(struct file *fp, struct thread *td)
1432 struct fcrypt *fcr = fp->f_data;
1433 struct csession *cse;
1435 while ((cse = TAILQ_FIRST(&fcr->csessions))) {
1436 TAILQ_REMOVE(&fcr->csessions, cse, next);
1437 KASSERT(cse->refs == 1,
1438 ("%s: crypto session %p with %d refs", __func__, cse,
1448 cryptof_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
1451 kif->kf_type = KF_TYPE_CRYPTO;
1455 static struct csession *
1456 csefind(struct fcrypt *fcr, u_int ses)
1458 struct csession *cse;
1460 mtx_lock(&fcr->lock);
1461 TAILQ_FOREACH(cse, &fcr->csessions, next) {
1462 if (cse->ses == ses) {
1463 refcount_acquire(&cse->refs);
1464 mtx_unlock(&fcr->lock);
1468 mtx_unlock(&fcr->lock);
1473 csedelete(struct fcrypt *fcr, u_int ses)
1475 struct csession *cse;
1477 mtx_lock(&fcr->lock);
1478 TAILQ_FOREACH(cse, &fcr->csessions, next) {
1479 if (cse->ses == ses) {
1480 TAILQ_REMOVE(&fcr->csessions, cse, next);
1481 mtx_unlock(&fcr->lock);
1486 mtx_unlock(&fcr->lock);
1491 csecreate(struct fcrypt *fcr, crypto_session_t cses,
1492 struct crypto_session_params *csp, struct enc_xform *txform,
1493 void *key, struct auth_hash *thash, void *mackey)
1495 struct csession *cse;
1497 cse = malloc(sizeof(struct csession), M_XDATA, M_NOWAIT | M_ZERO);
1500 mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF);
1501 refcount_init(&cse->refs, 1);
1503 cse->mackey = mackey;
1504 cse->mode = csp->csp_mode;
1506 cse->txform = txform;
1508 cse->hashsize = thash->hashsize;
1509 else if (csp->csp_cipher_alg == CRYPTO_AES_NIST_GCM_16)
1510 cse->hashsize = AES_GMAC_HASH_LEN;
1511 else if (csp->csp_cipher_alg == CRYPTO_AES_CCM_16)
1512 cse->hashsize = AES_CBC_MAC_HASH_LEN;
1513 cse->ivsize = csp->csp_ivlen;
1514 mtx_lock(&fcr->lock);
1515 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
1516 cse->ses = fcr->sesn++;
1517 mtx_unlock(&fcr->lock);
1522 csefree(struct csession *cse)
1525 if (!refcount_release(&cse->refs))
1527 crypto_freesession(cse->cses);
1528 mtx_destroy(&cse->lock);
1530 free(cse->key, M_XDATA);
1532 free(cse->mackey, M_XDATA);
1537 cryptoioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1545 error = falloc_noinstall(td, &f);
1549 fcr = malloc(sizeof(struct fcrypt), M_XDATA, M_WAITOK | M_ZERO);
1550 TAILQ_INIT(&fcr->csessions);
1551 mtx_init(&fcr->lock, "fcrypt", NULL, MTX_DEF);
1553 finit(f, FREAD | FWRITE, DTYPE_CRYPTO, fcr, &cryptofops);
1554 error = finstall(td, f, &fd, 0, NULL);
1556 mtx_destroy(&fcr->lock);
1559 *(uint32_t *)data = fd;
1563 error = cryptodev_find((struct crypt_find_op *)data);
1566 error = crypto_getfeat((int *)data);
1575 static struct cdevsw crypto_cdevsw = {
1576 .d_version = D_VERSION,
1577 .d_ioctl = cryptoioctl,
1580 static struct cdev *crypto_dev;
1583 * Initialization code, both for static and dynamic loading.
1586 cryptodev_modevent(module_t mod, int type, void *unused)
1591 printf("crypto: <crypto device>\n");
1592 crypto_dev = make_dev(&crypto_cdevsw, 0,
1593 UID_ROOT, GID_WHEEL, 0666,
1597 /*XXX disallow if active sessions */
1598 destroy_dev(crypto_dev);
1604 static moduledata_t cryptodev_mod = {
1609 MODULE_VERSION(cryptodev, 1);
1610 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1611 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
1612 MODULE_DEPEND(cryptodev, zlib, 1, 1, 1);