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