diff options
Diffstat (limited to 'src/lib/libcrypto/rsa/rsa_eay.c')
-rw-r--r-- | src/lib/libcrypto/rsa/rsa_eay.c | 164 |
1 files changed, 89 insertions, 75 deletions
diff --git a/src/lib/libcrypto/rsa/rsa_eay.c b/src/lib/libcrypto/rsa/rsa_eay.c index 42a77f11cd..776324860c 100644 --- a/src/lib/libcrypto/rsa/rsa_eay.c +++ b/src/lib/libcrypto/rsa/rsa_eay.c | |||
@@ -68,11 +68,10 @@ | |||
68 | 68 | ||
69 | #include <stdio.h> | 69 | #include <stdio.h> |
70 | #include "cryptlib.h" | 70 | #include "cryptlib.h" |
71 | #include "bn.h" | 71 | #include <openssl/bn.h> |
72 | #include "rsa.h" | 72 | #include <openssl/rsa.h> |
73 | #include "rand.h" | 73 | #include <openssl/rand.h> |
74 | 74 | ||
75 | #ifndef NOPROTO | ||
76 | static int RSA_eay_public_encrypt(int flen, unsigned char *from, | 75 | static int RSA_eay_public_encrypt(int flen, unsigned char *from, |
77 | unsigned char *to, RSA *rsa,int padding); | 76 | unsigned char *to, RSA *rsa,int padding); |
78 | static int RSA_eay_private_encrypt(int flen, unsigned char *from, | 77 | static int RSA_eay_private_encrypt(int flen, unsigned char *from, |
@@ -84,16 +83,6 @@ static int RSA_eay_private_decrypt(int flen, unsigned char *from, | |||
84 | static int RSA_eay_mod_exp(BIGNUM *r0, BIGNUM *i, RSA *rsa); | 83 | static int RSA_eay_mod_exp(BIGNUM *r0, BIGNUM *i, RSA *rsa); |
85 | static int RSA_eay_init(RSA *rsa); | 84 | static int RSA_eay_init(RSA *rsa); |
86 | static int RSA_eay_finish(RSA *rsa); | 85 | static int RSA_eay_finish(RSA *rsa); |
87 | #else | ||
88 | static int RSA_eay_public_encrypt(); | ||
89 | static int RSA_eay_private_encrypt(); | ||
90 | static int RSA_eay_public_decrypt(); | ||
91 | static int RSA_eay_private_decrypt(); | ||
92 | static int RSA_eay_mod_exp(); | ||
93 | static int RSA_eay_init(); | ||
94 | static int RSA_eay_finish(); | ||
95 | #endif | ||
96 | |||
97 | static RSA_METHOD rsa_pkcs1_eay_meth={ | 86 | static RSA_METHOD rsa_pkcs1_eay_meth={ |
98 | "Eric Young's PKCS#1 RSA", | 87 | "Eric Young's PKCS#1 RSA", |
99 | RSA_eay_public_encrypt, | 88 | RSA_eay_public_encrypt, |
@@ -108,31 +97,75 @@ static RSA_METHOD rsa_pkcs1_eay_meth={ | |||
108 | NULL, | 97 | NULL, |
109 | }; | 98 | }; |
110 | 99 | ||
111 | RSA_METHOD *RSA_PKCS1_SSLeay() | 100 | RSA_METHOD *RSA_PKCS1_SSLeay(void) |
112 | { | 101 | { |
113 | return(&rsa_pkcs1_eay_meth); | 102 | return(&rsa_pkcs1_eay_meth); |
114 | } | 103 | } |
115 | 104 | ||
116 | static int RSA_eay_public_encrypt(flen, from, to, rsa, padding) | 105 | static int RSA_eay_public_encrypt(int flen, unsigned char *from, |
117 | int flen; | 106 | unsigned char *to, RSA *rsa, int padding) |
118 | unsigned char *from; | ||
119 | unsigned char *to; | ||
120 | RSA *rsa; | ||
121 | int padding; | ||
122 | { | 107 | { |
123 | BIGNUM *f=NULL,*ret=NULL; | 108 | BIGNUM f,ret; |
124 | int i,j,k,num=0,r= -1; | 109 | int i,j,k,num=0,r= -1; |
125 | unsigned char *buf=NULL; | 110 | unsigned char *buf=NULL; |
126 | BN_CTX *ctx=NULL; | 111 | BN_CTX *ctx=NULL; |
127 | 112 | ||
128 | /* Body of this routine removed for OpenBSD - will return | 113 | BN_init(&f); |
129 | * when the RSA patent expires | 114 | BN_init(&ret); |
130 | */ | 115 | if ((ctx=BN_CTX_new()) == NULL) goto err; |
116 | num=BN_num_bytes(rsa->n); | ||
117 | if ((buf=(unsigned char *)Malloc(num)) == NULL) | ||
118 | { | ||
119 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,ERR_R_MALLOC_FAILURE); | ||
120 | goto err; | ||
121 | } | ||
122 | |||
123 | switch (padding) | ||
124 | { | ||
125 | case RSA_PKCS1_PADDING: | ||
126 | i=RSA_padding_add_PKCS1_type_2(buf,num,from,flen); | ||
127 | break; | ||
128 | #ifndef NO_SHA | ||
129 | case RSA_PKCS1_OAEP_PADDING: | ||
130 | i=RSA_padding_add_PKCS1_OAEP(buf,num,from,flen,NULL,0); | ||
131 | break; | ||
132 | #endif | ||
133 | case RSA_SSLV23_PADDING: | ||
134 | i=RSA_padding_add_SSLv23(buf,num,from,flen); | ||
135 | break; | ||
136 | case RSA_NO_PADDING: | ||
137 | i=RSA_padding_add_none(buf,num,from,flen); | ||
138 | break; | ||
139 | default: | ||
140 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE); | ||
141 | goto err; | ||
142 | } | ||
143 | if (i <= 0) goto err; | ||
144 | |||
145 | if (BN_bin2bn(buf,num,&f) == NULL) goto err; | ||
146 | |||
147 | if ((rsa->_method_mod_n == NULL) && (rsa->flags & RSA_FLAG_CACHE_PUBLIC)) | ||
148 | { | ||
149 | if ((rsa->_method_mod_n=BN_MONT_CTX_new()) != NULL) | ||
150 | if (!BN_MONT_CTX_set(rsa->_method_mod_n,rsa->n,ctx)) | ||
151 | goto err; | ||
152 | } | ||
153 | |||
154 | if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->e,rsa->n,ctx, | ||
155 | rsa->_method_mod_n)) goto err; | ||
131 | 156 | ||
157 | /* put in leading 0 bytes if the number is less than the | ||
158 | * length of the modulus */ | ||
159 | j=BN_num_bytes(&ret); | ||
160 | i=BN_bn2bin(&ret,&(to[num-j])); | ||
161 | for (k=0; k<(num-i); k++) | ||
162 | to[k]=0; | ||
163 | |||
164 | r=num; | ||
132 | err: | 165 | err: |
133 | if (ctx != NULL) BN_CTX_free(ctx); | 166 | if (ctx != NULL) BN_CTX_free(ctx); |
134 | if (f != NULL) BN_free(f); | 167 | BN_clear_free(&f); |
135 | if (ret != NULL) BN_free(ret); | 168 | BN_clear_free(&ret); |
136 | if (buf != NULL) | 169 | if (buf != NULL) |
137 | { | 170 | { |
138 | memset(buf,0,num); | 171 | memset(buf,0,num); |
@@ -141,14 +174,10 @@ err: | |||
141 | return(r); | 174 | return(r); |
142 | } | 175 | } |
143 | 176 | ||
144 | static int RSA_eay_private_encrypt(flen, from, to, rsa, padding) | 177 | static int RSA_eay_private_encrypt(int flen, unsigned char *from, |
145 | int flen; | 178 | unsigned char *to, RSA *rsa, int padding) |
146 | unsigned char *from; | ||
147 | unsigned char *to; | ||
148 | RSA *rsa; | ||
149 | int padding; | ||
150 | { | 179 | { |
151 | BIGNUM *f=NULL,*ret=NULL; | 180 | BIGNUM f,ret; |
152 | int i,j,k,num=0,r= -1; | 181 | int i,j,k,num=0,r= -1; |
153 | unsigned char *buf=NULL; | 182 | unsigned char *buf=NULL; |
154 | BN_CTX *ctx=NULL; | 183 | BN_CTX *ctx=NULL; |
@@ -159,8 +188,8 @@ int padding; | |||
159 | 188 | ||
160 | err: | 189 | err: |
161 | if (ctx != NULL) BN_CTX_free(ctx); | 190 | if (ctx != NULL) BN_CTX_free(ctx); |
162 | if (ret != NULL) BN_free(ret); | 191 | BN_clear_free(&ret); |
163 | if (f != NULL) BN_free(f); | 192 | BN_clear_free(&f); |
164 | if (buf != NULL) | 193 | if (buf != NULL) |
165 | { | 194 | { |
166 | memset(buf,0,num); | 195 | memset(buf,0,num); |
@@ -169,14 +198,10 @@ err: | |||
169 | return(r); | 198 | return(r); |
170 | } | 199 | } |
171 | 200 | ||
172 | static int RSA_eay_private_decrypt(flen, from, to, rsa,padding) | 201 | static int RSA_eay_private_decrypt(int flen, unsigned char *from, |
173 | int flen; | 202 | unsigned char *to, RSA *rsa, int padding) |
174 | unsigned char *from; | ||
175 | unsigned char *to; | ||
176 | RSA *rsa; | ||
177 | int padding; | ||
178 | { | 203 | { |
179 | BIGNUM *f=NULL,*ret=NULL; | 204 | BIGNUM f,ret; |
180 | int j,num=0,r= -1; | 205 | int j,num=0,r= -1; |
181 | unsigned char *p; | 206 | unsigned char *p; |
182 | unsigned char *buf=NULL; | 207 | unsigned char *buf=NULL; |
@@ -188,8 +213,8 @@ int padding; | |||
188 | 213 | ||
189 | err: | 214 | err: |
190 | if (ctx != NULL) BN_CTX_free(ctx); | 215 | if (ctx != NULL) BN_CTX_free(ctx); |
191 | if (f != NULL) BN_free(f); | 216 | BN_clear_free(&f); |
192 | if (ret != NULL) BN_free(ret); | 217 | BN_clear_free(&ret); |
193 | if (buf != NULL) | 218 | if (buf != NULL) |
194 | { | 219 | { |
195 | memset(buf,0,num); | 220 | memset(buf,0,num); |
@@ -198,28 +223,23 @@ err: | |||
198 | return(r); | 223 | return(r); |
199 | } | 224 | } |
200 | 225 | ||
201 | static int RSA_eay_public_decrypt(flen, from, to, rsa, padding) | 226 | static int RSA_eay_public_decrypt(int flen, unsigned char *from, |
202 | int flen; | 227 | unsigned char *to, RSA *rsa, int padding) |
203 | unsigned char *from; | ||
204 | unsigned char *to; | ||
205 | RSA *rsa; | ||
206 | int padding; | ||
207 | { | 228 | { |
208 | BIGNUM *f=NULL,*ret=NULL; | 229 | BIGNUM f,ret; |
209 | int i,num=0,r= -1; | 230 | int i,num=0,r= -1; |
210 | unsigned char *p; | 231 | unsigned char *p; |
211 | unsigned char *buf=NULL; | 232 | unsigned char *buf=NULL; |
212 | BN_CTX *ctx=NULL; | 233 | BN_CTX *ctx=NULL; |
213 | 234 | ||
214 | |||
215 | /* Body of this routine removed for OpenBSD - will return | 235 | /* Body of this routine removed for OpenBSD - will return |
216 | * when the RSA patent expires | 236 | * when the RSA patent expires |
217 | */ | 237 | */ |
218 | 238 | ||
219 | err: | 239 | err: |
220 | if (ctx != NULL) BN_CTX_free(ctx); | 240 | if (ctx != NULL) BN_CTX_free(ctx); |
221 | if (f != NULL) BN_free(f); | 241 | BN_clear_free(&f); |
222 | if (ret != NULL) BN_free(ret); | 242 | BN_clear_free(&ret); |
223 | if (buf != NULL) | 243 | if (buf != NULL) |
224 | { | 244 | { |
225 | memset(buf,0,num); | 245 | memset(buf,0,num); |
@@ -228,46 +248,40 @@ err: | |||
228 | return(r); | 248 | return(r); |
229 | } | 249 | } |
230 | 250 | ||
231 | static int RSA_eay_mod_exp(r0, I, rsa) | 251 | static int RSA_eay_mod_exp(BIGNUM *r0, BIGNUM *I, RSA *rsa) |
232 | BIGNUM *r0; | ||
233 | BIGNUM *I; | ||
234 | RSA *rsa; | ||
235 | { | 252 | { |
236 | BIGNUM *r1=NULL,*m1=NULL; | 253 | BIGNUM r1,m1; |
237 | int ret=0; | 254 | int ret=0; |
238 | BN_CTX *ctx; | 255 | BN_CTX *ctx; |
239 | 256 | ||
240 | if ((ctx=BN_CTX_new()) == NULL) goto err; | 257 | if ((ctx=BN_CTX_new()) == NULL) goto err; |
241 | m1=BN_new(); | 258 | BN_init(&m1); |
242 | r1=BN_new(); | 259 | BN_init(&r1); |
243 | if ((m1 == NULL) || (r1 == NULL)) goto err; | ||
244 | 260 | ||
245 | /* Body of this routine removed for OpenBSD - will return | 261 | /* Body of this routine removed for OpenBSD - will return |
246 | * when the RSA patent expires | 262 | * when the RSA patent expires |
247 | */ | 263 | */ |
248 | err: | 264 | err: |
249 | if (m1 != NULL) BN_free(m1); | 265 | BN_clear_free(&m1); |
250 | if (r1 != NULL) BN_free(r1); | 266 | BN_clear_free(&r1); |
251 | BN_CTX_free(ctx); | 267 | BN_CTX_free(ctx); |
252 | return(ret); | 268 | return(ret); |
253 | } | 269 | } |
254 | 270 | ||
255 | static int RSA_eay_init(rsa) | 271 | static int RSA_eay_init(RSA *rsa) |
256 | RSA *rsa; | ||
257 | { | 272 | { |
258 | rsa->flags|=RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE; | 273 | rsa->flags|=RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE; |
259 | return(1); | 274 | return(1); |
260 | } | 275 | } |
261 | 276 | ||
262 | static int RSA_eay_finish(rsa) | 277 | static int RSA_eay_finish(RSA *rsa) |
263 | RSA *rsa; | ||
264 | { | 278 | { |
265 | if (rsa->method_mod_n != NULL) | 279 | if (rsa->_method_mod_n != NULL) |
266 | BN_MONT_CTX_free((BN_MONT_CTX *)rsa->method_mod_n); | 280 | BN_MONT_CTX_free(rsa->_method_mod_n); |
267 | if (rsa->method_mod_p != NULL) | 281 | if (rsa->_method_mod_p != NULL) |
268 | BN_MONT_CTX_free((BN_MONT_CTX *)rsa->method_mod_p); | 282 | BN_MONT_CTX_free(rsa->_method_mod_p); |
269 | if (rsa->method_mod_q != NULL) | 283 | if (rsa->_method_mod_q != NULL) |
270 | BN_MONT_CTX_free((BN_MONT_CTX *)rsa->method_mod_q); | 284 | BN_MONT_CTX_free(rsa->_method_mod_q); |
271 | return(1); | 285 | return(1); |
272 | } | 286 | } |
273 | 287 | ||