rsa_eay.c 25 KB

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