rsa_impl.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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 <string.h>
  58. #include <openssl/bn.h>
  59. #include <openssl/err.h>
  60. #include <openssl/mem.h>
  61. #include <openssl/thread.h>
  62. #include "internal.h"
  63. #include "../internal.h"
  64. static int check_modulus_and_exponent_sizes(const RSA *rsa) {
  65. unsigned rsa_bits = BN_num_bits(rsa->n);
  66. if (rsa_bits > 16 * 1024) {
  67. OPENSSL_PUT_ERROR(RSA, RSA_R_MODULUS_TOO_LARGE);
  68. return 0;
  69. }
  70. if (BN_ucmp(rsa->n, rsa->e) <= 0) {
  71. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_E_VALUE);
  72. return 0;
  73. }
  74. /* For large moduli only, enforce exponent limit. */
  75. if (rsa_bits > 3072 && BN_num_bits(rsa->e) > 64) {
  76. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_E_VALUE);
  77. return 0;
  78. }
  79. return 1;
  80. }
  81. size_t rsa_default_size(const RSA *rsa) {
  82. return BN_num_bytes(rsa->n);
  83. }
  84. int rsa_default_encrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
  85. const uint8_t *in, size_t in_len, int padding) {
  86. const unsigned rsa_size = RSA_size(rsa);
  87. BIGNUM *f, *result;
  88. uint8_t *buf = NULL;
  89. BN_CTX *ctx = NULL;
  90. int i, ret = 0;
  91. if (max_out < rsa_size) {
  92. OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
  93. return 0;
  94. }
  95. if (!check_modulus_and_exponent_sizes(rsa)) {
  96. return 0;
  97. }
  98. ctx = BN_CTX_new();
  99. if (ctx == NULL) {
  100. goto err;
  101. }
  102. BN_CTX_start(ctx);
  103. f = BN_CTX_get(ctx);
  104. result = BN_CTX_get(ctx);
  105. buf = OPENSSL_malloc(rsa_size);
  106. if (!f || !result || !buf) {
  107. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  108. goto err;
  109. }
  110. switch (padding) {
  111. case RSA_PKCS1_PADDING:
  112. i = RSA_padding_add_PKCS1_type_2(buf, rsa_size, in, in_len);
  113. break;
  114. case RSA_PKCS1_OAEP_PADDING:
  115. /* Use the default parameters: SHA-1 for both hashes and no label. */
  116. i = RSA_padding_add_PKCS1_OAEP_mgf1(buf, rsa_size, in, in_len,
  117. NULL, 0, NULL, NULL);
  118. break;
  119. case RSA_NO_PADDING:
  120. i = RSA_padding_add_none(buf, rsa_size, in, in_len);
  121. break;
  122. default:
  123. OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
  124. goto err;
  125. }
  126. if (i <= 0) {
  127. goto err;
  128. }
  129. if (BN_bin2bn(buf, rsa_size, f) == NULL) {
  130. goto err;
  131. }
  132. if (BN_ucmp(f, rsa->n) >= 0) {
  133. /* usually the padding functions would catch this */
  134. OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  135. goto err;
  136. }
  137. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) {
  138. if (BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx) == NULL) {
  139. goto err;
  140. }
  141. }
  142. if (!rsa->meth->bn_mod_exp(result, f, rsa->e, rsa->n, ctx, rsa->mont_n)) {
  143. goto err;
  144. }
  145. /* put in leading 0 bytes if the number is less than the length of the
  146. * modulus */
  147. if (!BN_bn2bin_padded(out, rsa_size, result)) {
  148. OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
  149. goto err;
  150. }
  151. *out_len = rsa_size;
  152. ret = 1;
  153. err:
  154. if (ctx != NULL) {
  155. BN_CTX_end(ctx);
  156. BN_CTX_free(ctx);
  157. }
  158. if (buf != NULL) {
  159. OPENSSL_cleanse(buf, rsa_size);
  160. OPENSSL_free(buf);
  161. }
  162. return ret;
  163. }
  164. /* MAX_BLINDINGS_PER_RSA defines the maximum number of cached BN_BLINDINGs per
  165. * RSA*. Then this limit is exceeded, BN_BLINDING objects will be created and
  166. * destroyed as needed. */
  167. #define MAX_BLINDINGS_PER_RSA 1024
  168. /* rsa_blinding_get returns a BN_BLINDING to use with |rsa|. It does this by
  169. * allocating one of the cached BN_BLINDING objects in |rsa->blindings|. If
  170. * none are free, the cache will be extended by a extra element and the new
  171. * BN_BLINDING is returned.
  172. *
  173. * On success, the index of the assigned BN_BLINDING is written to
  174. * |*index_used| and must be passed to |rsa_blinding_release| when finished. */
  175. static BN_BLINDING *rsa_blinding_get(RSA *rsa, unsigned *index_used,
  176. BN_CTX *ctx) {
  177. BN_BLINDING *ret = NULL;
  178. BN_BLINDING **new_blindings;
  179. uint8_t *new_blindings_inuse;
  180. char overflow = 0;
  181. CRYPTO_MUTEX_lock_write(&rsa->lock);
  182. unsigned i;
  183. for (i = 0; i < rsa->num_blindings; i++) {
  184. if (rsa->blindings_inuse[i] == 0) {
  185. rsa->blindings_inuse[i] = 1;
  186. ret = rsa->blindings[i];
  187. *index_used = i;
  188. break;
  189. }
  190. }
  191. if (ret != NULL) {
  192. CRYPTO_MUTEX_unlock(&rsa->lock);
  193. return ret;
  194. }
  195. overflow = rsa->num_blindings >= MAX_BLINDINGS_PER_RSA;
  196. /* We didn't find a free BN_BLINDING to use so increase the length of
  197. * the arrays by one and use the newly created element. */
  198. CRYPTO_MUTEX_unlock(&rsa->lock);
  199. ret = rsa_setup_blinding(rsa, ctx);
  200. if (ret == NULL) {
  201. return NULL;
  202. }
  203. if (overflow) {
  204. /* We cannot add any more cached BN_BLINDINGs so we use |ret|
  205. * and mark it for destruction in |rsa_blinding_release|. */
  206. *index_used = MAX_BLINDINGS_PER_RSA;
  207. return ret;
  208. }
  209. CRYPTO_MUTEX_lock_write(&rsa->lock);
  210. new_blindings =
  211. OPENSSL_malloc(sizeof(BN_BLINDING *) * (rsa->num_blindings + 1));
  212. if (new_blindings == NULL) {
  213. goto err1;
  214. }
  215. memcpy(new_blindings, rsa->blindings,
  216. sizeof(BN_BLINDING *) * rsa->num_blindings);
  217. new_blindings[rsa->num_blindings] = ret;
  218. new_blindings_inuse = OPENSSL_malloc(rsa->num_blindings + 1);
  219. if (new_blindings_inuse == NULL) {
  220. goto err2;
  221. }
  222. memcpy(new_blindings_inuse, rsa->blindings_inuse, rsa->num_blindings);
  223. new_blindings_inuse[rsa->num_blindings] = 1;
  224. *index_used = rsa->num_blindings;
  225. OPENSSL_free(rsa->blindings);
  226. rsa->blindings = new_blindings;
  227. OPENSSL_free(rsa->blindings_inuse);
  228. rsa->blindings_inuse = new_blindings_inuse;
  229. rsa->num_blindings++;
  230. CRYPTO_MUTEX_unlock(&rsa->lock);
  231. return ret;
  232. err2:
  233. OPENSSL_free(new_blindings);
  234. err1:
  235. CRYPTO_MUTEX_unlock(&rsa->lock);
  236. BN_BLINDING_free(ret);
  237. return NULL;
  238. }
  239. /* rsa_blinding_release marks the cached BN_BLINDING at the given index as free
  240. * for other threads to use. */
  241. static void rsa_blinding_release(RSA *rsa, BN_BLINDING *blinding,
  242. unsigned blinding_index) {
  243. if (blinding_index == MAX_BLINDINGS_PER_RSA) {
  244. /* This blinding wasn't cached. */
  245. BN_BLINDING_free(blinding);
  246. return;
  247. }
  248. CRYPTO_MUTEX_lock_write(&rsa->lock);
  249. rsa->blindings_inuse[blinding_index] = 0;
  250. CRYPTO_MUTEX_unlock(&rsa->lock);
  251. }
  252. /* signing */
  253. int rsa_default_sign_raw(RSA *rsa, size_t *out_len, uint8_t *out,
  254. size_t max_out, const uint8_t *in, size_t in_len,
  255. int padding) {
  256. const unsigned rsa_size = RSA_size(rsa);
  257. uint8_t *buf = NULL;
  258. int i, ret = 0;
  259. if (max_out < rsa_size) {
  260. OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
  261. return 0;
  262. }
  263. buf = OPENSSL_malloc(rsa_size);
  264. if (buf == NULL) {
  265. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  266. goto err;
  267. }
  268. switch (padding) {
  269. case RSA_PKCS1_PADDING:
  270. i = RSA_padding_add_PKCS1_type_1(buf, rsa_size, in, in_len);
  271. break;
  272. case RSA_NO_PADDING:
  273. i = RSA_padding_add_none(buf, rsa_size, in, in_len);
  274. break;
  275. default:
  276. OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
  277. goto err;
  278. }
  279. if (i <= 0) {
  280. goto err;
  281. }
  282. if (!RSA_private_transform(rsa, out, buf, rsa_size)) {
  283. goto err;
  284. }
  285. *out_len = rsa_size;
  286. ret = 1;
  287. err:
  288. if (buf != NULL) {
  289. OPENSSL_cleanse(buf, rsa_size);
  290. OPENSSL_free(buf);
  291. }
  292. return ret;
  293. }
  294. int rsa_default_decrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
  295. const uint8_t *in, size_t in_len, int padding) {
  296. const unsigned rsa_size = RSA_size(rsa);
  297. int r = -1;
  298. uint8_t *buf = NULL;
  299. int ret = 0;
  300. if (max_out < rsa_size) {
  301. OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
  302. return 0;
  303. }
  304. if (padding == RSA_NO_PADDING) {
  305. buf = out;
  306. } else {
  307. /* Allocate a temporary buffer to hold the padded plaintext. */
  308. buf = OPENSSL_malloc(rsa_size);
  309. if (buf == NULL) {
  310. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  311. goto err;
  312. }
  313. }
  314. if (in_len != rsa_size) {
  315. OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_LEN_NOT_EQUAL_TO_MOD_LEN);
  316. goto err;
  317. }
  318. if (!RSA_private_transform(rsa, buf, in, rsa_size)) {
  319. goto err;
  320. }
  321. switch (padding) {
  322. case RSA_PKCS1_PADDING:
  323. r = RSA_padding_check_PKCS1_type_2(out, rsa_size, buf, rsa_size);
  324. break;
  325. case RSA_PKCS1_OAEP_PADDING:
  326. /* Use the default parameters: SHA-1 for both hashes and no label. */
  327. r = RSA_padding_check_PKCS1_OAEP_mgf1(out, rsa_size, buf, rsa_size,
  328. NULL, 0, NULL, NULL);
  329. break;
  330. case RSA_NO_PADDING:
  331. r = rsa_size;
  332. break;
  333. default:
  334. OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
  335. goto err;
  336. }
  337. if (r < 0) {
  338. OPENSSL_PUT_ERROR(RSA, RSA_R_PADDING_CHECK_FAILED);
  339. } else {
  340. *out_len = r;
  341. ret = 1;
  342. }
  343. err:
  344. if (padding != RSA_NO_PADDING && buf != NULL) {
  345. OPENSSL_cleanse(buf, rsa_size);
  346. OPENSSL_free(buf);
  347. }
  348. return ret;
  349. }
  350. int rsa_default_verify_raw(RSA *rsa, size_t *out_len, uint8_t *out,
  351. size_t max_out, const uint8_t *in, size_t in_len,
  352. int padding) {
  353. const unsigned rsa_size = RSA_size(rsa);
  354. BIGNUM *f, *result;
  355. int ret = 0;
  356. int r = -1;
  357. uint8_t *buf = NULL;
  358. BN_CTX *ctx = NULL;
  359. if (max_out < rsa_size) {
  360. OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
  361. return 0;
  362. }
  363. if (!check_modulus_and_exponent_sizes(rsa)) {
  364. return 0;
  365. }
  366. ctx = BN_CTX_new();
  367. if (ctx == NULL) {
  368. goto err;
  369. }
  370. BN_CTX_start(ctx);
  371. f = BN_CTX_get(ctx);
  372. result = BN_CTX_get(ctx);
  373. if (padding == RSA_NO_PADDING) {
  374. buf = out;
  375. } else {
  376. /* Allocate a temporary buffer to hold the padded plaintext. */
  377. buf = OPENSSL_malloc(rsa_size);
  378. if (buf == NULL) {
  379. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  380. goto err;
  381. }
  382. }
  383. if (!f || !result) {
  384. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  385. goto err;
  386. }
  387. if (in_len != rsa_size) {
  388. OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_LEN_NOT_EQUAL_TO_MOD_LEN);
  389. goto err;
  390. }
  391. if (BN_bin2bn(in, in_len, f) == NULL) {
  392. goto err;
  393. }
  394. if (BN_ucmp(f, rsa->n) >= 0) {
  395. OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  396. goto err;
  397. }
  398. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) {
  399. if (BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx) == NULL) {
  400. goto err;
  401. }
  402. }
  403. if (!rsa->meth->bn_mod_exp(result, f, rsa->e, rsa->n, ctx, rsa->mont_n)) {
  404. goto err;
  405. }
  406. if (!BN_bn2bin_padded(buf, rsa_size, result)) {
  407. OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
  408. goto err;
  409. }
  410. switch (padding) {
  411. case RSA_PKCS1_PADDING:
  412. r = RSA_padding_check_PKCS1_type_1(out, rsa_size, buf, rsa_size);
  413. break;
  414. case RSA_NO_PADDING:
  415. r = rsa_size;
  416. break;
  417. default:
  418. OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
  419. goto err;
  420. }
  421. if (r < 0) {
  422. OPENSSL_PUT_ERROR(RSA, RSA_R_PADDING_CHECK_FAILED);
  423. } else {
  424. *out_len = r;
  425. ret = 1;
  426. }
  427. err:
  428. if (ctx != NULL) {
  429. BN_CTX_end(ctx);
  430. BN_CTX_free(ctx);
  431. }
  432. if (padding != RSA_NO_PADDING && buf != NULL) {
  433. OPENSSL_cleanse(buf, rsa_size);
  434. OPENSSL_free(buf);
  435. }
  436. return ret;
  437. }
  438. int rsa_default_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
  439. size_t len) {
  440. BIGNUM *f, *result;
  441. BN_CTX *ctx = NULL;
  442. unsigned blinding_index = 0;
  443. BN_BLINDING *blinding = NULL;
  444. int ret = 0;
  445. ctx = BN_CTX_new();
  446. if (ctx == NULL) {
  447. goto err;
  448. }
  449. BN_CTX_start(ctx);
  450. f = BN_CTX_get(ctx);
  451. result = BN_CTX_get(ctx);
  452. if (f == NULL || result == NULL) {
  453. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  454. goto err;
  455. }
  456. if (BN_bin2bn(in, len, f) == NULL) {
  457. goto err;
  458. }
  459. if (BN_ucmp(f, rsa->n) >= 0) {
  460. /* Usually the padding functions would catch this. */
  461. OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  462. goto err;
  463. }
  464. if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
  465. blinding = rsa_blinding_get(rsa, &blinding_index, ctx);
  466. if (blinding == NULL) {
  467. OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
  468. goto err;
  469. }
  470. if (!BN_BLINDING_convert(f, blinding, ctx)) {
  471. goto err;
  472. }
  473. }
  474. if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
  475. ((rsa->p != NULL) && (rsa->q != NULL) && (rsa->dmp1 != NULL) &&
  476. (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
  477. if (!rsa->meth->mod_exp(result, f, rsa, ctx)) {
  478. goto err;
  479. }
  480. } else {
  481. BIGNUM local_d;
  482. BIGNUM *d = NULL;
  483. BN_init(&local_d);
  484. d = &local_d;
  485. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  486. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) {
  487. if (BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx) ==
  488. NULL) {
  489. goto err;
  490. }
  491. }
  492. if (!rsa->meth->bn_mod_exp(result, f, d, rsa->n, ctx, rsa->mont_n)) {
  493. goto err;
  494. }
  495. }
  496. if (blinding) {
  497. if (!BN_BLINDING_invert(result, blinding, ctx)) {
  498. goto err;
  499. }
  500. }
  501. if (!BN_bn2bin_padded(out, len, result)) {
  502. OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
  503. goto err;
  504. }
  505. ret = 1;
  506. err:
  507. if (ctx != NULL) {
  508. BN_CTX_end(ctx);
  509. BN_CTX_free(ctx);
  510. }
  511. if (blinding != NULL) {
  512. rsa_blinding_release(rsa, blinding, blinding_index);
  513. }
  514. return ret;
  515. }
  516. static int mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) {
  517. BIGNUM *r1, *m1, *vrfy;
  518. BIGNUM local_dmp1, local_dmq1, local_c, local_r1;
  519. BIGNUM *dmp1, *dmq1, *c, *pr1;
  520. int ret = 0;
  521. size_t i, num_additional_primes = 0;
  522. if (rsa->additional_primes != NULL) {
  523. num_additional_primes = sk_RSA_additional_prime_num(rsa->additional_primes);
  524. }
  525. BN_CTX_start(ctx);
  526. r1 = BN_CTX_get(ctx);
  527. m1 = BN_CTX_get(ctx);
  528. vrfy = BN_CTX_get(ctx);
  529. {
  530. BIGNUM local_p, local_q;
  531. BIGNUM *p = NULL, *q = NULL;
  532. /* Make sure BN_mod_inverse in Montgomery intialization uses the
  533. * BN_FLG_CONSTTIME flag. */
  534. BN_init(&local_p);
  535. p = &local_p;
  536. BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
  537. BN_init(&local_q);
  538. q = &local_q;
  539. BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME);
  540. if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
  541. if (BN_MONT_CTX_set_locked(&rsa->mont_p, &rsa->lock, p, ctx) == NULL) {
  542. goto err;
  543. }
  544. if (BN_MONT_CTX_set_locked(&rsa->mont_q, &rsa->lock, q, ctx) == NULL) {
  545. goto err;
  546. }
  547. }
  548. }
  549. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) {
  550. if (BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx) == NULL) {
  551. goto err;
  552. }
  553. }
  554. /* compute I mod q */
  555. c = &local_c;
  556. BN_with_flags(c, I, BN_FLG_CONSTTIME);
  557. if (!BN_mod(r1, c, rsa->q, ctx)) {
  558. goto err;
  559. }
  560. /* compute r1^dmq1 mod q */
  561. dmq1 = &local_dmq1;
  562. BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
  563. if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx, rsa->mont_q)) {
  564. goto err;
  565. }
  566. /* compute I mod p */
  567. c = &local_c;
  568. BN_with_flags(c, I, BN_FLG_CONSTTIME);
  569. if (!BN_mod(r1, c, rsa->p, ctx)) {
  570. goto err;
  571. }
  572. /* compute r1^dmp1 mod p */
  573. dmp1 = &local_dmp1;
  574. BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
  575. if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx, rsa->mont_p)) {
  576. goto err;
  577. }
  578. if (!BN_sub(r0, r0, m1)) {
  579. goto err;
  580. }
  581. /* This will help stop the size of r0 increasing, which does
  582. * affect the multiply if it optimised for a power of 2 size */
  583. if (BN_is_negative(r0)) {
  584. if (!BN_add(r0, r0, rsa->p)) {
  585. goto err;
  586. }
  587. }
  588. if (!BN_mul(r1, r0, rsa->iqmp, ctx)) {
  589. goto err;
  590. }
  591. /* Turn BN_FLG_CONSTTIME flag on before division operation */
  592. pr1 = &local_r1;
  593. BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
  594. if (!BN_mod(r0, pr1, rsa->p, ctx)) {
  595. goto err;
  596. }
  597. /* If p < q it is occasionally possible for the correction of
  598. * adding 'p' if r0 is negative above to leave the result still
  599. * negative. This can break the private key operations: the following
  600. * second correction should *always* correct this rare occurrence.
  601. * This will *never* happen with OpenSSL generated keys because
  602. * they ensure p > q [steve] */
  603. if (BN_is_negative(r0)) {
  604. if (!BN_add(r0, r0, rsa->p)) {
  605. goto err;
  606. }
  607. }
  608. if (!BN_mul(r1, r0, rsa->q, ctx)) {
  609. goto err;
  610. }
  611. if (!BN_add(r0, r1, m1)) {
  612. goto err;
  613. }
  614. for (i = 0; i < num_additional_primes; i++) {
  615. /* multi-prime RSA. */
  616. BIGNUM local_exp, local_prime;
  617. BIGNUM *exp = &local_exp, *prime = &local_prime;
  618. RSA_additional_prime *ap =
  619. sk_RSA_additional_prime_value(rsa->additional_primes, i);
  620. BN_with_flags(exp, ap->exp, BN_FLG_CONSTTIME);
  621. BN_with_flags(prime, ap->prime, BN_FLG_CONSTTIME);
  622. /* c will already point to a BIGNUM with the correct flags. */
  623. if (!BN_mod(r1, c, prime, ctx)) {
  624. goto err;
  625. }
  626. if ((rsa->flags & RSA_FLAG_CACHE_PRIVATE) &&
  627. !BN_MONT_CTX_set_locked(&ap->mont, &rsa->lock, prime, ctx)) {
  628. goto err;
  629. }
  630. if (!rsa->meth->bn_mod_exp(m1, r1, exp, prime, ctx, ap->mont)) {
  631. goto err;
  632. }
  633. BN_set_flags(m1, BN_FLG_CONSTTIME);
  634. if (!BN_sub(m1, m1, r0) ||
  635. !BN_mul(m1, m1, ap->coeff, ctx) ||
  636. !BN_mod(m1, m1, prime, ctx) ||
  637. (BN_is_negative(m1) && !BN_add(m1, m1, prime)) ||
  638. !BN_mul(m1, m1, ap->r, ctx) ||
  639. !BN_add(r0, r0, m1)) {
  640. goto err;
  641. }
  642. }
  643. if (rsa->e && rsa->n) {
  644. if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx, rsa->mont_n)) {
  645. goto err;
  646. }
  647. /* If 'I' was greater than (or equal to) rsa->n, the operation
  648. * will be equivalent to using 'I mod n'. However, the result of
  649. * the verify will *always* be less than 'n' so we don't check
  650. * for absolute equality, just congruency. */
  651. if (!BN_sub(vrfy, vrfy, I)) {
  652. goto err;
  653. }
  654. if (!BN_mod(vrfy, vrfy, rsa->n, ctx)) {
  655. goto err;
  656. }
  657. if (BN_is_negative(vrfy)) {
  658. if (!BN_add(vrfy, vrfy, rsa->n)) {
  659. goto err;
  660. }
  661. }
  662. if (!BN_is_zero(vrfy)) {
  663. /* 'I' and 'vrfy' aren't congruent mod n. Don't leak
  664. * miscalculated CRT output, just do a raw (slower)
  665. * mod_exp and return that instead. */
  666. BIGNUM local_d;
  667. BIGNUM *d = NULL;
  668. d = &local_d;
  669. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  670. if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx, rsa->mont_n)) {
  671. goto err;
  672. }
  673. }
  674. }
  675. ret = 1;
  676. err:
  677. BN_CTX_end(ctx);
  678. return ret;
  679. }
  680. int rsa_default_multi_prime_keygen(RSA *rsa, int bits, int num_primes,
  681. BIGNUM *e_value, BN_GENCB *cb) {
  682. BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *tmp;
  683. BIGNUM local_r0, local_d, local_p;
  684. BIGNUM *pr0, *d, *p;
  685. int prime_bits, ok = -1, n = 0, i, j;
  686. BN_CTX *ctx = NULL;
  687. STACK_OF(RSA_additional_prime) *additional_primes = NULL;
  688. if (num_primes < 2) {
  689. ok = 0; /* we set our own err */
  690. OPENSSL_PUT_ERROR(RSA, RSA_R_MUST_HAVE_AT_LEAST_TWO_PRIMES);
  691. goto err;
  692. }
  693. ctx = BN_CTX_new();
  694. if (ctx == NULL) {
  695. goto err;
  696. }
  697. BN_CTX_start(ctx);
  698. r0 = BN_CTX_get(ctx);
  699. r1 = BN_CTX_get(ctx);
  700. r2 = BN_CTX_get(ctx);
  701. r3 = BN_CTX_get(ctx);
  702. if (r0 == NULL || r1 == NULL || r2 == NULL || r3 == NULL) {
  703. goto err;
  704. }
  705. if (num_primes > 2) {
  706. additional_primes = sk_RSA_additional_prime_new_null();
  707. if (additional_primes == NULL) {
  708. goto err;
  709. }
  710. }
  711. for (i = 2; i < num_primes; i++) {
  712. RSA_additional_prime *ap = OPENSSL_malloc(sizeof(RSA_additional_prime));
  713. if (ap == NULL) {
  714. goto err;
  715. }
  716. memset(ap, 0, sizeof(RSA_additional_prime));
  717. ap->prime = BN_new();
  718. ap->exp = BN_new();
  719. ap->coeff = BN_new();
  720. ap->r = BN_new();
  721. if (ap->prime == NULL ||
  722. ap->exp == NULL ||
  723. ap->coeff == NULL ||
  724. ap->r == NULL ||
  725. !sk_RSA_additional_prime_push(additional_primes, ap)) {
  726. RSA_additional_prime_free(ap);
  727. goto err;
  728. }
  729. }
  730. /* We need the RSA components non-NULL */
  731. if (!rsa->n && ((rsa->n = BN_new()) == NULL)) {
  732. goto err;
  733. }
  734. if (!rsa->d && ((rsa->d = BN_new()) == NULL)) {
  735. goto err;
  736. }
  737. if (!rsa->e && ((rsa->e = BN_new()) == NULL)) {
  738. goto err;
  739. }
  740. if (!rsa->p && ((rsa->p = BN_new()) == NULL)) {
  741. goto err;
  742. }
  743. if (!rsa->q && ((rsa->q = BN_new()) == NULL)) {
  744. goto err;
  745. }
  746. if (!rsa->dmp1 && ((rsa->dmp1 = BN_new()) == NULL)) {
  747. goto err;
  748. }
  749. if (!rsa->dmq1 && ((rsa->dmq1 = BN_new()) == NULL)) {
  750. goto err;
  751. }
  752. if (!rsa->iqmp && ((rsa->iqmp = BN_new()) == NULL)) {
  753. goto err;
  754. }
  755. if (!BN_copy(rsa->e, e_value)) {
  756. goto err;
  757. }
  758. /* generate p and q */
  759. prime_bits = (bits + (num_primes - 1)) / num_primes;
  760. for (;;) {
  761. if (!BN_generate_prime_ex(rsa->p, prime_bits, 0, NULL, NULL, cb) ||
  762. !BN_sub(r2, rsa->p, BN_value_one()) ||
  763. !BN_gcd(r1, r2, rsa->e, ctx)) {
  764. goto err;
  765. }
  766. if (BN_is_one(r1)) {
  767. break;
  768. }
  769. if (!BN_GENCB_call(cb, 2, n++)) {
  770. goto err;
  771. }
  772. }
  773. if (!BN_GENCB_call(cb, 3, 0)) {
  774. goto err;
  775. }
  776. prime_bits = ((bits - prime_bits) + (num_primes - 2)) / (num_primes - 1);
  777. for (;;) {
  778. /* When generating ridiculously small keys, we can get stuck
  779. * continually regenerating the same prime values. Check for
  780. * this and bail if it happens 3 times. */
  781. unsigned int degenerate = 0;
  782. do {
  783. if (!BN_generate_prime_ex(rsa->q, prime_bits, 0, NULL, NULL, cb)) {
  784. goto err;
  785. }
  786. } while ((BN_cmp(rsa->p, rsa->q) == 0) && (++degenerate < 3));
  787. if (degenerate == 3) {
  788. ok = 0; /* we set our own err */
  789. OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL);
  790. goto err;
  791. }
  792. if (!BN_sub(r2, rsa->q, BN_value_one()) ||
  793. !BN_gcd(r1, r2, rsa->e, ctx)) {
  794. goto err;
  795. }
  796. if (BN_is_one(r1)) {
  797. break;
  798. }
  799. if (!BN_GENCB_call(cb, 2, n++)) {
  800. goto err;
  801. }
  802. }
  803. if (!BN_GENCB_call(cb, 3, 1) ||
  804. !BN_mul(rsa->n, rsa->p, rsa->q, ctx)) {
  805. goto err;
  806. }
  807. for (i = 2; i < num_primes; i++) {
  808. RSA_additional_prime *ap =
  809. sk_RSA_additional_prime_value(additional_primes, i - 2);
  810. prime_bits = ((bits - BN_num_bits(rsa->n)) + (num_primes - (i + 1))) /
  811. (num_primes - i);
  812. for (;;) {
  813. if (!BN_generate_prime_ex(ap->prime, prime_bits, 0, NULL, NULL, cb)) {
  814. goto err;
  815. }
  816. if (BN_cmp(rsa->p, ap->prime) == 0 ||
  817. BN_cmp(rsa->q, ap->prime) == 0) {
  818. continue;
  819. }
  820. for (j = 0; j < i - 2; j++) {
  821. if (BN_cmp(sk_RSA_additional_prime_value(additional_primes, j)->prime,
  822. ap->prime) == 0) {
  823. break;
  824. }
  825. }
  826. if (j != i - 2) {
  827. continue;
  828. }
  829. if (!BN_sub(r2, ap->prime, BN_value_one()) ||
  830. !BN_gcd(r1, r2, rsa->e, ctx)) {
  831. goto err;
  832. }
  833. if (!BN_is_one(r1)) {
  834. continue;
  835. }
  836. if (i != num_primes - 1) {
  837. break;
  838. }
  839. /* For the last prime we'll check that it makes n large enough. In the
  840. * two prime case this isn't a problem because we generate primes with
  841. * the top two bits set and so the product is always of the expected
  842. * size. In the multi prime case, this doesn't follow. */
  843. if (!BN_mul(r1, rsa->n, ap->prime, ctx)) {
  844. goto err;
  845. }
  846. if (BN_num_bits(r1) == (unsigned) bits) {
  847. break;
  848. }
  849. if (!BN_GENCB_call(cb, 2, n++)) {
  850. goto err;
  851. }
  852. }
  853. /* ap->r is is the product of all the primes prior to the current one
  854. * (including p and q). */
  855. if (!BN_copy(ap->r, rsa->n)) {
  856. goto err;
  857. }
  858. if (i == num_primes - 1) {
  859. /* In the case of the last prime, we calculated n as |r1| in the loop
  860. * above. */
  861. if (!BN_copy(rsa->n, r1)) {
  862. goto err;
  863. }
  864. } else if (!BN_mul(rsa->n, rsa->n, ap->prime, ctx)) {
  865. goto err;
  866. }
  867. if (!BN_GENCB_call(cb, 3, 1)) {
  868. goto err;
  869. }
  870. }
  871. if (BN_cmp(rsa->p, rsa->q) < 0) {
  872. tmp = rsa->p;
  873. rsa->p = rsa->q;
  874. rsa->q = tmp;
  875. }
  876. /* calculate d */
  877. if (!BN_sub(r1, rsa->p, BN_value_one())) {
  878. goto err; /* p-1 */
  879. }
  880. if (!BN_sub(r2, rsa->q, BN_value_one())) {
  881. goto err; /* q-1 */
  882. }
  883. if (!BN_mul(r0, r1, r2, ctx)) {
  884. goto err; /* (p-1)(q-1) */
  885. }
  886. for (i = 2; i < num_primes; i++) {
  887. RSA_additional_prime *ap =
  888. sk_RSA_additional_prime_value(additional_primes, i - 2);
  889. if (!BN_sub(r3, ap->prime, BN_value_one()) ||
  890. !BN_mul(r0, r0, r3, ctx)) {
  891. goto err;
  892. }
  893. }
  894. pr0 = &local_r0;
  895. BN_with_flags(pr0, r0, BN_FLG_CONSTTIME);
  896. if (!BN_mod_inverse(rsa->d, rsa->e, pr0, ctx)) {
  897. goto err; /* d */
  898. }
  899. /* set up d for correct BN_FLG_CONSTTIME flag */
  900. d = &local_d;
  901. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  902. /* calculate d mod (p-1) */
  903. if (!BN_mod(rsa->dmp1, d, r1, ctx)) {
  904. goto err;
  905. }
  906. /* calculate d mod (q-1) */
  907. if (!BN_mod(rsa->dmq1, d, r2, ctx)) {
  908. goto err;
  909. }
  910. /* calculate inverse of q mod p */
  911. p = &local_p;
  912. BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
  913. if (!BN_mod_inverse(rsa->iqmp, rsa->q, p, ctx)) {
  914. goto err;
  915. }
  916. for (i = 2; i < num_primes; i++) {
  917. RSA_additional_prime *ap =
  918. sk_RSA_additional_prime_value(additional_primes, i - 2);
  919. if (!BN_sub(ap->exp, ap->prime, BN_value_one()) ||
  920. !BN_mod(ap->exp, rsa->d, ap->exp, ctx) ||
  921. !BN_mod_inverse(ap->coeff, ap->r, ap->prime, ctx)) {
  922. goto err;
  923. }
  924. }
  925. ok = 1;
  926. rsa->additional_primes = additional_primes;
  927. additional_primes = NULL;
  928. err:
  929. if (ok == -1) {
  930. OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
  931. ok = 0;
  932. }
  933. if (ctx != NULL) {
  934. BN_CTX_end(ctx);
  935. BN_CTX_free(ctx);
  936. }
  937. sk_RSA_additional_prime_pop_free(additional_primes,
  938. RSA_additional_prime_free);
  939. return ok;
  940. }
  941. int rsa_default_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
  942. return rsa_default_multi_prime_keygen(rsa, bits, 2 /* num primes */, e_value,
  943. cb);
  944. }
  945. /* Many of these methods are NULL to more easily drop unused functions. The
  946. * wrapper functions will select the appropriate |rsa_default_*| for all
  947. * methods. */
  948. const RSA_METHOD RSA_default_method = {
  949. {
  950. 0 /* references */,
  951. 1 /* is_static */,
  952. },
  953. NULL /* app_data */,
  954. NULL /* init */,
  955. NULL /* finish (defaults to rsa_default_finish) */,
  956. NULL /* size (defaults to rsa_default_size) */,
  957. NULL /* sign */,
  958. NULL /* verify */,
  959. NULL /* encrypt (defaults to rsa_default_encrypt) */,
  960. NULL /* sign_raw (defaults to rsa_default_sign_raw) */,
  961. NULL /* decrypt (defaults to rsa_default_decrypt) */,
  962. NULL /* verify_raw (defaults to rsa_default_verify_raw) */,
  963. NULL /* private_transform (defaults to rsa_default_private_transform) */,
  964. mod_exp,
  965. BN_mod_exp_mont /* bn_mod_exp */,
  966. RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE,
  967. NULL /* keygen (defaults to rsa_default_keygen) */,
  968. NULL /* multi_prime_keygen (defaults to rsa_default_multi_prime_keygen) */,
  969. NULL /* supports_digest */,
  970. };