Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

rsa_impl.c 30 KiB

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