1/* 2 * Copyright (c) 2002 Bob Beck <beck@openbsd.org> 3 * Copyright (c) 2002 Theo de Raadt 4 * Copyright (c) 2002 Markus Friedl 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY 17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY 20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 */ 28 29#include <openssl/objects.h> 30#include <openssl/engine.h> 31#include <openssl/evp.h> 32#include <openssl/bn.h> 33 34#if (defined(__unix__) || defined(unix)) && !defined(USG) && \ 35 (defined(OpenBSD) || defined(__FreeBSD__)) 36# include <sys/param.h> 37# if (OpenBSD >= 200112) || ((__FreeBSD_version >= 470101 && __FreeBSD_version < 500000) || __FreeBSD_version >= 500041) 38# define HAVE_CRYPTODEV 39# endif 40# if (OpenBSD >= 200110) 41# define HAVE_SYSLOG_R 42# endif 43#endif 44 45#ifndef HAVE_CRYPTODEV 46 47void ENGINE_load_cryptodev(void) 48{ 49 /* This is a NOP on platforms without /dev/crypto */ 50 return; 51} 52 53#else 54 55# include <sys/types.h> 56# include <crypto/cryptodev.h> 57# include <openssl/dh.h> 58# include <openssl/dsa.h> 59# include <openssl/err.h> 60# include <openssl/rsa.h> 61# include <sys/ioctl.h> 62# include <errno.h> 63# include <stdio.h> 64# include <unistd.h> 65# include <fcntl.h> 66# include <stdarg.h> 67# include <syslog.h> 68# include <errno.h> 69# include <string.h> 70 71struct dev_crypto_state { 72 struct session_op d_sess; 73 int d_fd; 74# ifdef USE_CRYPTODEV_DIGESTS 75 char dummy_mac_key[HASH_MAX_LEN]; 76 unsigned char digest_res[HASH_MAX_LEN]; 77 char *mac_data; 78 int mac_len; 79# endif 80}; 81 82static u_int32_t cryptodev_asymfeat = 0; 83 84static int get_asym_dev_crypto(void); 85static int open_dev_crypto(void); 86static int get_dev_crypto(void); 87static int get_cryptodev_ciphers(const int **cnids); 88# ifdef USE_CRYPTODEV_DIGESTS 89static int get_cryptodev_digests(const int **cnids); 90# endif 91static int cryptodev_usable_ciphers(const int **nids); 92static int cryptodev_usable_digests(const int **nids); 93static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 94 const unsigned char *in, size_t inl); 95static int cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, 96 const unsigned char *iv, int enc); 97static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx); 98static int cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher, 99 const int **nids, int nid); 100static int cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest, 101 const int **nids, int nid); 102static int bn2crparam(const BIGNUM *a, struct crparam *crp); 103static int crparam2bn(struct crparam *crp, BIGNUM *a); 104static void zapparams(struct crypt_kop *kop); 105static int cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, 106 int slen, BIGNUM *s); 107 108static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, 109 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, 110 BN_MONT_CTX *m_ctx); 111static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, 112 BN_CTX *ctx); 113static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, 114 BN_CTX *ctx); 115static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, 116 const BIGNUM *p, const BIGNUM *m, 117 BN_CTX *ctx, BN_MONT_CTX *m_ctx); 118static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g, 119 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, 120 BIGNUM *p, BN_CTX *ctx, 121 BN_MONT_CTX *mont); 122static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen, 123 DSA *dsa); 124static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len, 125 DSA_SIG *sig, DSA *dsa); 126static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a, 127 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, 128 BN_MONT_CTX *m_ctx); 129static int cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, 130 DH *dh); 131static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, 132 void (*f) (void)); 133void ENGINE_load_cryptodev(void); 134 135static const ENGINE_CMD_DEFN cryptodev_defns[] = { 136 {0, NULL, NULL, 0} 137}; 138 139static struct { 140 int id; 141 int nid; 142 int ivmax; 143 int keylen; 144} ciphers[] = { 145 { 146 CRYPTO_ARC4, NID_rc4, 0, 16, 147 }, 148 { 149 CRYPTO_DES_CBC, NID_des_cbc, 8, 8, 150 }, 151 { 152 CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24, 153 }, 154 { 155 CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16, 156 }, 157 { 158 CRYPTO_AES_CBC, NID_aes_192_cbc, 16, 24, 159 }, 160 { 161 CRYPTO_AES_CBC, NID_aes_256_cbc, 16, 32, 162 }, 163# ifdef CRYPTO_AES_CTR 164 { 165 CRYPTO_AES_CTR, NID_aes_128_ctr, 14, 16, 166 }, 167 { 168 CRYPTO_AES_CTR, NID_aes_192_ctr, 14, 24, 169 }, 170 { 171 CRYPTO_AES_CTR, NID_aes_256_ctr, 14, 32, 172 }, 173# endif 174 { 175 CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16, 176 }, 177 { 178 CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16, 179 }, 180 { 181 CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0, 182 }, 183 { 184 0, NID_undef, 0, 0, 185 }, 186}; 187 188# ifdef USE_CRYPTODEV_DIGESTS 189static struct { 190 int id; 191 int nid; 192 int keylen; 193} digests[] = { 194 { 195 CRYPTO_MD5_HMAC, NID_hmacWithMD5, 16 196 }, 197 { 198 CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, 20 199 }, 200 { 201 CRYPTO_RIPEMD160_HMAC, NID_ripemd160, 16 202 /* ? */ 203 }, 204 { 205 CRYPTO_MD5_KPDK, NID_undef, 0 206 }, 207 { 208 CRYPTO_SHA1_KPDK, NID_undef, 0 209 }, 210 { 211 CRYPTO_MD5, NID_md5, 16 212 }, 213 { 214 CRYPTO_SHA1, NID_sha1, 20 215 }, 216 { 217 0, NID_undef, 0 218 }, 219}; 220# endif 221 222/* 223 * Return a fd if /dev/crypto seems usable, 0 otherwise. 224 */ 225static int open_dev_crypto(void) 226{ 227 static int fd = -1; 228 229 if (fd == -1) { 230 if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1) 231 return (-1); 232 /* close on exec */ 233 if (fcntl(fd, F_SETFD, 1) == -1) { 234 close(fd); 235 fd = -1; 236 return (-1); 237 } 238 } 239 return (fd); 240} 241 242static int get_dev_crypto(void) 243{ 244 int fd, retfd; 245 246 if ((fd = open_dev_crypto()) == -1) 247 return (-1); 248# ifndef CRIOGET_NOT_NEEDED 249 if (ioctl(fd, CRIOGET, &retfd) == -1) 250 return (-1); 251 252 /* close on exec */ 253 if (fcntl(retfd, F_SETFD, 1) == -1) { 254 close(retfd); 255 return (-1); 256 } 257# else 258 retfd = fd; 259# endif 260 return (retfd); 261} 262 263static void put_dev_crypto(int fd) 264{ 265# ifndef CRIOGET_NOT_NEEDED 266 close(fd); 267# endif 268} 269 270/* Caching version for asym operations */ 271static int get_asym_dev_crypto(void) 272{ 273 static int fd = -1; 274 275 if (fd == -1) 276 fd = get_dev_crypto(); 277 return fd; 278} 279 280/* 281 * Find out what ciphers /dev/crypto will let us have a session for. 282 * XXX note, that some of these openssl doesn't deal with yet! 283 * returning them here is harmless, as long as we return NULL 284 * when asked for a handler in the cryptodev_engine_ciphers routine 285 */ 286static int get_cryptodev_ciphers(const int **cnids) 287{ 288 static int nids[CRYPTO_ALGORITHM_MAX]; 289 struct session_op sess; 290 int fd, i, count = 0; 291 292 if ((fd = get_dev_crypto()) < 0) { 293 *cnids = NULL; 294 return (0); 295 } 296 memset(&sess, 0, sizeof(sess)); 297 sess.key = (caddr_t) "123456789abcdefghijklmno"; 298 299 for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) { 300 if (ciphers[i].nid == NID_undef) 301 continue; 302 sess.cipher = ciphers[i].id; 303 sess.keylen = ciphers[i].keylen; 304 sess.mac = 0; 305 if (ioctl(fd, CIOCGSESSION, &sess) != -1 && 306 ioctl(fd, CIOCFSESSION, &sess.ses) != -1) 307 nids[count++] = ciphers[i].nid; 308 } 309 put_dev_crypto(fd); 310 311 if (count > 0) 312 *cnids = nids; 313 else 314 *cnids = NULL; 315 return (count); 316} 317 318# ifdef USE_CRYPTODEV_DIGESTS 319/* 320 * Find out what digests /dev/crypto will let us have a session for. 321 * XXX note, that some of these openssl doesn't deal with yet! 322 * returning them here is harmless, as long as we return NULL 323 * when asked for a handler in the cryptodev_engine_digests routine 324 */ 325static int get_cryptodev_digests(const int **cnids) 326{ 327 static int nids[CRYPTO_ALGORITHM_MAX]; 328 struct session_op sess; 329 int fd, i, count = 0; 330 331 if ((fd = get_dev_crypto()) < 0) { 332 *cnids = NULL; 333 return (0); 334 } 335 memset(&sess, 0, sizeof(sess)); 336 sess.mackey = (caddr_t) "123456789abcdefghijklmno"; 337 for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) { 338 if (digests[i].nid == NID_undef) 339 continue; 340 sess.mac = digests[i].id; 341 sess.mackeylen = digests[i].keylen; 342 sess.cipher = 0; 343 if (ioctl(fd, CIOCGSESSION, &sess) != -1 && 344 ioctl(fd, CIOCFSESSION, &sess.ses) != -1) 345 nids[count++] = digests[i].nid; 346 } 347 put_dev_crypto(fd); 348 349 if (count > 0) 350 *cnids = nids; 351 else 352 *cnids = NULL; 353 return (count); 354} 355# endif /* 0 */ 356 357/* 358 * Find the useable ciphers|digests from dev/crypto - this is the first 359 * thing called by the engine init crud which determines what it 360 * can use for ciphers from this engine. We want to return 361 * only what we can do, anythine else is handled by software. 362 * 363 * If we can't initialize the device to do anything useful for 364 * any reason, we want to return a NULL array, and 0 length, 365 * which forces everything to be done is software. By putting 366 * the initalization of the device in here, we ensure we can 367 * use this engine as the default, and if for whatever reason 368 * /dev/crypto won't do what we want it will just be done in 369 * software 370 * 371 * This can (should) be greatly expanded to perhaps take into 372 * account speed of the device, and what we want to do. 373 * (although the disabling of particular alg's could be controlled 374 * by the device driver with sysctl's.) - this is where we 375 * want most of the decisions made about what we actually want 376 * to use from /dev/crypto. 377 */ 378static int cryptodev_usable_ciphers(const int **nids) 379{ 380 return (get_cryptodev_ciphers(nids)); 381} 382 383static int cryptodev_usable_digests(const int **nids) 384{ 385# ifdef USE_CRYPTODEV_DIGESTS 386 return (get_cryptodev_digests(nids)); 387# else 388 /* 389 * XXXX just disable all digests for now, because it sucks. 390 * we need a better way to decide this - i.e. I may not 391 * want digests on slow cards like hifn on fast machines, 392 * but might want them on slow or loaded machines, etc. 393 * will also want them when using crypto cards that don't 394 * suck moose gonads - would be nice to be able to decide something 395 * as reasonable default without having hackery that's card dependent. 396 * of course, the default should probably be just do everything, 397 * with perhaps a sysctl to turn algoritms off (or have them off 398 * by default) on cards that generally suck like the hifn. 399 */ 400 *nids = NULL; 401 return (0); 402# endif 403} 404 405static int 406cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 407 const unsigned char *in, size_t inl) 408{ 409 struct crypt_op cryp; 410 struct dev_crypto_state *state = ctx->cipher_data; 411 struct session_op *sess = &state->d_sess; 412 const void *iiv; 413 unsigned char save_iv[EVP_MAX_IV_LENGTH]; 414 415 if (state->d_fd < 0) 416 return (0); 417 if (!inl) 418 return (1); 419 if ((inl % ctx->cipher->block_size) != 0) 420 return (0); 421 422 memset(&cryp, 0, sizeof(cryp)); 423 424 cryp.ses = sess->ses; 425 cryp.flags = 0; 426 cryp.len = inl; 427 cryp.src = (caddr_t) in; 428 cryp.dst = (caddr_t) out; 429 cryp.mac = 0; 430 431 cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT; 432 433 if (ctx->cipher->iv_len) { 434 cryp.iv = (caddr_t) ctx->iv; 435 if (!ctx->encrypt) { 436 iiv = in + inl - ctx->cipher->iv_len; 437 memcpy(save_iv, iiv, ctx->cipher->iv_len); 438 } 439 } else 440 cryp.iv = NULL; 441 442 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) { 443 /* 444 * XXX need better errror handling this can fail for a number of 445 * different reasons. 446 */ 447 return (0); 448 } 449 450 if (ctx->cipher->iv_len) { 451 if (ctx->encrypt) 452 iiv = out + inl - ctx->cipher->iv_len; 453 else 454 iiv = save_iv; 455 memcpy(ctx->iv, iiv, ctx->cipher->iv_len); 456 } 457 return (1); 458} 459 460static int 461cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, 462 const unsigned char *iv, int enc) 463{ 464 struct dev_crypto_state *state = ctx->cipher_data; 465 struct session_op *sess = &state->d_sess; 466 int cipher = -1, i; 467 468 for (i = 0; ciphers[i].id; i++) 469 if (ctx->cipher->nid == ciphers[i].nid && 470 ctx->cipher->iv_len <= ciphers[i].ivmax && 471 ctx->key_len == ciphers[i].keylen) { 472 cipher = ciphers[i].id; 473 break; 474 } 475 476 if (!ciphers[i].id) { 477 state->d_fd = -1; 478 return (0); 479 } 480 481 memset(sess, 0, sizeof(struct session_op)); 482 483 if ((state->d_fd = get_dev_crypto()) < 0) 484 return (0); 485 486 sess->key = (caddr_t) key; 487 sess->keylen = ctx->key_len; 488 sess->cipher = cipher; 489 490 if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) { 491 put_dev_crypto(state->d_fd); 492 state->d_fd = -1; 493 return (0); 494 } 495 return (1); 496} 497 498/* 499 * free anything we allocated earlier when initting a 500 * session, and close the session. 501 */ 502static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx) 503{ 504 int ret = 0; 505 struct dev_crypto_state *state = ctx->cipher_data; 506 struct session_op *sess = &state->d_sess; 507 508 if (state->d_fd < 0) 509 return (0); 510 511 /* 512 * XXX if this ioctl fails, someting's wrong. the invoker may have called 513 * us with a bogus ctx, or we could have a device that for whatever 514 * reason just doesn't want to play ball - it's not clear what's right 515 * here - should this be an error? should it just increase a counter, 516 * hmm. For right now, we return 0 - I don't believe that to be "right". 517 * we could call the gorpy openssl lib error handlers that print messages 518 * to users of the library. hmm.. 519 */ 520 521 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) { 522 ret = 0; 523 } else { 524 ret = 1; 525 } 526 put_dev_crypto(state->d_fd); 527 state->d_fd = -1; 528 529 return (ret); 530} 531 532/* 533 * libcrypto EVP stuff - this is how we get wired to EVP so the engine 534 * gets called when libcrypto requests a cipher NID. 535 */ 536 537/* RC4 */ 538const EVP_CIPHER cryptodev_rc4 = { 539 NID_rc4, 540 1, 16, 0, 541 EVP_CIPH_VARIABLE_LENGTH, 542 cryptodev_init_key, 543 cryptodev_cipher, 544 cryptodev_cleanup, 545 sizeof(struct dev_crypto_state), 546 NULL, 547 NULL, 548 NULL 549}; 550 551/* DES CBC EVP */ 552const EVP_CIPHER cryptodev_des_cbc = { 553 NID_des_cbc, 554 8, 8, 8, 555 EVP_CIPH_CBC_MODE, 556 cryptodev_init_key, 557 cryptodev_cipher, 558 cryptodev_cleanup, 559 sizeof(struct dev_crypto_state), 560 EVP_CIPHER_set_asn1_iv, 561 EVP_CIPHER_get_asn1_iv, 562 NULL 563}; 564 565/* 3DES CBC EVP */ 566const EVP_CIPHER cryptodev_3des_cbc = { 567 NID_des_ede3_cbc, 568 8, 24, 8, 569 EVP_CIPH_CBC_MODE, 570 cryptodev_init_key, 571 cryptodev_cipher, 572 cryptodev_cleanup, 573 sizeof(struct dev_crypto_state), 574 EVP_CIPHER_set_asn1_iv, 575 EVP_CIPHER_get_asn1_iv, 576 NULL 577}; 578 579const EVP_CIPHER cryptodev_bf_cbc = { 580 NID_bf_cbc, 581 8, 16, 8, 582 EVP_CIPH_CBC_MODE, 583 cryptodev_init_key, 584 cryptodev_cipher, 585 cryptodev_cleanup, 586 sizeof(struct dev_crypto_state), 587 EVP_CIPHER_set_asn1_iv, 588 EVP_CIPHER_get_asn1_iv, 589 NULL 590}; 591 592const EVP_CIPHER cryptodev_cast_cbc = { 593 NID_cast5_cbc, 594 8, 16, 8, 595 EVP_CIPH_CBC_MODE, 596 cryptodev_init_key, 597 cryptodev_cipher, 598 cryptodev_cleanup, 599 sizeof(struct dev_crypto_state), 600 EVP_CIPHER_set_asn1_iv, 601 EVP_CIPHER_get_asn1_iv, 602 NULL 603}; 604 605const EVP_CIPHER cryptodev_aes_cbc = { 606 NID_aes_128_cbc, 607 16, 16, 16, 608 EVP_CIPH_CBC_MODE, 609 cryptodev_init_key, 610 cryptodev_cipher, 611 cryptodev_cleanup, 612 sizeof(struct dev_crypto_state), 613 EVP_CIPHER_set_asn1_iv, 614 EVP_CIPHER_get_asn1_iv, 615 NULL 616}; 617 618const EVP_CIPHER cryptodev_aes_192_cbc = { 619 NID_aes_192_cbc, 620 16, 24, 16, 621 EVP_CIPH_CBC_MODE, 622 cryptodev_init_key, 623 cryptodev_cipher, 624 cryptodev_cleanup, 625 sizeof(struct dev_crypto_state), 626 EVP_CIPHER_set_asn1_iv, 627 EVP_CIPHER_get_asn1_iv, 628 NULL 629}; 630 631const EVP_CIPHER cryptodev_aes_256_cbc = { 632 NID_aes_256_cbc, 633 16, 32, 16, 634 EVP_CIPH_CBC_MODE, 635 cryptodev_init_key, 636 cryptodev_cipher, 637 cryptodev_cleanup, 638 sizeof(struct dev_crypto_state), 639 EVP_CIPHER_set_asn1_iv, 640 EVP_CIPHER_get_asn1_iv, 641 NULL 642}; 643 644# ifdef CRYPTO_AES_CTR 645const EVP_CIPHER cryptodev_aes_ctr = { 646 NID_aes_128_ctr, 647 16, 16, 14, 648 EVP_CIPH_CTR_MODE, 649 cryptodev_init_key, 650 cryptodev_cipher, 651 cryptodev_cleanup, 652 sizeof(struct dev_crypto_state), 653 EVP_CIPHER_set_asn1_iv, 654 EVP_CIPHER_get_asn1_iv, 655 NULL 656}; 657 658const EVP_CIPHER cryptodev_aes_ctr_192 = { 659 NID_aes_192_ctr, 660 16, 24, 14, 661 EVP_CIPH_CTR_MODE, 662 cryptodev_init_key, 663 cryptodev_cipher, 664 cryptodev_cleanup, 665 sizeof(struct dev_crypto_state), 666 EVP_CIPHER_set_asn1_iv, 667 EVP_CIPHER_get_asn1_iv, 668 NULL 669}; 670 671const EVP_CIPHER cryptodev_aes_ctr_256 = { 672 NID_aes_256_ctr, 673 16, 32, 14, 674 EVP_CIPH_CTR_MODE, 675 cryptodev_init_key, 676 cryptodev_cipher, 677 cryptodev_cleanup, 678 sizeof(struct dev_crypto_state), 679 EVP_CIPHER_set_asn1_iv, 680 EVP_CIPHER_get_asn1_iv, 681 NULL 682}; 683# endif 684/* 685 * Registered by the ENGINE when used to find out how to deal with 686 * a particular NID in the ENGINE. this says what we'll do at the 687 * top level - note, that list is restricted by what we answer with 688 */ 689static int 690cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher, 691 const int **nids, int nid) 692{ 693 if (!cipher) 694 return (cryptodev_usable_ciphers(nids)); 695 696 switch (nid) { 697 case NID_rc4: 698 *cipher = &cryptodev_rc4; 699 break; 700 case NID_des_ede3_cbc: 701 *cipher = &cryptodev_3des_cbc; 702 break; 703 case NID_des_cbc: 704 *cipher = &cryptodev_des_cbc; 705 break; 706 case NID_bf_cbc: 707 *cipher = &cryptodev_bf_cbc; 708 break; 709 case NID_cast5_cbc: 710 *cipher = &cryptodev_cast_cbc; 711 break; 712 case NID_aes_128_cbc: 713 *cipher = &cryptodev_aes_cbc; 714 break; 715 case NID_aes_192_cbc: 716 *cipher = &cryptodev_aes_192_cbc; 717 break; 718 case NID_aes_256_cbc: 719 *cipher = &cryptodev_aes_256_cbc; 720 break; 721# ifdef CRYPTO_AES_CTR 722 case NID_aes_128_ctr: 723 *cipher = &cryptodev_aes_ctr; 724 break; 725 case NID_aes_192_ctr: 726 *cipher = &cryptodev_aes_ctr_192; 727 break; 728 case NID_aes_256_ctr: 729 *cipher = &cryptodev_aes_ctr_256; 730 break; 731# endif 732 default: 733 *cipher = NULL; 734 break; 735 } 736 return (*cipher != NULL); 737} 738 739# ifdef USE_CRYPTODEV_DIGESTS 740 741/* convert digest type to cryptodev */ 742static int digest_nid_to_cryptodev(int nid) 743{ 744 int i; 745 746 for (i = 0; digests[i].id; i++) 747 if (digests[i].nid == nid) 748 return (digests[i].id); 749 return (0); 750} 751 752static int digest_key_length(int nid) 753{ 754 int i; 755 756 for (i = 0; digests[i].id; i++) 757 if (digests[i].nid == nid) 758 return digests[i].keylen; 759 return (0); 760} 761 762static int cryptodev_digest_init(EVP_MD_CTX *ctx) 763{ 764 struct dev_crypto_state *state = ctx->md_data; 765 struct session_op *sess = &state->d_sess; 766 int digest; 767 768 if ((digest = digest_nid_to_cryptodev(ctx->digest->type)) == NID_undef) { 769 printf("cryptodev_digest_init: Can't get digest \n"); 770 return (0); 771 } 772 773 memset(state, 0, sizeof(struct dev_crypto_state)); 774 775 if ((state->d_fd = get_dev_crypto()) < 0) { 776 printf("cryptodev_digest_init: Can't get Dev \n"); 777 return (0); 778 } 779 780 sess->mackey = state->dummy_mac_key; 781 sess->mackeylen = digest_key_length(ctx->digest->type); 782 sess->mac = digest; 783 784 if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) { 785 put_dev_crypto(state->d_fd); 786 state->d_fd = -1; 787 printf("cryptodev_digest_init: Open session failed\n"); 788 return (0); 789 } 790 791 return (1); 792} 793 794static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data, 795 size_t count) 796{ 797 struct crypt_op cryp; 798 struct dev_crypto_state *state = ctx->md_data; 799 struct session_op *sess = &state->d_sess; 800 801 if (!data || state->d_fd < 0) { 802 printf("cryptodev_digest_update: illegal inputs \n"); 803 return (0); 804 } 805 806 if (!count) { 807 return (0); 808 } 809 810 if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) { 811 /* if application doesn't support one buffer */ 812 state->mac_data = 813 OPENSSL_realloc(state->mac_data, state->mac_len + count); 814 815 if (!state->mac_data) { 816 printf("cryptodev_digest_update: realloc failed\n"); 817 return (0); 818 } 819 820 memcpy(state->mac_data + state->mac_len, data, count); 821 state->mac_len += count; 822 823 return (1); 824 } 825 826 memset(&cryp, 0, sizeof(cryp)); 827 828 cryp.ses = sess->ses; 829 cryp.flags = 0; 830 cryp.len = count; 831 cryp.src = (caddr_t) data; 832 cryp.dst = NULL; 833 cryp.mac = (caddr_t) state->digest_res; 834 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) { 835 printf("cryptodev_digest_update: digest failed\n"); 836 return (0); 837 } 838 return (1); 839} 840 841static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md) 842{ 843 struct crypt_op cryp; 844 struct dev_crypto_state *state = ctx->md_data; 845 struct session_op *sess = &state->d_sess; 846 847 int ret = 1; 848 849 if (!md || state->d_fd < 0) { 850 printf("cryptodev_digest_final: illegal input\n"); 851 return (0); 852 } 853 854 if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) { 855 /* if application doesn't support one buffer */ 856 memset(&cryp, 0, sizeof(cryp)); 857 cryp.ses = sess->ses; 858 cryp.flags = 0; 859 cryp.len = state->mac_len; 860 cryp.src = state->mac_data; 861 cryp.dst = NULL; 862 cryp.mac = (caddr_t) md; 863 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) { 864 printf("cryptodev_digest_final: digest failed\n"); 865 return (0); 866 } 867 868 return 1; 869 } 870 871 memcpy(md, state->digest_res, ctx->digest->md_size); 872 873 return (ret); 874} 875 876static int cryptodev_digest_cleanup(EVP_MD_CTX *ctx) 877{ 878 int ret = 1; 879 struct dev_crypto_state *state = ctx->md_data; 880 struct session_op *sess = &state->d_sess; 881 882 if (state == NULL) 883 return 0; 884 885 if (state->d_fd < 0) { 886 printf("cryptodev_digest_cleanup: illegal input\n"); 887 return (0); 888 } 889 890 if (state->mac_data) { 891 OPENSSL_free(state->mac_data); 892 state->mac_data = NULL; 893 state->mac_len = 0; 894 } 895 896 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) < 0) { 897 printf("cryptodev_digest_cleanup: failed to close session\n"); 898 ret = 0; 899 } else { 900 ret = 1; 901 } 902 put_dev_crypto(state->d_fd); 903 state->d_fd = -1; 904 905 return (ret); 906} 907 908static int cryptodev_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from) 909{ 910 struct dev_crypto_state *fstate = from->md_data; 911 struct dev_crypto_state *dstate = to->md_data; 912 struct session_op *sess; 913 int digest; 914 915 if (dstate == NULL || fstate == NULL) 916 return 1; 917 918 memcpy(dstate, fstate, sizeof(struct dev_crypto_state)); 919 920 sess = &dstate->d_sess; 921 922 digest = digest_nid_to_cryptodev(to->digest->type); 923 924 sess->mackey = dstate->dummy_mac_key; 925 sess->mackeylen = digest_key_length(to->digest->type); 926 sess->mac = digest; 927 928 dstate->d_fd = get_dev_crypto(); 929 930 if (ioctl(dstate->d_fd, CIOCGSESSION, sess) < 0) { 931 put_dev_crypto(dstate->d_fd); 932 dstate->d_fd = -1; 933 printf("cryptodev_digest_init: Open session failed\n"); 934 return (0); 935 } 936 937 if (fstate->mac_len != 0) { 938 if (fstate->mac_data != NULL) { 939 dstate->mac_data = OPENSSL_malloc(fstate->mac_len); 940 memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len); 941 dstate->mac_len = fstate->mac_len; 942 } 943 } 944 945 return 1; 946} 947 948const EVP_MD cryptodev_sha1 = { 949 NID_sha1, 950 NID_undef, 951 SHA_DIGEST_LENGTH, 952 EVP_MD_FLAG_ONESHOT, 953 cryptodev_digest_init, 954 cryptodev_digest_update, 955 cryptodev_digest_final, 956 cryptodev_digest_copy, 957 cryptodev_digest_cleanup, 958 EVP_PKEY_NULL_method, 959 SHA_CBLOCK, 960 sizeof(struct dev_crypto_state), 961}; 962 963const EVP_MD cryptodev_md5 = { 964 NID_md5, 965 NID_undef, 966 16 /* MD5_DIGEST_LENGTH */ , 967 EVP_MD_FLAG_ONESHOT, 968 cryptodev_digest_init, 969 cryptodev_digest_update, 970 cryptodev_digest_final, 971 cryptodev_digest_copy, 972 cryptodev_digest_cleanup, 973 EVP_PKEY_NULL_method, 974 64 /* MD5_CBLOCK */ , 975 sizeof(struct dev_crypto_state), 976}; 977 978# endif /* USE_CRYPTODEV_DIGESTS */ 979 980static int 981cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest, 982 const int **nids, int nid) 983{ 984 if (!digest) 985 return (cryptodev_usable_digests(nids)); 986 987 switch (nid) { 988# ifdef USE_CRYPTODEV_DIGESTS 989 case NID_md5: 990 *digest = &cryptodev_md5; 991 break; 992 case NID_sha1: 993 *digest = &cryptodev_sha1; 994 break; 995 default: 996# endif /* USE_CRYPTODEV_DIGESTS */ 997 *digest = NULL; 998 break; 999 } 1000 return (*digest != NULL); 1001} 1002 1003/* 1004 * Convert a BIGNUM to the representation that /dev/crypto needs. 1005 * Upon completion of use, the caller is responsible for freeing 1006 * crp->crp_p. 1007 */ 1008static int bn2crparam(const BIGNUM *a, struct crparam *crp) 1009{ 1010 int i, j, k; 1011 ssize_t bytes, bits; 1012 u_char *b; 1013 1014 crp->crp_p = NULL; 1015 crp->crp_nbits = 0; 1016 1017 bits = BN_num_bits(a); 1018 bytes = (bits + 7) / 8; 1019 1020 b = malloc(bytes); 1021 if (b == NULL) 1022 return (1); 1023 memset(b, 0, bytes); 1024 1025 crp->crp_p = (caddr_t) b; 1026 crp->crp_nbits = bits; 1027 1028 for (i = 0, j = 0; i < a->top; i++) { 1029 for (k = 0; k < BN_BITS2 / 8; k++) { 1030 if ((j + k) >= bytes) 1031 return (0); 1032 b[j + k] = a->d[i] >> (k * 8); 1033 } 1034 j += BN_BITS2 / 8; 1035 } 1036 return (0); 1037} 1038 1039/* Convert a /dev/crypto parameter to a BIGNUM */ 1040static int crparam2bn(struct crparam *crp, BIGNUM *a) 1041{ 1042 u_int8_t *pd; 1043 int i, bytes; 1044 1045 bytes = (crp->crp_nbits + 7) / 8; 1046 1047 if (bytes == 0) 1048 return (-1); 1049 1050 if ((pd = (u_int8_t *) malloc(bytes)) == NULL) 1051 return (-1); 1052 1053 for (i = 0; i < bytes; i++) 1054 pd[i] = crp->crp_p[bytes - i - 1]; 1055 1056 BN_bin2bn(pd, bytes, a); 1057 free(pd); 1058 1059 return (0); 1060} 1061 1062static void zapparams(struct crypt_kop *kop) 1063{ 1064 int i; 1065 1066 for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) { 1067 if (kop->crk_param[i].crp_p) 1068 free(kop->crk_param[i].crp_p); 1069 kop->crk_param[i].crp_p = NULL; 1070 kop->crk_param[i].crp_nbits = 0; 1071 } 1072} 1073 1074static int 1075cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen, 1076 BIGNUM *s) 1077{ 1078 int fd, ret = -1; 1079 1080 if ((fd = get_asym_dev_crypto()) < 0) 1081 return (ret); 1082 1083 if (r) { 1084 kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char)); 1085 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8; 1086 kop->crk_oparams++; 1087 } 1088 if (s) { 1089 kop->crk_param[kop->crk_iparams + 1].crp_p = 1090 calloc(slen, sizeof(char)); 1091 kop->crk_param[kop->crk_iparams + 1].crp_nbits = slen * 8; 1092 kop->crk_oparams++; 1093 } 1094 1095 if (ioctl(fd, CIOCKEY, kop) == 0) { 1096 if (r) 1097 crparam2bn(&kop->crk_param[kop->crk_iparams], r); 1098 if (s) 1099 crparam2bn(&kop->crk_param[kop->crk_iparams + 1], s); 1100 ret = 0; 1101 } 1102 1103 return (ret); 1104} 1105 1106static int 1107cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 1108 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont) 1109{ 1110 struct crypt_kop kop; 1111 int ret = 1; 1112 1113 /* 1114 * Currently, we know we can do mod exp iff we can do any asymmetric 1115 * operations at all. 1116 */ 1117 if (cryptodev_asymfeat == 0) { 1118 ret = BN_mod_exp(r, a, p, m, ctx); 1119 return (ret); 1120 } 1121 1122 memset(&kop, 0, sizeof kop); 1123 kop.crk_op = CRK_MOD_EXP; 1124 1125 /* inputs: a^p % m */ 1126 if (bn2crparam(a, &kop.crk_param[0])) 1127 goto err; 1128 if (bn2crparam(p, &kop.crk_param[1])) 1129 goto err; 1130 if (bn2crparam(m, &kop.crk_param[2])) 1131 goto err; 1132 kop.crk_iparams = 3; 1133 1134 if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL)) { 1135 const RSA_METHOD *meth = RSA_PKCS1_SSLeay(); 1136 printf("OCF asym process failed, Running in software\n"); 1137 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont); 1138 1139 } else if (ECANCELED == kop.crk_status) { 1140 const RSA_METHOD *meth = RSA_PKCS1_SSLeay(); 1141 printf("OCF hardware operation cancelled. Running in Software\n"); 1142 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont); 1143 } 1144 /* else cryptodev operation worked ok ==> ret = 1 */ 1145 1146 err: 1147 zapparams(&kop); 1148 return (ret); 1149} 1150 1151static int 1152cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, 1153 BN_CTX *ctx) 1154{ 1155 int r; 1156 ctx = BN_CTX_new(); 1157 r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL); 1158 BN_CTX_free(ctx); 1159 return (r); 1160} 1161 1162static int 1163cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) 1164{ 1165 struct crypt_kop kop; 1166 int ret = 1; 1167 1168 if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) { 1169 /* XXX 0 means failure?? */ 1170 return (0); 1171 } 1172 1173 memset(&kop, 0, sizeof kop); 1174 kop.crk_op = CRK_MOD_EXP_CRT; 1175 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */ 1176 if (bn2crparam(rsa->p, &kop.crk_param[0])) 1177 goto err; 1178 if (bn2crparam(rsa->q, &kop.crk_param[1])) 1179 goto err; 1180 if (bn2crparam(I, &kop.crk_param[2])) 1181 goto err; 1182 if (bn2crparam(rsa->dmp1, &kop.crk_param[3])) 1183 goto err; 1184 if (bn2crparam(rsa->dmq1, &kop.crk_param[4])) 1185 goto err; 1186 if (bn2crparam(rsa->iqmp, &kop.crk_param[5])) 1187 goto err; 1188 kop.crk_iparams = 6; 1189 1190 if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL)) { 1191 const RSA_METHOD *meth = RSA_PKCS1_SSLeay(); 1192 printf("OCF asym process failed, running in Software\n"); 1193 ret = (*meth->rsa_mod_exp) (r0, I, rsa, ctx); 1194 1195 } else if (ECANCELED == kop.crk_status) { 1196 const RSA_METHOD *meth = RSA_PKCS1_SSLeay(); 1197 printf("OCF hardware operation cancelled. Running in Software\n"); 1198 ret = (*meth->rsa_mod_exp) (r0, I, rsa, ctx); 1199 } 1200 /* else cryptodev operation worked ok ==> ret = 1 */ 1201 1202 err: 1203 zapparams(&kop); 1204 return (ret); 1205} 1206 1207static RSA_METHOD cryptodev_rsa = { 1208 "cryptodev RSA method", 1209 NULL, /* rsa_pub_enc */ 1210 NULL, /* rsa_pub_dec */ 1211 NULL, /* rsa_priv_enc */ 1212 NULL, /* rsa_priv_dec */ 1213 NULL, 1214 NULL, 1215 NULL, /* init */ 1216 NULL, /* finish */ 1217 0, /* flags */ 1218 NULL, /* app_data */ 1219 NULL, /* rsa_sign */ 1220 NULL /* rsa_verify */ 1221}; 1222 1223static int 1224cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p, 1225 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) 1226{ 1227 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx)); 1228} 1229 1230static int 1231cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g, 1232 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p, 1233 BN_CTX *ctx, BN_MONT_CTX *mont) 1234{ 1235 BIGNUM t2; 1236 int ret = 0; 1237 1238 BN_init(&t2); 1239 1240 /* v = ( g^u1 * y^u2 mod p ) mod q */ 1241 /* let t1 = g ^ u1 mod p */ 1242 ret = 0; 1243 1244 if (!dsa->meth->bn_mod_exp(dsa, t1, dsa->g, u1, dsa->p, ctx, mont)) 1245 goto err; 1246 1247 /* let t2 = y ^ u2 mod p */ 1248 if (!dsa->meth->bn_mod_exp(dsa, &t2, dsa->pub_key, u2, dsa->p, ctx, mont)) 1249 goto err; 1250 /* let u1 = t1 * t2 mod p */ 1251 if (!BN_mod_mul(u1, t1, &t2, dsa->p, ctx)) 1252 goto err; 1253 1254 BN_copy(t1, u1); 1255 1256 ret = 1; 1257 err: 1258 BN_free(&t2); 1259 return (ret); 1260} 1261 1262static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen, 1263 DSA *dsa) 1264{ 1265 struct crypt_kop kop; 1266 BIGNUM *r = NULL, *s = NULL; 1267 DSA_SIG *dsaret = NULL; 1268 1269 if ((r = BN_new()) == NULL) 1270 goto err; 1271 if ((s = BN_new()) == NULL) { 1272 BN_free(r); 1273 goto err; 1274 } 1275 1276 memset(&kop, 0, sizeof kop); 1277 kop.crk_op = CRK_DSA_SIGN; 1278 1279 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */ 1280 kop.crk_param[0].crp_p = (caddr_t) dgst; 1281 kop.crk_param[0].crp_nbits = dlen * 8; 1282 if (bn2crparam(dsa->p, &kop.crk_param[1])) 1283 goto err; 1284 if (bn2crparam(dsa->q, &kop.crk_param[2])) 1285 goto err; 1286 if (bn2crparam(dsa->g, &kop.crk_param[3])) 1287 goto err; 1288 if (bn2crparam(dsa->priv_key, &kop.crk_param[4])) 1289 goto err; 1290 kop.crk_iparams = 5; 1291 1292 if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r, 1293 BN_num_bytes(dsa->q), s) == 0) { 1294 dsaret = DSA_SIG_new(); 1295 if (dsaret == NULL) 1296 goto err; 1297 dsaret->r = r; 1298 dsaret->s = s; 1299 r = s = NULL; 1300 } else { 1301 const DSA_METHOD *meth = DSA_OpenSSL(); 1302 dsaret = (meth->dsa_do_sign) (dgst, dlen, dsa); 1303 } 1304 err: 1305 BN_free(r); 1306 BN_free(s); 1307 kop.crk_param[0].crp_p = NULL; 1308 zapparams(&kop); 1309 return (dsaret); 1310} 1311 1312static int 1313cryptodev_dsa_verify(const unsigned char *dgst, int dlen, 1314 DSA_SIG *sig, DSA *dsa) 1315{ 1316 struct crypt_kop kop; 1317 int dsaret = 1; 1318 1319 memset(&kop, 0, sizeof kop); 1320 kop.crk_op = CRK_DSA_VERIFY; 1321 1322 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */ 1323 kop.crk_param[0].crp_p = (caddr_t) dgst; 1324 kop.crk_param[0].crp_nbits = dlen * 8; 1325 if (bn2crparam(dsa->p, &kop.crk_param[1])) 1326 goto err; 1327 if (bn2crparam(dsa->q, &kop.crk_param[2])) 1328 goto err; 1329 if (bn2crparam(dsa->g, &kop.crk_param[3])) 1330 goto err; 1331 if (bn2crparam(dsa->pub_key, &kop.crk_param[4])) 1332 goto err; 1333 if (bn2crparam(sig->r, &kop.crk_param[5])) 1334 goto err; 1335 if (bn2crparam(sig->s, &kop.crk_param[6])) 1336 goto err; 1337 kop.crk_iparams = 7; 1338 1339 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) { 1340 /* 1341 * OCF success value is 0, if not zero, change dsaret to fail 1342 */ 1343 if (0 != kop.crk_status) 1344 dsaret = 0; 1345 } else { 1346 const DSA_METHOD *meth = DSA_OpenSSL(); 1347 1348 dsaret = (meth->dsa_do_verify) (dgst, dlen, sig, dsa); 1349 } 1350 err: 1351 kop.crk_param[0].crp_p = NULL; 1352 zapparams(&kop); 1353 return (dsaret); 1354} 1355 1356static DSA_METHOD cryptodev_dsa = { 1357 "cryptodev DSA method", 1358 NULL, 1359 NULL, /* dsa_sign_setup */ 1360 NULL, 1361 NULL, /* dsa_mod_exp */ 1362 NULL, 1363 NULL, /* init */ 1364 NULL, /* finish */ 1365 0, /* flags */ 1366 NULL /* app_data */ 1367}; 1368 1369static int 1370cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a, 1371 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, 1372 BN_MONT_CTX *m_ctx) 1373{ 1374 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx)); 1375} 1376 1377static int 1378cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) 1379{ 1380 struct crypt_kop kop; 1381 int dhret = 1; 1382 int fd, keylen; 1383 1384 if ((fd = get_asym_dev_crypto()) < 0) { 1385 const DH_METHOD *meth = DH_OpenSSL(); 1386 1387 return ((meth->compute_key) (key, pub_key, dh)); 1388 } 1389 1390 keylen = BN_num_bits(dh->p); 1391 1392 memset(&kop, 0, sizeof kop); 1393 kop.crk_op = CRK_DH_COMPUTE_KEY; 1394 1395 /* inputs: dh->priv_key pub_key dh->p key */ 1396 if (bn2crparam(dh->priv_key, &kop.crk_param[0])) 1397 goto err; 1398 if (bn2crparam(pub_key, &kop.crk_param[1])) 1399 goto err; 1400 if (bn2crparam(dh->p, &kop.crk_param[2])) 1401 goto err; 1402 kop.crk_iparams = 3; 1403 1404 kop.crk_param[3].crp_p = (caddr_t) key; 1405 kop.crk_param[3].crp_nbits = keylen * 8; 1406 kop.crk_oparams = 1; 1407 1408 if (ioctl(fd, CIOCKEY, &kop) == -1) { 1409 const DH_METHOD *meth = DH_OpenSSL(); 1410 1411 dhret = (meth->compute_key) (key, pub_key, dh); 1412 } 1413 err: 1414 kop.crk_param[3].crp_p = NULL; 1415 zapparams(&kop); 1416 return (dhret); 1417} 1418 1419static DH_METHOD cryptodev_dh = { 1420 "cryptodev DH method", 1421 NULL, /* cryptodev_dh_generate_key */ 1422 NULL, 1423 NULL, 1424 NULL, 1425 NULL, 1426 0, /* flags */ 1427 NULL /* app_data */ 1428}; 1429 1430/* 1431 * ctrl right now is just a wrapper that doesn't do much 1432 * but I expect we'll want some options soon. 1433 */ 1434static int 1435cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void)) 1436{ 1437# ifdef HAVE_SYSLOG_R 1438 struct syslog_data sd = SYSLOG_DATA_INIT; 1439# endif 1440 1441 switch (cmd) { 1442 default: 1443# ifdef HAVE_SYSLOG_R 1444 syslog_r(LOG_ERR, &sd, "cryptodev_ctrl: unknown command %d", cmd); 1445# else 1446 syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd); 1447# endif 1448 break; 1449 } 1450 return (1); 1451} 1452 1453void ENGINE_load_cryptodev(void) 1454{ 1455 ENGINE *engine = ENGINE_new(); 1456 int fd; 1457 1458 if (engine == NULL) 1459 return; 1460 if ((fd = get_dev_crypto()) < 0) { 1461 ENGINE_free(engine); 1462 return; 1463 } 1464 1465 /* 1466 * find out what asymmetric crypto algorithms we support 1467 */ 1468 if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) { 1469 put_dev_crypto(fd); 1470 ENGINE_free(engine); 1471 return; 1472 } 1473 put_dev_crypto(fd); 1474 1475 if (!ENGINE_set_id(engine, "cryptodev") || 1476 !ENGINE_set_name(engine, "BSD cryptodev engine") || 1477 !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) || 1478 !ENGINE_set_digests(engine, cryptodev_engine_digests) || 1479 !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) || 1480 !ENGINE_set_cmd_defns(engine, cryptodev_defns)) { 1481 ENGINE_free(engine); 1482 return; 1483 } 1484 1485 if (ENGINE_set_RSA(engine, &cryptodev_rsa)) { 1486 const RSA_METHOD *rsa_meth = RSA_PKCS1_SSLeay(); 1487 1488 cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp; 1489 cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp; 1490 cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc; 1491 cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec; 1492 cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc; 1493 cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec; 1494 if (cryptodev_asymfeat & CRF_MOD_EXP) { 1495 cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp; 1496 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT) 1497 cryptodev_rsa.rsa_mod_exp = cryptodev_rsa_mod_exp; 1498 else 1499 cryptodev_rsa.rsa_mod_exp = cryptodev_rsa_nocrt_mod_exp; 1500 } 1501 } 1502 1503 if (ENGINE_set_DSA(engine, &cryptodev_dsa)) { 1504 const DSA_METHOD *meth = DSA_OpenSSL(); 1505 1506 memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD)); 1507 if (cryptodev_asymfeat & CRF_DSA_SIGN) 1508 cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign; 1509 if (cryptodev_asymfeat & CRF_MOD_EXP) { 1510 cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp; 1511 cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp; 1512 } 1513 if (cryptodev_asymfeat & CRF_DSA_VERIFY) 1514 cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify; 1515 } 1516 1517 if (ENGINE_set_DH(engine, &cryptodev_dh)) { 1518 const DH_METHOD *dh_meth = DH_OpenSSL(); 1519 1520 cryptodev_dh.generate_key = dh_meth->generate_key; 1521 cryptodev_dh.compute_key = dh_meth->compute_key; 1522 cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp; 1523 if (cryptodev_asymfeat & CRF_MOD_EXP) { 1524 cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh; 1525 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY) 1526 cryptodev_dh.compute_key = cryptodev_dh_compute_key; 1527 } 1528 } 1529 1530 ENGINE_add(engine); 1531 ENGINE_free(engine); 1532 ERR_clear_error(); 1533} 1534 1535#endif /* HAVE_CRYPTODEV */ 1536