diff options
| author | beck <> | 2000-12-15 02:58:47 +0000 |
|---|---|---|
| committer | beck <> | 2000-12-15 02:58:47 +0000 |
| commit | 9200bb13d15da4b2a23e6bc92c20e95b74aa2113 (patch) | |
| tree | 5c52d628ec1e34be76e7ef2a4235d248b7c44d24 /src/lib/libcrypto/rsa/rsa_lib.c | |
| parent | e131d25072e3d4197ba4b9bcc0d1b27d34d6488d (diff) | |
| download | openbsd-9200bb13d15da4b2a23e6bc92c20e95b74aa2113.tar.gz openbsd-9200bb13d15da4b2a23e6bc92c20e95b74aa2113.tar.bz2 openbsd-9200bb13d15da4b2a23e6bc92c20e95b74aa2113.zip | |
openssl-engine-0.9.6 merge
Diffstat (limited to 'src/lib/libcrypto/rsa/rsa_lib.c')
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_lib.c | 123 |
1 files changed, 89 insertions, 34 deletions
diff --git a/src/lib/libcrypto/rsa/rsa_lib.c b/src/lib/libcrypto/rsa/rsa_lib.c index 074a4f5074..5e1e8fcdf3 100644 --- a/src/lib/libcrypto/rsa/rsa_lib.c +++ b/src/lib/libcrypto/rsa/rsa_lib.c | |||
| @@ -62,6 +62,7 @@ | |||
| 62 | #include <openssl/lhash.h> | 62 | #include <openssl/lhash.h> |
| 63 | #include <openssl/bn.h> | 63 | #include <openssl/bn.h> |
| 64 | #include <openssl/rsa.h> | 64 | #include <openssl/rsa.h> |
| 65 | #include <openssl/engine.h> | ||
| 65 | 66 | ||
| 66 | const char *RSA_version="RSA" OPENSSL_VERSION_PTEXT; | 67 | const char *RSA_version="RSA" OPENSSL_VERSION_PTEXT; |
| 67 | 68 | ||
| @@ -74,21 +75,49 @@ RSA *RSA_new(void) | |||
| 74 | return(RSA_new_method(NULL)); | 75 | return(RSA_new_method(NULL)); |
| 75 | } | 76 | } |
| 76 | 77 | ||
| 77 | void RSA_set_default_method(RSA_METHOD *meth) | 78 | void RSA_set_default_openssl_method(RSA_METHOD *meth) |
| 78 | { | 79 | { |
| 79 | default_RSA_meth=meth; | 80 | ENGINE *e; |
| 81 | /* We'll need to notify the "openssl" ENGINE of this | ||
| 82 | * change too. We won't bother locking things down at | ||
| 83 | * our end as there was never any locking in these | ||
| 84 | * functions! */ | ||
| 85 | if(default_RSA_meth != meth) | ||
| 86 | { | ||
| 87 | default_RSA_meth = meth; | ||
| 88 | e = ENGINE_by_id("openssl"); | ||
| 89 | if(e) | ||
| 90 | { | ||
| 91 | ENGINE_set_RSA(e, meth); | ||
| 92 | ENGINE_free(e); | ||
| 93 | } | ||
| 94 | } | ||
| 80 | } | 95 | } |
| 81 | 96 | ||
| 82 | RSA_METHOD *RSA_get_default_method(void) | 97 | RSA_METHOD *RSA_get_default_openssl_method(void) |
| 83 | { | 98 | { |
| 99 | if (default_RSA_meth == NULL) | ||
| 100 | { | ||
| 101 | #ifdef RSA_NULL | ||
| 102 | default_RSA_meth=RSA_null_method(); | ||
| 103 | #else | ||
| 104 | #ifdef RSAref | ||
| 105 | default_RSA_meth=RSA_PKCS1_RSAref(); | ||
| 106 | #else | ||
| 107 | default_RSA_meth=RSA_PKCS1_SSLeay(); | ||
| 108 | #endif | ||
| 109 | #endif | ||
| 110 | } | ||
| 111 | |||
| 84 | return default_RSA_meth; | 112 | return default_RSA_meth; |
| 85 | } | 113 | } |
| 86 | 114 | ||
| 87 | RSA_METHOD *RSA_get_method(RSA *rsa) | 115 | RSA_METHOD *RSA_get_method(RSA *rsa) |
| 88 | { | 116 | { |
| 89 | return rsa->meth; | 117 | return ENGINE_get_RSA(rsa->engine); |
| 90 | } | 118 | } |
| 91 | 119 | ||
| 120 | #if 0 | ||
| 92 | RSA_METHOD *RSA_set_method(RSA *rsa, RSA_METHOD *meth) | 121 | RSA_METHOD *RSA_set_method(RSA *rsa, RSA_METHOD *meth) |
| 93 | { | 122 | { |
| 94 | RSA_METHOD *mtmp; | 123 | RSA_METHOD *mtmp; |
| @@ -98,34 +127,52 @@ RSA_METHOD *RSA_set_method(RSA *rsa, RSA_METHOD *meth) | |||
| 98 | if (meth->init) meth->init(rsa); | 127 | if (meth->init) meth->init(rsa); |
| 99 | return mtmp; | 128 | return mtmp; |
| 100 | } | 129 | } |
| 130 | #else | ||
| 131 | int RSA_set_method(RSA *rsa, ENGINE *engine) | ||
| 132 | { | ||
| 133 | ENGINE *mtmp; | ||
| 134 | RSA_METHOD *meth; | ||
| 135 | mtmp = rsa->engine; | ||
| 136 | meth = ENGINE_get_RSA(mtmp); | ||
| 137 | if (!ENGINE_init(engine)) | ||
| 138 | return 0; | ||
| 139 | if (meth->finish) meth->finish(rsa); | ||
| 140 | rsa->engine = engine; | ||
| 141 | meth = ENGINE_get_RSA(engine); | ||
| 142 | if (meth->init) meth->init(rsa); | ||
| 143 | /* SHOULD ERROR CHECK THIS!!! */ | ||
| 144 | ENGINE_finish(mtmp); | ||
| 145 | return 1; | ||
| 146 | } | ||
| 147 | #endif | ||
| 101 | 148 | ||
| 149 | #if 0 | ||
| 102 | RSA *RSA_new_method(RSA_METHOD *meth) | 150 | RSA *RSA_new_method(RSA_METHOD *meth) |
| 151 | #else | ||
| 152 | RSA *RSA_new_method(ENGINE *engine) | ||
| 153 | #endif | ||
| 103 | { | 154 | { |
| 155 | RSA_METHOD *meth; | ||
| 104 | RSA *ret; | 156 | RSA *ret; |
| 105 | 157 | ||
| 106 | if (default_RSA_meth == NULL) | 158 | ret=(RSA *)OPENSSL_malloc(sizeof(RSA)); |
| 107 | { | ||
| 108 | #ifdef RSA_NULL | ||
| 109 | default_RSA_meth=RSA_null_method(); | ||
| 110 | #else | ||
| 111 | #ifdef RSAref | ||
| 112 | default_RSA_meth=RSA_PKCS1_RSAref(); | ||
| 113 | #else | ||
| 114 | default_RSA_meth=RSA_PKCS1_SSLeay(); | ||
| 115 | #endif | ||
| 116 | #endif | ||
| 117 | } | ||
| 118 | ret=(RSA *)Malloc(sizeof(RSA)); | ||
| 119 | if (ret == NULL) | 159 | if (ret == NULL) |
| 120 | { | 160 | { |
| 121 | RSAerr(RSA_F_RSA_NEW_METHOD,ERR_R_MALLOC_FAILURE); | 161 | RSAerr(RSA_F_RSA_NEW_METHOD,ERR_R_MALLOC_FAILURE); |
| 122 | return(NULL); | 162 | return(NULL); |
| 123 | } | 163 | } |
| 124 | 164 | ||
| 125 | if (meth == NULL) | 165 | if (engine == NULL) |
| 126 | ret->meth=default_RSA_meth; | 166 | { |
| 167 | if((ret->engine=ENGINE_get_default_RSA()) == NULL) | ||
| 168 | { | ||
| 169 | OPENSSL_free(ret); | ||
| 170 | return NULL; | ||
| 171 | } | ||
| 172 | } | ||
| 127 | else | 173 | else |
| 128 | ret->meth=meth; | 174 | ret->engine=engine; |
| 175 | meth = ENGINE_get_RSA(ret->engine); | ||
| 129 | 176 | ||
| 130 | ret->pad=0; | 177 | ret->pad=0; |
| 131 | ret->version=0; | 178 | ret->version=0; |
| @@ -143,10 +190,10 @@ RSA *RSA_new_method(RSA_METHOD *meth) | |||
| 143 | ret->_method_mod_q=NULL; | 190 | ret->_method_mod_q=NULL; |
| 144 | ret->blinding=NULL; | 191 | ret->blinding=NULL; |
| 145 | ret->bignum_data=NULL; | 192 | ret->bignum_data=NULL; |
| 146 | ret->flags=ret->meth->flags; | 193 | ret->flags=meth->flags; |
| 147 | if ((ret->meth->init != NULL) && !ret->meth->init(ret)) | 194 | if ((meth->init != NULL) && !meth->init(ret)) |
| 148 | { | 195 | { |
| 149 | Free(ret); | 196 | OPENSSL_free(ret); |
| 150 | ret=NULL; | 197 | ret=NULL; |
| 151 | } | 198 | } |
| 152 | else | 199 | else |
| @@ -156,6 +203,7 @@ RSA *RSA_new_method(RSA_METHOD *meth) | |||
| 156 | 203 | ||
| 157 | void RSA_free(RSA *r) | 204 | void RSA_free(RSA *r) |
| 158 | { | 205 | { |
| 206 | RSA_METHOD *meth; | ||
| 159 | int i; | 207 | int i; |
| 160 | 208 | ||
| 161 | if (r == NULL) return; | 209 | if (r == NULL) return; |
| @@ -175,8 +223,10 @@ void RSA_free(RSA *r) | |||
| 175 | 223 | ||
| 176 | CRYPTO_free_ex_data(rsa_meth,r,&r->ex_data); | 224 | CRYPTO_free_ex_data(rsa_meth,r,&r->ex_data); |
| 177 | 225 | ||
| 178 | if (r->meth->finish != NULL) | 226 | meth = ENGINE_get_RSA(r->engine); |
| 179 | r->meth->finish(r); | 227 | if (meth->finish != NULL) |
| 228 | meth->finish(r); | ||
| 229 | ENGINE_finish(r->engine); | ||
| 180 | 230 | ||
| 181 | if (r->n != NULL) BN_clear_free(r->n); | 231 | if (r->n != NULL) BN_clear_free(r->n); |
| 182 | if (r->e != NULL) BN_clear_free(r->e); | 232 | if (r->e != NULL) BN_clear_free(r->e); |
| @@ -187,8 +237,8 @@ void RSA_free(RSA *r) | |||
| 187 | if (r->dmq1 != NULL) BN_clear_free(r->dmq1); | 237 | if (r->dmq1 != NULL) BN_clear_free(r->dmq1); |
| 188 | if (r->iqmp != NULL) BN_clear_free(r->iqmp); | 238 | if (r->iqmp != NULL) BN_clear_free(r->iqmp); |
| 189 | if (r->blinding != NULL) BN_BLINDING_free(r->blinding); | 239 | if (r->blinding != NULL) BN_BLINDING_free(r->blinding); |
| 190 | if (r->bignum_data != NULL) Free_locked(r->bignum_data); | 240 | if (r->bignum_data != NULL) OPENSSL_free_locked(r->bignum_data); |
| 191 | Free(r); | 241 | OPENSSL_free(r); |
| 192 | } | 242 | } |
| 193 | 243 | ||
| 194 | int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, | 244 | int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, |
| @@ -217,30 +267,34 @@ int RSA_size(RSA *r) | |||
| 217 | int RSA_public_encrypt(int flen, unsigned char *from, unsigned char *to, | 267 | int RSA_public_encrypt(int flen, unsigned char *from, unsigned char *to, |
| 218 | RSA *rsa, int padding) | 268 | RSA *rsa, int padding) |
| 219 | { | 269 | { |
| 220 | return(rsa->meth->rsa_pub_enc(flen, from, to, rsa, padding)); | 270 | return(ENGINE_get_RSA(rsa->engine)->rsa_pub_enc(flen, |
| 271 | from, to, rsa, padding)); | ||
| 221 | } | 272 | } |
| 222 | 273 | ||
| 223 | int RSA_private_encrypt(int flen, unsigned char *from, unsigned char *to, | 274 | int RSA_private_encrypt(int flen, unsigned char *from, unsigned char *to, |
| 224 | RSA *rsa, int padding) | 275 | RSA *rsa, int padding) |
| 225 | { | 276 | { |
| 226 | return(rsa->meth->rsa_priv_enc(flen, from, to, rsa, padding)); | 277 | return(ENGINE_get_RSA(rsa->engine)->rsa_priv_enc(flen, |
| 278 | from, to, rsa, padding)); | ||
| 227 | } | 279 | } |
| 228 | 280 | ||
| 229 | int RSA_private_decrypt(int flen, unsigned char *from, unsigned char *to, | 281 | int RSA_private_decrypt(int flen, unsigned char *from, unsigned char *to, |
| 230 | RSA *rsa, int padding) | 282 | RSA *rsa, int padding) |
| 231 | { | 283 | { |
| 232 | return(rsa->meth->rsa_priv_dec(flen, from, to, rsa, padding)); | 284 | return(ENGINE_get_RSA(rsa->engine)->rsa_priv_dec(flen, |
| 285 | from, to, rsa, padding)); | ||
| 233 | } | 286 | } |
| 234 | 287 | ||
| 235 | int RSA_public_decrypt(int flen, unsigned char *from, unsigned char *to, | 288 | int RSA_public_decrypt(int flen, unsigned char *from, unsigned char *to, |
| 236 | RSA *rsa, int padding) | 289 | RSA *rsa, int padding) |
| 237 | { | 290 | { |
| 238 | return(rsa->meth->rsa_pub_dec(flen, from, to, rsa, padding)); | 291 | return(ENGINE_get_RSA(rsa->engine)->rsa_pub_dec(flen, |
| 292 | from, to, rsa, padding)); | ||
| 239 | } | 293 | } |
| 240 | 294 | ||
| 241 | int RSA_flags(RSA *r) | 295 | int RSA_flags(RSA *r) |
| 242 | { | 296 | { |
| 243 | return((r == NULL)?0:r->meth->flags); | 297 | return((r == NULL)?0:ENGINE_get_RSA(r->engine)->flags); |
| 244 | } | 298 | } |
| 245 | 299 | ||
| 246 | void RSA_blinding_off(RSA *rsa) | 300 | void RSA_blinding_off(RSA *rsa) |
| @@ -274,7 +328,8 @@ int RSA_blinding_on(RSA *rsa, BN_CTX *p_ctx) | |||
| 274 | if (!BN_rand(A,BN_num_bits(rsa->n)-1,1,0)) goto err; | 328 | if (!BN_rand(A,BN_num_bits(rsa->n)-1,1,0)) goto err; |
| 275 | if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err; | 329 | if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err; |
| 276 | 330 | ||
| 277 | if (!rsa->meth->bn_mod_exp(A,A,rsa->e,rsa->n,ctx,rsa->_method_mod_n)) | 331 | if (!ENGINE_get_RSA(rsa->engine)->bn_mod_exp(A,A, |
| 332 | rsa->e,rsa->n,ctx,rsa->_method_mod_n)) | ||
| 278 | goto err; | 333 | goto err; |
| 279 | rsa->blinding=BN_BLINDING_new(A,Ai,rsa->n); | 334 | rsa->blinding=BN_BLINDING_new(A,Ai,rsa->n); |
| 280 | rsa->flags|=RSA_FLAG_BLINDING; | 335 | rsa->flags|=RSA_FLAG_BLINDING; |
| @@ -305,7 +360,7 @@ int RSA_memory_lock(RSA *r) | |||
| 305 | j=1; | 360 | j=1; |
| 306 | for (i=0; i<6; i++) | 361 | for (i=0; i<6; i++) |
| 307 | j+= (*t[i])->top; | 362 | j+= (*t[i])->top; |
| 308 | if ((p=Malloc_locked((off+j)*sizeof(BN_ULONG))) == NULL) | 363 | if ((p=OPENSSL_malloc_locked((off+j)*sizeof(BN_ULONG))) == NULL) |
| 309 | { | 364 | { |
| 310 | RSAerr(RSA_F_MEMORY_LOCK,ERR_R_MALLOC_FAILURE); | 365 | RSAerr(RSA_F_MEMORY_LOCK,ERR_R_MALLOC_FAILURE); |
| 311 | return(0); | 366 | return(0); |
