Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  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. buf = OPENSSL_malloc(rsa_size);
  318. if (buf == NULL) {
  319. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  320. goto err;
  321. }
  322. if (in_len != rsa_size) {
  323. OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_LEN_NOT_EQUAL_TO_MOD_LEN);
  324. goto err;
  325. }
  326. if (!RSA_private_transform(rsa, buf, in, rsa_size)) {
  327. goto err;
  328. }
  329. switch (padding) {
  330. case RSA_PKCS1_PADDING:
  331. r = RSA_padding_check_PKCS1_type_2(out, rsa_size, buf, rsa_size);
  332. break;
  333. case RSA_PKCS1_OAEP_PADDING:
  334. /* Use the default parameters: SHA-1 for both hashes and no label. */
  335. r = RSA_padding_check_PKCS1_OAEP_mgf1(out, rsa_size, buf, rsa_size,
  336. NULL, 0, NULL, NULL);
  337. break;
  338. case RSA_NO_PADDING:
  339. r = RSA_padding_check_none(out, rsa_size, buf, rsa_size);
  340. break;
  341. default:
  342. OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
  343. goto err;
  344. }
  345. if (r < 0) {
  346. OPENSSL_PUT_ERROR(RSA, RSA_R_PADDING_CHECK_FAILED);
  347. } else {
  348. *out_len = r;
  349. ret = 1;
  350. }
  351. err:
  352. if (buf != NULL) {
  353. OPENSSL_cleanse(buf, rsa_size);
  354. OPENSSL_free(buf);
  355. }
  356. return ret;
  357. }
  358. static int verify_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
  359. const uint8_t *in, size_t in_len, int padding) {
  360. const unsigned rsa_size = RSA_size(rsa);
  361. BIGNUM *f, *result;
  362. int ret = 0;
  363. int r = -1;
  364. uint8_t *buf = NULL;
  365. BN_CTX *ctx = NULL;
  366. if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
  367. OPENSSL_PUT_ERROR(RSA, RSA_R_MODULUS_TOO_LARGE);
  368. return 0;
  369. }
  370. if (BN_ucmp(rsa->n, rsa->e) <= 0) {
  371. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_E_VALUE);
  372. return 0;
  373. }
  374. if (max_out < rsa_size) {
  375. OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
  376. return 0;
  377. }
  378. /* for large moduli, enforce exponent limit */
  379. if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS &&
  380. BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
  381. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_E_VALUE);
  382. return 0;
  383. }
  384. ctx = BN_CTX_new();
  385. if (ctx == NULL) {
  386. goto err;
  387. }
  388. BN_CTX_start(ctx);
  389. f = BN_CTX_get(ctx);
  390. result = BN_CTX_get(ctx);
  391. buf = OPENSSL_malloc(rsa_size);
  392. if (!f || !result || !buf) {
  393. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  394. goto err;
  395. }
  396. if (in_len != rsa_size) {
  397. OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_LEN_NOT_EQUAL_TO_MOD_LEN);
  398. goto err;
  399. }
  400. if (BN_bin2bn(in, in_len, f) == NULL) {
  401. goto err;
  402. }
  403. if (BN_ucmp(f, rsa->n) >= 0) {
  404. OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  405. goto err;
  406. }
  407. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) {
  408. if (BN_MONT_CTX_set_locked(&rsa->_method_mod_n, &rsa->lock, rsa->n, ctx) ==
  409. NULL) {
  410. goto err;
  411. }
  412. }
  413. if (!rsa->meth->bn_mod_exp(result, f, rsa->e, rsa->n, ctx,
  414. rsa->_method_mod_n)) {
  415. goto err;
  416. }
  417. if (!BN_bn2bin_padded(buf, rsa_size, result)) {
  418. OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
  419. goto err;
  420. }
  421. switch (padding) {
  422. case RSA_PKCS1_PADDING:
  423. r = RSA_padding_check_PKCS1_type_1(out, rsa_size, buf, rsa_size);
  424. break;
  425. case RSA_NO_PADDING:
  426. r = RSA_padding_check_none(out, rsa_size, buf, rsa_size);
  427. break;
  428. default:
  429. OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
  430. goto err;
  431. }
  432. if (r < 0) {
  433. OPENSSL_PUT_ERROR(RSA, RSA_R_PADDING_CHECK_FAILED);
  434. } else {
  435. *out_len = r;
  436. ret = 1;
  437. }
  438. err:
  439. if (ctx != NULL) {
  440. BN_CTX_end(ctx);
  441. BN_CTX_free(ctx);
  442. }
  443. if (buf != NULL) {
  444. OPENSSL_cleanse(buf, rsa_size);
  445. OPENSSL_free(buf);
  446. }
  447. return ret;
  448. }
  449. static int private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
  450. size_t len) {
  451. BIGNUM *f, *result;
  452. BN_CTX *ctx = NULL;
  453. unsigned blinding_index = 0;
  454. BN_BLINDING *blinding = NULL;
  455. int ret = 0;
  456. ctx = BN_CTX_new();
  457. if (ctx == NULL) {
  458. goto err;
  459. }
  460. BN_CTX_start(ctx);
  461. f = BN_CTX_get(ctx);
  462. result = BN_CTX_get(ctx);
  463. if (f == NULL || result == NULL) {
  464. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  465. goto err;
  466. }
  467. if (BN_bin2bn(in, len, f) == NULL) {
  468. goto err;
  469. }
  470. if (BN_ucmp(f, rsa->n) >= 0) {
  471. /* Usually the padding functions would catch this. */
  472. OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  473. goto err;
  474. }
  475. if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
  476. blinding = rsa_blinding_get(rsa, &blinding_index, ctx);
  477. if (blinding == NULL) {
  478. OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
  479. goto err;
  480. }
  481. if (!BN_BLINDING_convert_ex(f, NULL, blinding, ctx)) {
  482. goto err;
  483. }
  484. }
  485. if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
  486. ((rsa->p != NULL) && (rsa->q != NULL) && (rsa->dmp1 != NULL) &&
  487. (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
  488. if (!rsa->meth->mod_exp(result, f, rsa, ctx)) {
  489. goto err;
  490. }
  491. } else {
  492. BIGNUM local_d;
  493. BIGNUM *d = NULL;
  494. BN_init(&local_d);
  495. d = &local_d;
  496. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  497. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) {
  498. if (BN_MONT_CTX_set_locked(&rsa->_method_mod_n, &rsa->lock, rsa->n,
  499. ctx) == NULL) {
  500. goto err;
  501. }
  502. }
  503. if (!rsa->meth->bn_mod_exp(result, f, d, rsa->n, ctx, rsa->_method_mod_n)) {
  504. goto err;
  505. }
  506. }
  507. if (blinding) {
  508. if (!BN_BLINDING_invert_ex(result, NULL, blinding, ctx)) {
  509. goto err;
  510. }
  511. }
  512. if (!BN_bn2bin_padded(out, len, result)) {
  513. OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
  514. goto err;
  515. }
  516. ret = 1;
  517. err:
  518. if (ctx != NULL) {
  519. BN_CTX_end(ctx);
  520. BN_CTX_free(ctx);
  521. }
  522. if (blinding != NULL) {
  523. rsa_blinding_release(rsa, blinding, blinding_index);
  524. }
  525. return ret;
  526. }
  527. static int mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) {
  528. BIGNUM *r1, *m1, *vrfy;
  529. BIGNUM local_dmp1, local_dmq1, local_c, local_r1;
  530. BIGNUM *dmp1, *dmq1, *c, *pr1;
  531. int ret = 0;
  532. size_t i, num_additional_primes = 0;
  533. if (rsa->additional_primes != NULL) {
  534. num_additional_primes = sk_RSA_additional_prime_num(rsa->additional_primes);
  535. }
  536. BN_CTX_start(ctx);
  537. r1 = BN_CTX_get(ctx);
  538. m1 = BN_CTX_get(ctx);
  539. vrfy = BN_CTX_get(ctx);
  540. {
  541. BIGNUM local_p, local_q;
  542. BIGNUM *p = NULL, *q = NULL;
  543. /* Make sure BN_mod_inverse in Montgomery intialization uses the
  544. * BN_FLG_CONSTTIME flag (unless RSA_FLAG_NO_CONSTTIME is set) */
  545. BN_init(&local_p);
  546. p = &local_p;
  547. BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
  548. BN_init(&local_q);
  549. q = &local_q;
  550. BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME);
  551. if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
  552. if (BN_MONT_CTX_set_locked(&rsa->_method_mod_p, &rsa->lock, p, ctx) ==
  553. NULL) {
  554. goto err;
  555. }
  556. if (BN_MONT_CTX_set_locked(&rsa->_method_mod_q, &rsa->lock, q, ctx) ==
  557. NULL) {
  558. goto err;
  559. }
  560. }
  561. }
  562. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) {
  563. if (BN_MONT_CTX_set_locked(&rsa->_method_mod_n, &rsa->lock, rsa->n, ctx) ==
  564. NULL) {
  565. goto err;
  566. }
  567. }
  568. /* compute I mod q */
  569. c = &local_c;
  570. BN_with_flags(c, I, BN_FLG_CONSTTIME);
  571. if (!BN_mod(r1, c, rsa->q, ctx)) {
  572. goto err;
  573. }
  574. /* compute r1^dmq1 mod q */
  575. dmq1 = &local_dmq1;
  576. BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
  577. if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx, rsa->_method_mod_q)) {
  578. goto err;
  579. }
  580. /* compute I mod p */
  581. c = &local_c;
  582. BN_with_flags(c, I, BN_FLG_CONSTTIME);
  583. if (!BN_mod(r1, c, rsa->p, ctx)) {
  584. goto err;
  585. }
  586. /* compute r1^dmp1 mod p */
  587. dmp1 = &local_dmp1;
  588. BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
  589. if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx, rsa->_method_mod_p)) {
  590. goto err;
  591. }
  592. if (!BN_sub(r0, r0, m1)) {
  593. goto err;
  594. }
  595. /* This will help stop the size of r0 increasing, which does
  596. * affect the multiply if it optimised for a power of 2 size */
  597. if (BN_is_negative(r0)) {
  598. if (!BN_add(r0, r0, rsa->p)) {
  599. goto err;
  600. }
  601. }
  602. if (!BN_mul(r1, r0, rsa->iqmp, ctx)) {
  603. goto err;
  604. }
  605. /* Turn BN_FLG_CONSTTIME flag on before division operation */
  606. pr1 = &local_r1;
  607. BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
  608. if (!BN_mod(r0, pr1, rsa->p, ctx)) {
  609. goto err;
  610. }
  611. /* If p < q it is occasionally possible for the correction of
  612. * adding 'p' if r0 is negative above to leave the result still
  613. * negative. This can break the private key operations: the following
  614. * second correction should *always* correct this rare occurrence.
  615. * This will *never* happen with OpenSSL generated keys because
  616. * they ensure p > q [steve] */
  617. if (BN_is_negative(r0)) {
  618. if (!BN_add(r0, r0, rsa->p)) {
  619. goto err;
  620. }
  621. }
  622. if (!BN_mul(r1, r0, rsa->q, ctx)) {
  623. goto err;
  624. }
  625. if (!BN_add(r0, r1, m1)) {
  626. goto err;
  627. }
  628. for (i = 0; i < num_additional_primes; i++) {
  629. /* multi-prime RSA. */
  630. BIGNUM local_exp, local_prime;
  631. BIGNUM *exp = &local_exp, *prime = &local_prime;
  632. RSA_additional_prime *ap =
  633. sk_RSA_additional_prime_value(rsa->additional_primes, i);
  634. BN_with_flags(exp, ap->exp, BN_FLG_CONSTTIME);
  635. BN_with_flags(prime, ap->prime, BN_FLG_CONSTTIME);
  636. /* c will already point to a BIGNUM with the correct flags. */
  637. if (!BN_mod(r1, c, prime, ctx)) {
  638. goto err;
  639. }
  640. if ((rsa->flags & RSA_FLAG_CACHE_PRIVATE) &&
  641. !BN_MONT_CTX_set_locked(&ap->method_mod, &rsa->lock, prime, ctx)) {
  642. goto err;
  643. }
  644. if (!rsa->meth->bn_mod_exp(m1, r1, exp, prime, ctx, ap->method_mod)) {
  645. goto err;
  646. }
  647. BN_set_flags(m1, BN_FLG_CONSTTIME);
  648. if (!BN_sub(m1, m1, r0) ||
  649. !BN_mul(m1, m1, ap->coeff, ctx) ||
  650. !BN_mod(m1, m1, prime, ctx) ||
  651. (BN_is_negative(m1) && !BN_add(m1, m1, prime)) ||
  652. !BN_mul(m1, m1, ap->r, ctx) ||
  653. !BN_add(r0, r0, m1)) {
  654. goto err;
  655. }
  656. }
  657. if (rsa->e && rsa->n) {
  658. if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
  659. rsa->_method_mod_n)) {
  660. goto err;
  661. }
  662. /* If 'I' was greater than (or equal to) rsa->n, the operation
  663. * will be equivalent to using 'I mod n'. However, the result of
  664. * the verify will *always* be less than 'n' so we don't check
  665. * for absolute equality, just congruency. */
  666. if (!BN_sub(vrfy, vrfy, I)) {
  667. goto err;
  668. }
  669. if (!BN_mod(vrfy, vrfy, rsa->n, ctx)) {
  670. goto err;
  671. }
  672. if (BN_is_negative(vrfy)) {
  673. if (!BN_add(vrfy, vrfy, rsa->n)) {
  674. goto err;
  675. }
  676. }
  677. if (!BN_is_zero(vrfy)) {
  678. /* 'I' and 'vrfy' aren't congruent mod n. Don't leak
  679. * miscalculated CRT output, just do a raw (slower)
  680. * mod_exp and return that instead. */
  681. BIGNUM local_d;
  682. BIGNUM *d = NULL;
  683. d = &local_d;
  684. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  685. if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx, rsa->_method_mod_n)) {
  686. goto err;
  687. }
  688. }
  689. }
  690. ret = 1;
  691. err:
  692. BN_CTX_end(ctx);
  693. return ret;
  694. }
  695. static int keygen_multiprime(RSA *rsa, int bits, int num_primes,
  696. BIGNUM *e_value, BN_GENCB *cb) {
  697. BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *tmp;
  698. BIGNUM local_r0, local_d, local_p;
  699. BIGNUM *pr0, *d, *p;
  700. int prime_bits, ok = -1, n = 0, i, j;
  701. BN_CTX *ctx = NULL;
  702. STACK_OF(RSA_additional_prime) *additional_primes = NULL;
  703. if (num_primes < 2) {
  704. ok = 0; /* we set our own err */
  705. OPENSSL_PUT_ERROR(RSA, RSA_R_MUST_HAVE_AT_LEAST_TWO_PRIMES);
  706. goto err;
  707. }
  708. ctx = BN_CTX_new();
  709. if (ctx == NULL) {
  710. goto err;
  711. }
  712. BN_CTX_start(ctx);
  713. r0 = BN_CTX_get(ctx);
  714. r1 = BN_CTX_get(ctx);
  715. r2 = BN_CTX_get(ctx);
  716. r3 = BN_CTX_get(ctx);
  717. if (r3 == NULL) {
  718. goto err;
  719. }
  720. if (num_primes > 2) {
  721. additional_primes = sk_RSA_additional_prime_new_null();
  722. if (additional_primes == NULL) {
  723. goto err;
  724. }
  725. }
  726. for (i = 2; i < num_primes; i++) {
  727. RSA_additional_prime *ap = OPENSSL_malloc(sizeof(RSA_additional_prime));
  728. if (ap == NULL) {
  729. goto err;
  730. }
  731. memset(ap, 0, sizeof(RSA_additional_prime));
  732. ap->prime = BN_new();
  733. ap->exp = BN_new();
  734. ap->coeff = BN_new();
  735. ap->r = BN_new();
  736. if (ap->prime == NULL ||
  737. ap->exp == NULL ||
  738. ap->coeff == NULL ||
  739. ap->r == NULL ||
  740. !sk_RSA_additional_prime_push(additional_primes, ap)) {
  741. RSA_additional_prime_free(ap);
  742. goto err;
  743. }
  744. }
  745. /* We need the RSA components non-NULL */
  746. if (!rsa->n && ((rsa->n = BN_new()) == NULL)) {
  747. goto err;
  748. }
  749. if (!rsa->d && ((rsa->d = BN_new()) == NULL)) {
  750. goto err;
  751. }
  752. if (!rsa->e && ((rsa->e = BN_new()) == NULL)) {
  753. goto err;
  754. }
  755. if (!rsa->p && ((rsa->p = BN_new()) == NULL)) {
  756. goto err;
  757. }
  758. if (!rsa->q && ((rsa->q = BN_new()) == NULL)) {
  759. goto err;
  760. }
  761. if (!rsa->dmp1 && ((rsa->dmp1 = BN_new()) == NULL)) {
  762. goto err;
  763. }
  764. if (!rsa->dmq1 && ((rsa->dmq1 = BN_new()) == NULL)) {
  765. goto err;
  766. }
  767. if (!rsa->iqmp && ((rsa->iqmp = BN_new()) == NULL)) {
  768. goto err;
  769. }
  770. if (!BN_copy(rsa->e, e_value)) {
  771. goto err;
  772. }
  773. /* generate p and q */
  774. prime_bits = (bits + (num_primes - 1)) / num_primes;
  775. for (;;) {
  776. if (!BN_generate_prime_ex(rsa->p, prime_bits, 0, NULL, NULL, cb) ||
  777. !BN_sub(r2, rsa->p, BN_value_one()) ||
  778. !BN_gcd(r1, r2, rsa->e, ctx)) {
  779. goto err;
  780. }
  781. if (BN_is_one(r1)) {
  782. break;
  783. }
  784. if (!BN_GENCB_call(cb, 2, n++)) {
  785. goto err;
  786. }
  787. }
  788. if (!BN_GENCB_call(cb, 3, 0)) {
  789. goto err;
  790. }
  791. prime_bits = ((bits - prime_bits) + (num_primes - 2)) / (num_primes - 1);
  792. for (;;) {
  793. /* When generating ridiculously small keys, we can get stuck
  794. * continually regenerating the same prime values. Check for
  795. * this and bail if it happens 3 times. */
  796. unsigned int degenerate = 0;
  797. do {
  798. if (!BN_generate_prime_ex(rsa->q, prime_bits, 0, NULL, NULL, cb)) {
  799. goto err;
  800. }
  801. } while ((BN_cmp(rsa->p, rsa->q) == 0) && (++degenerate < 3));
  802. if (degenerate == 3) {
  803. ok = 0; /* we set our own err */
  804. OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL);
  805. goto err;
  806. }
  807. if (!BN_sub(r2, rsa->q, BN_value_one()) ||
  808. !BN_gcd(r1, r2, rsa->e, ctx)) {
  809. goto err;
  810. }
  811. if (BN_is_one(r1)) {
  812. break;
  813. }
  814. if (!BN_GENCB_call(cb, 2, n++)) {
  815. goto err;
  816. }
  817. }
  818. if (!BN_GENCB_call(cb, 3, 1) ||
  819. !BN_mul(rsa->n, rsa->p, rsa->q, ctx)) {
  820. goto err;
  821. }
  822. for (i = 2; i < num_primes; i++) {
  823. RSA_additional_prime *ap =
  824. sk_RSA_additional_prime_value(additional_primes, i - 2);
  825. prime_bits = ((bits - BN_num_bits(rsa->n)) + (num_primes - (i + 1))) /
  826. (num_primes - i);
  827. for (;;) {
  828. if (!BN_generate_prime_ex(ap->prime, prime_bits, 0, NULL, NULL, cb)) {
  829. goto err;
  830. }
  831. if (BN_cmp(rsa->p, ap->prime) == 0 ||
  832. BN_cmp(rsa->q, ap->prime) == 0) {
  833. continue;
  834. }
  835. for (j = 0; j < i - 2; j++) {
  836. if (BN_cmp(sk_RSA_additional_prime_value(additional_primes, j)->prime,
  837. ap->prime) == 0) {
  838. break;
  839. }
  840. }
  841. if (j != i - 2) {
  842. continue;
  843. }
  844. if (!BN_sub(r2, ap->prime, BN_value_one()) ||
  845. !BN_gcd(r1, r2, rsa->e, ctx)) {
  846. goto err;
  847. }
  848. if (!BN_is_one(r1)) {
  849. continue;
  850. }
  851. if (i != num_primes - 1) {
  852. break;
  853. }
  854. /* For the last prime we'll check that it makes n large enough. In the
  855. * two prime case this isn't a problem because we generate primes with
  856. * the top two bits set and so the product is always of the expected
  857. * size. In the multi prime case, this doesn't follow. */
  858. if (!BN_mul(r1, rsa->n, ap->prime, ctx)) {
  859. goto err;
  860. }
  861. if (BN_num_bits(r1) == bits) {
  862. break;
  863. }
  864. if (!BN_GENCB_call(cb, 2, n++)) {
  865. goto err;
  866. }
  867. }
  868. /* ap->r is is the product of all the primes prior to the current one
  869. * (including p and q). */
  870. if (!BN_copy(ap->r, rsa->n)) {
  871. goto err;
  872. }
  873. if (i == num_primes - 1) {
  874. /* In the case of the last prime, we calculated n as |r1| in the loop
  875. * above. */
  876. if (!BN_copy(rsa->n, r1)) {
  877. goto err;
  878. }
  879. } else if (!BN_mul(rsa->n, rsa->n, ap->prime, ctx)) {
  880. goto err;
  881. }
  882. if (!BN_GENCB_call(cb, 3, 1)) {
  883. goto err;
  884. }
  885. }
  886. if (BN_cmp(rsa->p, rsa->q) < 0) {
  887. tmp = rsa->p;
  888. rsa->p = rsa->q;
  889. rsa->q = tmp;
  890. }
  891. /* calculate d */
  892. if (!BN_sub(r1, rsa->p, BN_value_one())) {
  893. goto err; /* p-1 */
  894. }
  895. if (!BN_sub(r2, rsa->q, BN_value_one())) {
  896. goto err; /* q-1 */
  897. }
  898. if (!BN_mul(r0, r1, r2, ctx)) {
  899. goto err; /* (p-1)(q-1) */
  900. }
  901. for (i = 2; i < num_primes; i++) {
  902. RSA_additional_prime *ap =
  903. sk_RSA_additional_prime_value(additional_primes, i - 2);
  904. if (!BN_sub(r3, ap->prime, BN_value_one()) ||
  905. !BN_mul(r0, r0, r3, ctx)) {
  906. goto err;
  907. }
  908. }
  909. pr0 = &local_r0;
  910. BN_with_flags(pr0, r0, BN_FLG_CONSTTIME);
  911. if (!BN_mod_inverse(rsa->d, rsa->e, pr0, ctx)) {
  912. goto err; /* d */
  913. }
  914. /* set up d for correct BN_FLG_CONSTTIME flag */
  915. d = &local_d;
  916. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  917. /* calculate d mod (p-1) */
  918. if (!BN_mod(rsa->dmp1, d, r1, ctx)) {
  919. goto err;
  920. }
  921. /* calculate d mod (q-1) */
  922. if (!BN_mod(rsa->dmq1, d, r2, ctx)) {
  923. goto err;
  924. }
  925. /* calculate inverse of q mod p */
  926. p = &local_p;
  927. BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
  928. if (!BN_mod_inverse(rsa->iqmp, rsa->q, p, ctx)) {
  929. goto err;
  930. }
  931. for (i = 2; i < num_primes; i++) {
  932. RSA_additional_prime *ap =
  933. sk_RSA_additional_prime_value(additional_primes, i - 2);
  934. if (!BN_sub(ap->exp, ap->prime, BN_value_one()) ||
  935. !BN_mod(ap->exp, rsa->d, ap->exp, ctx) ||
  936. !BN_mod_inverse(ap->coeff, ap->r, ap->prime, ctx)) {
  937. goto err;
  938. }
  939. }
  940. ok = 1;
  941. rsa->additional_primes = additional_primes;
  942. additional_primes = NULL;
  943. err:
  944. if (ok == -1) {
  945. OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
  946. ok = 0;
  947. }
  948. if (ctx != NULL) {
  949. BN_CTX_end(ctx);
  950. BN_CTX_free(ctx);
  951. }
  952. sk_RSA_additional_prime_pop_free(additional_primes,
  953. RSA_additional_prime_free);
  954. return ok;
  955. }
  956. static int keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
  957. return keygen_multiprime(rsa, bits, 2 /* num primes */, e_value, cb);
  958. }
  959. const struct rsa_meth_st RSA_default_method = {
  960. {
  961. 0 /* references */,
  962. 1 /* is_static */,
  963. },
  964. NULL /* app_data */,
  965. NULL /* init */,
  966. finish,
  967. size,
  968. NULL /* sign */,
  969. NULL /* verify */,
  970. encrypt,
  971. sign_raw,
  972. decrypt,
  973. verify_raw,
  974. private_transform,
  975. mod_exp /* mod_exp */,
  976. BN_mod_exp_mont /* bn_mod_exp */,
  977. RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE,
  978. keygen,
  979. keygen_multiprime,
  980. };