1 /* $OpenBSD: cryptodev.c,v 1.52 2002/06/19 07:22:46 deraadt Exp $ */ 2 3 /*- 4 * Copyright (c) 2001 Theo de Raadt 5 * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting 6 * Copyright (c) 2014 The FreeBSD Foundation 7 * All rights reserved. 8 * 9 * Portions of this software were developed by John-Mark Gurney 10 * under sponsorship of the FreeBSD Foundation and 11 * Rubicon Communications, LLC (Netgate). 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 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. 24 * 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. 35 * 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. 39 */ 40 41 #include <sys/cdefs.h> 42 __FBSDID("$FreeBSD$"); 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/malloc.h> 47 #include <sys/mbuf.h> 48 #include <sys/lock.h> 49 #include <sys/mutex.h> 50 #include <sys/sysctl.h> 51 #include <sys/file.h> 52 #include <sys/filedesc.h> 53 #include <sys/errno.h> 54 #include <sys/uio.h> 55 #include <sys/random.h> 56 #include <sys/conf.h> 57 #include <sys/kernel.h> 58 #include <sys/module.h> 59 #include <sys/fcntl.h> 60 #include <sys/bus.h> 61 #include <sys/user.h> 62 #include <sys/sdt.h> 63 64 #include <opencrypto/cryptodev.h> 65 #include <opencrypto/xform.h> 66 67 SDT_PROVIDER_DECLARE(opencrypto); 68 69 SDT_PROBE_DEFINE1(opencrypto, dev, ioctl, error, "int"/*line number*/); 70 71 #ifdef COMPAT_FREEBSD32 72 #include <sys/mount.h> 73 #include <compat/freebsd32/freebsd32.h> 74 75 struct session_op32 { 76 u_int32_t cipher; 77 u_int32_t mac; 78 u_int32_t keylen; 79 u_int32_t key; 80 int mackeylen; 81 u_int32_t mackey; 82 u_int32_t ses; 83 }; 84 85 struct session2_op32 { 86 u_int32_t cipher; 87 u_int32_t mac; 88 u_int32_t keylen; 89 u_int32_t key; 90 int mackeylen; 91 u_int32_t mackey; 92 u_int32_t ses; 93 int crid; 94 int pad[4]; 95 }; 96 97 struct crypt_op32 { 98 u_int32_t ses; 99 u_int16_t op; 100 u_int16_t flags; 101 u_int len; 102 u_int32_t src, dst; 103 u_int32_t mac; 104 u_int32_t iv; 105 }; 106 107 struct crparam32 { 108 u_int32_t crp_p; 109 u_int crp_nbits; 110 }; 111 112 struct crypt_kop32 { 113 u_int crk_op; 114 u_int crk_status; 115 u_short crk_iparams; 116 u_short crk_oparams; 117 u_int crk_crid; 118 struct crparam32 crk_param[CRK_MAXPARAM]; 119 }; 120 121 struct cryptotstat32 { 122 struct timespec32 acc; 123 struct timespec32 min; 124 struct timespec32 max; 125 u_int32_t count; 126 }; 127 128 struct cryptostats32 { 129 u_int32_t cs_ops; 130 u_int32_t cs_errs; 131 u_int32_t cs_kops; 132 u_int32_t cs_kerrs; 133 u_int32_t cs_intrs; 134 u_int32_t cs_rets; 135 u_int32_t cs_blocks; 136 u_int32_t cs_kblocks; 137 struct cryptotstat32 cs_invoke; 138 struct cryptotstat32 cs_done; 139 struct cryptotstat32 cs_cb; 140 struct cryptotstat32 cs_finis; 141 }; 142 143 #define CIOCGSESSION32 _IOWR('c', 101, struct session_op32) 144 #define CIOCCRYPT32 _IOWR('c', 103, struct crypt_op32) 145 #define CIOCKEY32 _IOWR('c', 104, struct crypt_kop32) 146 #define CIOCGSESSION232 _IOWR('c', 106, struct session2_op32) 147 #define CIOCKEY232 _IOWR('c', 107, struct crypt_kop32) 148 149 static void 150 session_op_from_32(const struct session_op32 *from, struct session_op *to) 151 { 152 153 CP(*from, *to, cipher); 154 CP(*from, *to, mac); 155 CP(*from, *to, keylen); 156 PTRIN_CP(*from, *to, key); 157 CP(*from, *to, mackeylen); 158 PTRIN_CP(*from, *to, mackey); 159 CP(*from, *to, ses); 160 } 161 162 static void 163 session2_op_from_32(const struct session2_op32 *from, struct session2_op *to) 164 { 165 166 session_op_from_32((const struct session_op32 *)from, 167 (struct session_op *)to); 168 CP(*from, *to, crid); 169 } 170 171 static void 172 session_op_to_32(const struct session_op *from, struct session_op32 *to) 173 { 174 175 CP(*from, *to, cipher); 176 CP(*from, *to, mac); 177 CP(*from, *to, keylen); 178 PTROUT_CP(*from, *to, key); 179 CP(*from, *to, mackeylen); 180 PTROUT_CP(*from, *to, mackey); 181 CP(*from, *to, ses); 182 } 183 184 static void 185 session2_op_to_32(const struct session2_op *from, struct session2_op32 *to) 186 { 187 188 session_op_to_32((const struct session_op *)from, 189 (struct session_op32 *)to); 190 CP(*from, *to, crid); 191 } 192 193 static void 194 crypt_op_from_32(const struct crypt_op32 *from, struct crypt_op *to) 195 { 196 197 CP(*from, *to, ses); 198 CP(*from, *to, op); 199 CP(*from, *to, flags); 200 CP(*from, *to, len); 201 PTRIN_CP(*from, *to, src); 202 PTRIN_CP(*from, *to, dst); 203 PTRIN_CP(*from, *to, mac); 204 PTRIN_CP(*from, *to, iv); 205 } 206 207 static void 208 crypt_op_to_32(const struct crypt_op *from, struct crypt_op32 *to) 209 { 210 211 CP(*from, *to, ses); 212 CP(*from, *to, op); 213 CP(*from, *to, flags); 214 CP(*from, *to, len); 215 PTROUT_CP(*from, *to, src); 216 PTROUT_CP(*from, *to, dst); 217 PTROUT_CP(*from, *to, mac); 218 PTROUT_CP(*from, *to, iv); 219 } 220 221 static void 222 crparam_from_32(const struct crparam32 *from, struct crparam *to) 223 { 224 225 PTRIN_CP(*from, *to, crp_p); 226 CP(*from, *to, crp_nbits); 227 } 228 229 static void 230 crparam_to_32(const struct crparam *from, struct crparam32 *to) 231 { 232 233 PTROUT_CP(*from, *to, crp_p); 234 CP(*from, *to, crp_nbits); 235 } 236 237 static void 238 crypt_kop_from_32(const struct crypt_kop32 *from, struct crypt_kop *to) 239 { 240 int i; 241 242 CP(*from, *to, crk_op); 243 CP(*from, *to, crk_status); 244 CP(*from, *to, crk_iparams); 245 CP(*from, *to, crk_oparams); 246 CP(*from, *to, crk_crid); 247 for (i = 0; i < CRK_MAXPARAM; i++) 248 crparam_from_32(&from->crk_param[i], &to->crk_param[i]); 249 } 250 251 static void 252 crypt_kop_to_32(const struct crypt_kop *from, struct crypt_kop32 *to) 253 { 254 int i; 255 256 CP(*from, *to, crk_op); 257 CP(*from, *to, crk_status); 258 CP(*from, *to, crk_iparams); 259 CP(*from, *to, crk_oparams); 260 CP(*from, *to, crk_crid); 261 for (i = 0; i < CRK_MAXPARAM; i++) 262 crparam_to_32(&from->crk_param[i], &to->crk_param[i]); 263 } 264 #endif 265 266 struct csession { 267 TAILQ_ENTRY(csession) next; 268 crypto_session_t cses; 269 u_int32_t ses; 270 struct mtx lock; /* for op submission */ 271 272 u_int32_t cipher; 273 struct enc_xform *txform; 274 u_int32_t mac; 275 struct auth_hash *thash; 276 277 caddr_t key; 278 int keylen; 279 280 caddr_t mackey; 281 int mackeylen; 282 }; 283 284 struct cryptop_data { 285 struct csession *cse; 286 287 struct iovec iovec[1]; 288 struct uio uio; 289 bool done; 290 }; 291 292 struct fcrypt { 293 TAILQ_HEAD(csessionlist, csession) csessions; 294 int sesn; 295 }; 296 297 static int cryptof_ioctl(struct file *, u_long, void *, 298 struct ucred *, struct thread *); 299 static int cryptof_stat(struct file *, struct stat *, 300 struct ucred *, struct thread *); 301 static int cryptof_close(struct file *, struct thread *); 302 static int cryptof_fill_kinfo(struct file *, struct kinfo_file *, 303 struct filedesc *); 304 305 static struct fileops cryptofops = { 306 .fo_read = invfo_rdwr, 307 .fo_write = invfo_rdwr, 308 .fo_truncate = invfo_truncate, 309 .fo_ioctl = cryptof_ioctl, 310 .fo_poll = invfo_poll, 311 .fo_kqfilter = invfo_kqfilter, 312 .fo_stat = cryptof_stat, 313 .fo_close = cryptof_close, 314 .fo_chmod = invfo_chmod, 315 .fo_chown = invfo_chown, 316 .fo_sendfile = invfo_sendfile, 317 .fo_fill_kinfo = cryptof_fill_kinfo, 318 }; 319 320 static struct csession *csefind(struct fcrypt *, u_int); 321 static int csedelete(struct fcrypt *, struct csession *); 322 static struct csession *cseadd(struct fcrypt *, struct csession *); 323 static struct csession *csecreate(struct fcrypt *, crypto_session_t, caddr_t, 324 u_int64_t, caddr_t, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *, 325 struct auth_hash *); 326 static void csefree(struct csession *); 327 328 static int cryptodev_op(struct csession *, struct crypt_op *, 329 struct ucred *, struct thread *td); 330 static int cryptodev_aead(struct csession *, struct crypt_aead *, 331 struct ucred *, struct thread *); 332 static int cryptodev_key(struct crypt_kop *); 333 static int cryptodev_find(struct crypt_find_op *); 334 335 /* 336 * Check a crypto identifier to see if it requested 337 * a software device/driver. This can be done either 338 * by device name/class or through search constraints. 339 */ 340 static int 341 checkforsoftware(int *cridp) 342 { 343 int crid; 344 345 crid = *cridp; 346 347 if (!crypto_devallowsoft) { 348 if (crid & CRYPTOCAP_F_SOFTWARE) { 349 if (crid & CRYPTOCAP_F_HARDWARE) { 350 *cridp = CRYPTOCAP_F_HARDWARE; 351 return 0; 352 } 353 return EINVAL; 354 } 355 if ((crid & CRYPTOCAP_F_HARDWARE) == 0 && 356 (crypto_getcaps(crid) & CRYPTOCAP_F_HARDWARE) == 0) 357 return EINVAL; 358 } 359 return 0; 360 } 361 362 /* ARGSUSED */ 363 static int 364 cryptof_ioctl( 365 struct file *fp, 366 u_long cmd, 367 void *data, 368 struct ucred *active_cred, 369 struct thread *td) 370 { 371 #define SES2(p) ((struct session2_op *)p) 372 struct cryptoini cria, crie; 373 struct fcrypt *fcr = fp->f_data; 374 struct csession *cse; 375 struct session_op *sop; 376 struct crypt_op *cop; 377 struct crypt_aead *caead; 378 struct enc_xform *txform = NULL; 379 struct auth_hash *thash = NULL; 380 struct crypt_kop *kop; 381 crypto_session_t cses; 382 u_int32_t ses; 383 int error = 0, crid; 384 #ifdef COMPAT_FREEBSD32 385 struct session2_op sopc; 386 struct crypt_op copc; 387 struct crypt_kop kopc; 388 #endif 389 390 switch (cmd) { 391 case CIOCGSESSION: 392 case CIOCGSESSION2: 393 #ifdef COMPAT_FREEBSD32 394 case CIOCGSESSION32: 395 case CIOCGSESSION232: 396 if (cmd == CIOCGSESSION32) { 397 session_op_from_32(data, (struct session_op *)&sopc); 398 sop = (struct session_op *)&sopc; 399 } else if (cmd == CIOCGSESSION232) { 400 session2_op_from_32(data, &sopc); 401 sop = (struct session_op *)&sopc; 402 } else 403 #endif 404 sop = (struct session_op *)data; 405 switch (sop->cipher) { 406 case 0: 407 break; 408 case CRYPTO_DES_CBC: 409 txform = &enc_xform_des; 410 break; 411 case CRYPTO_3DES_CBC: 412 txform = &enc_xform_3des; 413 break; 414 case CRYPTO_BLF_CBC: 415 txform = &enc_xform_blf; 416 break; 417 case CRYPTO_CAST_CBC: 418 txform = &enc_xform_cast5; 419 break; 420 case CRYPTO_SKIPJACK_CBC: 421 txform = &enc_xform_skipjack; 422 break; 423 case CRYPTO_AES_CBC: 424 txform = &enc_xform_rijndael128; 425 break; 426 case CRYPTO_AES_XTS: 427 txform = &enc_xform_aes_xts; 428 break; 429 case CRYPTO_NULL_CBC: 430 txform = &enc_xform_null; 431 break; 432 case CRYPTO_ARC4: 433 txform = &enc_xform_arc4; 434 break; 435 case CRYPTO_CAMELLIA_CBC: 436 txform = &enc_xform_camellia; 437 break; 438 case CRYPTO_AES_ICM: 439 txform = &enc_xform_aes_icm; 440 break; 441 case CRYPTO_AES_NIST_GCM_16: 442 txform = &enc_xform_aes_nist_gcm; 443 break; 444 case CRYPTO_CHACHA20: 445 txform = &enc_xform_chacha20; 446 break; 447 448 default: 449 CRYPTDEB("invalid cipher"); 450 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 451 return (EINVAL); 452 } 453 454 switch (sop->mac) { 455 case 0: 456 break; 457 case CRYPTO_MD5_HMAC: 458 thash = &auth_hash_hmac_md5; 459 break; 460 case CRYPTO_SHA1_HMAC: 461 thash = &auth_hash_hmac_sha1; 462 break; 463 case CRYPTO_SHA2_224_HMAC: 464 thash = &auth_hash_hmac_sha2_224; 465 break; 466 case CRYPTO_SHA2_256_HMAC: 467 thash = &auth_hash_hmac_sha2_256; 468 break; 469 case CRYPTO_SHA2_384_HMAC: 470 thash = &auth_hash_hmac_sha2_384; 471 break; 472 case CRYPTO_SHA2_512_HMAC: 473 thash = &auth_hash_hmac_sha2_512; 474 break; 475 case CRYPTO_RIPEMD160_HMAC: 476 thash = &auth_hash_hmac_ripemd_160; 477 break; 478 case CRYPTO_AES_128_NIST_GMAC: 479 thash = &auth_hash_nist_gmac_aes_128; 480 break; 481 case CRYPTO_AES_192_NIST_GMAC: 482 thash = &auth_hash_nist_gmac_aes_192; 483 break; 484 case CRYPTO_AES_256_NIST_GMAC: 485 thash = &auth_hash_nist_gmac_aes_256; 486 break; 487 488 #ifdef notdef 489 case CRYPTO_MD5: 490 thash = &auth_hash_md5; 491 break; 492 #endif 493 case CRYPTO_SHA1: 494 thash = &auth_hash_sha1; 495 break; 496 case CRYPTO_SHA2_224: 497 thash = &auth_hash_sha2_224; 498 break; 499 case CRYPTO_SHA2_256: 500 thash = &auth_hash_sha2_256; 501 break; 502 case CRYPTO_SHA2_384: 503 thash = &auth_hash_sha2_384; 504 break; 505 case CRYPTO_SHA2_512: 506 thash = &auth_hash_sha2_512; 507 break; 508 509 case CRYPTO_NULL_HMAC: 510 thash = &auth_hash_null; 511 break; 512 513 case CRYPTO_BLAKE2B: 514 thash = &auth_hash_blake2b; 515 break; 516 case CRYPTO_BLAKE2S: 517 thash = &auth_hash_blake2s; 518 break; 519 520 default: 521 CRYPTDEB("invalid mac"); 522 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 523 return (EINVAL); 524 } 525 526 bzero(&crie, sizeof(crie)); 527 bzero(&cria, sizeof(cria)); 528 529 if (txform) { 530 crie.cri_alg = txform->type; 531 crie.cri_klen = sop->keylen * 8; 532 if (sop->keylen > txform->maxkey || 533 sop->keylen < txform->minkey) { 534 CRYPTDEB("invalid cipher parameters"); 535 error = EINVAL; 536 SDT_PROBE1(opencrypto, dev, ioctl, error, 537 __LINE__); 538 goto bail; 539 } 540 541 crie.cri_key = malloc(crie.cri_klen / 8, 542 M_XDATA, M_WAITOK); 543 if ((error = copyin(sop->key, crie.cri_key, 544 crie.cri_klen / 8))) { 545 CRYPTDEB("invalid key"); 546 SDT_PROBE1(opencrypto, dev, ioctl, error, 547 __LINE__); 548 goto bail; 549 } 550 if (thash) 551 crie.cri_next = &cria; 552 } 553 554 if (thash) { 555 cria.cri_alg = thash->type; 556 cria.cri_klen = sop->mackeylen * 8; 557 if (thash->keysize != 0 && 558 sop->mackeylen > thash->keysize) { 559 CRYPTDEB("invalid mac key length"); 560 error = EINVAL; 561 SDT_PROBE1(opencrypto, dev, ioctl, error, 562 __LINE__); 563 goto bail; 564 } 565 566 if (cria.cri_klen) { 567 cria.cri_key = malloc(cria.cri_klen / 8, 568 M_XDATA, M_WAITOK); 569 if ((error = copyin(sop->mackey, cria.cri_key, 570 cria.cri_klen / 8))) { 571 CRYPTDEB("invalid mac key"); 572 SDT_PROBE1(opencrypto, dev, ioctl, 573 error, __LINE__); 574 goto bail; 575 } 576 } 577 } 578 579 /* NB: CIOCGSESSION2 has the crid */ 580 if (cmd == CIOCGSESSION2 581 #ifdef COMPAT_FREEBSD32 582 || cmd == CIOCGSESSION232 583 #endif 584 ) { 585 crid = SES2(sop)->crid; 586 error = checkforsoftware(&crid); 587 if (error) { 588 CRYPTDEB("checkforsoftware"); 589 SDT_PROBE1(opencrypto, dev, ioctl, error, 590 __LINE__); 591 goto bail; 592 } 593 } else 594 crid = CRYPTOCAP_F_HARDWARE; 595 error = crypto_newsession(&cses, (txform ? &crie : &cria), crid); 596 if (error) { 597 CRYPTDEB("crypto_newsession"); 598 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 599 goto bail; 600 } 601 602 cse = csecreate(fcr, cses, crie.cri_key, crie.cri_klen, 603 cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform, 604 thash); 605 606 if (cse == NULL) { 607 crypto_freesession(cses); 608 error = EINVAL; 609 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 610 CRYPTDEB("csecreate"); 611 goto bail; 612 } 613 sop->ses = cse->ses; 614 if (cmd == CIOCGSESSION2 615 #ifdef COMPAT_FREEBSD32 616 || cmd == CIOCGSESSION232 617 #endif 618 ) { 619 /* return hardware/driver id */ 620 SES2(sop)->crid = crypto_ses2hid(cse->cses); 621 } 622 bail: 623 if (error) { 624 if (crie.cri_key) 625 free(crie.cri_key, M_XDATA); 626 if (cria.cri_key) 627 free(cria.cri_key, M_XDATA); 628 } 629 #ifdef COMPAT_FREEBSD32 630 else { 631 if (cmd == CIOCGSESSION32) 632 session_op_to_32(sop, data); 633 else if (cmd == CIOCGSESSION232) 634 session2_op_to_32((struct session2_op *)sop, 635 data); 636 } 637 #endif 638 break; 639 case CIOCFSESSION: 640 ses = *(u_int32_t *)data; 641 cse = csefind(fcr, ses); 642 if (cse == NULL) { 643 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 644 return (EINVAL); 645 } 646 csedelete(fcr, cse); 647 csefree(cse); 648 break; 649 case CIOCCRYPT: 650 #ifdef COMPAT_FREEBSD32 651 case CIOCCRYPT32: 652 if (cmd == CIOCCRYPT32) { 653 cop = &copc; 654 crypt_op_from_32(data, cop); 655 } else 656 #endif 657 cop = (struct crypt_op *)data; 658 cse = csefind(fcr, cop->ses); 659 if (cse == NULL) { 660 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 661 return (EINVAL); 662 } 663 error = cryptodev_op(cse, cop, active_cred, td); 664 #ifdef COMPAT_FREEBSD32 665 if (error == 0 && cmd == CIOCCRYPT32) 666 crypt_op_to_32(cop, data); 667 #endif 668 break; 669 case CIOCKEY: 670 case CIOCKEY2: 671 #ifdef COMPAT_FREEBSD32 672 case CIOCKEY32: 673 case CIOCKEY232: 674 #endif 675 if (!crypto_userasymcrypto) { 676 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 677 return (EPERM); /* XXX compat? */ 678 } 679 #ifdef COMPAT_FREEBSD32 680 if (cmd == CIOCKEY32 || cmd == CIOCKEY232) { 681 kop = &kopc; 682 crypt_kop_from_32(data, kop); 683 } else 684 #endif 685 kop = (struct crypt_kop *)data; 686 if (cmd == CIOCKEY 687 #ifdef COMPAT_FREEBSD32 688 || cmd == CIOCKEY32 689 #endif 690 ) { 691 /* NB: crypto core enforces s/w driver use */ 692 kop->crk_crid = 693 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE; 694 } 695 mtx_lock(&Giant); 696 error = cryptodev_key(kop); 697 mtx_unlock(&Giant); 698 #ifdef COMPAT_FREEBSD32 699 if (cmd == CIOCKEY32 || cmd == CIOCKEY232) 700 crypt_kop_to_32(kop, data); 701 #endif 702 break; 703 case CIOCASYMFEAT: 704 if (!crypto_userasymcrypto) { 705 /* 706 * NB: if user asym crypto operations are 707 * not permitted return "no algorithms" 708 * so well-behaved applications will just 709 * fallback to doing them in software. 710 */ 711 *(int *)data = 0; 712 } else { 713 error = crypto_getfeat((int *)data); 714 if (error) 715 SDT_PROBE1(opencrypto, dev, ioctl, error, 716 __LINE__); 717 } 718 break; 719 case CIOCFINDDEV: 720 error = cryptodev_find((struct crypt_find_op *)data); 721 break; 722 case CIOCCRYPTAEAD: 723 caead = (struct crypt_aead *)data; 724 cse = csefind(fcr, caead->ses); 725 if (cse == NULL) { 726 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 727 return (EINVAL); 728 } 729 error = cryptodev_aead(cse, caead, active_cred, td); 730 break; 731 default: 732 error = EINVAL; 733 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 734 break; 735 } 736 return (error); 737 #undef SES2 738 } 739 740 static int cryptodev_cb(struct cryptop *); 741 742 static struct cryptop_data * 743 cod_alloc(struct csession *cse, size_t len, struct thread *td) 744 { 745 struct cryptop_data *cod; 746 struct uio *uio; 747 748 cod = malloc(sizeof(struct cryptop_data), M_XDATA, M_WAITOK | M_ZERO); 749 750 cod->cse = cse; 751 uio = &cod->uio; 752 uio->uio_iov = cod->iovec; 753 uio->uio_iovcnt = 1; 754 uio->uio_resid = len; 755 uio->uio_segflg = UIO_SYSSPACE; 756 uio->uio_rw = UIO_WRITE; 757 uio->uio_td = td; 758 uio->uio_iov[0].iov_len = len; 759 uio->uio_iov[0].iov_base = malloc(len, M_XDATA, M_WAITOK); 760 return (cod); 761 } 762 763 static void 764 cod_free(struct cryptop_data *cod) 765 { 766 767 free(cod->uio.uio_iov[0].iov_base, M_XDATA); 768 free(cod, M_XDATA); 769 } 770 771 static int 772 cryptodev_op( 773 struct csession *cse, 774 struct crypt_op *cop, 775 struct ucred *active_cred, 776 struct thread *td) 777 { 778 struct cryptop_data *cod = NULL; 779 struct cryptop *crp = NULL; 780 struct cryptodesc *crde = NULL, *crda = NULL; 781 int error; 782 783 if (cop->len > 256*1024-4) { 784 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 785 return (E2BIG); 786 } 787 788 if (cse->txform) { 789 if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0) { 790 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 791 return (EINVAL); 792 } 793 } 794 795 if (cse->thash) 796 cod = cod_alloc(cse, cop->len + cse->thash->hashsize, td); 797 else 798 cod = cod_alloc(cse, cop->len, td); 799 800 crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL)); 801 if (crp == NULL) { 802 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 803 error = ENOMEM; 804 goto bail; 805 } 806 807 if (cse->thash && cse->txform) { 808 if (cop->flags & COP_F_CIPHER_FIRST) { 809 crde = crp->crp_desc; 810 crda = crde->crd_next; 811 } else { 812 crda = crp->crp_desc; 813 crde = crda->crd_next; 814 } 815 } else if (cse->thash) { 816 crda = crp->crp_desc; 817 } else if (cse->txform) { 818 crde = crp->crp_desc; 819 } else { 820 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 821 error = EINVAL; 822 goto bail; 823 } 824 825 if ((error = copyin(cop->src, cod->uio.uio_iov[0].iov_base, 826 cop->len))) { 827 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 828 goto bail; 829 } 830 831 if (crda) { 832 crda->crd_skip = 0; 833 crda->crd_len = cop->len; 834 crda->crd_inject = cop->len; 835 836 crda->crd_alg = cse->mac; 837 crda->crd_key = cse->mackey; 838 crda->crd_klen = cse->mackeylen * 8; 839 } 840 841 if (crde) { 842 if (cop->op == COP_ENCRYPT) 843 crde->crd_flags |= CRD_F_ENCRYPT; 844 else 845 crde->crd_flags &= ~CRD_F_ENCRYPT; 846 crde->crd_len = cop->len; 847 crde->crd_inject = 0; 848 849 crde->crd_alg = cse->cipher; 850 crde->crd_key = cse->key; 851 crde->crd_klen = cse->keylen * 8; 852 } 853 854 crp->crp_ilen = cop->len; 855 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM 856 | (cop->flags & COP_F_BATCH); 857 crp->crp_uio = &cod->uio; 858 crp->crp_callback = cryptodev_cb; 859 crp->crp_session = cse->cses; 860 crp->crp_opaque = cod; 861 862 if (cop->iv) { 863 if (crde == NULL) { 864 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 865 error = EINVAL; 866 goto bail; 867 } 868 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */ 869 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 870 error = EINVAL; 871 goto bail; 872 } 873 if ((error = copyin(cop->iv, crde->crd_iv, 874 cse->txform->ivsize))) { 875 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 876 goto bail; 877 } 878 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT; 879 crde->crd_skip = 0; 880 } else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */ 881 crde->crd_skip = 0; 882 } else if (crde) { 883 crde->crd_flags |= CRD_F_IV_PRESENT; 884 crde->crd_skip = cse->txform->ivsize; 885 crde->crd_len -= cse->txform->ivsize; 886 } 887 888 if (cop->mac && crda == NULL) { 889 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 890 error = EINVAL; 891 goto bail; 892 } 893 894 again: 895 /* 896 * Let the dispatch run unlocked, then, interlock against the 897 * callback before checking if the operation completed and going 898 * to sleep. This insures drivers don't inherit our lock which 899 * results in a lock order reversal between crypto_dispatch forced 900 * entry and the crypto_done callback into us. 901 */ 902 error = crypto_dispatch(crp); 903 if (error != 0) { 904 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 905 goto bail; 906 } 907 908 mtx_lock(&cse->lock); 909 while (!cod->done) 910 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0); 911 mtx_unlock(&cse->lock); 912 913 if (crp->crp_etype == EAGAIN) { 914 crp->crp_etype = 0; 915 crp->crp_flags &= ~CRYPTO_F_DONE; 916 cod->done = false; 917 goto again; 918 } 919 920 if (crp->crp_etype != 0) { 921 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 922 error = crp->crp_etype; 923 goto bail; 924 } 925 926 if (cop->dst && 927 (error = copyout(cod->uio.uio_iov[0].iov_base, cop->dst, 928 cop->len))) { 929 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 930 goto bail; 931 } 932 933 if (cop->mac && 934 (error = copyout((caddr_t)cod->uio.uio_iov[0].iov_base + cop->len, 935 cop->mac, cse->thash->hashsize))) { 936 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 937 goto bail; 938 } 939 940 bail: 941 if (crp) 942 crypto_freereq(crp); 943 if (cod) 944 cod_free(cod); 945 946 return (error); 947 } 948 949 static int 950 cryptodev_aead( 951 struct csession *cse, 952 struct crypt_aead *caead, 953 struct ucred *active_cred, 954 struct thread *td) 955 { 956 struct cryptop_data *cod = NULL; 957 struct cryptop *crp = NULL; 958 struct cryptodesc *crde = NULL, *crda = NULL; 959 int error; 960 961 if (caead->len > 256*1024-4 || caead->aadlen > 256*1024-4) { 962 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 963 return (E2BIG); 964 } 965 966 if (cse->txform == NULL || cse->thash == NULL || caead->tag == NULL || 967 (caead->len % cse->txform->blocksize) != 0) { 968 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 969 return (EINVAL); 970 } 971 972 cod = cod_alloc(cse, caead->aadlen + caead->len + cse->thash->hashsize, 973 td); 974 975 crp = crypto_getreq(2); 976 if (crp == NULL) { 977 error = ENOMEM; 978 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 979 goto bail; 980 } 981 982 if (caead->flags & COP_F_CIPHER_FIRST) { 983 crde = crp->crp_desc; 984 crda = crde->crd_next; 985 } else { 986 crda = crp->crp_desc; 987 crde = crda->crd_next; 988 } 989 990 if ((error = copyin(caead->aad, cod->uio.uio_iov[0].iov_base, 991 caead->aadlen))) { 992 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 993 goto bail; 994 } 995 996 if ((error = copyin(caead->src, (char *)cod->uio.uio_iov[0].iov_base + 997 caead->aadlen, caead->len))) { 998 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 999 goto bail; 1000 } 1001 1002 /* 1003 * For GCM, crd_len covers only the AAD. For other ciphers 1004 * chained with an HMAC, crd_len covers both the AAD and the 1005 * cipher text. 1006 */ 1007 crda->crd_skip = 0; 1008 if (cse->cipher == CRYPTO_AES_NIST_GCM_16) 1009 crda->crd_len = caead->aadlen; 1010 else 1011 crda->crd_len = caead->aadlen + caead->len; 1012 crda->crd_inject = caead->aadlen + caead->len; 1013 1014 crda->crd_alg = cse->mac; 1015 crda->crd_key = cse->mackey; 1016 crda->crd_klen = cse->mackeylen * 8; 1017 1018 if (caead->op == COP_ENCRYPT) 1019 crde->crd_flags |= CRD_F_ENCRYPT; 1020 else 1021 crde->crd_flags &= ~CRD_F_ENCRYPT; 1022 crde->crd_skip = caead->aadlen; 1023 crde->crd_len = caead->len; 1024 crde->crd_inject = caead->aadlen; 1025 1026 crde->crd_alg = cse->cipher; 1027 crde->crd_key = cse->key; 1028 crde->crd_klen = cse->keylen * 8; 1029 1030 crp->crp_ilen = caead->aadlen + caead->len; 1031 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM 1032 | (caead->flags & COP_F_BATCH); 1033 crp->crp_uio = &cod->uio; 1034 crp->crp_callback = cryptodev_cb; 1035 crp->crp_session = cse->cses; 1036 crp->crp_opaque = cod; 1037 1038 if (caead->iv) { 1039 if (caead->ivlen > sizeof(crde->crd_iv)) { 1040 error = EINVAL; 1041 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1042 goto bail; 1043 } 1044 1045 if ((error = copyin(caead->iv, crde->crd_iv, caead->ivlen))) { 1046 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1047 goto bail; 1048 } 1049 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT; 1050 } else { 1051 crde->crd_flags |= CRD_F_IV_PRESENT; 1052 crde->crd_skip += cse->txform->ivsize; 1053 crde->crd_len -= cse->txform->ivsize; 1054 } 1055 1056 if ((error = copyin(caead->tag, (caddr_t)cod->uio.uio_iov[0].iov_base + 1057 caead->len + caead->aadlen, cse->thash->hashsize))) { 1058 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1059 goto bail; 1060 } 1061 again: 1062 /* 1063 * Let the dispatch run unlocked, then, interlock against the 1064 * callback before checking if the operation completed and going 1065 * to sleep. This insures drivers don't inherit our lock which 1066 * results in a lock order reversal between crypto_dispatch forced 1067 * entry and the crypto_done callback into us. 1068 */ 1069 error = crypto_dispatch(crp); 1070 if (error != 0) { 1071 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1072 goto bail; 1073 } 1074 1075 mtx_lock(&cse->lock); 1076 while (!cod->done) 1077 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0); 1078 mtx_unlock(&cse->lock); 1079 1080 if (crp->crp_etype == EAGAIN) { 1081 crp->crp_etype = 0; 1082 crp->crp_flags &= ~CRYPTO_F_DONE; 1083 cod->done = false; 1084 goto again; 1085 } 1086 1087 if (crp->crp_etype != 0) { 1088 error = crp->crp_etype; 1089 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1090 goto bail; 1091 } 1092 1093 if (caead->dst && (error = copyout( 1094 (caddr_t)cod->uio.uio_iov[0].iov_base + caead->aadlen, caead->dst, 1095 caead->len))) { 1096 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1097 goto bail; 1098 } 1099 1100 if ((error = copyout((caddr_t)cod->uio.uio_iov[0].iov_base + 1101 caead->aadlen + caead->len, caead->tag, cse->thash->hashsize))) { 1102 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1103 goto bail; 1104 } 1105 1106 bail: 1107 crypto_freereq(crp); 1108 if (cod) 1109 cod_free(cod); 1110 1111 return (error); 1112 } 1113 1114 static int 1115 cryptodev_cb(struct cryptop *crp) 1116 { 1117 struct cryptop_data *cod = crp->crp_opaque; 1118 1119 /* 1120 * Lock to ensure the wakeup() is not missed by the loops 1121 * waiting on cod->done in cryptodev_op() and 1122 * cryptodev_aead(). 1123 */ 1124 mtx_lock(&cod->cse->lock); 1125 cod->done = true; 1126 mtx_unlock(&cod->cse->lock); 1127 wakeup(cod); 1128 return (0); 1129 } 1130 1131 static int 1132 cryptodevkey_cb(void *op) 1133 { 1134 struct cryptkop *krp = (struct cryptkop *) op; 1135 1136 wakeup_one(krp); 1137 return (0); 1138 } 1139 1140 static int 1141 cryptodev_key(struct crypt_kop *kop) 1142 { 1143 struct cryptkop *krp = NULL; 1144 int error = EINVAL; 1145 int in, out, size, i; 1146 1147 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) { 1148 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1149 return (EFBIG); 1150 } 1151 1152 in = kop->crk_iparams; 1153 out = kop->crk_oparams; 1154 switch (kop->crk_op) { 1155 case CRK_MOD_EXP: 1156 if (in == 3 && out == 1) 1157 break; 1158 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1159 return (EINVAL); 1160 case CRK_MOD_EXP_CRT: 1161 if (in == 6 && out == 1) 1162 break; 1163 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1164 return (EINVAL); 1165 case CRK_DSA_SIGN: 1166 if (in == 5 && out == 2) 1167 break; 1168 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1169 return (EINVAL); 1170 case CRK_DSA_VERIFY: 1171 if (in == 7 && out == 0) 1172 break; 1173 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1174 return (EINVAL); 1175 case CRK_DH_COMPUTE_KEY: 1176 if (in == 3 && out == 1) 1177 break; 1178 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1179 return (EINVAL); 1180 default: 1181 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1182 return (EINVAL); 1183 } 1184 1185 krp = (struct cryptkop *)malloc(sizeof *krp, M_XDATA, M_WAITOK|M_ZERO); 1186 if (!krp) { 1187 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1188 return (ENOMEM); 1189 } 1190 krp->krp_op = kop->crk_op; 1191 krp->krp_status = kop->crk_status; 1192 krp->krp_iparams = kop->crk_iparams; 1193 krp->krp_oparams = kop->crk_oparams; 1194 krp->krp_crid = kop->crk_crid; 1195 krp->krp_status = 0; 1196 krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb; 1197 1198 for (i = 0; i < CRK_MAXPARAM; i++) { 1199 if (kop->crk_param[i].crp_nbits > 65536) { 1200 /* Limit is the same as in OpenBSD */ 1201 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1202 goto fail; 1203 } 1204 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits; 1205 } 1206 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) { 1207 size = (krp->krp_param[i].crp_nbits + 7) / 8; 1208 if (size == 0) 1209 continue; 1210 krp->krp_param[i].crp_p = malloc(size, M_XDATA, M_WAITOK); 1211 if (i >= krp->krp_iparams) 1212 continue; 1213 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size); 1214 if (error) { 1215 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1216 goto fail; 1217 } 1218 } 1219 1220 error = crypto_kdispatch(krp); 1221 if (error) { 1222 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1223 goto fail; 1224 } 1225 error = tsleep(krp, PSOCK, "crydev", 0); 1226 if (error) { 1227 /* XXX can this happen? if so, how do we recover? */ 1228 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1229 goto fail; 1230 } 1231 1232 kop->crk_crid = krp->krp_crid; /* device that did the work */ 1233 if (krp->krp_status != 0) { 1234 error = krp->krp_status; 1235 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1236 goto fail; 1237 } 1238 1239 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) { 1240 size = (krp->krp_param[i].crp_nbits + 7) / 8; 1241 if (size == 0) 1242 continue; 1243 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size); 1244 if (error) { 1245 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1246 goto fail; 1247 } 1248 } 1249 1250 fail: 1251 if (krp) { 1252 kop->crk_status = krp->krp_status; 1253 for (i = 0; i < CRK_MAXPARAM; i++) { 1254 if (krp->krp_param[i].crp_p) 1255 free(krp->krp_param[i].crp_p, M_XDATA); 1256 } 1257 free(krp, M_XDATA); 1258 } 1259 return (error); 1260 } 1261 1262 static int 1263 cryptodev_find(struct crypt_find_op *find) 1264 { 1265 device_t dev; 1266 size_t fnlen = sizeof find->name; 1267 1268 if (find->crid != -1) { 1269 dev = crypto_find_device_byhid(find->crid); 1270 if (dev == NULL) 1271 return (ENOENT); 1272 strncpy(find->name, device_get_nameunit(dev), fnlen); 1273 find->name[fnlen - 1] = '\x0'; 1274 } else { 1275 find->name[fnlen - 1] = '\x0'; 1276 find->crid = crypto_find_driver(find->name); 1277 if (find->crid == -1) 1278 return (ENOENT); 1279 } 1280 return (0); 1281 } 1282 1283 /* ARGSUSED */ 1284 static int 1285 cryptof_stat( 1286 struct file *fp, 1287 struct stat *sb, 1288 struct ucred *active_cred, 1289 struct thread *td) 1290 { 1291 1292 return (EOPNOTSUPP); 1293 } 1294 1295 /* ARGSUSED */ 1296 static int 1297 cryptof_close(struct file *fp, struct thread *td) 1298 { 1299 struct fcrypt *fcr = fp->f_data; 1300 struct csession *cse; 1301 1302 while ((cse = TAILQ_FIRST(&fcr->csessions))) { 1303 TAILQ_REMOVE(&fcr->csessions, cse, next); 1304 csefree(cse); 1305 } 1306 free(fcr, M_XDATA); 1307 fp->f_data = NULL; 1308 return 0; 1309 } 1310 1311 static int 1312 cryptof_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp) 1313 { 1314 1315 kif->kf_type = KF_TYPE_CRYPTO; 1316 return (0); 1317 } 1318 1319 static struct csession * 1320 csefind(struct fcrypt *fcr, u_int ses) 1321 { 1322 struct csession *cse; 1323 1324 TAILQ_FOREACH(cse, &fcr->csessions, next) 1325 if (cse->ses == ses) 1326 return (cse); 1327 return (NULL); 1328 } 1329 1330 static int 1331 csedelete(struct fcrypt *fcr, struct csession *cse_del) 1332 { 1333 struct csession *cse; 1334 1335 TAILQ_FOREACH(cse, &fcr->csessions, next) { 1336 if (cse == cse_del) { 1337 TAILQ_REMOVE(&fcr->csessions, cse, next); 1338 return (1); 1339 } 1340 } 1341 return (0); 1342 } 1343 1344 static struct csession * 1345 cseadd(struct fcrypt *fcr, struct csession *cse) 1346 { 1347 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next); 1348 cse->ses = fcr->sesn++; 1349 return (cse); 1350 } 1351 1352 struct csession * 1353 csecreate(struct fcrypt *fcr, crypto_session_t cses, caddr_t key, u_int64_t keylen, 1354 caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac, 1355 struct enc_xform *txform, struct auth_hash *thash) 1356 { 1357 struct csession *cse; 1358 1359 cse = malloc(sizeof(struct csession), M_XDATA, M_NOWAIT | M_ZERO); 1360 if (cse == NULL) 1361 return NULL; 1362 mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF); 1363 cse->key = key; 1364 cse->keylen = keylen/8; 1365 cse->mackey = mackey; 1366 cse->mackeylen = mackeylen/8; 1367 cse->cses = cses; 1368 cse->cipher = cipher; 1369 cse->mac = mac; 1370 cse->txform = txform; 1371 cse->thash = thash; 1372 cseadd(fcr, cse); 1373 return (cse); 1374 } 1375 1376 static void 1377 csefree(struct csession *cse) 1378 { 1379 1380 crypto_freesession(cse->cses); 1381 mtx_destroy(&cse->lock); 1382 if (cse->key) 1383 free(cse->key, M_XDATA); 1384 if (cse->mackey) 1385 free(cse->mackey, M_XDATA); 1386 free(cse, M_XDATA); 1387 } 1388 1389 static int 1390 cryptoopen(struct cdev *dev, int oflags, int devtype, struct thread *td) 1391 { 1392 return (0); 1393 } 1394 1395 static int 1396 cryptoread(struct cdev *dev, struct uio *uio, int ioflag) 1397 { 1398 return (EIO); 1399 } 1400 1401 static int 1402 cryptowrite(struct cdev *dev, struct uio *uio, int ioflag) 1403 { 1404 return (EIO); 1405 } 1406 1407 static int 1408 cryptoioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 1409 { 1410 struct file *f; 1411 struct fcrypt *fcr; 1412 int fd, error; 1413 1414 switch (cmd) { 1415 case CRIOGET: 1416 fcr = malloc(sizeof(struct fcrypt), M_XDATA, M_WAITOK); 1417 TAILQ_INIT(&fcr->csessions); 1418 fcr->sesn = 0; 1419 1420 error = falloc(td, &f, &fd, 0); 1421 1422 if (error) { 1423 free(fcr, M_XDATA); 1424 return (error); 1425 } 1426 /* falloc automatically provides an extra reference to 'f'. */ 1427 finit(f, FREAD | FWRITE, DTYPE_CRYPTO, fcr, &cryptofops); 1428 *(u_int32_t *)data = fd; 1429 fdrop(f, td); 1430 break; 1431 case CRIOFINDDEV: 1432 error = cryptodev_find((struct crypt_find_op *)data); 1433 break; 1434 case CRIOASYMFEAT: 1435 error = crypto_getfeat((int *)data); 1436 break; 1437 default: 1438 error = EINVAL; 1439 break; 1440 } 1441 return (error); 1442 } 1443 1444 static struct cdevsw crypto_cdevsw = { 1445 .d_version = D_VERSION, 1446 .d_flags = D_NEEDGIANT, 1447 .d_open = cryptoopen, 1448 .d_read = cryptoread, 1449 .d_write = cryptowrite, 1450 .d_ioctl = cryptoioctl, 1451 .d_name = "crypto", 1452 }; 1453 static struct cdev *crypto_dev; 1454 1455 /* 1456 * Initialization code, both for static and dynamic loading. 1457 */ 1458 static int 1459 cryptodev_modevent(module_t mod, int type, void *unused) 1460 { 1461 switch (type) { 1462 case MOD_LOAD: 1463 if (bootverbose) 1464 printf("crypto: <crypto device>\n"); 1465 crypto_dev = make_dev(&crypto_cdevsw, 0, 1466 UID_ROOT, GID_WHEEL, 0666, 1467 "crypto"); 1468 return 0; 1469 case MOD_UNLOAD: 1470 /*XXX disallow if active sessions */ 1471 destroy_dev(crypto_dev); 1472 return 0; 1473 } 1474 return EINVAL; 1475 } 1476 1477 static moduledata_t cryptodev_mod = { 1478 "cryptodev", 1479 cryptodev_modevent, 1480 0 1481 }; 1482 MODULE_VERSION(cryptodev, 1); 1483 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 1484 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1); 1485 MODULE_DEPEND(cryptodev, zlib, 1, 1, 1); 1486