Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

rsa_impl.c 30 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/rsa.h>
  57. #include <assert.h>
  58. #include <string.h>
  59. #include <openssl/bn.h>
  60. #include <openssl/err.h>
  61. #include <openssl/mem.h>
  62. #include <openssl/thread.h>
  63. #include "internal.h"
  64. #include "../internal.h"
  65. static int check_modulus_and_exponent_sizes(const RSA *rsa) {
  66. unsigned rsa_bits = BN_num_bits(rsa->n);
  67. if (rsa_bits > 16 * 1024) {
  68. OPENSSL_PUT_ERROR(RSA, RSA_R_MODULUS_TOO_LARGE);
  69. return 0;
  70. }
  71. /* Mitigate DoS attacks by limiting the exponent size. 33 bits was chosen as
  72. * the limit based on the recommendations in [1] and [2]. Windows CryptoAPI
  73. * doesn't support values larger than 32 bits [3], so it is unlikely that
  74. * exponents larger than 32 bits are being used for anything Windows commonly
  75. * does.
  76. *
  77. * [1] https://www.imperialviolet.org/2012/03/16/rsae.html
  78. * [2] https://www.imperialviolet.org/2012/03/17/rsados.html
  79. * [3] https://msdn.microsoft.com/en-us/library/aa387685(VS.85).aspx */
  80. static const unsigned kMaxExponentBits = 33;
  81. if (BN_num_bits(rsa->e) > kMaxExponentBits) {
  82. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_E_VALUE);
  83. return 0;
  84. }
  85. /* Verify |n > e|. Comparing |rsa_bits| to |kMaxExponentBits| is a small
  86. * shortcut to comparing |n| and |e| directly. In reality, |kMaxExponentBits|
  87. * is much smaller than the minimum RSA key size that any application should
  88. * accept. */
  89. if (rsa_bits <= kMaxExponentBits) {
  90. OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL);
  91. return 0;
  92. }
  93. assert(BN_ucmp(rsa->n, rsa->e) > 0);
  94. return 1;
  95. }
  96. size_t rsa_default_size(const RSA *rsa) {
  97. return BN_num_bytes(rsa->n);
  98. }
  99. int rsa_default_encrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
  100. const uint8_t *in, size_t in_len, int padding) {
  101. const unsigned rsa_size = RSA_size(rsa);
  102. BIGNUM *f, *result;
  103. uint8_t *buf = NULL;
  104. BN_CTX *ctx = NULL;
  105. int i, ret = 0;
  106. if (max_out < rsa_size) {
  107. OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
  108. return 0;
  109. }
  110. if (!check_modulus_and_exponent_sizes(rsa)) {
  111. return 0;
  112. }
  113. ctx = BN_CTX_new();
  114. if (ctx == NULL) {
  115. goto err;
  116. }
  117. BN_CTX_start(ctx);
  118. f = BN_CTX_get(ctx);
  119. result = BN_CTX_get(ctx);
  120. buf = OPENSSL_malloc(rsa_size);
  121. if (!f || !result || !buf) {
  122. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  123. goto err;
  124. }
  125. switch (padding) {
  126. case RSA_PKCS1_PADDING:
  127. i = RSA_padding_add_PKCS1_type_2(buf, rsa_size, in, in_len);
  128. break;
  129. case RSA_PKCS1_OAEP_PADDING:
  130. /* Use the default parameters: SHA-1 for both hashes and no label. */
  131. i = RSA_padding_add_PKCS1_OAEP_mgf1(buf, rsa_size, in, in_len,
  132. NULL, 0, NULL, NULL);
  133. break;
  134. case RSA_NO_PADDING:
  135. i = RSA_padding_add_none(buf, rsa_size, in, in_len);
  136. break;
  137. default:
  138. OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
  139. goto err;
  140. }
  141. if (i <= 0) {
  142. goto err;
  143. }
  144. if (BN_bin2bn(buf, rsa_size, f) == NULL) {
  145. goto err;
  146. }
  147. if (BN_ucmp(f, rsa->n) >= 0) {
  148. /* usually the padding functions would catch this */
  149. OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  150. goto err;
  151. }
  152. if (!BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx) ||
  153. !BN_mod_exp_mont(result, f, rsa->e, rsa->n, ctx, rsa->mont_n)) {
  154. goto err;
  155. }
  156. /* put in leading 0 bytes if the number is less than the length of the
  157. * modulus */
  158. if (!BN_bn2bin_padded(out, rsa_size, result)) {
  159. OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
  160. goto err;
  161. }
  162. *out_len = rsa_size;
  163. ret = 1;
  164. err:
  165. if (ctx != NULL) {
  166. BN_CTX_end(ctx);
  167. BN_CTX_free(ctx);
  168. }
  169. if (buf != NULL) {
  170. OPENSSL_cleanse(buf, rsa_size);
  171. OPENSSL_free(buf);
  172. }
  173. return ret;
  174. }
  175. /* MAX_BLINDINGS_PER_RSA defines the maximum number of cached BN_BLINDINGs per
  176. * RSA*. Then this limit is exceeded, BN_BLINDING objects will be created and
  177. * destroyed as needed. */
  178. #define MAX_BLINDINGS_PER_RSA 1024
  179. /* rsa_blinding_get returns a BN_BLINDING to use with |rsa|. It does this by
  180. * allocating one of the cached BN_BLINDING objects in |rsa->blindings|. If
  181. * none are free, the cache will be extended by a extra element and the new
  182. * BN_BLINDING is returned.
  183. *
  184. * On success, the index of the assigned BN_BLINDING is written to
  185. * |*index_used| and must be passed to |rsa_blinding_release| when finished. */
  186. static BN_BLINDING *rsa_blinding_get(RSA *rsa, unsigned *index_used,
  187. BN_CTX *ctx) {
  188. assert(ctx != NULL);
  189. assert(rsa->mont_n != NULL);
  190. BN_BLINDING *ret = NULL;
  191. BN_BLINDING **new_blindings;
  192. uint8_t *new_blindings_inuse;
  193. char overflow = 0;
  194. CRYPTO_MUTEX_lock_write(&rsa->lock);
  195. unsigned i;
  196. for (i = 0; i < rsa->num_blindings; i++) {
  197. if (rsa->blindings_inuse[i] == 0) {
  198. rsa->blindings_inuse[i] = 1;
  199. ret = rsa->blindings[i];
  200. *index_used = i;
  201. break;
  202. }
  203. }
  204. if (ret != NULL) {
  205. CRYPTO_MUTEX_unlock_write(&rsa->lock);
  206. return ret;
  207. }
  208. overflow = rsa->num_blindings >= MAX_BLINDINGS_PER_RSA;
  209. /* We didn't find a free BN_BLINDING to use so increase the length of
  210. * the arrays by one and use the newly created element. */
  211. CRYPTO_MUTEX_unlock_write(&rsa->lock);
  212. ret = BN_BLINDING_new();
  213. if (ret == NULL) {
  214. return NULL;
  215. }
  216. if (overflow) {
  217. /* We cannot add any more cached BN_BLINDINGs so we use |ret|
  218. * and mark it for destruction in |rsa_blinding_release|. */
  219. *index_used = MAX_BLINDINGS_PER_RSA;
  220. return ret;
  221. }
  222. CRYPTO_MUTEX_lock_write(&rsa->lock);
  223. new_blindings =
  224. OPENSSL_malloc(sizeof(BN_BLINDING *) * (rsa->num_blindings + 1));
  225. if (new_blindings == NULL) {
  226. goto err1;
  227. }
  228. memcpy(new_blindings, rsa->blindings,
  229. sizeof(BN_BLINDING *) * rsa->num_blindings);
  230. new_blindings[rsa->num_blindings] = ret;
  231. new_blindings_inuse = OPENSSL_malloc(rsa->num_blindings + 1);
  232. if (new_blindings_inuse == NULL) {
  233. goto err2;
  234. }
  235. memcpy(new_blindings_inuse, rsa->blindings_inuse, rsa->num_blindings);
  236. new_blindings_inuse[rsa->num_blindings] = 1;
  237. *index_used = rsa->num_blindings;
  238. OPENSSL_free(rsa->blindings);
  239. rsa->blindings = new_blindings;
  240. OPENSSL_free(rsa->blindings_inuse);
  241. rsa->blindings_inuse = new_blindings_inuse;
  242. rsa->num_blindings++;
  243. CRYPTO_MUTEX_unlock_write(&rsa->lock);
  244. return ret;
  245. err2:
  246. OPENSSL_free(new_blindings);
  247. err1:
  248. CRYPTO_MUTEX_unlock_write(&rsa->lock);
  249. BN_BLINDING_free(ret);
  250. return NULL;
  251. }
  252. /* rsa_blinding_release marks the cached BN_BLINDING at the given index as free
  253. * for other threads to use. */
  254. static void rsa_blinding_release(RSA *rsa, BN_BLINDING *blinding,
  255. unsigned blinding_index) {
  256. if (blinding_index == MAX_BLINDINGS_PER_RSA) {
  257. /* This blinding wasn't cached. */
  258. BN_BLINDING_free(blinding);
  259. return;
  260. }
  261. CRYPTO_MUTEX_lock_write(&rsa->lock);
  262. rsa->blindings_inuse[blinding_index] = 0;
  263. CRYPTO_MUTEX_unlock_write(&rsa->lock);
  264. }
  265. /* signing */
  266. int rsa_default_sign_raw(RSA *rsa, size_t *out_len, uint8_t *out,
  267. size_t max_out, const uint8_t *in, size_t in_len,
  268. int padding) {
  269. const unsigned rsa_size = RSA_size(rsa);
  270. uint8_t *buf = NULL;
  271. int i, ret = 0;
  272. if (max_out < rsa_size) {
  273. OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
  274. return 0;
  275. }
  276. buf = OPENSSL_malloc(rsa_size);
  277. if (buf == NULL) {
  278. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  279. goto err;
  280. }
  281. switch (padding) {
  282. case RSA_PKCS1_PADDING:
  283. i = RSA_padding_add_PKCS1_type_1(buf, rsa_size, in, in_len);
  284. break;
  285. case RSA_NO_PADDING:
  286. i = RSA_padding_add_none(buf, rsa_size, in, in_len);
  287. break;
  288. default:
  289. OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
  290. goto err;
  291. }
  292. if (i <= 0) {
  293. goto err;
  294. }
  295. if (!RSA_private_transform(rsa, out, buf, rsa_size)) {
  296. goto err;
  297. }
  298. *out_len = rsa_size;
  299. ret = 1;
  300. err:
  301. if (buf != NULL) {
  302. OPENSSL_cleanse(buf, rsa_size);
  303. OPENSSL_free(buf);
  304. }
  305. return ret;
  306. }
  307. int rsa_default_decrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
  308. const uint8_t *in, size_t in_len, int padding) {
  309. const unsigned rsa_size = RSA_size(rsa);
  310. int r = -1;
  311. uint8_t *buf = NULL;
  312. int ret = 0;
  313. if (max_out < rsa_size) {
  314. OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
  315. return 0;
  316. }
  317. if (padding == RSA_NO_PADDING) {
  318. buf = out;
  319. } else {
  320. /* Allocate a temporary buffer to hold the padded plaintext. */
  321. buf = OPENSSL_malloc(rsa_size);
  322. if (buf == NULL) {
  323. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  324. goto err;
  325. }
  326. }
  327. if (in_len != rsa_size) {
  328. OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_LEN_NOT_EQUAL_TO_MOD_LEN);
  329. goto err;
  330. }
  331. if (!RSA_private_transform(rsa, buf, in, rsa_size)) {
  332. goto err;
  333. }
  334. switch (padding) {
  335. case RSA_PKCS1_PADDING:
  336. r = RSA_padding_check_PKCS1_type_2(out, rsa_size, buf, rsa_size);
  337. break;
  338. case RSA_PKCS1_OAEP_PADDING:
  339. /* Use the default parameters: SHA-1 for both hashes and no label. */
  340. r = RSA_padding_check_PKCS1_OAEP_mgf1(out, rsa_size, buf, rsa_size,
  341. NULL, 0, NULL, NULL);
  342. break;
  343. case RSA_NO_PADDING:
  344. r = rsa_size;
  345. break;
  346. default:
  347. OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
  348. goto err;
  349. }
  350. if (r < 0) {
  351. OPENSSL_PUT_ERROR(RSA, RSA_R_PADDING_CHECK_FAILED);
  352. } else {
  353. *out_len = r;
  354. ret = 1;
  355. }
  356. err:
  357. if (padding != RSA_NO_PADDING && buf != NULL) {
  358. OPENSSL_cleanse(buf, rsa_size);
  359. OPENSSL_free(buf);
  360. }
  361. return ret;
  362. }
  363. static int mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
  364. int RSA_verify_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
  365. const uint8_t *in, size_t in_len, int padding) {
  366. if (rsa->n == NULL || rsa->e == NULL) {
  367. OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING);
  368. return 0;
  369. }
  370. const unsigned rsa_size = RSA_size(rsa);
  371. BIGNUM *f, *result;
  372. int r = -1;
  373. if (max_out < rsa_size) {
  374. OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
  375. return 0;
  376. }
  377. if (in_len != rsa_size) {
  378. OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_LEN_NOT_EQUAL_TO_MOD_LEN);
  379. return 0;
  380. }
  381. if (!check_modulus_and_exponent_sizes(rsa)) {
  382. return 0;
  383. }
  384. BN_CTX *ctx = BN_CTX_new();
  385. if (ctx == NULL) {
  386. return 0;
  387. }
  388. int ret = 0;
  389. uint8_t *buf = NULL;
  390. BN_CTX_start(ctx);
  391. f = BN_CTX_get(ctx);
  392. result = BN_CTX_get(ctx);
  393. if (f == NULL || result == NULL) {
  394. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  395. goto err;
  396. }
  397. if (padding == RSA_NO_PADDING) {
  398. buf = out;
  399. } else {
  400. /* Allocate a temporary buffer to hold the padded plaintext. */
  401. buf = OPENSSL_malloc(rsa_size);
  402. if (buf == NULL) {
  403. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  404. goto err;
  405. }
  406. }
  407. if (BN_bin2bn(in, in_len, f) == NULL) {
  408. goto err;
  409. }
  410. if (BN_ucmp(f, rsa->n) >= 0) {
  411. OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  412. goto err;
  413. }
  414. if (!BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx) ||
  415. !BN_mod_exp_mont(result, f, rsa->e, rsa->n, ctx, rsa->mont_n)) {
  416. goto err;
  417. }
  418. if (!BN_bn2bin_padded(buf, rsa_size, result)) {
  419. OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
  420. goto err;
  421. }
  422. switch (padding) {
  423. case RSA_PKCS1_PADDING:
  424. r = RSA_padding_check_PKCS1_type_1(out, rsa_size, buf, rsa_size);
  425. break;
  426. case RSA_NO_PADDING:
  427. r = rsa_size;
  428. break;
  429. default:
  430. OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
  431. goto err;
  432. }
  433. if (r < 0) {
  434. OPENSSL_PUT_ERROR(RSA, RSA_R_PADDING_CHECK_FAILED);
  435. } else {
  436. *out_len = r;
  437. ret = 1;
  438. }
  439. err:
  440. BN_CTX_end(ctx);
  441. BN_CTX_free(ctx);
  442. if (buf != out) {
  443. OPENSSL_free(buf);
  444. }
  445. return ret;
  446. }
  447. int rsa_default_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
  448. size_t len) {
  449. BIGNUM *f, *result;
  450. BN_CTX *ctx = NULL;
  451. unsigned blinding_index = 0;
  452. BN_BLINDING *blinding = NULL;
  453. int ret = 0;
  454. ctx = BN_CTX_new();
  455. if (ctx == NULL) {
  456. goto err;
  457. }
  458. BN_CTX_start(ctx);
  459. f = BN_CTX_get(ctx);
  460. result = BN_CTX_get(ctx);
  461. if (f == NULL || result == NULL) {
  462. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  463. goto err;
  464. }
  465. if (BN_bin2bn(in, len, f) == NULL) {
  466. goto err;
  467. }
  468. if (BN_ucmp(f, rsa->n) >= 0) {
  469. /* Usually the padding functions would catch this. */
  470. OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  471. goto err;
  472. }
  473. if (!BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx)) {
  474. OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
  475. goto err;
  476. }
  477. /* We cannot do blinding or verification without |e|, and continuing without
  478. * those countermeasures is dangerous. However, the Java/Android RSA API
  479. * requires support for keys where only |d| and |n| (and not |e|) are known.
  480. * The callers that require that bad behavior set |RSA_FLAG_NO_BLINDING|. */
  481. int disable_security = (rsa->flags & RSA_FLAG_NO_BLINDING) && rsa->e == NULL;
  482. if (!disable_security) {
  483. /* Keys without public exponents must have blinding explicitly disabled to
  484. * be used. */
  485. if (rsa->e == NULL) {
  486. OPENSSL_PUT_ERROR(RSA, RSA_R_NO_PUBLIC_EXPONENT);
  487. goto err;
  488. }
  489. blinding = rsa_blinding_get(rsa, &blinding_index, ctx);
  490. if (blinding == NULL) {
  491. OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
  492. goto err;
  493. }
  494. if (!BN_BLINDING_convert(f, blinding, rsa->e, rsa->mont_n, ctx)) {
  495. goto err;
  496. }
  497. }
  498. if (rsa->p != NULL && rsa->q != NULL && rsa->e != NULL && rsa->dmp1 != NULL &&
  499. rsa->dmq1 != NULL && rsa->iqmp != NULL) {
  500. if (!mod_exp(result, f, rsa, ctx)) {
  501. goto err;
  502. }
  503. } else {
  504. BIGNUM local_d;
  505. BIGNUM *d = NULL;
  506. BN_init(&local_d);
  507. d = &local_d;
  508. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  509. if (!BN_mod_exp_mont_consttime(result, f, d, rsa->n, ctx, rsa->mont_n)) {
  510. goto err;
  511. }
  512. }
  513. /* Verify the result to protect against fault attacks as described in the
  514. * 1997 paper "On the Importance of Checking Cryptographic Protocols for
  515. * Faults" by Dan Boneh, Richard A. DeMillo, and Richard J. Lipton. Some
  516. * implementations do this only when the CRT is used, but we do it in all
  517. * cases. Section 6 of the aforementioned paper describes an attack that
  518. * works when the CRT isn't used. That attack is much less likely to succeed
  519. * than the CRT attack, but there have likely been improvements since 1997.
  520. *
  521. * This check is cheap assuming |e| is small; it almost always is. */
  522. if (!disable_security) {
  523. BIGNUM *vrfy = BN_CTX_get(ctx);
  524. if (vrfy == NULL ||
  525. !BN_mod_exp_mont(vrfy, result, rsa->e, rsa->n, ctx, rsa->mont_n) ||
  526. !BN_equal_consttime(vrfy, f)) {
  527. OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
  528. goto err;
  529. }
  530. if (!BN_BLINDING_invert(result, blinding, rsa->mont_n, ctx)) {
  531. goto err;
  532. }
  533. }
  534. if (!BN_bn2bin_padded(out, len, result)) {
  535. OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
  536. goto err;
  537. }
  538. ret = 1;
  539. err:
  540. if (ctx != NULL) {
  541. BN_CTX_end(ctx);
  542. BN_CTX_free(ctx);
  543. }
  544. if (blinding != NULL) {
  545. rsa_blinding_release(rsa, blinding, blinding_index);
  546. }
  547. return ret;
  548. }
  549. static int mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) {
  550. assert(ctx != NULL);
  551. assert(rsa->n != NULL);
  552. assert(rsa->e != NULL);
  553. assert(rsa->d != NULL);
  554. assert(rsa->p != NULL);
  555. assert(rsa->q != NULL);
  556. assert(rsa->dmp1 != NULL);
  557. assert(rsa->dmq1 != NULL);
  558. assert(rsa->iqmp != NULL);
  559. BIGNUM *r1, *m1, *vrfy;
  560. BIGNUM local_dmp1, local_dmq1, local_c, local_r1;
  561. BIGNUM *dmp1, *dmq1, *c, *pr1;
  562. int ret = 0;
  563. size_t i, num_additional_primes = 0;
  564. if (rsa->additional_primes != NULL) {
  565. num_additional_primes = sk_RSA_additional_prime_num(rsa->additional_primes);
  566. }
  567. BN_CTX_start(ctx);
  568. r1 = BN_CTX_get(ctx);
  569. m1 = BN_CTX_get(ctx);
  570. vrfy = BN_CTX_get(ctx);
  571. if (r1 == NULL ||
  572. m1 == NULL ||
  573. vrfy == NULL) {
  574. goto err;
  575. }
  576. {
  577. BIGNUM local_p, local_q;
  578. BIGNUM *p = NULL, *q = NULL;
  579. /* Make sure BN_mod in Montgomery initialization uses BN_FLG_CONSTTIME. */
  580. BN_init(&local_p);
  581. p = &local_p;
  582. BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
  583. BN_init(&local_q);
  584. q = &local_q;
  585. BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME);
  586. if (!BN_MONT_CTX_set_locked(&rsa->mont_p, &rsa->lock, p, ctx) ||
  587. !BN_MONT_CTX_set_locked(&rsa->mont_q, &rsa->lock, q, ctx)) {
  588. goto err;
  589. }
  590. }
  591. if (!BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx)) {
  592. goto err;
  593. }
  594. /* compute I mod q */
  595. c = &local_c;
  596. BN_with_flags(c, I, BN_FLG_CONSTTIME);
  597. if (!BN_mod(r1, c, rsa->q, ctx)) {
  598. goto err;
  599. }
  600. /* compute r1^dmq1 mod q */
  601. dmq1 = &local_dmq1;
  602. BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
  603. if (!BN_mod_exp_mont_consttime(m1, r1, dmq1, rsa->q, ctx, rsa->mont_q)) {
  604. goto err;
  605. }
  606. /* compute I mod p */
  607. c = &local_c;
  608. BN_with_flags(c, I, BN_FLG_CONSTTIME);
  609. if (!BN_mod(r1, c, rsa->p, ctx)) {
  610. goto err;
  611. }
  612. /* compute r1^dmp1 mod p */
  613. dmp1 = &local_dmp1;
  614. BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
  615. if (!BN_mod_exp_mont_consttime(r0, r1, dmp1, rsa->p, ctx, rsa->mont_p)) {
  616. goto err;
  617. }
  618. if (!BN_sub(r0, r0, m1)) {
  619. goto err;
  620. }
  621. /* This will help stop the size of r0 increasing, which does
  622. * affect the multiply if it optimised for a power of 2 size */
  623. if (BN_is_negative(r0)) {
  624. if (!BN_add(r0, r0, rsa->p)) {
  625. goto err;
  626. }
  627. }
  628. if (!BN_mul(r1, r0, rsa->iqmp, ctx)) {
  629. goto err;
  630. }
  631. /* Turn BN_FLG_CONSTTIME flag on before division operation */
  632. pr1 = &local_r1;
  633. BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
  634. if (!BN_mod(r0, pr1, rsa->p, ctx)) {
  635. goto err;
  636. }
  637. /* If p < q it is occasionally possible for the correction of
  638. * adding 'p' if r0 is negative above to leave the result still
  639. * negative. This can break the private key operations: the following
  640. * second correction should *always* correct this rare occurrence.
  641. * This will *never* happen with OpenSSL generated keys because
  642. * they ensure p > q [steve] */
  643. if (BN_is_negative(r0)) {
  644. if (!BN_add(r0, r0, rsa->p)) {
  645. goto err;
  646. }
  647. }
  648. if (!BN_mul(r1, r0, rsa->q, ctx)) {
  649. goto err;
  650. }
  651. if (!BN_add(r0, r1, m1)) {
  652. goto err;
  653. }
  654. for (i = 0; i < num_additional_primes; i++) {
  655. /* multi-prime RSA. */
  656. BIGNUM local_exp, local_prime;
  657. BIGNUM *exp = &local_exp, *prime = &local_prime;
  658. RSA_additional_prime *ap =
  659. sk_RSA_additional_prime_value(rsa->additional_primes, i);
  660. BN_with_flags(exp, ap->exp, BN_FLG_CONSTTIME);
  661. BN_with_flags(prime, ap->prime, BN_FLG_CONSTTIME);
  662. /* c will already point to a BIGNUM with the correct flags. */
  663. if (!BN_mod(r1, c, prime, ctx)) {
  664. goto err;
  665. }
  666. if (!BN_MONT_CTX_set_locked(&ap->mont, &rsa->lock, prime, ctx) ||
  667. !BN_mod_exp_mont_consttime(m1, r1, exp, prime, ctx, ap->mont)) {
  668. goto err;
  669. }
  670. BN_set_flags(m1, BN_FLG_CONSTTIME);
  671. if (!BN_sub(m1, m1, r0) ||
  672. !BN_mul(m1, m1, ap->coeff, ctx) ||
  673. !BN_mod(m1, m1, prime, ctx) ||
  674. (BN_is_negative(m1) && !BN_add(m1, m1, prime)) ||
  675. !BN_mul(m1, m1, ap->r, ctx) ||
  676. !BN_add(r0, r0, m1)) {
  677. goto err;
  678. }
  679. }
  680. ret = 1;
  681. err:
  682. BN_CTX_end(ctx);
  683. return ret;
  684. }
  685. int rsa_default_multi_prime_keygen(RSA *rsa, int bits, int num_primes,
  686. BIGNUM *e_value, BN_GENCB *cb) {
  687. BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *tmp;
  688. BIGNUM local_r0, local_d, local_p;
  689. BIGNUM *pr0, *d, *p;
  690. int prime_bits, ok = -1, n = 0, i, j;
  691. BN_CTX *ctx = NULL;
  692. STACK_OF(RSA_additional_prime) *additional_primes = NULL;
  693. if (num_primes < 2) {
  694. ok = 0; /* we set our own err */
  695. OPENSSL_PUT_ERROR(RSA, RSA_R_MUST_HAVE_AT_LEAST_TWO_PRIMES);
  696. goto err;
  697. }
  698. ctx = BN_CTX_new();
  699. if (ctx == NULL) {
  700. goto err;
  701. }
  702. BN_CTX_start(ctx);
  703. r0 = BN_CTX_get(ctx);
  704. r1 = BN_CTX_get(ctx);
  705. r2 = BN_CTX_get(ctx);
  706. r3 = BN_CTX_get(ctx);
  707. if (r0 == NULL || r1 == NULL || r2 == NULL || r3 == NULL) {
  708. goto err;
  709. }
  710. if (num_primes > 2) {
  711. additional_primes = sk_RSA_additional_prime_new_null();
  712. if (additional_primes == NULL) {
  713. goto err;
  714. }
  715. }
  716. for (i = 2; i < num_primes; i++) {
  717. RSA_additional_prime *ap = OPENSSL_malloc(sizeof(RSA_additional_prime));
  718. if (ap == NULL) {
  719. goto err;
  720. }
  721. memset(ap, 0, sizeof(RSA_additional_prime));
  722. ap->prime = BN_new();
  723. ap->exp = BN_new();
  724. ap->coeff = BN_new();
  725. ap->r = BN_new();
  726. if (ap->prime == NULL ||
  727. ap->exp == NULL ||
  728. ap->coeff == NULL ||
  729. ap->r == NULL ||
  730. !sk_RSA_additional_prime_push(additional_primes, ap)) {
  731. RSA_additional_prime_free(ap);
  732. goto err;
  733. }
  734. }
  735. /* We need the RSA components non-NULL */
  736. if (!rsa->n && ((rsa->n = BN_new()) == NULL)) {
  737. goto err;
  738. }
  739. if (!rsa->d && ((rsa->d = BN_new()) == NULL)) {
  740. goto err;
  741. }
  742. if (!rsa->e && ((rsa->e = BN_new()) == NULL)) {
  743. goto err;
  744. }
  745. if (!rsa->p && ((rsa->p = BN_new()) == NULL)) {
  746. goto err;
  747. }
  748. if (!rsa->q && ((rsa->q = BN_new()) == NULL)) {
  749. goto err;
  750. }
  751. if (!rsa->dmp1 && ((rsa->dmp1 = BN_new()) == NULL)) {
  752. goto err;
  753. }
  754. if (!rsa->dmq1 && ((rsa->dmq1 = BN_new()) == NULL)) {
  755. goto err;
  756. }
  757. if (!rsa->iqmp && ((rsa->iqmp = BN_new()) == NULL)) {
  758. goto err;
  759. }
  760. if (!BN_copy(rsa->e, e_value)) {
  761. goto err;
  762. }
  763. /* generate p and q */
  764. prime_bits = (bits + (num_primes - 1)) / num_primes;
  765. for (;;) {
  766. if (!BN_generate_prime_ex(rsa->p, prime_bits, 0, NULL, NULL, cb) ||
  767. !BN_sub(r2, rsa->p, BN_value_one()) ||
  768. !BN_gcd(r1, r2, rsa->e, ctx)) {
  769. goto err;
  770. }
  771. if (BN_is_one(r1)) {
  772. break;
  773. }
  774. if (!BN_GENCB_call(cb, 2, n++)) {
  775. goto err;
  776. }
  777. }
  778. if (!BN_GENCB_call(cb, 3, 0)) {
  779. goto err;
  780. }
  781. prime_bits = ((bits - prime_bits) + (num_primes - 2)) / (num_primes - 1);
  782. for (;;) {
  783. /* When generating ridiculously small keys, we can get stuck
  784. * continually regenerating the same prime values. Check for
  785. * this and bail if it happens 3 times. */
  786. unsigned int degenerate = 0;
  787. do {
  788. if (!BN_generate_prime_ex(rsa->q, prime_bits, 0, NULL, NULL, cb)) {
  789. goto err;
  790. }
  791. } while ((BN_cmp(rsa->p, rsa->q) == 0) && (++degenerate < 3));
  792. if (degenerate == 3) {
  793. ok = 0; /* we set our own err */
  794. OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL);
  795. goto err;
  796. }
  797. if (!BN_sub(r2, rsa->q, BN_value_one()) ||
  798. !BN_gcd(r1, r2, rsa->e, ctx)) {
  799. goto err;
  800. }
  801. if (BN_is_one(r1)) {
  802. break;
  803. }
  804. if (!BN_GENCB_call(cb, 2, n++)) {
  805. goto err;
  806. }
  807. }
  808. if (!BN_GENCB_call(cb, 3, 1) ||
  809. !BN_mul(rsa->n, rsa->p, rsa->q, ctx)) {
  810. goto err;
  811. }
  812. for (i = 2; i < num_primes; i++) {
  813. RSA_additional_prime *ap =
  814. sk_RSA_additional_prime_value(additional_primes, i - 2);
  815. prime_bits = ((bits - BN_num_bits(rsa->n)) + (num_primes - (i + 1))) /
  816. (num_primes - i);
  817. for (;;) {
  818. if (!BN_generate_prime_ex(ap->prime, prime_bits, 0, NULL, NULL, cb)) {
  819. goto err;
  820. }
  821. if (BN_cmp(rsa->p, ap->prime) == 0 ||
  822. BN_cmp(rsa->q, ap->prime) == 0) {
  823. continue;
  824. }
  825. for (j = 0; j < i - 2; j++) {
  826. if (BN_cmp(sk_RSA_additional_prime_value(additional_primes, j)->prime,
  827. ap->prime) == 0) {
  828. break;
  829. }
  830. }
  831. if (j != i - 2) {
  832. continue;
  833. }
  834. if (!BN_sub(r2, ap->prime, BN_value_one()) ||
  835. !BN_gcd(r1, r2, rsa->e, ctx)) {
  836. goto err;
  837. }
  838. if (!BN_is_one(r1)) {
  839. continue;
  840. }
  841. if (i != num_primes - 1) {
  842. break;
  843. }
  844. /* For the last prime we'll check that it makes n large enough. In the
  845. * two prime case this isn't a problem because we generate primes with
  846. * the top two bits set and so the product is always of the expected
  847. * size. In the multi prime case, this doesn't follow. */
  848. if (!BN_mul(r1, rsa->n, ap->prime, ctx)) {
  849. goto err;
  850. }
  851. if (BN_num_bits(r1) == (unsigned) bits) {
  852. break;
  853. }
  854. if (!BN_GENCB_call(cb, 2, n++)) {
  855. goto err;
  856. }
  857. }
  858. /* ap->r is is the product of all the primes prior to the current one
  859. * (including p and q). */
  860. if (!BN_copy(ap->r, rsa->n)) {
  861. goto err;
  862. }
  863. if (i == num_primes - 1) {
  864. /* In the case of the last prime, we calculated n as |r1| in the loop
  865. * above. */
  866. if (!BN_copy(rsa->n, r1)) {
  867. goto err;
  868. }
  869. } else if (!BN_mul(rsa->n, rsa->n, ap->prime, ctx)) {
  870. goto err;
  871. }
  872. if (!BN_GENCB_call(cb, 3, 1)) {
  873. goto err;
  874. }
  875. }
  876. if (BN_cmp(rsa->p, rsa->q) < 0) {
  877. tmp = rsa->p;
  878. rsa->p = rsa->q;
  879. rsa->q = tmp;
  880. }
  881. /* calculate d */
  882. if (!BN_sub(r1, rsa->p, BN_value_one())) {
  883. goto err; /* p-1 */
  884. }
  885. if (!BN_sub(r2, rsa->q, BN_value_one())) {
  886. goto err; /* q-1 */
  887. }
  888. if (!BN_mul(r0, r1, r2, ctx)) {
  889. goto err; /* (p-1)(q-1) */
  890. }
  891. for (i = 2; i < num_primes; i++) {
  892. RSA_additional_prime *ap =
  893. sk_RSA_additional_prime_value(additional_primes, i - 2);
  894. if (!BN_sub(r3, ap->prime, BN_value_one()) ||
  895. !BN_mul(r0, r0, r3, ctx)) {
  896. goto err;
  897. }
  898. }
  899. pr0 = &local_r0;
  900. BN_with_flags(pr0, r0, BN_FLG_CONSTTIME);
  901. if (!BN_mod_inverse(rsa->d, rsa->e, pr0, ctx)) {
  902. goto err; /* d */
  903. }
  904. /* set up d for correct BN_FLG_CONSTTIME flag */
  905. d = &local_d;
  906. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  907. /* calculate d mod (p-1) */
  908. if (!BN_mod(rsa->dmp1, d, r1, ctx)) {
  909. goto err;
  910. }
  911. /* calculate d mod (q-1) */
  912. if (!BN_mod(rsa->dmq1, d, r2, ctx)) {
  913. goto err;
  914. }
  915. /* calculate inverse of q mod p */
  916. p = &local_p;
  917. BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
  918. if (!BN_mod_inverse(rsa->iqmp, rsa->q, p, ctx)) {
  919. goto err;
  920. }
  921. for (i = 2; i < num_primes; i++) {
  922. RSA_additional_prime *ap =
  923. sk_RSA_additional_prime_value(additional_primes, i - 2);
  924. if (!BN_sub(ap->exp, ap->prime, BN_value_one()) ||
  925. !BN_mod(ap->exp, rsa->d, ap->exp, ctx) ||
  926. !BN_mod_inverse(ap->coeff, ap->r, ap->prime, ctx)) {
  927. goto err;
  928. }
  929. }
  930. ok = 1;
  931. rsa->additional_primes = additional_primes;
  932. additional_primes = NULL;
  933. err:
  934. if (ok == -1) {
  935. OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
  936. ok = 0;
  937. }
  938. if (ctx != NULL) {
  939. BN_CTX_end(ctx);
  940. BN_CTX_free(ctx);
  941. }
  942. sk_RSA_additional_prime_pop_free(additional_primes,
  943. RSA_additional_prime_free);
  944. return ok;
  945. }
  946. int rsa_default_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
  947. return rsa_default_multi_prime_keygen(rsa, bits, 2 /* num primes */, e_value,
  948. cb);
  949. }
  950. /* All of the methods are NULL to make it easier for the compiler/linker to drop
  951. * unused functions. The wrapper functions will select the appropriate
  952. * |rsa_default_*| implementation. */
  953. const RSA_METHOD RSA_default_method = {
  954. {
  955. 0 /* references */,
  956. 1 /* is_static */,
  957. },
  958. NULL /* app_data */,
  959. NULL /* init */,
  960. NULL /* finish (defaults to rsa_default_finish) */,
  961. NULL /* size (defaults to rsa_default_size) */,
  962. NULL /* sign */,
  963. NULL /* verify */,
  964. NULL /* encrypt (defaults to rsa_default_encrypt) */,
  965. NULL /* sign_raw (defaults to rsa_default_sign_raw) */,
  966. NULL /* decrypt (defaults to rsa_default_decrypt) */,
  967. NULL /* verify_raw (defaults to rsa_default_verify_raw) */,
  968. NULL /* private_transform (defaults to rsa_default_private_transform) */,
  969. NULL /* mod_exp (ignored) */,
  970. NULL /* bn_mod_exp (ignored) */,
  971. RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE,
  972. NULL /* keygen (defaults to rsa_default_keygen) */,
  973. NULL /* multi_prime_keygen (defaults to rsa_default_multi_prime_keygen) */,
  974. NULL /* supports_digest */,
  975. };