rsa_lib.c (57511) | rsa_lib.c (59194) |
---|---|
1/* crypto/rsa/rsa_lib.c */ | 1/* crypto/rsa/rsa_lib.c */ |
2/* $FreeBSD: head/crypto/openssl/crypto/rsa/rsa_lib.c 57511 2000-02-26 13:13:03Z peter $ */ | 2/* $FreeBSD: head/crypto/openssl/crypto/rsa/rsa_lib.c 59194 2000-04-13 07:15:03Z kris $ */ |
3/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 4 * All rights reserved. 5 * 6 * This package is an SSL implementation written 7 * by Eric Young (eay@cryptsoft.com). 8 * The implementation was written so as to conform with Netscapes SSL. 9 * 10 * This library is free for commercial and non-commercial use as long as --- 52 unchanged lines hidden (view full) --- 63#include <openssl/lhash.h> 64#include <openssl/bn.h> 65#include <openssl/rsa.h> 66 67const char *RSA_version="RSA" OPENSSL_VERSION_PTEXT; 68 69static RSA_METHOD *default_RSA_meth=NULL; 70static int rsa_meth_num=0; | 3/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 4 * All rights reserved. 5 * 6 * This package is an SSL implementation written 7 * by Eric Young (eay@cryptsoft.com). 8 * The implementation was written so as to conform with Netscapes SSL. 9 * 10 * This library is free for commercial and non-commercial use as long as --- 52 unchanged lines hidden (view full) --- 63#include <openssl/lhash.h> 64#include <openssl/bn.h> 65#include <openssl/rsa.h> 66 67const char *RSA_version="RSA" OPENSSL_VERSION_PTEXT; 68 69static RSA_METHOD *default_RSA_meth=NULL; 70static int rsa_meth_num=0; |
71static STACK *rsa_meth=NULL; | 71static STACK_OF(CRYPTO_EX_DATA_FUNCS) *rsa_meth=NULL; |
72 73RSA *RSA_new(void) 74 { 75 return(RSA_new_method(NULL)); 76 } 77 78void RSA_set_default_method(RSA_METHOD *meth) 79 { --- 21 unchanged lines hidden (view full) --- 101} 102 103RSA *RSA_new_method(RSA_METHOD *meth) 104 { 105 RSA *ret; 106 107 if (default_RSA_meth == NULL) 108 { | 72 73RSA *RSA_new(void) 74 { 75 return(RSA_new_method(NULL)); 76 } 77 78void RSA_set_default_method(RSA_METHOD *meth) 79 { --- 21 unchanged lines hidden (view full) --- 101} 102 103RSA *RSA_new_method(RSA_METHOD *meth) 104 { 105 RSA *ret; 106 107 if (default_RSA_meth == NULL) 108 { |
109#ifdef RSA_NULL 110 default_RSA_meth=RSA_null_method(); 111#else |
|
109 default_RSA_meth=RSA_PKCS1(); | 112 default_RSA_meth=RSA_PKCS1(); |
113#endif |
|
110 } 111 ret=(RSA *)Malloc(sizeof(RSA)); 112 if (ret == NULL) 113 { 114 RSAerr(RSA_F_RSA_NEW_METHOD,ERR_R_MALLOC_FAILURE); 115 return(NULL); 116 } 117 --- 20 unchanged lines hidden (view full) --- 138 ret->bignum_data=NULL; 139 ret->flags=ret->meth->flags; 140 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) 141 { 142 Free(ret); 143 ret=NULL; 144 } 145 else | 114 } 115 ret=(RSA *)Malloc(sizeof(RSA)); 116 if (ret == NULL) 117 { 118 RSAerr(RSA_F_RSA_NEW_METHOD,ERR_R_MALLOC_FAILURE); 119 return(NULL); 120 } 121 --- 20 unchanged lines hidden (view full) --- 142 ret->bignum_data=NULL; 143 ret->flags=ret->meth->flags; 144 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) 145 { 146 Free(ret); 147 ret=NULL; 148 } 149 else |
146 CRYPTO_new_ex_data(rsa_meth,(char *)ret,&ret->ex_data); | 150 CRYPTO_new_ex_data(rsa_meth,ret,&ret->ex_data); |
147 return(ret); 148 } 149 150void RSA_free(RSA *r) 151 { 152 int i; 153 154 if (r == NULL) return; --- 6 unchanged lines hidden (view full) --- 161#ifdef REF_CHECK 162 if (i < 0) 163 { 164 fprintf(stderr,"RSA_free, bad reference count\n"); 165 abort(); 166 } 167#endif 168 | 151 return(ret); 152 } 153 154void RSA_free(RSA *r) 155 { 156 int i; 157 158 if (r == NULL) return; --- 6 unchanged lines hidden (view full) --- 165#ifdef REF_CHECK 166 if (i < 0) 167 { 168 fprintf(stderr,"RSA_free, bad reference count\n"); 169 abort(); 170 } 171#endif 172 |
169 CRYPTO_free_ex_data(rsa_meth,(char *)r,&r->ex_data); | 173 CRYPTO_free_ex_data(rsa_meth,r,&r->ex_data); |
170 171 if (r->meth->finish != NULL) 172 r->meth->finish(r); 173 174 if (r->n != NULL) BN_clear_free(r->n); 175 if (r->e != NULL) BN_clear_free(r->e); 176 if (r->d != NULL) BN_clear_free(r->d); 177 if (r->p != NULL) BN_clear_free(r->p); 178 if (r->q != NULL) BN_clear_free(r->q); 179 if (r->dmp1 != NULL) BN_clear_free(r->dmp1); 180 if (r->dmq1 != NULL) BN_clear_free(r->dmq1); 181 if (r->iqmp != NULL) BN_clear_free(r->iqmp); 182 if (r->blinding != NULL) BN_BLINDING_free(r->blinding); 183 if (r->bignum_data != NULL) Free_locked(r->bignum_data); 184 Free(r); 185 } 186 | 174 175 if (r->meth->finish != NULL) 176 r->meth->finish(r); 177 178 if (r->n != NULL) BN_clear_free(r->n); 179 if (r->e != NULL) BN_clear_free(r->e); 180 if (r->d != NULL) BN_clear_free(r->d); 181 if (r->p != NULL) BN_clear_free(r->p); 182 if (r->q != NULL) BN_clear_free(r->q); 183 if (r->dmp1 != NULL) BN_clear_free(r->dmp1); 184 if (r->dmq1 != NULL) BN_clear_free(r->dmq1); 185 if (r->iqmp != NULL) BN_clear_free(r->iqmp); 186 if (r->blinding != NULL) BN_BLINDING_free(r->blinding); 187 if (r->bignum_data != NULL) Free_locked(r->bignum_data); 188 Free(r); 189 } 190 |
187int RSA_get_ex_new_index(long argl, char *argp, int (*new_func)(), 188 int (*dup_func)(), void (*free_func)()) | 191int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 192 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) |
189 { 190 rsa_meth_num++; 191 return(CRYPTO_get_ex_new_index(rsa_meth_num-1, 192 &rsa_meth,argl,argp,new_func,dup_func,free_func)); 193 } 194 | 193 { 194 rsa_meth_num++; 195 return(CRYPTO_get_ex_new_index(rsa_meth_num-1, 196 &rsa_meth,argl,argp,new_func,dup_func,free_func)); 197 } 198 |
195int RSA_set_ex_data(RSA *r, int idx, char *arg) | 199int RSA_set_ex_data(RSA *r, int idx, void *arg) |
196 { 197 return(CRYPTO_set_ex_data(&r->ex_data,idx,arg)); 198 } 199 | 200 { 201 return(CRYPTO_set_ex_data(&r->ex_data,idx,arg)); 202 } 203 |
200char *RSA_get_ex_data(RSA *r, int idx) | 204void *RSA_get_ex_data(RSA *r, int idx) |
201 { 202 return(CRYPTO_get_ex_data(&r->ex_data,idx)); 203 } 204 205int RSA_size(RSA *r) 206 { 207 return(BN_num_bytes(r->n)); 208 } --- 48 unchanged lines hidden (view full) --- 257 if ((ctx=BN_CTX_new()) == NULL) goto err; 258 } 259 else 260 ctx=p_ctx; 261 262 if (rsa->blinding != NULL) 263 BN_BLINDING_free(rsa->blinding); 264 | 205 { 206 return(CRYPTO_get_ex_data(&r->ex_data,idx)); 207 } 208 209int RSA_size(RSA *r) 210 { 211 return(BN_num_bytes(r->n)); 212 } --- 48 unchanged lines hidden (view full) --- 261 if ((ctx=BN_CTX_new()) == NULL) goto err; 262 } 263 else 264 ctx=p_ctx; 265 266 if (rsa->blinding != NULL) 267 BN_BLINDING_free(rsa->blinding); 268 |
265 A= &(ctx->bn[0]); 266 ctx->tos++; | 269 BN_CTX_start(ctx); 270 A = BN_CTX_get(ctx); |
267 if (!BN_rand(A,BN_num_bits(rsa->n)-1,1,0)) goto err; 268 if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err; 269 270 if (!rsa->meth->bn_mod_exp(A,A,rsa->e,rsa->n,ctx,rsa->_method_mod_n)) 271 goto err; 272 rsa->blinding=BN_BLINDING_new(A,Ai,rsa->n); | 271 if (!BN_rand(A,BN_num_bits(rsa->n)-1,1,0)) goto err; 272 if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err; 273 274 if (!rsa->meth->bn_mod_exp(A,A,rsa->e,rsa->n,ctx,rsa->_method_mod_n)) 275 goto err; 276 rsa->blinding=BN_BLINDING_new(A,Ai,rsa->n); |
273 ctx->tos--; | |
274 rsa->flags|=RSA_FLAG_BLINDING; 275 BN_free(Ai); 276 ret=1; 277err: | 277 rsa->flags|=RSA_FLAG_BLINDING; 278 BN_free(Ai); 279 ret=1; 280err: |
281 BN_CTX_end(ctx); |
|
278 if (ctx != p_ctx) BN_CTX_free(ctx); 279 return(ret); 280 } 281 282int RSA_memory_lock(RSA *r) 283 { 284 int i,j,k,off; 285 char *p; --- 41 unchanged lines hidden --- | 282 if (ctx != p_ctx) BN_CTX_free(ctx); 283 return(ret); 284 } 285 286int RSA_memory_lock(RSA *r) 287 { 288 int i,j,k,off; 289 char *p; --- 41 unchanged lines hidden --- |