diff options
Diffstat (limited to 'src/lib/libcrypto/rsa/rsa_eay.c')
-rw-r--r-- | src/lib/libcrypto/rsa/rsa_eay.c | 774 |
1 files changed, 0 insertions, 774 deletions
diff --git a/src/lib/libcrypto/rsa/rsa_eay.c b/src/lib/libcrypto/rsa/rsa_eay.c deleted file mode 100644 index be4ac96ce3..0000000000 --- a/src/lib/libcrypto/rsa/rsa_eay.c +++ /dev/null | |||
@@ -1,774 +0,0 @@ | |||
1 | /* crypto/rsa/rsa_eay.c */ | ||
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * This package is an SSL implementation written | ||
6 | * by Eric Young (eay@cryptsoft.com). | ||
7 | * The implementation was written so as to conform with Netscapes SSL. | ||
8 | * | ||
9 | * This library is free for commercial and non-commercial use as long as | ||
10 | * the following conditions are aheared to. The following conditions | ||
11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
13 | * included with this distribution is covered by the same copyright terms | ||
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
15 | * | ||
16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
17 | * the code are not to be removed. | ||
18 | * If this package is used in a product, Eric Young should be given attribution | ||
19 | * as the author of the parts of the library used. | ||
20 | * This can be in the form of a textual message at program startup or | ||
21 | * in documentation (online or textual) provided with the package. | ||
22 | * | ||
23 | * Redistribution and use in source and binary forms, with or without | ||
24 | * modification, are permitted provided that the following conditions | ||
25 | * are met: | ||
26 | * 1. Redistributions of source code must retain the copyright | ||
27 | * notice, this list of conditions and the following disclaimer. | ||
28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
29 | * notice, this list of conditions and the following disclaimer in the | ||
30 | * documentation and/or other materials provided with the distribution. | ||
31 | * 3. All advertising materials mentioning features or use of this software | ||
32 | * must display the following acknowledgement: | ||
33 | * "This product includes cryptographic software written by | ||
34 | * Eric Young (eay@cryptsoft.com)" | ||
35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
36 | * being used are not cryptographic related :-). | ||
37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
38 | * the apps directory (application code) you must include an acknowledgement: | ||
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
40 | * | ||
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
51 | * SUCH DAMAGE. | ||
52 | * | ||
53 | * The licence and distribution terms for any publically available version or | ||
54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
55 | * copied and put under another distribution licence | ||
56 | * [including the GNU Public Licence.] | ||
57 | */ | ||
58 | /* ==================================================================== | ||
59 | * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. | ||
60 | * | ||
61 | * Redistribution and use in source and binary forms, with or without | ||
62 | * modification, are permitted provided that the following conditions | ||
63 | * are met: | ||
64 | * | ||
65 | * 1. Redistributions of source code must retain the above copyright | ||
66 | * notice, this list of conditions and the following disclaimer. | ||
67 | * | ||
68 | * 2. Redistributions in binary form must reproduce the above copyright | ||
69 | * notice, this list of conditions and the following disclaimer in | ||
70 | * the documentation and/or other materials provided with the | ||
71 | * distribution. | ||
72 | * | ||
73 | * 3. All advertising materials mentioning features or use of this | ||
74 | * software must display the following acknowledgment: | ||
75 | * "This product includes software developed by the OpenSSL Project | ||
76 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | ||
77 | * | ||
78 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
79 | * endorse or promote products derived from this software without | ||
80 | * prior written permission. For written permission, please contact | ||
81 | * openssl-core@openssl.org. | ||
82 | * | ||
83 | * 5. Products derived from this software may not be called "OpenSSL" | ||
84 | * nor may "OpenSSL" appear in their names without prior written | ||
85 | * permission of the OpenSSL Project. | ||
86 | * | ||
87 | * 6. Redistributions of any form whatsoever must retain the following | ||
88 | * acknowledgment: | ||
89 | * "This product includes software developed by the OpenSSL Project | ||
90 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | ||
91 | * | ||
92 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
93 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
94 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
95 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
96 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
97 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
98 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
99 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
100 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
101 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
102 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
103 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
104 | * ==================================================================== | ||
105 | * | ||
106 | * This product includes cryptographic software written by Eric Young | ||
107 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
108 | * Hudson (tjh@cryptsoft.com). | ||
109 | * | ||
110 | */ | ||
111 | |||
112 | #include <stdio.h> | ||
113 | #include "cryptlib.h" | ||
114 | #include <openssl/bn.h> | ||
115 | #include <openssl/rsa.h> | ||
116 | #include <openssl/rand.h> | ||
117 | |||
118 | #if !defined(RSA_NULL) && !defined(OPENSSL_FIPS) | ||
119 | |||
120 | static int RSA_eay_public_encrypt(int flen, const unsigned char *from, | ||
121 | unsigned char *to, RSA *rsa,int padding); | ||
122 | static int RSA_eay_private_encrypt(int flen, const unsigned char *from, | ||
123 | unsigned char *to, RSA *rsa,int padding); | ||
124 | static int RSA_eay_public_decrypt(int flen, const unsigned char *from, | ||
125 | unsigned char *to, RSA *rsa,int padding); | ||
126 | static int RSA_eay_private_decrypt(int flen, const unsigned char *from, | ||
127 | unsigned char *to, RSA *rsa,int padding); | ||
128 | static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa); | ||
129 | static int RSA_eay_init(RSA *rsa); | ||
130 | static int RSA_eay_finish(RSA *rsa); | ||
131 | static RSA_METHOD rsa_pkcs1_eay_meth={ | ||
132 | "Eric Young's PKCS#1 RSA", | ||
133 | RSA_eay_public_encrypt, | ||
134 | RSA_eay_public_decrypt, /* signature verification */ | ||
135 | RSA_eay_private_encrypt, /* signing */ | ||
136 | RSA_eay_private_decrypt, | ||
137 | RSA_eay_mod_exp, | ||
138 | BN_mod_exp_mont, /* XXX probably we should not use Montgomery if e == 3 */ | ||
139 | RSA_eay_init, | ||
140 | RSA_eay_finish, | ||
141 | 0, /* flags */ | ||
142 | NULL, | ||
143 | 0, /* rsa_sign */ | ||
144 | 0 /* rsa_verify */ | ||
145 | }; | ||
146 | |||
147 | const RSA_METHOD *RSA_PKCS1_SSLeay(void) | ||
148 | { | ||
149 | return(&rsa_pkcs1_eay_meth); | ||
150 | } | ||
151 | |||
152 | static int RSA_eay_public_encrypt(int flen, const unsigned char *from, | ||
153 | unsigned char *to, RSA *rsa, int padding) | ||
154 | { | ||
155 | BIGNUM f,ret; | ||
156 | int i,j,k,num=0,r= -1; | ||
157 | unsigned char *buf=NULL; | ||
158 | BN_CTX *ctx=NULL; | ||
159 | |||
160 | BN_init(&f); | ||
161 | BN_init(&ret); | ||
162 | if ((ctx=BN_CTX_new()) == NULL) goto err; | ||
163 | num=BN_num_bytes(rsa->n); | ||
164 | if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL) | ||
165 | { | ||
166 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,ERR_R_MALLOC_FAILURE); | ||
167 | goto err; | ||
168 | } | ||
169 | |||
170 | switch (padding) | ||
171 | { | ||
172 | case RSA_PKCS1_PADDING: | ||
173 | i=RSA_padding_add_PKCS1_type_2(buf,num,from,flen); | ||
174 | break; | ||
175 | #ifndef OPENSSL_NO_SHA | ||
176 | case RSA_PKCS1_OAEP_PADDING: | ||
177 | i=RSA_padding_add_PKCS1_OAEP(buf,num,from,flen,NULL,0); | ||
178 | break; | ||
179 | #endif | ||
180 | case RSA_SSLV23_PADDING: | ||
181 | i=RSA_padding_add_SSLv23(buf,num,from,flen); | ||
182 | break; | ||
183 | case RSA_NO_PADDING: | ||
184 | i=RSA_padding_add_none(buf,num,from,flen); | ||
185 | break; | ||
186 | default: | ||
187 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE); | ||
188 | goto err; | ||
189 | } | ||
190 | if (i <= 0) goto err; | ||
191 | |||
192 | if (BN_bin2bn(buf,num,&f) == NULL) goto err; | ||
193 | |||
194 | if (BN_ucmp(&f, rsa->n) >= 0) | ||
195 | { | ||
196 | /* usually the padding functions would catch this */ | ||
197 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS); | ||
198 | goto err; | ||
199 | } | ||
200 | |||
201 | if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) | ||
202 | { | ||
203 | if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, | ||
204 | CRYPTO_LOCK_RSA, rsa->n, ctx)) | ||
205 | goto err; | ||
206 | } | ||
207 | |||
208 | if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->e,rsa->n,ctx, | ||
209 | rsa->_method_mod_n)) goto err; | ||
210 | |||
211 | /* put in leading 0 bytes if the number is less than the | ||
212 | * length of the modulus */ | ||
213 | j=BN_num_bytes(&ret); | ||
214 | i=BN_bn2bin(&ret,&(to[num-j])); | ||
215 | for (k=0; k<(num-i); k++) | ||
216 | to[k]=0; | ||
217 | |||
218 | r=num; | ||
219 | err: | ||
220 | if (ctx != NULL) BN_CTX_free(ctx); | ||
221 | BN_clear_free(&f); | ||
222 | BN_clear_free(&ret); | ||
223 | if (buf != NULL) | ||
224 | { | ||
225 | OPENSSL_cleanse(buf,num); | ||
226 | OPENSSL_free(buf); | ||
227 | } | ||
228 | return(r); | ||
229 | } | ||
230 | |||
231 | static int rsa_eay_blinding(RSA *rsa, BN_CTX *ctx) | ||
232 | { | ||
233 | int ret = 1; | ||
234 | CRYPTO_w_lock(CRYPTO_LOCK_RSA); | ||
235 | /* Check again inside the lock - the macro's check is racey */ | ||
236 | if(rsa->blinding == NULL) | ||
237 | ret = RSA_blinding_on(rsa, ctx); | ||
238 | CRYPTO_w_unlock(CRYPTO_LOCK_RSA); | ||
239 | return ret; | ||
240 | } | ||
241 | |||
242 | #define BLINDING_HELPER(rsa, ctx, err_instr) \ | ||
243 | do { \ | ||
244 | if((!((rsa)->flags & RSA_FLAG_NO_BLINDING)) && \ | ||
245 | ((rsa)->blinding == NULL) && \ | ||
246 | !rsa_eay_blinding(rsa, ctx)) \ | ||
247 | err_instr \ | ||
248 | } while(0) | ||
249 | |||
250 | static BN_BLINDING *setup_blinding(RSA *rsa, BN_CTX *ctx) | ||
251 | { | ||
252 | BIGNUM *A, *Ai; | ||
253 | BN_BLINDING *ret = NULL; | ||
254 | |||
255 | /* added in OpenSSL 0.9.6j and 0.9.7b */ | ||
256 | |||
257 | /* NB: similar code appears in RSA_blinding_on (rsa_lib.c); | ||
258 | * this should be placed in a new function of its own, but for reasons | ||
259 | * of binary compatibility can't */ | ||
260 | |||
261 | BN_CTX_start(ctx); | ||
262 | A = BN_CTX_get(ctx); | ||
263 | if ((RAND_status() == 0) && rsa->d != NULL && rsa->d->d != NULL) | ||
264 | { | ||
265 | /* if PRNG is not properly seeded, resort to secret exponent as unpredictable seed */ | ||
266 | RAND_add(rsa->d->d, rsa->d->dmax * sizeof rsa->d->d[0], 0); | ||
267 | if (!BN_pseudo_rand_range(A,rsa->n)) goto err; | ||
268 | } | ||
269 | else | ||
270 | { | ||
271 | if (!BN_rand_range(A,rsa->n)) goto err; | ||
272 | } | ||
273 | if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err; | ||
274 | |||
275 | if (!rsa->meth->bn_mod_exp(A,A,rsa->e,rsa->n,ctx,rsa->_method_mod_n)) | ||
276 | goto err; | ||
277 | ret = BN_BLINDING_new(A,Ai,rsa->n); | ||
278 | BN_free(Ai); | ||
279 | err: | ||
280 | BN_CTX_end(ctx); | ||
281 | return ret; | ||
282 | } | ||
283 | |||
284 | /* signing */ | ||
285 | static int RSA_eay_private_encrypt(int flen, const unsigned char *from, | ||
286 | unsigned char *to, RSA *rsa, int padding) | ||
287 | { | ||
288 | BIGNUM f,ret, *res; | ||
289 | int i,j,k,num=0,r= -1; | ||
290 | unsigned char *buf=NULL; | ||
291 | BN_CTX *ctx=NULL; | ||
292 | int local_blinding = 0; | ||
293 | BN_BLINDING *blinding = NULL; | ||
294 | |||
295 | BN_init(&f); | ||
296 | BN_init(&ret); | ||
297 | |||
298 | if ((ctx=BN_CTX_new()) == NULL) goto err; | ||
299 | num=BN_num_bytes(rsa->n); | ||
300 | if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL) | ||
301 | { | ||
302 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,ERR_R_MALLOC_FAILURE); | ||
303 | goto err; | ||
304 | } | ||
305 | |||
306 | switch (padding) | ||
307 | { | ||
308 | case RSA_PKCS1_PADDING: | ||
309 | i=RSA_padding_add_PKCS1_type_1(buf,num,from,flen); | ||
310 | break; | ||
311 | case RSA_NO_PADDING: | ||
312 | i=RSA_padding_add_none(buf,num,from,flen); | ||
313 | break; | ||
314 | case RSA_SSLV23_PADDING: | ||
315 | default: | ||
316 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE); | ||
317 | goto err; | ||
318 | } | ||
319 | if (i <= 0) goto err; | ||
320 | |||
321 | if (BN_bin2bn(buf,num,&f) == NULL) goto err; | ||
322 | |||
323 | if (BN_ucmp(&f, rsa->n) >= 0) | ||
324 | { | ||
325 | /* usually the padding functions would catch this */ | ||
326 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS); | ||
327 | goto err; | ||
328 | } | ||
329 | |||
330 | BLINDING_HELPER(rsa, ctx, goto err;); | ||
331 | blinding = rsa->blinding; | ||
332 | |||
333 | /* Now unless blinding is disabled, 'blinding' is non-NULL. | ||
334 | * But the BN_BLINDING object may be owned by some other thread | ||
335 | * (we don't want to keep it constant and we don't want to use | ||
336 | * lots of locking to avoid race conditions, so only a single | ||
337 | * thread can use it; other threads have to use local blinding | ||
338 | * factors) */ | ||
339 | if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) | ||
340 | { | ||
341 | if (blinding == NULL) | ||
342 | { | ||
343 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR); | ||
344 | goto err; | ||
345 | } | ||
346 | } | ||
347 | |||
348 | if (blinding != NULL) | ||
349 | { | ||
350 | if (blinding->thread_id != CRYPTO_thread_id()) | ||
351 | { | ||
352 | /* we need a local one-time blinding factor */ | ||
353 | |||
354 | blinding = setup_blinding(rsa, ctx); | ||
355 | if (blinding == NULL) | ||
356 | goto err; | ||
357 | local_blinding = 1; | ||
358 | } | ||
359 | } | ||
360 | |||
361 | if (blinding) | ||
362 | if (!BN_BLINDING_convert(&f, blinding, ctx)) goto err; | ||
363 | |||
364 | if ( (rsa->flags & RSA_FLAG_EXT_PKEY) || | ||
365 | ((rsa->p != NULL) && | ||
366 | (rsa->q != NULL) && | ||
367 | (rsa->dmp1 != NULL) && | ||
368 | (rsa->dmq1 != NULL) && | ||
369 | (rsa->iqmp != NULL)) ) | ||
370 | { | ||
371 | if (!rsa->meth->rsa_mod_exp(&ret,&f,rsa)) goto err; | ||
372 | } | ||
373 | else | ||
374 | { | ||
375 | BIGNUM local_d; | ||
376 | BIGNUM *d = NULL; | ||
377 | |||
378 | if (!(rsa->flags & RSA_FLAG_NO_EXP_CONSTTIME)) | ||
379 | { | ||
380 | BN_init(&local_d); | ||
381 | d = &local_d; | ||
382 | BN_with_flags(d, rsa->d, BN_FLG_EXP_CONSTTIME); | ||
383 | } | ||
384 | else | ||
385 | d = rsa->d; | ||
386 | if (!rsa->meth->bn_mod_exp(&ret,&f,d,rsa->n,ctx,NULL)) goto err; | ||
387 | } | ||
388 | |||
389 | if (blinding) | ||
390 | if (!BN_BLINDING_invert(&ret, blinding, ctx)) goto err; | ||
391 | |||
392 | if (padding == RSA_X931_PADDING) | ||
393 | { | ||
394 | BN_sub(&f, rsa->n, &ret); | ||
395 | if (BN_cmp(&ret, &f)) | ||
396 | res = &f; | ||
397 | else | ||
398 | res = &ret; | ||
399 | } | ||
400 | else | ||
401 | res = &ret; | ||
402 | |||
403 | /* put in leading 0 bytes if the number is less than the | ||
404 | * length of the modulus */ | ||
405 | j=BN_num_bytes(res); | ||
406 | i=BN_bn2bin(res,&(to[num-j])); | ||
407 | for (k=0; k<(num-i); k++) | ||
408 | to[k]=0; | ||
409 | |||
410 | r=num; | ||
411 | err: | ||
412 | if (ctx != NULL) BN_CTX_free(ctx); | ||
413 | BN_clear_free(&ret); | ||
414 | BN_clear_free(&f); | ||
415 | if (local_blinding) | ||
416 | BN_BLINDING_free(blinding); | ||
417 | if (buf != NULL) | ||
418 | { | ||
419 | OPENSSL_cleanse(buf,num); | ||
420 | OPENSSL_free(buf); | ||
421 | } | ||
422 | return(r); | ||
423 | } | ||
424 | |||
425 | static int RSA_eay_private_decrypt(int flen, const unsigned char *from, | ||
426 | unsigned char *to, RSA *rsa, int padding) | ||
427 | { | ||
428 | BIGNUM f,ret; | ||
429 | int j,num=0,r= -1; | ||
430 | unsigned char *p; | ||
431 | unsigned char *buf=NULL; | ||
432 | BN_CTX *ctx=NULL; | ||
433 | int local_blinding = 0; | ||
434 | BN_BLINDING *blinding = NULL; | ||
435 | |||
436 | BN_init(&f); | ||
437 | BN_init(&ret); | ||
438 | ctx=BN_CTX_new(); | ||
439 | if (ctx == NULL) goto err; | ||
440 | |||
441 | num=BN_num_bytes(rsa->n); | ||
442 | |||
443 | if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL) | ||
444 | { | ||
445 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE); | ||
446 | goto err; | ||
447 | } | ||
448 | |||
449 | /* This check was for equality but PGP does evil things | ||
450 | * and chops off the top '0' bytes */ | ||
451 | if (flen > num) | ||
452 | { | ||
453 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN); | ||
454 | goto err; | ||
455 | } | ||
456 | |||
457 | /* make data into a big number */ | ||
458 | if (BN_bin2bn(from,(int)flen,&f) == NULL) goto err; | ||
459 | |||
460 | if (BN_ucmp(&f, rsa->n) >= 0) | ||
461 | { | ||
462 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS); | ||
463 | goto err; | ||
464 | } | ||
465 | |||
466 | BLINDING_HELPER(rsa, ctx, goto err;); | ||
467 | blinding = rsa->blinding; | ||
468 | |||
469 | /* Now unless blinding is disabled, 'blinding' is non-NULL. | ||
470 | * But the BN_BLINDING object may be owned by some other thread | ||
471 | * (we don't want to keep it constant and we don't want to use | ||
472 | * lots of locking to avoid race conditions, so only a single | ||
473 | * thread can use it; other threads have to use local blinding | ||
474 | * factors) */ | ||
475 | if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) | ||
476 | { | ||
477 | if (blinding == NULL) | ||
478 | { | ||
479 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR); | ||
480 | goto err; | ||
481 | } | ||
482 | } | ||
483 | |||
484 | if (blinding != NULL) | ||
485 | { | ||
486 | if (blinding->thread_id != CRYPTO_thread_id()) | ||
487 | { | ||
488 | /* we need a local one-time blinding factor */ | ||
489 | |||
490 | blinding = setup_blinding(rsa, ctx); | ||
491 | if (blinding == NULL) | ||
492 | goto err; | ||
493 | local_blinding = 1; | ||
494 | } | ||
495 | } | ||
496 | |||
497 | if (blinding) | ||
498 | if (!BN_BLINDING_convert(&f, blinding, ctx)) goto err; | ||
499 | |||
500 | /* do the decrypt */ | ||
501 | if ( (rsa->flags & RSA_FLAG_EXT_PKEY) || | ||
502 | ((rsa->p != NULL) && | ||
503 | (rsa->q != NULL) && | ||
504 | (rsa->dmp1 != NULL) && | ||
505 | (rsa->dmq1 != NULL) && | ||
506 | (rsa->iqmp != NULL)) ) | ||
507 | { | ||
508 | if (!rsa->meth->rsa_mod_exp(&ret,&f,rsa)) goto err; | ||
509 | } | ||
510 | else | ||
511 | { | ||
512 | BIGNUM local_d; | ||
513 | BIGNUM *d = NULL; | ||
514 | |||
515 | if (!(rsa->flags & RSA_FLAG_NO_EXP_CONSTTIME)) | ||
516 | { | ||
517 | d = &local_d; | ||
518 | BN_with_flags(d, rsa->d, BN_FLG_EXP_CONSTTIME); | ||
519 | } | ||
520 | else | ||
521 | d = rsa->d; | ||
522 | if (!rsa->meth->bn_mod_exp(&ret,&f,d,rsa->n,ctx,NULL)) | ||
523 | goto err; | ||
524 | } | ||
525 | |||
526 | if (blinding) | ||
527 | if (!BN_BLINDING_invert(&ret, blinding, ctx)) goto err; | ||
528 | |||
529 | p=buf; | ||
530 | j=BN_bn2bin(&ret,p); /* j is only used with no-padding mode */ | ||
531 | |||
532 | switch (padding) | ||
533 | { | ||
534 | case RSA_PKCS1_PADDING: | ||
535 | r=RSA_padding_check_PKCS1_type_2(to,num,buf,j,num); | ||
536 | break; | ||
537 | #ifndef OPENSSL_NO_SHA | ||
538 | case RSA_PKCS1_OAEP_PADDING: | ||
539 | r=RSA_padding_check_PKCS1_OAEP(to,num,buf,j,num,NULL,0); | ||
540 | break; | ||
541 | #endif | ||
542 | case RSA_SSLV23_PADDING: | ||
543 | r=RSA_padding_check_SSLv23(to,num,buf,j,num); | ||
544 | break; | ||
545 | case RSA_NO_PADDING: | ||
546 | r=RSA_padding_check_none(to,num,buf,j,num); | ||
547 | break; | ||
548 | default: | ||
549 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE); | ||
550 | goto err; | ||
551 | } | ||
552 | if (r < 0) | ||
553 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_PADDING_CHECK_FAILED); | ||
554 | |||
555 | err: | ||
556 | if (ctx != NULL) BN_CTX_free(ctx); | ||
557 | BN_clear_free(&f); | ||
558 | BN_clear_free(&ret); | ||
559 | if (local_blinding) | ||
560 | BN_BLINDING_free(blinding); | ||
561 | if (buf != NULL) | ||
562 | { | ||
563 | OPENSSL_cleanse(buf,num); | ||
564 | OPENSSL_free(buf); | ||
565 | } | ||
566 | return(r); | ||
567 | } | ||
568 | |||
569 | /* signature verification */ | ||
570 | static int RSA_eay_public_decrypt(int flen, const unsigned char *from, | ||
571 | unsigned char *to, RSA *rsa, int padding) | ||
572 | { | ||
573 | BIGNUM f,ret; | ||
574 | int i,num=0,r= -1; | ||
575 | unsigned char *p; | ||
576 | unsigned char *buf=NULL; | ||
577 | BN_CTX *ctx=NULL; | ||
578 | |||
579 | BN_init(&f); | ||
580 | BN_init(&ret); | ||
581 | ctx=BN_CTX_new(); | ||
582 | if (ctx == NULL) goto err; | ||
583 | |||
584 | num=BN_num_bytes(rsa->n); | ||
585 | buf=(unsigned char *)OPENSSL_malloc(num); | ||
586 | if (buf == NULL) | ||
587 | { | ||
588 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,ERR_R_MALLOC_FAILURE); | ||
589 | goto err; | ||
590 | } | ||
591 | |||
592 | /* This check was for equality but PGP does evil things | ||
593 | * and chops off the top '0' bytes */ | ||
594 | if (flen > num) | ||
595 | { | ||
596 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN); | ||
597 | goto err; | ||
598 | } | ||
599 | |||
600 | if (BN_bin2bn(from,flen,&f) == NULL) goto err; | ||
601 | |||
602 | if (BN_ucmp(&f, rsa->n) >= 0) | ||
603 | { | ||
604 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS); | ||
605 | goto err; | ||
606 | } | ||
607 | |||
608 | /* do the decrypt */ | ||
609 | |||
610 | if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) | ||
611 | { | ||
612 | if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, | ||
613 | CRYPTO_LOCK_RSA, rsa->n, ctx)) | ||
614 | goto err; | ||
615 | } | ||
616 | |||
617 | if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->e,rsa->n,ctx, | ||
618 | rsa->_method_mod_n)) goto err; | ||
619 | |||
620 | if ((padding == RSA_X931_PADDING) && ((ret.d[0] & 0xf) != 12)) | ||
621 | BN_sub(&ret, rsa->n, &ret); | ||
622 | |||
623 | p=buf; | ||
624 | i=BN_bn2bin(&ret,p); | ||
625 | |||
626 | switch (padding) | ||
627 | { | ||
628 | case RSA_PKCS1_PADDING: | ||
629 | r=RSA_padding_check_PKCS1_type_1(to,num,buf,i,num); | ||
630 | break; | ||
631 | case RSA_NO_PADDING: | ||
632 | r=RSA_padding_check_none(to,num,buf,i,num); | ||
633 | break; | ||
634 | default: | ||
635 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE); | ||
636 | goto err; | ||
637 | } | ||
638 | if (r < 0) | ||
639 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_PADDING_CHECK_FAILED); | ||
640 | |||
641 | err: | ||
642 | if (ctx != NULL) BN_CTX_free(ctx); | ||
643 | BN_clear_free(&f); | ||
644 | BN_clear_free(&ret); | ||
645 | if (buf != NULL) | ||
646 | { | ||
647 | OPENSSL_cleanse(buf,num); | ||
648 | OPENSSL_free(buf); | ||
649 | } | ||
650 | return(r); | ||
651 | } | ||
652 | |||
653 | static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa) | ||
654 | { | ||
655 | BIGNUM r1,m1,vrfy; | ||
656 | BIGNUM local_dmp1, local_dmq1; | ||
657 | BIGNUM *dmp1, *dmq1; | ||
658 | int ret=0; | ||
659 | BN_CTX *ctx; | ||
660 | |||
661 | BN_init(&m1); | ||
662 | BN_init(&r1); | ||
663 | BN_init(&vrfy); | ||
664 | if ((ctx=BN_CTX_new()) == NULL) goto err; | ||
665 | |||
666 | if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) | ||
667 | { | ||
668 | if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_p, | ||
669 | CRYPTO_LOCK_RSA, rsa->p, ctx)) | ||
670 | goto err; | ||
671 | if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_q, | ||
672 | CRYPTO_LOCK_RSA, rsa->q, ctx)) | ||
673 | goto err; | ||
674 | } | ||
675 | |||
676 | if (!BN_mod(&r1,I,rsa->q,ctx)) goto err; | ||
677 | if (!(rsa->flags & RSA_FLAG_NO_EXP_CONSTTIME)) | ||
678 | { | ||
679 | dmq1 = &local_dmq1; | ||
680 | BN_with_flags(dmq1, rsa->dmq1, BN_FLG_EXP_CONSTTIME); | ||
681 | } | ||
682 | else | ||
683 | dmq1 = rsa->dmq1; | ||
684 | if (!rsa->meth->bn_mod_exp(&m1,&r1,dmq1,rsa->q,ctx, | ||
685 | rsa->_method_mod_q)) goto err; | ||
686 | |||
687 | if (!BN_mod(&r1,I,rsa->p,ctx)) goto err; | ||
688 | if (!(rsa->flags & RSA_FLAG_NO_EXP_CONSTTIME)) | ||
689 | { | ||
690 | dmp1 = &local_dmp1; | ||
691 | BN_with_flags(dmp1, rsa->dmp1, BN_FLG_EXP_CONSTTIME); | ||
692 | } | ||
693 | else | ||
694 | dmp1 = rsa->dmp1; | ||
695 | if (!rsa->meth->bn_mod_exp(r0,&r1,dmp1,rsa->p,ctx, | ||
696 | rsa->_method_mod_p)) goto err; | ||
697 | |||
698 | if (!BN_sub(r0,r0,&m1)) goto err; | ||
699 | /* This will help stop the size of r0 increasing, which does | ||
700 | * affect the multiply if it optimised for a power of 2 size */ | ||
701 | if (r0->neg) | ||
702 | if (!BN_add(r0,r0,rsa->p)) goto err; | ||
703 | |||
704 | if (!BN_mul(&r1,r0,rsa->iqmp,ctx)) goto err; | ||
705 | if (!BN_mod(r0,&r1,rsa->p,ctx)) goto err; | ||
706 | /* If p < q it is occasionally possible for the correction of | ||
707 | * adding 'p' if r0 is negative above to leave the result still | ||
708 | * negative. This can break the private key operations: the following | ||
709 | * second correction should *always* correct this rare occurrence. | ||
710 | * This will *never* happen with OpenSSL generated keys because | ||
711 | * they ensure p > q [steve] | ||
712 | */ | ||
713 | if (r0->neg) | ||
714 | if (!BN_add(r0,r0,rsa->p)) goto err; | ||
715 | if (!BN_mul(&r1,r0,rsa->q,ctx)) goto err; | ||
716 | if (!BN_add(r0,&r1,&m1)) goto err; | ||
717 | |||
718 | if (rsa->e && rsa->n) | ||
719 | { | ||
720 | if (!rsa->meth->bn_mod_exp(&vrfy,r0,rsa->e,rsa->n,ctx,NULL)) goto err; | ||
721 | /* If 'I' was greater than (or equal to) rsa->n, the operation | ||
722 | * will be equivalent to using 'I mod n'. However, the result of | ||
723 | * the verify will *always* be less than 'n' so we don't check | ||
724 | * for absolute equality, just congruency. */ | ||
725 | if (!BN_sub(&vrfy, &vrfy, I)) goto err; | ||
726 | if (!BN_mod(&vrfy, &vrfy, rsa->n, ctx)) goto err; | ||
727 | if (vrfy.neg) | ||
728 | if (!BN_add(&vrfy, &vrfy, rsa->n)) goto err; | ||
729 | if (!BN_is_zero(&vrfy)) | ||
730 | { | ||
731 | /* 'I' and 'vrfy' aren't congruent mod n. Don't leak | ||
732 | * miscalculated CRT output, just do a raw (slower) | ||
733 | * mod_exp and return that instead. */ | ||
734 | |||
735 | BIGNUM local_d; | ||
736 | BIGNUM *d = NULL; | ||
737 | |||
738 | if (!(rsa->flags & RSA_FLAG_NO_EXP_CONSTTIME)) | ||
739 | { | ||
740 | d = &local_d; | ||
741 | BN_with_flags(d, rsa->d, BN_FLG_EXP_CONSTTIME); | ||
742 | } | ||
743 | else | ||
744 | d = rsa->d; | ||
745 | if (!rsa->meth->bn_mod_exp(r0,I,d,rsa->n,ctx,NULL)) goto err; | ||
746 | } | ||
747 | } | ||
748 | ret=1; | ||
749 | err: | ||
750 | BN_clear_free(&m1); | ||
751 | BN_clear_free(&r1); | ||
752 | BN_clear_free(&vrfy); | ||
753 | BN_CTX_free(ctx); | ||
754 | return(ret); | ||
755 | } | ||
756 | |||
757 | static int RSA_eay_init(RSA *rsa) | ||
758 | { | ||
759 | rsa->flags|=RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE; | ||
760 | return(1); | ||
761 | } | ||
762 | |||
763 | static int RSA_eay_finish(RSA *rsa) | ||
764 | { | ||
765 | if (rsa->_method_mod_n != NULL) | ||
766 | BN_MONT_CTX_free(rsa->_method_mod_n); | ||
767 | if (rsa->_method_mod_p != NULL) | ||
768 | BN_MONT_CTX_free(rsa->_method_mod_p); | ||
769 | if (rsa->_method_mod_q != NULL) | ||
770 | BN_MONT_CTX_free(rsa->_method_mod_q); | ||
771 | return(1); | ||
772 | } | ||
773 | |||
774 | #endif | ||