diff options
| author | beck <> | 1999-09-29 04:37:45 +0000 |
|---|---|---|
| committer | beck <> | 1999-09-29 04:37:45 +0000 |
| commit | de8f24ea083384bb66b32ec105dc4743c5663cdf (patch) | |
| tree | 1412176ae62a3cab2cf2b0b92150fcbceaac6092 /src/lib/libcrypto/rsa/rsa_lib.c | |
| parent | cb929d29896bcb87c2a97417fbd03e50078fc178 (diff) | |
| download | openbsd-de8f24ea083384bb66b32ec105dc4743c5663cdf.tar.gz openbsd-de8f24ea083384bb66b32ec105dc4743c5663cdf.tar.bz2 openbsd-de8f24ea083384bb66b32ec105dc4743c5663cdf.zip | |
OpenSSL 0.9.4 merge
Diffstat (limited to 'src/lib/libcrypto/rsa/rsa_lib.c')
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_lib.c | 167 |
1 files changed, 101 insertions, 66 deletions
diff --git a/src/lib/libcrypto/rsa/rsa_lib.c b/src/lib/libcrypto/rsa/rsa_lib.c index 95a56f8a28..c0ca2923a6 100644 --- a/src/lib/libcrypto/rsa/rsa_lib.c +++ b/src/lib/libcrypto/rsa/rsa_lib.c | |||
| @@ -57,31 +57,49 @@ | |||
| 57 | */ | 57 | */ |
| 58 | 58 | ||
| 59 | #include <stdio.h> | 59 | #include <stdio.h> |
| 60 | #include "crypto.h" | 60 | #include <openssl/crypto.h> |
| 61 | #include "cryptlib.h" | 61 | #include "cryptlib.h" |
| 62 | #include "lhash.h" | 62 | #include <openssl/lhash.h> |
| 63 | #include "bn.h" | 63 | #include <openssl/bn.h> |
| 64 | #include "rsa.h" | 64 | #include <openssl/rsa.h> |
| 65 | 65 | ||
| 66 | char *RSA_version="RSA part of SSLeay 0.9.0b 29-Jun-1998"; | 66 | const char *RSA_version="RSA" OPENSSL_VERSION_PTEXT; |
| 67 | 67 | ||
| 68 | static RSA_METHOD *default_RSA_meth=NULL; | 68 | static RSA_METHOD *default_RSA_meth=NULL; |
| 69 | static int rsa_meth_num=0; | 69 | static int rsa_meth_num=0; |
| 70 | static STACK *rsa_meth=NULL; | 70 | static STACK *rsa_meth=NULL; |
| 71 | 71 | ||
| 72 | RSA *RSA_new() | 72 | RSA *RSA_new(void) |
| 73 | { | 73 | { |
| 74 | return(RSA_new_method(NULL)); | 74 | return(RSA_new_method(NULL)); |
| 75 | } | 75 | } |
| 76 | 76 | ||
| 77 | void RSA_set_default_method(meth) | 77 | void RSA_set_default_method(RSA_METHOD *meth) |
| 78 | RSA_METHOD *meth; | ||
| 79 | { | 78 | { |
| 80 | default_RSA_meth=meth; | 79 | default_RSA_meth=meth; |
| 81 | } | 80 | } |
| 82 | 81 | ||
| 83 | RSA *RSA_new_method(meth) | 82 | RSA_METHOD *RSA_get_default_method(void) |
| 84 | RSA_METHOD *meth; | 83 | { |
| 84 | return default_RSA_meth; | ||
| 85 | } | ||
| 86 | |||
| 87 | RSA_METHOD *RSA_get_method(RSA *rsa) | ||
| 88 | { | ||
| 89 | return rsa->meth; | ||
| 90 | } | ||
| 91 | |||
| 92 | RSA_METHOD *RSA_set_method(RSA *rsa, RSA_METHOD *meth) | ||
| 93 | { | ||
| 94 | RSA_METHOD *mtmp; | ||
| 95 | mtmp = rsa->meth; | ||
| 96 | if (mtmp->finish) mtmp->finish(rsa); | ||
| 97 | rsa->meth = meth; | ||
| 98 | if (meth->init) meth->init(rsa); | ||
| 99 | return mtmp; | ||
| 100 | } | ||
| 101 | |||
| 102 | RSA *RSA_new_method(RSA_METHOD *meth) | ||
| 85 | { | 103 | { |
| 86 | RSA *ret; | 104 | RSA *ret; |
| 87 | 105 | ||
| @@ -116,22 +134,23 @@ RSA_METHOD *meth; | |||
| 116 | ret->dmq1=NULL; | 134 | ret->dmq1=NULL; |
| 117 | ret->iqmp=NULL; | 135 | ret->iqmp=NULL; |
| 118 | ret->references=1; | 136 | ret->references=1; |
| 119 | ret->method_mod_n=NULL; | 137 | ret->_method_mod_n=NULL; |
| 120 | ret->method_mod_p=NULL; | 138 | ret->_method_mod_p=NULL; |
| 121 | ret->method_mod_q=NULL; | 139 | ret->_method_mod_q=NULL; |
| 122 | ret->blinding=NULL; | 140 | ret->blinding=NULL; |
| 141 | ret->bignum_data=NULL; | ||
| 123 | ret->flags=ret->meth->flags; | 142 | ret->flags=ret->meth->flags; |
| 124 | if ((ret->meth->init != NULL) && !ret->meth->init(ret)) | 143 | if ((ret->meth->init != NULL) && !ret->meth->init(ret)) |
| 125 | { | 144 | { |
| 126 | Free(ret); | 145 | Free(ret); |
| 127 | ret=NULL; | 146 | ret=NULL; |
| 128 | } | 147 | } |
| 129 | CRYPTO_new_ex_data(rsa_meth,(char *)ret,&ret->ex_data); | 148 | else |
| 149 | CRYPTO_new_ex_data(rsa_meth,(char *)ret,&ret->ex_data); | ||
| 130 | return(ret); | 150 | return(ret); |
| 131 | } | 151 | } |
| 132 | 152 | ||
| 133 | void RSA_free(r) | 153 | void RSA_free(RSA *r) |
| 134 | RSA *r; | ||
| 135 | { | 154 | { |
| 136 | int i; | 155 | int i; |
| 137 | 156 | ||
| @@ -164,90 +183,63 @@ RSA *r; | |||
| 164 | if (r->dmq1 != NULL) BN_clear_free(r->dmq1); | 183 | if (r->dmq1 != NULL) BN_clear_free(r->dmq1); |
| 165 | if (r->iqmp != NULL) BN_clear_free(r->iqmp); | 184 | if (r->iqmp != NULL) BN_clear_free(r->iqmp); |
| 166 | if (r->blinding != NULL) BN_BLINDING_free(r->blinding); | 185 | if (r->blinding != NULL) BN_BLINDING_free(r->blinding); |
| 186 | if (r->bignum_data != NULL) Free_locked(r->bignum_data); | ||
| 167 | Free(r); | 187 | Free(r); |
| 168 | } | 188 | } |
| 169 | 189 | ||
| 170 | int RSA_get_ex_new_index(argl,argp,new_func,dup_func,free_func) | 190 | int RSA_get_ex_new_index(long argl, char *argp, int (*new_func)(), |
| 171 | long argl; | 191 | int (*dup_func)(), void (*free_func)()) |
| 172 | char *argp; | ||
| 173 | int (*new_func)(); | ||
| 174 | int (*dup_func)(); | ||
| 175 | void (*free_func)(); | ||
| 176 | { | 192 | { |
| 177 | rsa_meth_num++; | 193 | rsa_meth_num++; |
| 178 | return(CRYPTO_get_ex_new_index(rsa_meth_num-1, | 194 | return(CRYPTO_get_ex_new_index(rsa_meth_num-1, |
| 179 | &rsa_meth,argl,argp,new_func,dup_func,free_func)); | 195 | &rsa_meth,argl,argp,new_func,dup_func,free_func)); |
| 180 | } | 196 | } |
| 181 | 197 | ||
| 182 | int RSA_set_ex_data(r,idx,arg) | 198 | int RSA_set_ex_data(RSA *r, int idx, char *arg) |
| 183 | RSA *r; | ||
| 184 | int idx; | ||
| 185 | char *arg; | ||
| 186 | { | 199 | { |
| 187 | return(CRYPTO_set_ex_data(&r->ex_data,idx,arg)); | 200 | return(CRYPTO_set_ex_data(&r->ex_data,idx,arg)); |
| 188 | } | 201 | } |
| 189 | 202 | ||
| 190 | char *RSA_get_ex_data(r,idx) | 203 | char *RSA_get_ex_data(RSA *r, int idx) |
| 191 | RSA *r; | ||
| 192 | int idx; | ||
| 193 | { | 204 | { |
| 194 | return(CRYPTO_get_ex_data(&r->ex_data,idx)); | 205 | return(CRYPTO_get_ex_data(&r->ex_data,idx)); |
| 195 | } | 206 | } |
| 196 | 207 | ||
| 197 | int RSA_size(r) | 208 | int RSA_size(RSA *r) |
| 198 | RSA *r; | ||
| 199 | { | 209 | { |
| 200 | return(BN_num_bytes(r->n)); | 210 | return(BN_num_bytes(r->n)); |
| 201 | } | 211 | } |
| 202 | 212 | ||
| 203 | int RSA_public_encrypt(flen, from, to, rsa, padding) | 213 | int RSA_public_encrypt(int flen, unsigned char *from, unsigned char *to, |
| 204 | int flen; | 214 | RSA *rsa, int padding) |
| 205 | unsigned char *from; | ||
| 206 | unsigned char *to; | ||
| 207 | RSA *rsa; | ||
| 208 | int padding; | ||
| 209 | { | 215 | { |
| 210 | return(rsa->meth->rsa_pub_enc(flen, from, to, rsa, padding)); | 216 | return(rsa->meth->rsa_pub_enc(flen, from, to, rsa, padding)); |
| 211 | } | 217 | } |
| 212 | 218 | ||
| 213 | int RSA_private_encrypt(flen, from, to, rsa, padding) | 219 | int RSA_private_encrypt(int flen, unsigned char *from, unsigned char *to, |
| 214 | int flen; | 220 | RSA *rsa, int padding) |
| 215 | unsigned char *from; | ||
| 216 | unsigned char *to; | ||
| 217 | RSA *rsa; | ||
| 218 | int padding; | ||
| 219 | { | 221 | { |
| 220 | return(rsa->meth->rsa_priv_enc(flen, from, to, rsa, padding)); | 222 | return(rsa->meth->rsa_priv_enc(flen, from, to, rsa, padding)); |
| 221 | } | 223 | } |
| 222 | 224 | ||
| 223 | int RSA_private_decrypt(flen, from, to, rsa, padding) | 225 | int RSA_private_decrypt(int flen, unsigned char *from, unsigned char *to, |
| 224 | int flen; | 226 | RSA *rsa, int padding) |
| 225 | unsigned char *from; | ||
| 226 | unsigned char *to; | ||
| 227 | RSA *rsa; | ||
| 228 | int padding; | ||
| 229 | { | 227 | { |
| 230 | return(rsa->meth->rsa_priv_dec(flen, from, to, rsa, padding)); | 228 | return(rsa->meth->rsa_priv_dec(flen, from, to, rsa, padding)); |
| 231 | } | 229 | } |
| 232 | 230 | ||
| 233 | int RSA_public_decrypt(flen, from, to, rsa, padding) | 231 | int RSA_public_decrypt(int flen, unsigned char *from, unsigned char *to, |
| 234 | int flen; | 232 | RSA *rsa, int padding) |
| 235 | unsigned char *from; | ||
| 236 | unsigned char *to; | ||
| 237 | RSA *rsa; | ||
| 238 | int padding; | ||
| 239 | { | 233 | { |
| 240 | return(rsa->meth->rsa_pub_dec(flen, from, to, rsa, padding)); | 234 | return(rsa->meth->rsa_pub_dec(flen, from, to, rsa, padding)); |
| 241 | } | 235 | } |
| 242 | 236 | ||
| 243 | int RSA_flags(r) | 237 | int RSA_flags(RSA *r) |
| 244 | RSA *r; | ||
| 245 | { | 238 | { |
| 246 | return((r == NULL)?0:r->meth->flags); | 239 | return((r == NULL)?0:r->meth->flags); |
| 247 | } | 240 | } |
| 248 | 241 | ||
| 249 | void RSA_blinding_off(rsa) | 242 | void RSA_blinding_off(RSA *rsa) |
| 250 | RSA *rsa; | ||
| 251 | { | 243 | { |
| 252 | if (rsa->blinding != NULL) | 244 | if (rsa->blinding != NULL) |
| 253 | { | 245 | { |
| @@ -257,9 +249,7 @@ RSA *rsa; | |||
| 257 | rsa->flags&= ~RSA_FLAG_BLINDING; | 249 | rsa->flags&= ~RSA_FLAG_BLINDING; |
| 258 | } | 250 | } |
| 259 | 251 | ||
| 260 | int RSA_blinding_on(rsa,p_ctx) | 252 | int RSA_blinding_on(RSA *rsa, BN_CTX *p_ctx) |
| 261 | RSA *rsa; | ||
| 262 | BN_CTX *p_ctx; | ||
| 263 | { | 253 | { |
| 264 | BIGNUM *A,*Ai; | 254 | BIGNUM *A,*Ai; |
| 265 | BN_CTX *ctx; | 255 | BN_CTX *ctx; |
| @@ -275,13 +265,13 @@ BN_CTX *p_ctx; | |||
| 275 | if (rsa->blinding != NULL) | 265 | if (rsa->blinding != NULL) |
| 276 | BN_BLINDING_free(rsa->blinding); | 266 | BN_BLINDING_free(rsa->blinding); |
| 277 | 267 | ||
| 278 | A=ctx->bn[0]; | 268 | A= &(ctx->bn[0]); |
| 279 | ctx->tos++; | 269 | ctx->tos++; |
| 280 | if (!BN_rand(A,BN_num_bits(rsa->n)-1,1,0)) goto err; | 270 | if (!BN_rand(A,BN_num_bits(rsa->n)-1,1,0)) goto err; |
| 281 | if ((Ai=BN_mod_inverse(A,rsa->n,ctx)) == NULL) goto err; | 271 | if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err; |
| 282 | 272 | ||
| 283 | if (!rsa->meth->bn_mod_exp(A,A,rsa->e,rsa->n,ctx, | 273 | if (!rsa->meth->bn_mod_exp(A,A,rsa->e,rsa->n,ctx,rsa->_method_mod_n)) |
| 284 | (char *)rsa->method_mod_n)) goto err; | 274 | goto err; |
| 285 | rsa->blinding=BN_BLINDING_new(A,Ai,rsa->n); | 275 | rsa->blinding=BN_BLINDING_new(A,Ai,rsa->n); |
| 286 | ctx->tos--; | 276 | ctx->tos--; |
| 287 | rsa->flags|=RSA_FLAG_BLINDING; | 277 | rsa->flags|=RSA_FLAG_BLINDING; |
| @@ -292,3 +282,48 @@ err: | |||
| 292 | return(ret); | 282 | return(ret); |
| 293 | } | 283 | } |
| 294 | 284 | ||
| 285 | int RSA_memory_lock(RSA *r) | ||
| 286 | { | ||
| 287 | int i,j,k,off; | ||
| 288 | char *p; | ||
| 289 | BIGNUM *bn,**t[6],*b; | ||
| 290 | BN_ULONG *ul; | ||
| 291 | |||
| 292 | if (r->d == NULL) return(1); | ||
| 293 | t[0]= &r->d; | ||
| 294 | t[1]= &r->p; | ||
| 295 | t[2]= &r->q; | ||
| 296 | t[3]= &r->dmp1; | ||
| 297 | t[4]= &r->dmq1; | ||
| 298 | t[5]= &r->iqmp; | ||
| 299 | k=sizeof(BIGNUM)*6; | ||
| 300 | off=k/sizeof(BN_ULONG)+1; | ||
| 301 | j=1; | ||
| 302 | for (i=0; i<6; i++) | ||
| 303 | j+= (*t[i])->top; | ||
| 304 | if ((p=Malloc_locked((off+j)*sizeof(BN_ULONG))) == NULL) | ||
| 305 | { | ||
| 306 | RSAerr(RSA_F_MEMORY_LOCK,ERR_R_MALLOC_FAILURE); | ||
| 307 | return(0); | ||
| 308 | } | ||
| 309 | bn=(BIGNUM *)p; | ||
| 310 | ul=(BN_ULONG *)&(p[off]); | ||
| 311 | for (i=0; i<6; i++) | ||
| 312 | { | ||
| 313 | b= *(t[i]); | ||
| 314 | *(t[i])= &(bn[i]); | ||
| 315 | memcpy((char *)&(bn[i]),(char *)b,sizeof(BIGNUM)); | ||
| 316 | bn[i].flags=BN_FLG_STATIC_DATA; | ||
| 317 | bn[i].d=ul; | ||
| 318 | memcpy((char *)ul,b->d,sizeof(BN_ULONG)*b->top); | ||
| 319 | ul+=b->top; | ||
| 320 | BN_clear_free(b); | ||
| 321 | } | ||
| 322 | |||
| 323 | /* I should fix this so it can still be done */ | ||
| 324 | r->flags&= ~(RSA_FLAG_CACHE_PRIVATE|RSA_FLAG_CACHE_PUBLIC); | ||
| 325 | |||
| 326 | r->bignum_data=p; | ||
| 327 | return(1); | ||
| 328 | } | ||
| 329 | |||
