diff options
| author | deraadt <> | 2002-06-19 02:37:16 +0000 |
|---|---|---|
| committer | deraadt <> | 2002-06-19 02:37:16 +0000 |
| commit | 3f0857c77df4ed44541226bd4f586edfe44b499b (patch) | |
| tree | 575c7b623ad86bd6fad0a9e5da0ac9bc89da5e7a /src/lib/libcrypto/engine/hw_cryptodev.c | |
| parent | 82d47451abfdc1a8cff7b01169c03445fcc7756a (diff) | |
| download | openbsd-3f0857c77df4ed44541226bd4f586edfe44b499b.tar.gz openbsd-3f0857c77df4ed44541226bd4f586edfe44b499b.tar.bz2 openbsd-3f0857c77df4ed44541226bd4f586edfe44b499b.zip | |
KNF, -Wall, and other cleanups. still does not failover 100% correctly
for operations when /dev/crypto is missing, for instance in chroot
Diffstat (limited to 'src/lib/libcrypto/engine/hw_cryptodev.c')
| -rw-r--r-- | src/lib/libcrypto/engine/hw_cryptodev.c | 74 |
1 files changed, 52 insertions, 22 deletions
diff --git a/src/lib/libcrypto/engine/hw_cryptodev.c b/src/lib/libcrypto/engine/hw_cryptodev.c index f2d43d55e5..98d690abd0 100644 --- a/src/lib/libcrypto/engine/hw_cryptodev.c +++ b/src/lib/libcrypto/engine/hw_cryptodev.c | |||
| @@ -52,15 +52,41 @@ struct dev_crypto_state { | |||
| 52 | 52 | ||
| 53 | static u_int32_t cryptodev_asymfeat = 0; | 53 | static u_int32_t cryptodev_asymfeat = 0; |
| 54 | 54 | ||
| 55 | static int get_asym_dev_crypto(void); | ||
| 56 | static int open_dev_crypto(void); | ||
| 57 | static int get_dev_crypto(void); | ||
| 58 | static int cryptodev_max_iv(int cipher); | ||
| 59 | static int cryptodev_key_length_valid(int cipher, int len); | ||
| 60 | static int cipher_nid_to_cryptodev(int nid); | ||
| 61 | static int get_cryptodev_ciphers(const int **cnids); | ||
| 62 | static int get_cryptodev_digests(const int **cnids); | ||
| 63 | static int cryptodev_usable_ciphers(const int **nids); | ||
| 64 | static int cryptodev_usable_digests(const int **nids); | ||
| 65 | static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | ||
| 66 | const unsigned char *in, unsigned int inl); | ||
| 67 | static int cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | ||
| 68 | const unsigned char *iv, int enc); | ||
| 69 | static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx); | ||
| 70 | static int cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher, | ||
| 71 | const int **nids, int nid); | ||
| 72 | static int cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest, | ||
| 73 | const int **nids, int nid); | ||
| 55 | static int bn2crparam(const BIGNUM *a, struct crparam *crp); | 74 | static int bn2crparam(const BIGNUM *a, struct crparam *crp); |
| 56 | static int crparam2bn(struct crparam *crp, BIGNUM *a); | 75 | static int crparam2bn(struct crparam *crp, BIGNUM *a); |
| 57 | static void zapparams(struct crypt_kop *kop); | 76 | static void zapparams(struct crypt_kop *kop); |
| 77 | static int cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, | ||
| 78 | int slen, BIGNUM *s); | ||
| 58 | 79 | ||
| 59 | static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa); | ||
| 60 | static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, | 80 | static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, |
| 61 | const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); | 81 | const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); |
| 82 | static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, | ||
| 83 | RSA *rsa); | ||
| 84 | static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa); | ||
| 62 | static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, | 85 | static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, |
| 63 | const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); | 86 | const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); |
| 87 | static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g, | ||
| 88 | BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p, | ||
| 89 | BN_CTX *ctx, BN_MONT_CTX *mont); | ||
| 64 | static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, | 90 | static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst, |
| 65 | int dlen, DSA *dsa); | 91 | int dlen, DSA *dsa); |
| 66 | static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len, | 92 | static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len, |
| @@ -70,6 +96,9 @@ static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a, | |||
| 70 | BN_MONT_CTX *m_ctx); | 96 | BN_MONT_CTX *m_ctx); |
| 71 | static int cryptodev_dh_compute_key(unsigned char *key, | 97 | static int cryptodev_dh_compute_key(unsigned char *key, |
| 72 | const BIGNUM *pub_key, DH *dh); | 98 | const BIGNUM *pub_key, DH *dh); |
| 99 | static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, | ||
| 100 | void (*f)()); | ||
| 101 | void ENGINE_load_cryptodev(void); | ||
| 73 | 102 | ||
| 74 | static const ENGINE_CMD_DEFN cryptodev_defns[] = { | 103 | static const ENGINE_CMD_DEFN cryptodev_defns[] = { |
| 75 | {ENGINE_CMD_BASE, | 104 | {ENGINE_CMD_BASE, |
| @@ -112,7 +141,7 @@ static struct { | |||
| 112 | * Return a fd if /dev/crypto seems usable, 0 otherwise. | 141 | * Return a fd if /dev/crypto seems usable, 0 otherwise. |
| 113 | */ | 142 | */ |
| 114 | static int | 143 | static int |
| 115 | open_dev_crypto() | 144 | open_dev_crypto(void) |
| 116 | { | 145 | { |
| 117 | static int fd = -1; | 146 | static int fd = -1; |
| 118 | 147 | ||
| @@ -130,7 +159,7 @@ open_dev_crypto() | |||
| 130 | } | 159 | } |
| 131 | 160 | ||
| 132 | static int | 161 | static int |
| 133 | get_dev_crypto() | 162 | get_dev_crypto(void) |
| 134 | { | 163 | { |
| 135 | int fd, retfd; | 164 | int fd, retfd; |
| 136 | 165 | ||
| @@ -149,7 +178,7 @@ get_dev_crypto() | |||
| 149 | 178 | ||
| 150 | /* Caching version for asym operations */ | 179 | /* Caching version for asym operations */ |
| 151 | static int | 180 | static int |
| 152 | get_asym_dev_crypto() | 181 | get_asym_dev_crypto(void) |
| 153 | { | 182 | { |
| 154 | static int fd = -1; | 183 | static int fd = -1; |
| 155 | 184 | ||
| @@ -298,13 +327,13 @@ get_cryptodev_digests(const int **cnids) | |||
| 298 | * want most of the decisions made about what we actually want | 327 | * want most of the decisions made about what we actually want |
| 299 | * to use from /dev/crypto. | 328 | * to use from /dev/crypto. |
| 300 | */ | 329 | */ |
| 301 | int | 330 | static int |
| 302 | cryptodev_usable_ciphers(const int **nids) | 331 | cryptodev_usable_ciphers(const int **nids) |
| 303 | { | 332 | { |
| 304 | return (get_cryptodev_ciphers(nids)); | 333 | return (get_cryptodev_ciphers(nids)); |
| 305 | } | 334 | } |
| 306 | 335 | ||
| 307 | int | 336 | static int |
| 308 | cryptodev_usable_digests(const int **nids) | 337 | cryptodev_usable_digests(const int **nids) |
| 309 | { | 338 | { |
| 310 | /* | 339 | /* |
| @@ -323,7 +352,7 @@ cryptodev_usable_digests(const int **nids) | |||
| 323 | return (0); | 352 | return (0); |
| 324 | } | 353 | } |
| 325 | 354 | ||
| 326 | int | 355 | static int |
| 327 | cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 356 | cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
| 328 | const unsigned char *in, unsigned int inl) | 357 | const unsigned char *in, unsigned int inl) |
| 329 | { | 358 | { |
| @@ -379,7 +408,7 @@ cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | |||
| 379 | return (1); | 408 | return (1); |
| 380 | } | 409 | } |
| 381 | 410 | ||
| 382 | int | 411 | static int |
| 383 | cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | 412 | cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
| 384 | const unsigned char *iv, int enc) | 413 | const unsigned char *iv, int enc) |
| 385 | { | 414 | { |
| @@ -420,7 +449,7 @@ cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | |||
| 420 | * free anything we allocated earlier when initting a | 449 | * free anything we allocated earlier when initting a |
| 421 | * session, and close the session. | 450 | * session, and close the session. |
| 422 | */ | 451 | */ |
| 423 | int | 452 | static int |
| 424 | cryptodev_cleanup(EVP_CIPHER_CTX *ctx) | 453 | cryptodev_cleanup(EVP_CIPHER_CTX *ctx) |
| 425 | { | 454 | { |
| 426 | int ret = 0; | 455 | int ret = 0; |
| @@ -545,7 +574,7 @@ const EVP_CIPHER cryptodev_aes_cbc = { | |||
| 545 | * a particular NID in the ENGINE. this says what we'll do at the | 574 | * a particular NID in the ENGINE. this says what we'll do at the |
| 546 | * top level - note, that list is restricted by what we answer with | 575 | * top level - note, that list is restricted by what we answer with |
| 547 | */ | 576 | */ |
| 548 | int | 577 | static int |
| 549 | cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher, | 578 | cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher, |
| 550 | const int **nids, int nid) | 579 | const int **nids, int nid) |
| 551 | { | 580 | { |
| @@ -578,7 +607,7 @@ cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher, | |||
| 578 | return (*cipher != NULL); | 607 | return (*cipher != NULL); |
| 579 | } | 608 | } |
| 580 | 609 | ||
| 581 | int | 610 | static int |
| 582 | cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest, | 611 | cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest, |
| 583 | const int **nids, int nid) | 612 | const int **nids, int nid) |
| 584 | { | 613 | { |
| @@ -596,7 +625,6 @@ cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest, | |||
| 596 | return (*digest != NULL); | 625 | return (*digest != NULL); |
| 597 | } | 626 | } |
| 598 | 627 | ||
| 599 | |||
| 600 | /* | 628 | /* |
| 601 | * Convert a BIGNUM to the representation that /dev/crypto needs. | 629 | * Convert a BIGNUM to the representation that /dev/crypto needs. |
| 602 | * Upon completion of use, the caller is responsible for freeing | 630 | * Upon completion of use, the caller is responsible for freeing |
| @@ -671,7 +699,7 @@ zapparams(struct crypt_kop *kop) | |||
| 671 | } | 699 | } |
| 672 | 700 | ||
| 673 | static int | 701 | static int |
| 674 | cryptodev_sym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen, BIGNUM *s) | 702 | cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen, BIGNUM *s) |
| 675 | { | 703 | { |
| 676 | int fd, ret = -1; | 704 | int fd, ret = -1; |
| 677 | 705 | ||
| @@ -727,7 +755,7 @@ cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | |||
| 727 | goto err; | 755 | goto err; |
| 728 | kop.crk_iparams = 3; | 756 | kop.crk_iparams = 3; |
| 729 | 757 | ||
| 730 | if (cryptodev_sym(&kop, BN_num_bytes(m), r, 0, NULL) == -1) { | 758 | if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL) == -1) { |
| 731 | const RSA_METHOD *meth = RSA_PKCS1_SSLeay(); | 759 | const RSA_METHOD *meth = RSA_PKCS1_SSLeay(); |
| 732 | ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont); | 760 | ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont); |
| 733 | } | 761 | } |
| @@ -776,7 +804,7 @@ cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa) | |||
| 776 | goto err; | 804 | goto err; |
| 777 | kop.crk_iparams = 6; | 805 | kop.crk_iparams = 6; |
| 778 | 806 | ||
| 779 | if (cryptodev_sym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL) == -1) { | 807 | if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL) == -1) { |
| 780 | const RSA_METHOD *meth = RSA_PKCS1_SSLeay(); | 808 | const RSA_METHOD *meth = RSA_PKCS1_SSLeay(); |
| 781 | ret = (*meth->rsa_mod_exp)(r0, I, rsa); | 809 | ret = (*meth->rsa_mod_exp)(r0, I, rsa); |
| 782 | } | 810 | } |
| @@ -871,7 +899,7 @@ cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa) | |||
| 871 | goto err; | 899 | goto err; |
| 872 | kop.crk_iparams = 5; | 900 | kop.crk_iparams = 5; |
| 873 | 901 | ||
| 874 | if (cryptodev_sym(&kop, BN_num_bytes(dsa->q), r, | 902 | if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r, |
| 875 | BN_num_bytes(dsa->q), s) == 0) { | 903 | BN_num_bytes(dsa->q), s) == 0) { |
| 876 | dsaret = DSA_SIG_new(); | 904 | dsaret = DSA_SIG_new(); |
| 877 | dsaret->r = r; | 905 | dsaret->r = r; |
| @@ -915,7 +943,7 @@ cryptodev_dsa_verify(const unsigned char *dgst, int dlen, | |||
| 915 | goto err; | 943 | goto err; |
| 916 | kop.crk_iparams = 7; | 944 | kop.crk_iparams = 7; |
| 917 | 945 | ||
| 918 | if (cryptodev_sym(&kop, 0, NULL, 0, NULL) == 0) { | 946 | if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) { |
| 919 | dsaret = kop.crk_status; | 947 | dsaret = kop.crk_status; |
| 920 | } else { | 948 | } else { |
| 921 | const DSA_METHOD *meth = DSA_OpenSSL(); | 949 | const DSA_METHOD *meth = DSA_OpenSSL(); |
| @@ -928,7 +956,6 @@ err: | |||
| 928 | return (dsaret); | 956 | return (dsaret); |
| 929 | } | 957 | } |
| 930 | 958 | ||
| 931 | |||
| 932 | static DSA_METHOD cryptodev_dsa = { | 959 | static DSA_METHOD cryptodev_dsa = { |
| 933 | "cryptodev DSA method", | 960 | "cryptodev DSA method", |
| 934 | NULL, | 961 | NULL, |
| @@ -957,8 +984,11 @@ cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) | |||
| 957 | int dhret = 1; | 984 | int dhret = 1; |
| 958 | int fd, keylen; | 985 | int fd, keylen; |
| 959 | 986 | ||
| 960 | if ((fd = get_asym_dev_crypto()) < 0) | 987 | if ((fd = get_asym_dev_crypto()) < 0) { |
| 961 | return (-1); | 988 | const DH_METHOD *meth = DH_OpenSSL(); |
| 989 | |||
| 990 | return ((meth->compute_key)(key, pub_key, dh)); | ||
| 991 | } | ||
| 962 | 992 | ||
| 963 | keylen = BN_num_bits(dh->p); | 993 | keylen = BN_num_bits(dh->p); |
| 964 | 994 | ||
| @@ -1076,11 +1106,11 @@ ENGINE_load_cryptodev(void) | |||
| 1076 | memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD)); | 1106 | memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD)); |
| 1077 | if (cryptodev_asymfeat & CRF_DSA_SIGN) | 1107 | if (cryptodev_asymfeat & CRF_DSA_SIGN) |
| 1078 | cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign; | 1108 | cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign; |
| 1079 | if (cryptodev_asymfeat & CRF_MOD_EXP) { | 1109 | if (cryptodev_asymfeat & CRF_MOD_EXP) { |
| 1080 | cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp; | 1110 | cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp; |
| 1081 | cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp; | 1111 | cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp; |
| 1082 | } | 1112 | } |
| 1083 | if (cryptodev_asymfeat & CRF_DSA_VERIFY) | 1113 | if (cryptodev_asymfeat & CRF_DSA_VERIFY) |
| 1084 | cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify; | 1114 | cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify; |
| 1085 | } | 1115 | } |
| 1086 | 1116 | ||
