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 case CRYPTO_AES_CCM_16: 448 txform = &enc_xform_ccm; 449 break; 450 451 default: 452 CRYPTDEB("invalid cipher"); 453 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 454 return (EINVAL); 455 } 456 457 switch (sop->mac) { 458 case 0: 459 break; 460 case CRYPTO_MD5_HMAC: 461 thash = &auth_hash_hmac_md5; 462 break; 463 case CRYPTO_POLY1305: 464 thash = &auth_hash_poly1305; 465 break; 466 case CRYPTO_SHA1_HMAC: 467 thash = &auth_hash_hmac_sha1; 468 break; 469 case CRYPTO_SHA2_224_HMAC: 470 thash = &auth_hash_hmac_sha2_224; 471 break; 472 case CRYPTO_SHA2_256_HMAC: 473 thash = &auth_hash_hmac_sha2_256; 474 break; 475 case CRYPTO_SHA2_384_HMAC: 476 thash = &auth_hash_hmac_sha2_384; 477 break; 478 case CRYPTO_SHA2_512_HMAC: 479 thash = &auth_hash_hmac_sha2_512; 480 break; 481 case CRYPTO_RIPEMD160_HMAC: 482 thash = &auth_hash_hmac_ripemd_160; 483 break; 484 case CRYPTO_AES_128_NIST_GMAC: 485 thash = &auth_hash_nist_gmac_aes_128; 486 break; 487 case CRYPTO_AES_192_NIST_GMAC: 488 thash = &auth_hash_nist_gmac_aes_192; 489 break; 490 case CRYPTO_AES_256_NIST_GMAC: 491 thash = &auth_hash_nist_gmac_aes_256; 492 break; 493 494 case CRYPTO_AES_CCM_CBC_MAC: 495 switch (sop->keylen) { 496 case 16: 497 thash = &auth_hash_ccm_cbc_mac_128; 498 break; 499 case 24: 500 thash = &auth_hash_ccm_cbc_mac_192; 501 break; 502 case 32: 503 thash = &auth_hash_ccm_cbc_mac_256; 504 break; 505 default: 506 CRYPTDEB("Invalid CBC MAC key size %d", 507 sop->keylen); 508 SDT_PROBE1(opencrypto, dev, ioctl, 509 error, __LINE__); 510 return (EINVAL); 511 } 512 break; 513 #ifdef notdef 514 case CRYPTO_MD5: 515 thash = &auth_hash_md5; 516 break; 517 #endif 518 case CRYPTO_SHA1: 519 thash = &auth_hash_sha1; 520 break; 521 case CRYPTO_SHA2_224: 522 thash = &auth_hash_sha2_224; 523 break; 524 case CRYPTO_SHA2_256: 525 thash = &auth_hash_sha2_256; 526 break; 527 case CRYPTO_SHA2_384: 528 thash = &auth_hash_sha2_384; 529 break; 530 case CRYPTO_SHA2_512: 531 thash = &auth_hash_sha2_512; 532 break; 533 534 case CRYPTO_NULL_HMAC: 535 thash = &auth_hash_null; 536 break; 537 538 case CRYPTO_BLAKE2B: 539 thash = &auth_hash_blake2b; 540 break; 541 case CRYPTO_BLAKE2S: 542 thash = &auth_hash_blake2s; 543 break; 544 545 default: 546 CRYPTDEB("invalid mac"); 547 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 548 return (EINVAL); 549 } 550 551 bzero(&crie, sizeof(crie)); 552 bzero(&cria, sizeof(cria)); 553 554 if (txform) { 555 crie.cri_alg = txform->type; 556 crie.cri_klen = sop->keylen * 8; 557 if (sop->keylen > txform->maxkey || 558 sop->keylen < txform->minkey) { 559 CRYPTDEB("invalid cipher parameters"); 560 error = EINVAL; 561 SDT_PROBE1(opencrypto, dev, ioctl, error, 562 __LINE__); 563 goto bail; 564 } 565 566 crie.cri_key = malloc(crie.cri_klen / 8, 567 M_XDATA, M_WAITOK); 568 if ((error = copyin(sop->key, crie.cri_key, 569 crie.cri_klen / 8))) { 570 CRYPTDEB("invalid key"); 571 SDT_PROBE1(opencrypto, dev, ioctl, error, 572 __LINE__); 573 goto bail; 574 } 575 if (thash) 576 crie.cri_next = &cria; 577 } 578 579 if (thash) { 580 cria.cri_alg = thash->type; 581 cria.cri_klen = sop->mackeylen * 8; 582 if (thash->keysize != 0 && 583 sop->mackeylen > thash->keysize) { 584 CRYPTDEB("invalid mac key length"); 585 error = EINVAL; 586 SDT_PROBE1(opencrypto, dev, ioctl, error, 587 __LINE__); 588 goto bail; 589 } 590 591 if (cria.cri_klen) { 592 cria.cri_key = malloc(cria.cri_klen / 8, 593 M_XDATA, M_WAITOK); 594 if ((error = copyin(sop->mackey, cria.cri_key, 595 cria.cri_klen / 8))) { 596 CRYPTDEB("invalid mac key"); 597 SDT_PROBE1(opencrypto, dev, ioctl, 598 error, __LINE__); 599 goto bail; 600 } 601 } 602 } 603 604 /* NB: CIOCGSESSION2 has the crid */ 605 if (cmd == CIOCGSESSION2 606 #ifdef COMPAT_FREEBSD32 607 || cmd == CIOCGSESSION232 608 #endif 609 ) { 610 crid = SES2(sop)->crid; 611 error = checkforsoftware(&crid); 612 if (error) { 613 CRYPTDEB("checkforsoftware"); 614 SDT_PROBE1(opencrypto, dev, ioctl, error, 615 __LINE__); 616 goto bail; 617 } 618 } else 619 crid = CRYPTOCAP_F_HARDWARE; 620 error = crypto_newsession(&cses, (txform ? &crie : &cria), crid); 621 if (error) { 622 CRYPTDEB("crypto_newsession"); 623 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 624 goto bail; 625 } 626 627 cse = csecreate(fcr, cses, crie.cri_key, crie.cri_klen, 628 cria.cri_key, cria.cri_klen, sop->cipher, sop->mac, txform, 629 thash); 630 631 if (cse == NULL) { 632 crypto_freesession(cses); 633 error = EINVAL; 634 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 635 CRYPTDEB("csecreate"); 636 goto bail; 637 } 638 sop->ses = cse->ses; 639 if (cmd == CIOCGSESSION2 640 #ifdef COMPAT_FREEBSD32 641 || cmd == CIOCGSESSION232 642 #endif 643 ) { 644 /* return hardware/driver id */ 645 SES2(sop)->crid = crypto_ses2hid(cse->cses); 646 } 647 bail: 648 if (error) { 649 if (crie.cri_key) 650 free(crie.cri_key, M_XDATA); 651 if (cria.cri_key) 652 free(cria.cri_key, M_XDATA); 653 } 654 #ifdef COMPAT_FREEBSD32 655 else { 656 if (cmd == CIOCGSESSION32) 657 session_op_to_32(sop, data); 658 else if (cmd == CIOCGSESSION232) 659 session2_op_to_32((struct session2_op *)sop, 660 data); 661 } 662 #endif 663 break; 664 case CIOCFSESSION: 665 ses = *(u_int32_t *)data; 666 cse = csefind(fcr, ses); 667 if (cse == NULL) { 668 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 669 return (EINVAL); 670 } 671 csedelete(fcr, cse); 672 csefree(cse); 673 break; 674 case CIOCCRYPT: 675 #ifdef COMPAT_FREEBSD32 676 case CIOCCRYPT32: 677 if (cmd == CIOCCRYPT32) { 678 cop = &copc; 679 crypt_op_from_32(data, cop); 680 } else 681 #endif 682 cop = (struct crypt_op *)data; 683 cse = csefind(fcr, cop->ses); 684 if (cse == NULL) { 685 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 686 return (EINVAL); 687 } 688 error = cryptodev_op(cse, cop, active_cred, td); 689 #ifdef COMPAT_FREEBSD32 690 if (error == 0 && cmd == CIOCCRYPT32) 691 crypt_op_to_32(cop, data); 692 #endif 693 break; 694 case CIOCKEY: 695 case CIOCKEY2: 696 #ifdef COMPAT_FREEBSD32 697 case CIOCKEY32: 698 case CIOCKEY232: 699 #endif 700 if (!crypto_userasymcrypto) { 701 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 702 return (EPERM); /* XXX compat? */ 703 } 704 #ifdef COMPAT_FREEBSD32 705 if (cmd == CIOCKEY32 || cmd == CIOCKEY232) { 706 kop = &kopc; 707 crypt_kop_from_32(data, kop); 708 } else 709 #endif 710 kop = (struct crypt_kop *)data; 711 if (cmd == CIOCKEY 712 #ifdef COMPAT_FREEBSD32 713 || cmd == CIOCKEY32 714 #endif 715 ) { 716 /* NB: crypto core enforces s/w driver use */ 717 kop->crk_crid = 718 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE; 719 } 720 mtx_lock(&Giant); 721 error = cryptodev_key(kop); 722 mtx_unlock(&Giant); 723 #ifdef COMPAT_FREEBSD32 724 if (cmd == CIOCKEY32 || cmd == CIOCKEY232) 725 crypt_kop_to_32(kop, data); 726 #endif 727 break; 728 case CIOCASYMFEAT: 729 if (!crypto_userasymcrypto) { 730 /* 731 * NB: if user asym crypto operations are 732 * not permitted return "no algorithms" 733 * so well-behaved applications will just 734 * fallback to doing them in software. 735 */ 736 *(int *)data = 0; 737 } else { 738 error = crypto_getfeat((int *)data); 739 if (error) 740 SDT_PROBE1(opencrypto, dev, ioctl, error, 741 __LINE__); 742 } 743 break; 744 case CIOCFINDDEV: 745 error = cryptodev_find((struct crypt_find_op *)data); 746 break; 747 case CIOCCRYPTAEAD: 748 caead = (struct crypt_aead *)data; 749 cse = csefind(fcr, caead->ses); 750 if (cse == NULL) { 751 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 752 return (EINVAL); 753 } 754 error = cryptodev_aead(cse, caead, active_cred, td); 755 break; 756 default: 757 error = EINVAL; 758 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 759 break; 760 } 761 return (error); 762 #undef SES2 763 } 764 765 static int cryptodev_cb(struct cryptop *); 766 767 static struct cryptop_data * 768 cod_alloc(struct csession *cse, size_t len, struct thread *td) 769 { 770 struct cryptop_data *cod; 771 struct uio *uio; 772 773 cod = malloc(sizeof(struct cryptop_data), M_XDATA, M_WAITOK | M_ZERO); 774 775 cod->cse = cse; 776 uio = &cod->uio; 777 uio->uio_iov = cod->iovec; 778 uio->uio_iovcnt = 1; 779 uio->uio_resid = len; 780 uio->uio_segflg = UIO_SYSSPACE; 781 uio->uio_rw = UIO_WRITE; 782 uio->uio_td = td; 783 uio->uio_iov[0].iov_len = len; 784 uio->uio_iov[0].iov_base = malloc(len, M_XDATA, M_WAITOK); 785 return (cod); 786 } 787 788 static void 789 cod_free(struct cryptop_data *cod) 790 { 791 792 free(cod->uio.uio_iov[0].iov_base, M_XDATA); 793 free(cod, M_XDATA); 794 } 795 796 static int 797 cryptodev_op( 798 struct csession *cse, 799 struct crypt_op *cop, 800 struct ucred *active_cred, 801 struct thread *td) 802 { 803 struct cryptop_data *cod = NULL; 804 struct cryptop *crp = NULL; 805 struct cryptodesc *crde = NULL, *crda = NULL; 806 int error; 807 808 if (cop->len > 256*1024-4) { 809 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 810 return (E2BIG); 811 } 812 813 if (cse->txform) { 814 if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0) { 815 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 816 return (EINVAL); 817 } 818 } 819 820 if (cse->thash) 821 cod = cod_alloc(cse, cop->len + cse->thash->hashsize, td); 822 else 823 cod = cod_alloc(cse, cop->len, td); 824 825 crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL)); 826 if (crp == NULL) { 827 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 828 error = ENOMEM; 829 goto bail; 830 } 831 832 if (cse->thash && cse->txform) { 833 if (cop->flags & COP_F_CIPHER_FIRST) { 834 crde = crp->crp_desc; 835 crda = crde->crd_next; 836 } else { 837 crda = crp->crp_desc; 838 crde = crda->crd_next; 839 } 840 } else if (cse->thash) { 841 crda = crp->crp_desc; 842 } else if (cse->txform) { 843 crde = crp->crp_desc; 844 } else { 845 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 846 error = EINVAL; 847 goto bail; 848 } 849 850 if ((error = copyin(cop->src, cod->uio.uio_iov[0].iov_base, 851 cop->len))) { 852 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 853 goto bail; 854 } 855 856 if (crda) { 857 crda->crd_skip = 0; 858 crda->crd_len = cop->len; 859 crda->crd_inject = cop->len; 860 861 crda->crd_alg = cse->mac; 862 crda->crd_key = cse->mackey; 863 crda->crd_klen = cse->mackeylen * 8; 864 } 865 866 if (crde) { 867 if (cop->op == COP_ENCRYPT) 868 crde->crd_flags |= CRD_F_ENCRYPT; 869 else 870 crde->crd_flags &= ~CRD_F_ENCRYPT; 871 crde->crd_len = cop->len; 872 crde->crd_inject = 0; 873 874 crde->crd_alg = cse->cipher; 875 crde->crd_key = cse->key; 876 crde->crd_klen = cse->keylen * 8; 877 } 878 879 crp->crp_ilen = cop->len; 880 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM 881 | (cop->flags & COP_F_BATCH); 882 crp->crp_uio = &cod->uio; 883 crp->crp_callback = cryptodev_cb; 884 crp->crp_session = cse->cses; 885 crp->crp_opaque = cod; 886 887 if (cop->iv) { 888 if (crde == NULL) { 889 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 890 error = EINVAL; 891 goto bail; 892 } 893 if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */ 894 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 895 error = EINVAL; 896 goto bail; 897 } 898 if ((error = copyin(cop->iv, crde->crd_iv, 899 cse->txform->ivsize))) { 900 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 901 goto bail; 902 } 903 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT; 904 crde->crd_skip = 0; 905 } else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */ 906 crde->crd_skip = 0; 907 } else if (crde) { 908 crde->crd_flags |= CRD_F_IV_PRESENT; 909 crde->crd_skip = cse->txform->ivsize; 910 crde->crd_len -= cse->txform->ivsize; 911 } 912 913 if (cop->mac && crda == NULL) { 914 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 915 error = EINVAL; 916 goto bail; 917 } 918 919 again: 920 /* 921 * Let the dispatch run unlocked, then, interlock against the 922 * callback before checking if the operation completed and going 923 * to sleep. This insures drivers don't inherit our lock which 924 * results in a lock order reversal between crypto_dispatch forced 925 * entry and the crypto_done callback into us. 926 */ 927 error = crypto_dispatch(crp); 928 if (error != 0) { 929 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 930 goto bail; 931 } 932 933 mtx_lock(&cse->lock); 934 while (!cod->done) 935 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0); 936 mtx_unlock(&cse->lock); 937 938 if (crp->crp_etype == EAGAIN) { 939 crp->crp_etype = 0; 940 crp->crp_flags &= ~CRYPTO_F_DONE; 941 cod->done = false; 942 goto again; 943 } 944 945 if (crp->crp_etype != 0) { 946 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 947 error = crp->crp_etype; 948 goto bail; 949 } 950 951 if (cop->dst && 952 (error = copyout(cod->uio.uio_iov[0].iov_base, cop->dst, 953 cop->len))) { 954 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 955 goto bail; 956 } 957 958 if (cop->mac && 959 (error = copyout((caddr_t)cod->uio.uio_iov[0].iov_base + cop->len, 960 cop->mac, cse->thash->hashsize))) { 961 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 962 goto bail; 963 } 964 965 bail: 966 if (crp) 967 crypto_freereq(crp); 968 if (cod) 969 cod_free(cod); 970 971 return (error); 972 } 973 974 static int 975 cryptodev_aead( 976 struct csession *cse, 977 struct crypt_aead *caead, 978 struct ucred *active_cred, 979 struct thread *td) 980 { 981 struct cryptop_data *cod = NULL; 982 struct cryptop *crp = NULL; 983 struct cryptodesc *crde = NULL, *crda = NULL; 984 int error; 985 986 if (caead->len > 256*1024-4 || caead->aadlen > 256*1024-4) { 987 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 988 return (E2BIG); 989 } 990 991 if (cse->txform == NULL || cse->thash == NULL || caead->tag == NULL || 992 (caead->len % cse->txform->blocksize) != 0) { 993 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 994 return (EINVAL); 995 } 996 997 cod = cod_alloc(cse, caead->aadlen + caead->len + cse->thash->hashsize, 998 td); 999 1000 crp = crypto_getreq(2); 1001 if (crp == NULL) { 1002 error = ENOMEM; 1003 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1004 goto bail; 1005 } 1006 1007 if (caead->flags & COP_F_CIPHER_FIRST) { 1008 crde = crp->crp_desc; 1009 crda = crde->crd_next; 1010 } else { 1011 crda = crp->crp_desc; 1012 crde = crda->crd_next; 1013 } 1014 1015 if ((error = copyin(caead->aad, cod->uio.uio_iov[0].iov_base, 1016 caead->aadlen))) { 1017 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1018 goto bail; 1019 } 1020 1021 if ((error = copyin(caead->src, (char *)cod->uio.uio_iov[0].iov_base + 1022 caead->aadlen, caead->len))) { 1023 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1024 goto bail; 1025 } 1026 1027 /* 1028 * For GCM/CCM, crd_len covers only the AAD. For other ciphers 1029 * chained with an HMAC, crd_len covers both the AAD and the 1030 * cipher text. 1031 */ 1032 crda->crd_skip = 0; 1033 if (cse->cipher == CRYPTO_AES_NIST_GCM_16 || 1034 cse->cipher == CRYPTO_AES_CCM_16) 1035 crda->crd_len = caead->aadlen; 1036 else 1037 crda->crd_len = caead->aadlen + caead->len; 1038 crda->crd_inject = caead->aadlen + caead->len; 1039 1040 crda->crd_alg = cse->mac; 1041 crda->crd_key = cse->mackey; 1042 crda->crd_klen = cse->mackeylen * 8; 1043 1044 if (caead->op == COP_ENCRYPT) 1045 crde->crd_flags |= CRD_F_ENCRYPT; 1046 else 1047 crde->crd_flags &= ~CRD_F_ENCRYPT; 1048 crde->crd_skip = caead->aadlen; 1049 crde->crd_len = caead->len; 1050 crde->crd_inject = caead->aadlen; 1051 1052 crde->crd_alg = cse->cipher; 1053 crde->crd_key = cse->key; 1054 crde->crd_klen = cse->keylen * 8; 1055 1056 crp->crp_ilen = caead->aadlen + caead->len; 1057 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM 1058 | (caead->flags & COP_F_BATCH); 1059 crp->crp_uio = &cod->uio; 1060 crp->crp_callback = cryptodev_cb; 1061 crp->crp_session = cse->cses; 1062 crp->crp_opaque = cod; 1063 1064 if (caead->iv) { 1065 if (caead->ivlen > sizeof(crde->crd_iv)) { 1066 error = EINVAL; 1067 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1068 goto bail; 1069 } 1070 1071 if ((error = copyin(caead->iv, crde->crd_iv, caead->ivlen))) { 1072 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1073 goto bail; 1074 } 1075 crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT; 1076 } else { 1077 crde->crd_flags |= CRD_F_IV_PRESENT; 1078 crde->crd_skip += cse->txform->ivsize; 1079 crde->crd_len -= cse->txform->ivsize; 1080 } 1081 1082 if ((error = copyin(caead->tag, (caddr_t)cod->uio.uio_iov[0].iov_base + 1083 caead->len + caead->aadlen, cse->thash->hashsize))) { 1084 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1085 goto bail; 1086 } 1087 again: 1088 /* 1089 * Let the dispatch run unlocked, then, interlock against the 1090 * callback before checking if the operation completed and going 1091 * to sleep. This insures drivers don't inherit our lock which 1092 * results in a lock order reversal between crypto_dispatch forced 1093 * entry and the crypto_done callback into us. 1094 */ 1095 error = crypto_dispatch(crp); 1096 if (error != 0) { 1097 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1098 goto bail; 1099 } 1100 1101 mtx_lock(&cse->lock); 1102 while (!cod->done) 1103 mtx_sleep(cod, &cse->lock, PWAIT, "crydev", 0); 1104 mtx_unlock(&cse->lock); 1105 1106 if (crp->crp_etype == EAGAIN) { 1107 crp->crp_etype = 0; 1108 crp->crp_flags &= ~CRYPTO_F_DONE; 1109 cod->done = false; 1110 goto again; 1111 } 1112 1113 if (crp->crp_etype != 0) { 1114 error = crp->crp_etype; 1115 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1116 goto bail; 1117 } 1118 1119 if (caead->dst && (error = copyout( 1120 (caddr_t)cod->uio.uio_iov[0].iov_base + caead->aadlen, caead->dst, 1121 caead->len))) { 1122 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1123 goto bail; 1124 } 1125 1126 if ((error = copyout((caddr_t)cod->uio.uio_iov[0].iov_base + 1127 caead->aadlen + caead->len, caead->tag, cse->thash->hashsize))) { 1128 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1129 goto bail; 1130 } 1131 1132 bail: 1133 crypto_freereq(crp); 1134 if (cod) 1135 cod_free(cod); 1136 1137 return (error); 1138 } 1139 1140 static int 1141 cryptodev_cb(struct cryptop *crp) 1142 { 1143 struct cryptop_data *cod = crp->crp_opaque; 1144 1145 /* 1146 * Lock to ensure the wakeup() is not missed by the loops 1147 * waiting on cod->done in cryptodev_op() and 1148 * cryptodev_aead(). 1149 */ 1150 mtx_lock(&cod->cse->lock); 1151 cod->done = true; 1152 mtx_unlock(&cod->cse->lock); 1153 wakeup(cod); 1154 return (0); 1155 } 1156 1157 static int 1158 cryptodevkey_cb(void *op) 1159 { 1160 struct cryptkop *krp = (struct cryptkop *) op; 1161 1162 wakeup_one(krp); 1163 return (0); 1164 } 1165 1166 static int 1167 cryptodev_key(struct crypt_kop *kop) 1168 { 1169 struct cryptkop *krp = NULL; 1170 int error = EINVAL; 1171 int in, out, size, i; 1172 1173 if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) { 1174 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1175 return (EFBIG); 1176 } 1177 1178 in = kop->crk_iparams; 1179 out = kop->crk_oparams; 1180 switch (kop->crk_op) { 1181 case CRK_MOD_EXP: 1182 if (in == 3 && out == 1) 1183 break; 1184 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1185 return (EINVAL); 1186 case CRK_MOD_EXP_CRT: 1187 if (in == 6 && out == 1) 1188 break; 1189 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1190 return (EINVAL); 1191 case CRK_DSA_SIGN: 1192 if (in == 5 && out == 2) 1193 break; 1194 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1195 return (EINVAL); 1196 case CRK_DSA_VERIFY: 1197 if (in == 7 && out == 0) 1198 break; 1199 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1200 return (EINVAL); 1201 case CRK_DH_COMPUTE_KEY: 1202 if (in == 3 && out == 1) 1203 break; 1204 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1205 return (EINVAL); 1206 default: 1207 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1208 return (EINVAL); 1209 } 1210 1211 krp = (struct cryptkop *)malloc(sizeof *krp, M_XDATA, M_WAITOK|M_ZERO); 1212 if (!krp) { 1213 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1214 return (ENOMEM); 1215 } 1216 krp->krp_op = kop->crk_op; 1217 krp->krp_status = kop->crk_status; 1218 krp->krp_iparams = kop->crk_iparams; 1219 krp->krp_oparams = kop->crk_oparams; 1220 krp->krp_crid = kop->crk_crid; 1221 krp->krp_status = 0; 1222 krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb; 1223 1224 for (i = 0; i < CRK_MAXPARAM; i++) { 1225 if (kop->crk_param[i].crp_nbits > 65536) { 1226 /* Limit is the same as in OpenBSD */ 1227 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1228 goto fail; 1229 } 1230 krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits; 1231 } 1232 for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) { 1233 size = (krp->krp_param[i].crp_nbits + 7) / 8; 1234 if (size == 0) 1235 continue; 1236 krp->krp_param[i].crp_p = malloc(size, M_XDATA, M_WAITOK); 1237 if (i >= krp->krp_iparams) 1238 continue; 1239 error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size); 1240 if (error) { 1241 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1242 goto fail; 1243 } 1244 } 1245 1246 error = crypto_kdispatch(krp); 1247 if (error) { 1248 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1249 goto fail; 1250 } 1251 error = tsleep(krp, PSOCK, "crydev", 0); 1252 if (error) { 1253 /* XXX can this happen? if so, how do we recover? */ 1254 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1255 goto fail; 1256 } 1257 1258 kop->crk_crid = krp->krp_crid; /* device that did the work */ 1259 if (krp->krp_status != 0) { 1260 error = krp->krp_status; 1261 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1262 goto fail; 1263 } 1264 1265 for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) { 1266 size = (krp->krp_param[i].crp_nbits + 7) / 8; 1267 if (size == 0) 1268 continue; 1269 error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size); 1270 if (error) { 1271 SDT_PROBE1(opencrypto, dev, ioctl, error, __LINE__); 1272 goto fail; 1273 } 1274 } 1275 1276 fail: 1277 if (krp) { 1278 kop->crk_status = krp->krp_status; 1279 for (i = 0; i < CRK_MAXPARAM; i++) { 1280 if (krp->krp_param[i].crp_p) 1281 free(krp->krp_param[i].crp_p, M_XDATA); 1282 } 1283 free(krp, M_XDATA); 1284 } 1285 return (error); 1286 } 1287 1288 static int 1289 cryptodev_find(struct crypt_find_op *find) 1290 { 1291 device_t dev; 1292 size_t fnlen = sizeof find->name; 1293 1294 if (find->crid != -1) { 1295 dev = crypto_find_device_byhid(find->crid); 1296 if (dev == NULL) 1297 return (ENOENT); 1298 strncpy(find->name, device_get_nameunit(dev), fnlen); 1299 find->name[fnlen - 1] = '\x0'; 1300 } else { 1301 find->name[fnlen - 1] = '\x0'; 1302 find->crid = crypto_find_driver(find->name); 1303 if (find->crid == -1) 1304 return (ENOENT); 1305 } 1306 return (0); 1307 } 1308 1309 /* ARGSUSED */ 1310 static int 1311 cryptof_stat( 1312 struct file *fp, 1313 struct stat *sb, 1314 struct ucred *active_cred, 1315 struct thread *td) 1316 { 1317 1318 return (EOPNOTSUPP); 1319 } 1320 1321 /* ARGSUSED */ 1322 static int 1323 cryptof_close(struct file *fp, struct thread *td) 1324 { 1325 struct fcrypt *fcr = fp->f_data; 1326 struct csession *cse; 1327 1328 while ((cse = TAILQ_FIRST(&fcr->csessions))) { 1329 TAILQ_REMOVE(&fcr->csessions, cse, next); 1330 csefree(cse); 1331 } 1332 free(fcr, M_XDATA); 1333 fp->f_data = NULL; 1334 return 0; 1335 } 1336 1337 static int 1338 cryptof_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp) 1339 { 1340 1341 kif->kf_type = KF_TYPE_CRYPTO; 1342 return (0); 1343 } 1344 1345 static struct csession * 1346 csefind(struct fcrypt *fcr, u_int ses) 1347 { 1348 struct csession *cse; 1349 1350 TAILQ_FOREACH(cse, &fcr->csessions, next) 1351 if (cse->ses == ses) 1352 return (cse); 1353 return (NULL); 1354 } 1355 1356 static int 1357 csedelete(struct fcrypt *fcr, struct csession *cse_del) 1358 { 1359 struct csession *cse; 1360 1361 TAILQ_FOREACH(cse, &fcr->csessions, next) { 1362 if (cse == cse_del) { 1363 TAILQ_REMOVE(&fcr->csessions, cse, next); 1364 return (1); 1365 } 1366 } 1367 return (0); 1368 } 1369 1370 static struct csession * 1371 cseadd(struct fcrypt *fcr, struct csession *cse) 1372 { 1373 TAILQ_INSERT_TAIL(&fcr->csessions, cse, next); 1374 cse->ses = fcr->sesn++; 1375 return (cse); 1376 } 1377 1378 struct csession * 1379 csecreate(struct fcrypt *fcr, crypto_session_t cses, caddr_t key, u_int64_t keylen, 1380 caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac, 1381 struct enc_xform *txform, struct auth_hash *thash) 1382 { 1383 struct csession *cse; 1384 1385 cse = malloc(sizeof(struct csession), M_XDATA, M_NOWAIT | M_ZERO); 1386 if (cse == NULL) 1387 return NULL; 1388 mtx_init(&cse->lock, "cryptodev", "crypto session lock", MTX_DEF); 1389 cse->key = key; 1390 cse->keylen = keylen/8; 1391 cse->mackey = mackey; 1392 cse->mackeylen = mackeylen/8; 1393 cse->cses = cses; 1394 cse->cipher = cipher; 1395 cse->mac = mac; 1396 cse->txform = txform; 1397 cse->thash = thash; 1398 cseadd(fcr, cse); 1399 return (cse); 1400 } 1401 1402 static void 1403 csefree(struct csession *cse) 1404 { 1405 1406 crypto_freesession(cse->cses); 1407 mtx_destroy(&cse->lock); 1408 if (cse->key) 1409 free(cse->key, M_XDATA); 1410 if (cse->mackey) 1411 free(cse->mackey, M_XDATA); 1412 free(cse, M_XDATA); 1413 } 1414 1415 static int 1416 cryptoopen(struct cdev *dev, int oflags, int devtype, struct thread *td) 1417 { 1418 return (0); 1419 } 1420 1421 static int 1422 cryptoread(struct cdev *dev, struct uio *uio, int ioflag) 1423 { 1424 return (EIO); 1425 } 1426 1427 static int 1428 cryptowrite(struct cdev *dev, struct uio *uio, int ioflag) 1429 { 1430 return (EIO); 1431 } 1432 1433 static int 1434 cryptoioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 1435 { 1436 struct file *f; 1437 struct fcrypt *fcr; 1438 int fd, error; 1439 1440 switch (cmd) { 1441 case CRIOGET: 1442 fcr = malloc(sizeof(struct fcrypt), M_XDATA, M_WAITOK); 1443 TAILQ_INIT(&fcr->csessions); 1444 fcr->sesn = 0; 1445 1446 error = falloc(td, &f, &fd, 0); 1447 1448 if (error) { 1449 free(fcr, M_XDATA); 1450 return (error); 1451 } 1452 /* falloc automatically provides an extra reference to 'f'. */ 1453 finit(f, FREAD | FWRITE, DTYPE_CRYPTO, fcr, &cryptofops); 1454 *(u_int32_t *)data = fd; 1455 fdrop(f, td); 1456 break; 1457 case CRIOFINDDEV: 1458 error = cryptodev_find((struct crypt_find_op *)data); 1459 break; 1460 case CRIOASYMFEAT: 1461 error = crypto_getfeat((int *)data); 1462 break; 1463 default: 1464 error = EINVAL; 1465 break; 1466 } 1467 return (error); 1468 } 1469 1470 static struct cdevsw crypto_cdevsw = { 1471 .d_version = D_VERSION, 1472 .d_flags = D_NEEDGIANT, 1473 .d_open = cryptoopen, 1474 .d_read = cryptoread, 1475 .d_write = cryptowrite, 1476 .d_ioctl = cryptoioctl, 1477 .d_name = "crypto", 1478 }; 1479 static struct cdev *crypto_dev; 1480 1481 /* 1482 * Initialization code, both for static and dynamic loading. 1483 */ 1484 static int 1485 cryptodev_modevent(module_t mod, int type, void *unused) 1486 { 1487 switch (type) { 1488 case MOD_LOAD: 1489 if (bootverbose) 1490 printf("crypto: <crypto device>\n"); 1491 crypto_dev = make_dev(&crypto_cdevsw, 0, 1492 UID_ROOT, GID_WHEEL, 0666, 1493 "crypto"); 1494 return 0; 1495 case MOD_UNLOAD: 1496 /*XXX disallow if active sessions */ 1497 destroy_dev(crypto_dev); 1498 return 0; 1499 } 1500 return EINVAL; 1501 } 1502 1503 static moduledata_t cryptodev_mod = { 1504 "cryptodev", 1505 cryptodev_modevent, 1506 0 1507 }; 1508 MODULE_VERSION(cryptodev, 1); 1509 DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 1510 MODULE_DEPEND(cryptodev, crypto, 1, 1, 1); 1511 MODULE_DEPEND(cryptodev, zlib, 1, 1, 1); 1512