]>
Commit | Line | Data |
---|---|---|
58964a49 RE |
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 | */ | |
46a64376 | 58 | /* ==================================================================== |
675f605d | 59 | * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. |
46a64376 BM |
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 | */ | |
58964a49 RE |
111 | |
112 | #include <stdio.h> | |
113 | #include "cryptlib.h" | |
ec577822 BM |
114 | #include <openssl/bn.h> |
115 | #include <openssl/rsa.h> | |
116 | #include <openssl/rand.h> | |
c553721e DSH |
117 | #ifdef OPENSSL_FIPS |
118 | #include <openssl/fips.h> | |
119 | #endif | |
58964a49 | 120 | |
c1cd88a0 DSH |
121 | #ifndef RSA_NULL |
122 | ||
29c1f061 | 123 | static int RSA_eay_public_encrypt(int flen, const unsigned char *from, |
58964a49 | 124 | unsigned char *to, RSA *rsa,int padding); |
29c1f061 | 125 | static int RSA_eay_private_encrypt(int flen, const unsigned char *from, |
58964a49 | 126 | unsigned char *to, RSA *rsa,int padding); |
29c1f061 | 127 | static int RSA_eay_public_decrypt(int flen, const unsigned char *from, |
58964a49 | 128 | unsigned char *to, RSA *rsa,int padding); |
29c1f061 | 129 | static int RSA_eay_private_decrypt(int flen, const unsigned char *from, |
58964a49 | 130 | unsigned char *to, RSA *rsa,int padding); |
46ef873f | 131 | static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa, BN_CTX *ctx); |
58964a49 RE |
132 | static int RSA_eay_init(RSA *rsa); |
133 | static int RSA_eay_finish(RSA *rsa); | |
58964a49 RE |
134 | static RSA_METHOD rsa_pkcs1_eay_meth={ |
135 | "Eric Young's PKCS#1 RSA", | |
136 | RSA_eay_public_encrypt, | |
24cff6ce BM |
137 | RSA_eay_public_decrypt, /* signature verification */ |
138 | RSA_eay_private_encrypt, /* signing */ | |
58964a49 RE |
139 | RSA_eay_private_decrypt, |
140 | RSA_eay_mod_exp, | |
123d24d6 | 141 | BN_mod_exp_mont, /* XXX probably we should not use Montgomery if e == 3 */ |
58964a49 RE |
142 | RSA_eay_init, |
143 | RSA_eay_finish, | |
c553721e | 144 | RSA_FLAG_FIPS_METHOD, /* flags */ |
58964a49 | 145 | NULL, |
be6d7700 | 146 | 0, /* rsa_sign */ |
2814c629 GT |
147 | 0, /* rsa_verify */ |
148 | NULL /* rsa_keygen */ | |
58964a49 RE |
149 | }; |
150 | ||
7be7c2ed | 151 | const RSA_METHOD *RSA_PKCS1_SSLeay(void) |
58964a49 RE |
152 | { |
153 | return(&rsa_pkcs1_eay_meth); | |
154 | } | |
155 | ||
29c1f061 | 156 | static int RSA_eay_public_encrypt(int flen, const unsigned char *from, |
6b691a5c | 157 | unsigned char *to, RSA *rsa, int padding) |
58964a49 | 158 | { |
46ef873f | 159 | BIGNUM *f,*ret; |
58964a49 RE |
160 | int i,j,k,num=0,r= -1; |
161 | unsigned char *buf=NULL; | |
162 | BN_CTX *ctx=NULL; | |
163 | ||
c553721e DSH |
164 | #ifdef OPENSSL_FIPS |
165 | if(FIPS_selftest_failed()) | |
166 | { | |
167 | FIPSerr(FIPS_F_RSA_EAY_PUBLIC_ENCRYPT,FIPS_R_FIPS_SELFTEST_FAILED); | |
168 | goto err; | |
169 | } | |
170 | ||
171 | if (FIPS_mode() && (BN_num_bits(rsa->n) < OPENSSL_RSA_FIPS_MIN_MODULUS_BITS)) | |
172 | { | |
173 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_KEY_SIZE_TOO_SMALL); | |
174 | return -1; | |
175 | } | |
176 | #endif | |
177 | ||
5e3225cc BM |
178 | if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) |
179 | { | |
180 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_MODULUS_TOO_LARGE); | |
181 | return -1; | |
182 | } | |
183 | ||
184 | if (BN_ucmp(rsa->n, rsa->e) <= 0) | |
185 | { | |
186 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE); | |
187 | return -1; | |
188 | } | |
189 | ||
190 | /* for large moduli, enforce exponent limit */ | |
191 | if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) | |
192 | { | |
193 | if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) | |
194 | { | |
195 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE); | |
196 | return -1; | |
197 | } | |
198 | } | |
199 | ||
58964a49 | 200 | if ((ctx=BN_CTX_new()) == NULL) goto err; |
46ef873f GT |
201 | BN_CTX_start(ctx); |
202 | f = BN_CTX_get(ctx); | |
203 | ret = BN_CTX_get(ctx); | |
58964a49 | 204 | num=BN_num_bytes(rsa->n); |
46ef873f GT |
205 | buf = OPENSSL_malloc(num); |
206 | if (!f || !ret || !buf) | |
58964a49 RE |
207 | { |
208 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,ERR_R_MALLOC_FAILURE); | |
209 | goto err; | |
210 | } | |
211 | ||
212 | switch (padding) | |
213 | { | |
214 | case RSA_PKCS1_PADDING: | |
215 | i=RSA_padding_add_PKCS1_type_2(buf,num,from,flen); | |
216 | break; | |
cf1b7d96 | 217 | #ifndef OPENSSL_NO_SHA |
a4949896 BL |
218 | case RSA_PKCS1_OAEP_PADDING: |
219 | i=RSA_padding_add_PKCS1_OAEP(buf,num,from,flen,NULL,0); | |
220 | break; | |
79df9d62 | 221 | #endif |
58964a49 RE |
222 | case RSA_SSLV23_PADDING: |
223 | i=RSA_padding_add_SSLv23(buf,num,from,flen); | |
224 | break; | |
225 | case RSA_NO_PADDING: | |
226 | i=RSA_padding_add_none(buf,num,from,flen); | |
227 | break; | |
228 | default: | |
229 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE); | |
230 | goto err; | |
231 | } | |
232 | if (i <= 0) goto err; | |
233 | ||
46ef873f | 234 | if (BN_bin2bn(buf,num,f) == NULL) goto err; |
58964a49 | 235 | |
46ef873f | 236 | if (BN_ucmp(f, rsa->n) >= 0) |
7c9882eb | 237 | { |
24cff6ce BM |
238 | /* usually the padding functions would catch this */ |
239 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS); | |
240 | goto err; | |
241 | } | |
242 | ||
db99c525 BM |
243 | if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) |
244 | if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx)) | |
245 | goto err; | |
79221bc2 | 246 | |
46ef873f | 247 | if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx, |
03f8b042 | 248 | rsa->_method_mod_n)) goto err; |
58964a49 RE |
249 | |
250 | /* put in leading 0 bytes if the number is less than the | |
251 | * length of the modulus */ | |
46ef873f GT |
252 | j=BN_num_bytes(ret); |
253 | i=BN_bn2bin(ret,&(to[num-j])); | |
58964a49 RE |
254 | for (k=0; k<(num-i); k++) |
255 | to[k]=0; | |
256 | ||
257 | r=num; | |
258 | err: | |
46ef873f GT |
259 | if (ctx != NULL) |
260 | { | |
261 | BN_CTX_end(ctx); | |
262 | BN_CTX_free(ctx); | |
263 | } | |
58964a49 RE |
264 | if (buf != NULL) |
265 | { | |
4579924b | 266 | OPENSSL_cleanse(buf,num); |
26a3a48d | 267 | OPENSSL_free(buf); |
58964a49 RE |
268 | } |
269 | return(r); | |
270 | } | |
271 | ||
675f605d | 272 | static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx) |
800e400d NL |
273 | { |
274 | BN_BLINDING *ret; | |
675f605d | 275 | int got_write_lock = 0; |
4c329696 | 276 | CRYPTO_THREADID cur; |
675f605d BM |
277 | |
278 | CRYPTO_r_lock(CRYPTO_LOCK_RSA); | |
c554155b | 279 | |
800e400d NL |
280 | if (rsa->blinding == NULL) |
281 | { | |
675f605d BM |
282 | CRYPTO_r_unlock(CRYPTO_LOCK_RSA); |
283 | CRYPTO_w_lock(CRYPTO_LOCK_RSA); | |
284 | got_write_lock = 1; | |
285 | ||
800e400d | 286 | if (rsa->blinding == NULL) |
675f605d | 287 | rsa->blinding = RSA_setup_blinding(rsa, ctx); |
800e400d | 288 | } |
c554155b | 289 | |
800e400d NL |
290 | ret = rsa->blinding; |
291 | if (ret == NULL) | |
675f605d | 292 | goto err; |
5679bcce | 293 | |
4c329696 GT |
294 | CRYPTO_THREADID_current(&cur); |
295 | if (!CRYPTO_THREADID_cmp(&cur, BN_BLINDING_thread_id(ret))) | |
800e400d | 296 | { |
675f605d BM |
297 | /* rsa->blinding is ours! */ |
298 | ||
299 | *local = 1; | |
300 | } | |
301 | else | |
302 | { | |
303 | /* resort to rsa->mt_blinding instead */ | |
304 | ||
305 | *local = 0; /* instructs rsa_blinding_convert(), rsa_blinding_invert() | |
306 | * that the BN_BLINDING is shared, meaning that accesses | |
307 | * require locks, and that the blinding factor must be | |
308 | * stored outside the BN_BLINDING | |
309 | */ | |
310 | ||
800e400d NL |
311 | if (rsa->mt_blinding == NULL) |
312 | { | |
675f605d BM |
313 | if (!got_write_lock) |
314 | { | |
315 | CRYPTO_r_unlock(CRYPTO_LOCK_RSA); | |
316 | CRYPTO_w_lock(CRYPTO_LOCK_RSA); | |
317 | got_write_lock = 1; | |
318 | } | |
319 | ||
800e400d NL |
320 | if (rsa->mt_blinding == NULL) |
321 | rsa->mt_blinding = RSA_setup_blinding(rsa, ctx); | |
800e400d NL |
322 | } |
323 | ret = rsa->mt_blinding; | |
324 | } | |
5679bcce | 325 | |
675f605d BM |
326 | err: |
327 | if (got_write_lock) | |
328 | CRYPTO_w_unlock(CRYPTO_LOCK_RSA); | |
329 | else | |
330 | CRYPTO_r_unlock(CRYPTO_LOCK_RSA); | |
800e400d NL |
331 | return ret; |
332 | } | |
5679bcce | 333 | |
800e400d NL |
334 | static int rsa_blinding_convert(BN_BLINDING *b, int local, BIGNUM *f, |
335 | BIGNUM *r, BN_CTX *ctx) | |
336 | { | |
337 | if (local) | |
338 | return BN_BLINDING_convert_ex(f, NULL, b, ctx); | |
339 | else | |
5679bcce | 340 | { |
800e400d | 341 | int ret; |
ce75ca04 | 342 | CRYPTO_r_lock(CRYPTO_LOCK_RSA_BLINDING); |
800e400d | 343 | ret = BN_BLINDING_convert_ex(f, r, b, ctx); |
ce75ca04 | 344 | CRYPTO_r_unlock(CRYPTO_LOCK_RSA_BLINDING); |
800e400d | 345 | return ret; |
5679bcce | 346 | } |
800e400d NL |
347 | } |
348 | ||
349 | static int rsa_blinding_invert(BN_BLINDING *b, int local, BIGNUM *f, | |
350 | BIGNUM *r, BN_CTX *ctx) | |
351 | { | |
352 | if (local) | |
353 | return BN_BLINDING_invert_ex(f, NULL, b, ctx); | |
5679bcce BM |
354 | else |
355 | { | |
800e400d | 356 | int ret; |
ce75ca04 | 357 | CRYPTO_w_lock(CRYPTO_LOCK_RSA_BLINDING); |
800e400d | 358 | ret = BN_BLINDING_invert_ex(f, r, b, ctx); |
ce75ca04 | 359 | CRYPTO_w_unlock(CRYPTO_LOCK_RSA_BLINDING); |
800e400d | 360 | return ret; |
5679bcce | 361 | } |
800e400d | 362 | } |
5679bcce | 363 | |
24cff6ce | 364 | /* signing */ |
29c1f061 | 365 | static int RSA_eay_private_encrypt(int flen, const unsigned char *from, |
6b691a5c | 366 | unsigned char *to, RSA *rsa, int padding) |
58964a49 | 367 | { |
499fca2d | 368 | BIGNUM *f, *ret, *br, *res; |
58964a49 RE |
369 | int i,j,k,num=0,r= -1; |
370 | unsigned char *buf=NULL; | |
371 | BN_CTX *ctx=NULL; | |
5679bcce BM |
372 | int local_blinding = 0; |
373 | BN_BLINDING *blinding = NULL; | |
58964a49 | 374 | |
c553721e DSH |
375 | #ifdef OPENSSL_FIPS |
376 | if(FIPS_selftest_failed()) | |
377 | { | |
378 | FIPSerr(FIPS_F_RSA_EAY_PUBLIC_ENCRYPT,FIPS_R_FIPS_SELFTEST_FAILED); | |
379 | goto err; | |
380 | } | |
381 | ||
382 | if (FIPS_mode() && (BN_num_bits(rsa->n) < OPENSSL_RSA_FIPS_MIN_MODULUS_BITS)) | |
383 | { | |
384 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_KEY_SIZE_TOO_SMALL); | |
385 | return -1; | |
386 | } | |
387 | #endif | |
388 | ||
58964a49 | 389 | if ((ctx=BN_CTX_new()) == NULL) goto err; |
46ef873f | 390 | BN_CTX_start(ctx); |
800e400d NL |
391 | f = BN_CTX_get(ctx); |
392 | br = BN_CTX_get(ctx); | |
46ef873f | 393 | ret = BN_CTX_get(ctx); |
800e400d | 394 | num = BN_num_bytes(rsa->n); |
46ef873f GT |
395 | buf = OPENSSL_malloc(num); |
396 | if(!f || !ret || !buf) | |
58964a49 RE |
397 | { |
398 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,ERR_R_MALLOC_FAILURE); | |
399 | goto err; | |
400 | } | |
401 | ||
402 | switch (padding) | |
403 | { | |
404 | case RSA_PKCS1_PADDING: | |
405 | i=RSA_padding_add_PKCS1_type_1(buf,num,from,flen); | |
406 | break; | |
499fca2d DSH |
407 | case RSA_X931_PADDING: |
408 | i=RSA_padding_add_X931(buf,num,from,flen); | |
409 | break; | |
58964a49 RE |
410 | case RSA_NO_PADDING: |
411 | i=RSA_padding_add_none(buf,num,from,flen); | |
412 | break; | |
413 | case RSA_SSLV23_PADDING: | |
414 | default: | |
415 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE); | |
416 | goto err; | |
417 | } | |
418 | if (i <= 0) goto err; | |
419 | ||
46ef873f | 420 | if (BN_bin2bn(buf,num,f) == NULL) goto err; |
24cff6ce | 421 | |
46ef873f | 422 | if (BN_ucmp(f, rsa->n) >= 0) |
24cff6ce BM |
423 | { |
424 | /* usually the padding functions would catch this */ | |
425 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS); | |
426 | goto err; | |
427 | } | |
58964a49 | 428 | |
c554155b | 429 | if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) |
5679bcce | 430 | { |
675f605d | 431 | blinding = rsa_get_blinding(rsa, &local_blinding, ctx); |
5679bcce BM |
432 | if (blinding == NULL) |
433 | { | |
434 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR); | |
435 | goto err; | |
436 | } | |
437 | } | |
438 | ||
439 | if (blinding != NULL) | |
800e400d NL |
440 | if (!rsa_blinding_convert(blinding, local_blinding, f, br, ctx)) |
441 | goto err; | |
58964a49 | 442 | |
770d19b8 DSH |
443 | if ( (rsa->flags & RSA_FLAG_EXT_PKEY) || |
444 | ((rsa->p != NULL) && | |
58964a49 RE |
445 | (rsa->q != NULL) && |
446 | (rsa->dmp1 != NULL) && | |
447 | (rsa->dmq1 != NULL) && | |
770d19b8 | 448 | (rsa->iqmp != NULL)) ) |
46a64376 BM |
449 | { |
450 | if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx)) goto err; | |
451 | } | |
58964a49 RE |
452 | else |
453 | { | |
46a64376 BM |
454 | BIGNUM local_d; |
455 | BIGNUM *d = NULL; | |
456 | ||
bd31fb21 | 457 | if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) |
46a64376 | 458 | { |
a28a5d9c | 459 | BN_init(&local_d); |
46a64376 | 460 | d = &local_d; |
bd31fb21 | 461 | BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME); |
46a64376 BM |
462 | } |
463 | else | |
7c9882eb | 464 | d= rsa->d; |
46a64376 | 465 | |
db99c525 BM |
466 | if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) |
467 | if(!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx)) | |
468 | goto err; | |
46a64376 BM |
469 | |
470 | if (!rsa->meth->bn_mod_exp(ret,f,d,rsa->n,ctx, | |
b12753df | 471 | rsa->_method_mod_n)) goto err; |
58964a49 RE |
472 | } |
473 | ||
5679bcce | 474 | if (blinding) |
800e400d NL |
475 | if (!rsa_blinding_invert(blinding, local_blinding, ret, br, ctx)) |
476 | goto err; | |
58964a49 | 477 | |
499fca2d DSH |
478 | if (padding == RSA_X931_PADDING) |
479 | { | |
480 | BN_sub(f, rsa->n, ret); | |
481 | if (BN_cmp(ret, f)) | |
482 | res = f; | |
483 | else | |
484 | res = ret; | |
485 | } | |
486 | else | |
487 | res = ret; | |
488 | ||
58964a49 RE |
489 | /* put in leading 0 bytes if the number is less than the |
490 | * length of the modulus */ | |
499fca2d DSH |
491 | j=BN_num_bytes(res); |
492 | i=BN_bn2bin(res,&(to[num-j])); | |
58964a49 RE |
493 | for (k=0; k<(num-i); k++) |
494 | to[k]=0; | |
495 | ||
496 | r=num; | |
497 | err: | |
46ef873f GT |
498 | if (ctx != NULL) |
499 | { | |
500 | BN_CTX_end(ctx); | |
501 | BN_CTX_free(ctx); | |
502 | } | |
58964a49 RE |
503 | if (buf != NULL) |
504 | { | |
4579924b | 505 | OPENSSL_cleanse(buf,num); |
26a3a48d | 506 | OPENSSL_free(buf); |
58964a49 RE |
507 | } |
508 | return(r); | |
509 | } | |
510 | ||
29c1f061 | 511 | static int RSA_eay_private_decrypt(int flen, const unsigned char *from, |
6b691a5c | 512 | unsigned char *to, RSA *rsa, int padding) |
58964a49 | 513 | { |
800e400d | 514 | BIGNUM *f, *ret, *br; |
58964a49 RE |
515 | int j,num=0,r= -1; |
516 | unsigned char *p; | |
517 | unsigned char *buf=NULL; | |
518 | BN_CTX *ctx=NULL; | |
5679bcce BM |
519 | int local_blinding = 0; |
520 | BN_BLINDING *blinding = NULL; | |
58964a49 | 521 | |
c553721e DSH |
522 | #ifdef OPENSSL_FIPS |
523 | if(FIPS_selftest_failed()) | |
524 | { | |
525 | FIPSerr(FIPS_F_RSA_EAY_PUBLIC_ENCRYPT,FIPS_R_FIPS_SELFTEST_FAILED); | |
526 | goto err; | |
527 | } | |
528 | ||
529 | if (FIPS_mode() && (BN_num_bits(rsa->n) < OPENSSL_RSA_FIPS_MIN_MODULUS_BITS)) | |
530 | { | |
531 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_KEY_SIZE_TOO_SMALL); | |
532 | return -1; | |
533 | } | |
534 | #endif | |
535 | ||
46ef873f GT |
536 | if((ctx = BN_CTX_new()) == NULL) goto err; |
537 | BN_CTX_start(ctx); | |
800e400d NL |
538 | f = BN_CTX_get(ctx); |
539 | br = BN_CTX_get(ctx); | |
46ef873f | 540 | ret = BN_CTX_get(ctx); |
800e400d | 541 | num = BN_num_bytes(rsa->n); |
46ef873f GT |
542 | buf = OPENSSL_malloc(num); |
543 | if(!f || !ret || !buf) | |
58964a49 RE |
544 | { |
545 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE); | |
546 | goto err; | |
547 | } | |
548 | ||
657e60fa | 549 | /* This check was for equality but PGP does evil things |
58964a49 RE |
550 | * and chops off the top '0' bytes */ |
551 | if (flen > num) | |
552 | { | |
553 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN); | |
554 | goto err; | |
555 | } | |
556 | ||
557 | /* make data into a big number */ | |
46ef873f | 558 | if (BN_bin2bn(from,(int)flen,f) == NULL) goto err; |
58964a49 | 559 | |
46ef873f | 560 | if (BN_ucmp(f, rsa->n) >= 0) |
24cff6ce BM |
561 | { |
562 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS); | |
563 | goto err; | |
564 | } | |
565 | ||
c554155b | 566 | if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) |
5679bcce | 567 | { |
675f605d | 568 | blinding = rsa_get_blinding(rsa, &local_blinding, ctx); |
5679bcce BM |
569 | if (blinding == NULL) |
570 | { | |
8afca8d9 | 571 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR); |
5679bcce BM |
572 | goto err; |
573 | } | |
574 | } | |
575 | ||
576 | if (blinding != NULL) | |
800e400d NL |
577 | if (!rsa_blinding_convert(blinding, local_blinding, f, br, ctx)) |
578 | goto err; | |
58964a49 RE |
579 | |
580 | /* do the decrypt */ | |
770d19b8 DSH |
581 | if ( (rsa->flags & RSA_FLAG_EXT_PKEY) || |
582 | ((rsa->p != NULL) && | |
58964a49 RE |
583 | (rsa->q != NULL) && |
584 | (rsa->dmp1 != NULL) && | |
585 | (rsa->dmq1 != NULL) && | |
770d19b8 | 586 | (rsa->iqmp != NULL)) ) |
46a64376 BM |
587 | { |
588 | if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx)) goto err; | |
589 | } | |
58964a49 RE |
590 | else |
591 | { | |
46a64376 BM |
592 | BIGNUM local_d; |
593 | BIGNUM *d = NULL; | |
594 | ||
bd31fb21 | 595 | if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) |
46a64376 BM |
596 | { |
597 | d = &local_d; | |
bd31fb21 | 598 | BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME); |
46a64376 BM |
599 | } |
600 | else | |
601 | d = rsa->d; | |
602 | ||
db99c525 BM |
603 | if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) |
604 | if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx)) | |
605 | goto err; | |
46a64376 | 606 | if (!rsa->meth->bn_mod_exp(ret,f,d,rsa->n,ctx, |
b12753df | 607 | rsa->_method_mod_n)) |
46a64376 | 608 | goto err; |
58964a49 RE |
609 | } |
610 | ||
5679bcce | 611 | if (blinding) |
800e400d NL |
612 | if (!rsa_blinding_invert(blinding, local_blinding, ret, br, ctx)) |
613 | goto err; | |
58964a49 RE |
614 | |
615 | p=buf; | |
46ef873f | 616 | j=BN_bn2bin(ret,p); /* j is only used with no-padding mode */ |
58964a49 RE |
617 | |
618 | switch (padding) | |
619 | { | |
620 | case RSA_PKCS1_PADDING: | |
dfeab068 | 621 | r=RSA_padding_check_PKCS1_type_2(to,num,buf,j,num); |
58964a49 | 622 | break; |
cf1b7d96 | 623 | #ifndef OPENSSL_NO_SHA |
a4949896 BL |
624 | case RSA_PKCS1_OAEP_PADDING: |
625 | r=RSA_padding_check_PKCS1_OAEP(to,num,buf,j,num,NULL,0); | |
626 | break; | |
79df9d62 | 627 | #endif |
a4949896 | 628 | case RSA_SSLV23_PADDING: |
dfeab068 | 629 | r=RSA_padding_check_SSLv23(to,num,buf,j,num); |
58964a49 RE |
630 | break; |
631 | case RSA_NO_PADDING: | |
dfeab068 | 632 | r=RSA_padding_check_none(to,num,buf,j,num); |
58964a49 RE |
633 | break; |
634 | default: | |
635 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE); | |
636 | goto err; | |
637 | } | |
638 | if (r < 0) | |
639 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_PADDING_CHECK_FAILED); | |
640 | ||
641 | err: | |
46ef873f GT |
642 | if (ctx != NULL) |
643 | { | |
644 | BN_CTX_end(ctx); | |
645 | BN_CTX_free(ctx); | |
646 | } | |
58964a49 RE |
647 | if (buf != NULL) |
648 | { | |
4579924b | 649 | OPENSSL_cleanse(buf,num); |
26a3a48d | 650 | OPENSSL_free(buf); |
58964a49 RE |
651 | } |
652 | return(r); | |
653 | } | |
654 | ||
24cff6ce | 655 | /* signature verification */ |
29c1f061 | 656 | static int RSA_eay_public_decrypt(int flen, const unsigned char *from, |
6b691a5c | 657 | unsigned char *to, RSA *rsa, int padding) |
58964a49 | 658 | { |
46ef873f | 659 | BIGNUM *f,*ret; |
58964a49 RE |
660 | int i,num=0,r= -1; |
661 | unsigned char *p; | |
662 | unsigned char *buf=NULL; | |
663 | BN_CTX *ctx=NULL; | |
664 | ||
c553721e DSH |
665 | #ifdef OPENSSL_FIPS |
666 | if(FIPS_selftest_failed()) | |
667 | { | |
668 | FIPSerr(FIPS_F_RSA_EAY_PUBLIC_ENCRYPT,FIPS_R_FIPS_SELFTEST_FAILED); | |
669 | goto err; | |
670 | } | |
671 | ||
672 | if (FIPS_mode() && (BN_num_bits(rsa->n) < OPENSSL_RSA_FIPS_MIN_MODULUS_BITS)) | |
673 | { | |
674 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_KEY_SIZE_TOO_SMALL); | |
675 | return -1; | |
676 | } | |
677 | #endif | |
678 | ||
5e3225cc BM |
679 | if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) |
680 | { | |
681 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_MODULUS_TOO_LARGE); | |
682 | return -1; | |
683 | } | |
684 | ||
685 | if (BN_ucmp(rsa->n, rsa->e) <= 0) | |
686 | { | |
687 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE); | |
688 | return -1; | |
689 | } | |
690 | ||
691 | /* for large moduli, enforce exponent limit */ | |
692 | if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) | |
693 | { | |
694 | if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) | |
695 | { | |
696 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE); | |
697 | return -1; | |
698 | } | |
699 | } | |
700 | ||
46ef873f GT |
701 | if((ctx = BN_CTX_new()) == NULL) goto err; |
702 | BN_CTX_start(ctx); | |
703 | f = BN_CTX_get(ctx); | |
704 | ret = BN_CTX_get(ctx); | |
58964a49 | 705 | num=BN_num_bytes(rsa->n); |
46ef873f GT |
706 | buf = OPENSSL_malloc(num); |
707 | if(!f || !ret || !buf) | |
58964a49 RE |
708 | { |
709 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,ERR_R_MALLOC_FAILURE); | |
710 | goto err; | |
711 | } | |
712 | ||
657e60fa | 713 | /* This check was for equality but PGP does evil things |
58964a49 RE |
714 | * and chops off the top '0' bytes */ |
715 | if (flen > num) | |
716 | { | |
717 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN); | |
718 | goto err; | |
719 | } | |
720 | ||
46ef873f | 721 | if (BN_bin2bn(from,flen,f) == NULL) goto err; |
24cff6ce | 722 | |
46ef873f | 723 | if (BN_ucmp(f, rsa->n) >= 0) |
24cff6ce BM |
724 | { |
725 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS); | |
726 | goto err; | |
727 | } | |
728 | ||
db99c525 BM |
729 | if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) |
730 | if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx)) | |
731 | goto err; | |
79221bc2 | 732 | |
46ef873f | 733 | if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx, |
03f8b042 | 734 | rsa->_method_mod_n)) goto err; |
58964a49 | 735 | |
499fca2d | 736 | if ((padding == RSA_X931_PADDING) && ((ret->d[0] & 0xf) != 12)) |
776654ad | 737 | if (!BN_sub(ret, rsa->n, ret)) goto err; |
499fca2d | 738 | |
58964a49 | 739 | p=buf; |
46ef873f | 740 | i=BN_bn2bin(ret,p); |
58964a49 RE |
741 | |
742 | switch (padding) | |
743 | { | |
744 | case RSA_PKCS1_PADDING: | |
dfeab068 | 745 | r=RSA_padding_check_PKCS1_type_1(to,num,buf,i,num); |
58964a49 | 746 | break; |
499fca2d DSH |
747 | case RSA_X931_PADDING: |
748 | r=RSA_padding_check_X931(to,num,buf,i,num); | |
749 | break; | |
58964a49 | 750 | case RSA_NO_PADDING: |
dfeab068 | 751 | r=RSA_padding_check_none(to,num,buf,i,num); |
58964a49 RE |
752 | break; |
753 | default: | |
754 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE); | |
755 | goto err; | |
756 | } | |
757 | if (r < 0) | |
758 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_PADDING_CHECK_FAILED); | |
759 | ||
760 | err: | |
46ef873f GT |
761 | if (ctx != NULL) |
762 | { | |
763 | BN_CTX_end(ctx); | |
764 | BN_CTX_free(ctx); | |
765 | } | |
58964a49 RE |
766 | if (buf != NULL) |
767 | { | |
4579924b | 768 | OPENSSL_cleanse(buf,num); |
26a3a48d | 769 | OPENSSL_free(buf); |
58964a49 RE |
770 | } |
771 | return(r); | |
772 | } | |
773 | ||
46ef873f | 774 | static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) |
58964a49 | 775 | { |
46ef873f | 776 | BIGNUM *r1,*m1,*vrfy; |
bd31fb21 BM |
777 | BIGNUM local_dmp1,local_dmq1,local_c,local_r1; |
778 | BIGNUM *dmp1,*dmq1,*c,*pr1; | |
58964a49 | 779 | int ret=0; |
58964a49 | 780 | |
46ef873f GT |
781 | BN_CTX_start(ctx); |
782 | r1 = BN_CTX_get(ctx); | |
783 | m1 = BN_CTX_get(ctx); | |
784 | vrfy = BN_CTX_get(ctx); | |
58964a49 | 785 | |
7c9882eb BM |
786 | { |
787 | BIGNUM local_p, local_q; | |
788 | BIGNUM *p = NULL, *q = NULL; | |
bd31fb21 | 789 | |
7c9882eb BM |
790 | /* Make sure BN_mod_inverse in Montgomery intialization uses the |
791 | * BN_FLG_CONSTTIME flag (unless RSA_FLAG_NO_CONSTTIME is set) | |
792 | */ | |
793 | if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) | |
794 | { | |
795 | BN_init(&local_p); | |
796 | p = &local_p; | |
797 | BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME); | |
798 | ||
799 | BN_init(&local_q); | |
800 | q = &local_q; | |
801 | BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME); | |
802 | } | |
803 | else | |
804 | { | |
805 | p = rsa->p; | |
806 | q = rsa->q; | |
807 | } | |
808 | ||
db99c525 BM |
809 | if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) |
810 | { | |
811 | if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_p, CRYPTO_LOCK_RSA, p, ctx)) | |
812 | goto err; | |
813 | if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_q, CRYPTO_LOCK_RSA, q, ctx)) | |
814 | goto err; | |
815 | } | |
7c9882eb | 816 | } |
bd31fb21 | 817 | |
db99c525 BM |
818 | if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) |
819 | if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx)) | |
820 | goto err; | |
126fe085 | 821 | |
bd31fb21 BM |
822 | /* compute I mod q */ |
823 | if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) | |
824 | { | |
825 | c = &local_c; | |
826 | BN_with_flags(c, I, BN_FLG_CONSTTIME); | |
827 | if (!BN_mod(r1,c,rsa->q,ctx)) goto err; | |
828 | } | |
829 | else | |
830 | { | |
831 | if (!BN_mod(r1,I,rsa->q,ctx)) goto err; | |
832 | } | |
833 | ||
834 | /* compute r1^dmq1 mod q */ | |
835 | if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) | |
46a64376 BM |
836 | { |
837 | dmq1 = &local_dmq1; | |
bd31fb21 | 838 | BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME); |
46a64376 BM |
839 | } |
840 | else | |
841 | dmq1 = rsa->dmq1; | |
842 | if (!rsa->meth->bn_mod_exp(m1,r1,dmq1,rsa->q,ctx, | |
03f8b042 | 843 | rsa->_method_mod_q)) goto err; |
58964a49 | 844 | |
bd31fb21 BM |
845 | /* compute I mod p */ |
846 | if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) | |
847 | { | |
848 | c = &local_c; | |
849 | BN_with_flags(c, I, BN_FLG_CONSTTIME); | |
850 | if (!BN_mod(r1,c,rsa->p,ctx)) goto err; | |
851 | } | |
852 | else | |
853 | { | |
854 | if (!BN_mod(r1,I,rsa->p,ctx)) goto err; | |
855 | } | |
856 | ||
857 | /* compute r1^dmp1 mod p */ | |
858 | if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) | |
46a64376 BM |
859 | { |
860 | dmp1 = &local_dmp1; | |
bd31fb21 | 861 | BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME); |
46a64376 BM |
862 | } |
863 | else | |
864 | dmp1 = rsa->dmp1; | |
865 | if (!rsa->meth->bn_mod_exp(r0,r1,dmp1,rsa->p,ctx, | |
03f8b042 | 866 | rsa->_method_mod_p)) goto err; |
58964a49 | 867 | |
46ef873f | 868 | if (!BN_sub(r0,r0,m1)) goto err; |
dfeab068 RE |
869 | /* This will help stop the size of r0 increasing, which does |
870 | * affect the multiply if it optimised for a power of 2 size */ | |
ff22e913 | 871 | if (BN_is_negative(r0)) |
dfeab068 | 872 | if (!BN_add(r0,r0,rsa->p)) goto err; |
58964a49 | 873 | |
46ef873f | 874 | if (!BN_mul(r1,r0,rsa->iqmp,ctx)) goto err; |
bd31fb21 BM |
875 | |
876 | /* Turn BN_FLG_CONSTTIME flag on before division operation */ | |
877 | if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) | |
878 | { | |
879 | pr1 = &local_r1; | |
880 | BN_with_flags(pr1, r1, BN_FLG_CONSTTIME); | |
881 | } | |
882 | else | |
883 | pr1 = r1; | |
884 | if (!BN_mod(r0,pr1,rsa->p,ctx)) goto err; | |
885 | ||
abd4c915 DSH |
886 | /* If p < q it is occasionally possible for the correction of |
887 | * adding 'p' if r0 is negative above to leave the result still | |
888 | * negative. This can break the private key operations: the following | |
889 | * second correction should *always* correct this rare occurrence. | |
890 | * This will *never* happen with OpenSSL generated keys because | |
891 | * they ensure p > q [steve] | |
892 | */ | |
ff22e913 | 893 | if (BN_is_negative(r0)) |
abd4c915 | 894 | if (!BN_add(r0,r0,rsa->p)) goto err; |
46ef873f GT |
895 | if (!BN_mul(r1,r0,rsa->q,ctx)) goto err; |
896 | if (!BN_add(r0,r1,m1)) goto err; | |
58964a49 | 897 | |
6a5b52ef UM |
898 | if (rsa->e && rsa->n) |
899 | { | |
46ef873f | 900 | if (!rsa->meth->bn_mod_exp(vrfy,r0,rsa->e,rsa->n,ctx,rsa->_method_mod_n)) goto err; |
81d1998e GT |
901 | /* If 'I' was greater than (or equal to) rsa->n, the operation |
902 | * will be equivalent to using 'I mod n'. However, the result of | |
903 | * the verify will *always* be less than 'n' so we don't check | |
904 | * for absolute equality, just congruency. */ | |
46ef873f GT |
905 | if (!BN_sub(vrfy, vrfy, I)) goto err; |
906 | if (!BN_mod(vrfy, vrfy, rsa->n, ctx)) goto err; | |
ff22e913 | 907 | if (BN_is_negative(vrfy)) |
46ef873f GT |
908 | if (!BN_add(vrfy, vrfy, rsa->n)) goto err; |
909 | if (!BN_is_zero(vrfy)) | |
46a64376 | 910 | { |
81d1998e GT |
911 | /* 'I' and 'vrfy' aren't congruent mod n. Don't leak |
912 | * miscalculated CRT output, just do a raw (slower) | |
913 | * mod_exp and return that instead. */ | |
46a64376 BM |
914 | |
915 | BIGNUM local_d; | |
916 | BIGNUM *d = NULL; | |
917 | ||
bd31fb21 | 918 | if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) |
46a64376 BM |
919 | { |
920 | d = &local_d; | |
bd31fb21 | 921 | BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME); |
46a64376 BM |
922 | } |
923 | else | |
924 | d = rsa->d; | |
925 | if (!rsa->meth->bn_mod_exp(r0,I,d,rsa->n,ctx, | |
926 | rsa->_method_mod_n)) goto err; | |
927 | } | |
6a5b52ef | 928 | } |
58964a49 RE |
929 | ret=1; |
930 | err: | |
46ef873f | 931 | BN_CTX_end(ctx); |
58964a49 RE |
932 | return(ret); |
933 | } | |
934 | ||
6b691a5c | 935 | static int RSA_eay_init(RSA *rsa) |
58964a49 | 936 | { |
c553721e DSH |
937 | #ifdef OPENSSL_FIPS |
938 | FIPS_selftest_check(); | |
939 | #endif | |
58964a49 RE |
940 | rsa->flags|=RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE; |
941 | return(1); | |
942 | } | |
943 | ||
6b691a5c | 944 | static int RSA_eay_finish(RSA *rsa) |
58964a49 | 945 | { |
03f8b042 BL |
946 | if (rsa->_method_mod_n != NULL) |
947 | BN_MONT_CTX_free(rsa->_method_mod_n); | |
948 | if (rsa->_method_mod_p != NULL) | |
949 | BN_MONT_CTX_free(rsa->_method_mod_p); | |
950 | if (rsa->_method_mod_q != NULL) | |
951 | BN_MONT_CTX_free(rsa->_method_mod_q); | |
58964a49 RE |
952 | return(1); |
953 | } | |
954 | ||
c1cd88a0 | 955 | #endif |