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