rsa_ossl.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. /*
  2. * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * RSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include "internal/cryptlib.h"
  15. #include "crypto/bn.h"
  16. #include "rsa_local.h"
  17. #include "internal/constant_time.h"
  18. static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
  19. unsigned char *to, RSA *rsa, int padding);
  20. static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
  21. unsigned char *to, RSA *rsa, int padding);
  22. static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
  23. unsigned char *to, RSA *rsa, int padding);
  24. static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
  25. unsigned char *to, RSA *rsa, int padding);
  26. static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa,
  27. BN_CTX *ctx);
  28. static int rsa_ossl_init(RSA *rsa);
  29. static int rsa_ossl_finish(RSA *rsa);
  30. static RSA_METHOD rsa_pkcs1_ossl_meth = {
  31. "OpenSSL PKCS#1 RSA",
  32. rsa_ossl_public_encrypt,
  33. rsa_ossl_public_decrypt, /* signature verification */
  34. rsa_ossl_private_encrypt, /* signing */
  35. rsa_ossl_private_decrypt,
  36. rsa_ossl_mod_exp,
  37. BN_mod_exp_mont, /* XXX probably we should not use Montgomery
  38. * if e == 3 */
  39. rsa_ossl_init,
  40. rsa_ossl_finish,
  41. RSA_FLAG_FIPS_METHOD, /* flags */
  42. NULL,
  43. 0, /* rsa_sign */
  44. 0, /* rsa_verify */
  45. NULL, /* rsa_keygen */
  46. NULL /* rsa_multi_prime_keygen */
  47. };
  48. static const RSA_METHOD *default_RSA_meth = &rsa_pkcs1_ossl_meth;
  49. void RSA_set_default_method(const RSA_METHOD *meth)
  50. {
  51. default_RSA_meth = meth;
  52. }
  53. const RSA_METHOD *RSA_get_default_method(void)
  54. {
  55. return default_RSA_meth;
  56. }
  57. const RSA_METHOD *RSA_PKCS1_OpenSSL(void)
  58. {
  59. return &rsa_pkcs1_ossl_meth;
  60. }
  61. const RSA_METHOD *RSA_null_method(void)
  62. {
  63. return NULL;
  64. }
  65. static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
  66. unsigned char *to, RSA *rsa, int padding)
  67. {
  68. BIGNUM *f, *ret;
  69. int i, num = 0, r = -1;
  70. unsigned char *buf = NULL;
  71. BN_CTX *ctx = NULL;
  72. if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
  73. RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_MODULUS_TOO_LARGE);
  74. return -1;
  75. }
  76. if (BN_ucmp(rsa->n, rsa->e) <= 0) {
  77. RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
  78. return -1;
  79. }
  80. /* for large moduli, enforce exponent limit */
  81. if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
  82. if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
  83. RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
  84. return -1;
  85. }
  86. }
  87. if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
  88. goto err;
  89. BN_CTX_start(ctx);
  90. f = BN_CTX_get(ctx);
  91. ret = BN_CTX_get(ctx);
  92. num = BN_num_bytes(rsa->n);
  93. buf = OPENSSL_malloc(num);
  94. if (ret == NULL || buf == NULL) {
  95. RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, ERR_R_MALLOC_FAILURE);
  96. goto err;
  97. }
  98. switch (padding) {
  99. case RSA_PKCS1_PADDING:
  100. i = ossl_rsa_padding_add_PKCS1_type_2_ex(rsa->libctx, buf, num,
  101. from, flen);
  102. break;
  103. case RSA_PKCS1_OAEP_PADDING:
  104. i = ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(rsa->libctx, buf, num,
  105. from, flen, NULL, 0,
  106. NULL, NULL);
  107. break;
  108. #ifndef FIPS_MODULE
  109. case RSA_SSLV23_PADDING:
  110. i = ossl_rsa_padding_add_SSLv23_ex(rsa->libctx, buf, num, from, flen);
  111. break;
  112. #endif
  113. case RSA_NO_PADDING:
  114. i = RSA_padding_add_none(buf, num, from, flen);
  115. break;
  116. default:
  117. RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
  118. goto err;
  119. }
  120. if (i <= 0)
  121. goto err;
  122. if (BN_bin2bn(buf, num, f) == NULL)
  123. goto err;
  124. if (BN_ucmp(f, rsa->n) >= 0) {
  125. /* usually the padding functions would catch this */
  126. RSAerr(RSA_F_RSA_OSSL_PUBLIC_ENCRYPT,
  127. RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  128. goto err;
  129. }
  130. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
  131. if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
  132. rsa->n, ctx))
  133. goto err;
  134. if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
  135. rsa->_method_mod_n))
  136. goto err;
  137. /*
  138. * BN_bn2binpad puts in leading 0 bytes if the number is less than
  139. * the length of the modulus.
  140. */
  141. r = BN_bn2binpad(ret, to, num);
  142. err:
  143. BN_CTX_end(ctx);
  144. BN_CTX_free(ctx);
  145. OPENSSL_clear_free(buf, num);
  146. return r;
  147. }
  148. static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx)
  149. {
  150. BN_BLINDING *ret;
  151. CRYPTO_THREAD_write_lock(rsa->lock);
  152. if (rsa->blinding == NULL) {
  153. rsa->blinding = RSA_setup_blinding(rsa, ctx);
  154. }
  155. ret = rsa->blinding;
  156. if (ret == NULL)
  157. goto err;
  158. if (BN_BLINDING_is_current_thread(ret)) {
  159. /* rsa->blinding is ours! */
  160. *local = 1;
  161. } else {
  162. /* resort to rsa->mt_blinding instead */
  163. /*
  164. * instructs rsa_blinding_convert(), rsa_blinding_invert() that the
  165. * BN_BLINDING is shared, meaning that accesses require locks, and
  166. * that the blinding factor must be stored outside the BN_BLINDING
  167. */
  168. *local = 0;
  169. if (rsa->mt_blinding == NULL) {
  170. rsa->mt_blinding = RSA_setup_blinding(rsa, ctx);
  171. }
  172. ret = rsa->mt_blinding;
  173. }
  174. err:
  175. CRYPTO_THREAD_unlock(rsa->lock);
  176. return ret;
  177. }
  178. static int rsa_blinding_convert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
  179. BN_CTX *ctx)
  180. {
  181. if (unblind == NULL) {
  182. /*
  183. * Local blinding: store the unblinding factor in BN_BLINDING.
  184. */
  185. return BN_BLINDING_convert_ex(f, NULL, b, ctx);
  186. } else {
  187. /*
  188. * Shared blinding: store the unblinding factor outside BN_BLINDING.
  189. */
  190. int ret;
  191. BN_BLINDING_lock(b);
  192. ret = BN_BLINDING_convert_ex(f, unblind, b, ctx);
  193. BN_BLINDING_unlock(b);
  194. return ret;
  195. }
  196. }
  197. static int rsa_blinding_invert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
  198. BN_CTX *ctx)
  199. {
  200. /*
  201. * For local blinding, unblind is set to NULL, and BN_BLINDING_invert_ex
  202. * will use the unblinding factor stored in BN_BLINDING. If BN_BLINDING
  203. * is shared between threads, unblind must be non-null:
  204. * BN_BLINDING_invert_ex will then use the local unblinding factor, and
  205. * will only read the modulus from BN_BLINDING. In both cases it's safe
  206. * to access the blinding without a lock.
  207. */
  208. return BN_BLINDING_invert_ex(f, unblind, b, ctx);
  209. }
  210. /* signing */
  211. static int rsa_ossl_private_encrypt(int flen, const unsigned char *from,
  212. unsigned char *to, RSA *rsa, int padding)
  213. {
  214. BIGNUM *f, *ret, *res;
  215. int i, num = 0, r = -1;
  216. unsigned char *buf = NULL;
  217. BN_CTX *ctx = NULL;
  218. int local_blinding = 0;
  219. /*
  220. * Used only if the blinding structure is shared. A non-NULL unblind
  221. * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
  222. * the unblinding factor outside the blinding structure.
  223. */
  224. BIGNUM *unblind = NULL;
  225. BN_BLINDING *blinding = NULL;
  226. if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
  227. goto err;
  228. BN_CTX_start(ctx);
  229. f = BN_CTX_get(ctx);
  230. ret = BN_CTX_get(ctx);
  231. num = BN_num_bytes(rsa->n);
  232. buf = OPENSSL_malloc(num);
  233. if (ret == NULL || buf == NULL) {
  234. RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
  235. goto err;
  236. }
  237. switch (padding) {
  238. case RSA_PKCS1_PADDING:
  239. i = RSA_padding_add_PKCS1_type_1(buf, num, from, flen);
  240. break;
  241. case RSA_X931_PADDING:
  242. i = RSA_padding_add_X931(buf, num, from, flen);
  243. break;
  244. case RSA_NO_PADDING:
  245. i = RSA_padding_add_none(buf, num, from, flen);
  246. break;
  247. case RSA_SSLV23_PADDING:
  248. default:
  249. RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
  250. goto err;
  251. }
  252. if (i <= 0)
  253. goto err;
  254. if (BN_bin2bn(buf, num, f) == NULL)
  255. goto err;
  256. if (BN_ucmp(f, rsa->n) >= 0) {
  257. /* usually the padding functions would catch this */
  258. RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT,
  259. RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  260. goto err;
  261. }
  262. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
  263. if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
  264. rsa->n, ctx))
  265. goto err;
  266. if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
  267. blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
  268. if (blinding == NULL) {
  269. RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR);
  270. goto err;
  271. }
  272. }
  273. if (blinding != NULL) {
  274. if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
  275. RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
  276. goto err;
  277. }
  278. if (!rsa_blinding_convert(blinding, f, unblind, ctx))
  279. goto err;
  280. }
  281. if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
  282. (rsa->version == RSA_ASN1_VERSION_MULTI) ||
  283. ((rsa->p != NULL) &&
  284. (rsa->q != NULL) &&
  285. (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
  286. if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
  287. goto err;
  288. } else {
  289. BIGNUM *d = BN_new();
  290. if (d == NULL) {
  291. RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
  292. goto err;
  293. }
  294. if (rsa->d == NULL) {
  295. RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, RSA_R_MISSING_PRIVATE_KEY);
  296. BN_free(d);
  297. goto err;
  298. }
  299. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  300. if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
  301. rsa->_method_mod_n)) {
  302. BN_free(d);
  303. goto err;
  304. }
  305. /* We MUST free d before any further use of rsa->d */
  306. BN_free(d);
  307. }
  308. if (blinding)
  309. if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
  310. goto err;
  311. if (padding == RSA_X931_PADDING) {
  312. if (!BN_sub(f, rsa->n, ret))
  313. goto err;
  314. if (BN_cmp(ret, f) > 0)
  315. res = f;
  316. else
  317. res = ret;
  318. } else {
  319. res = ret;
  320. }
  321. /*
  322. * BN_bn2binpad puts in leading 0 bytes if the number is less than
  323. * the length of the modulus.
  324. */
  325. r = BN_bn2binpad(res, to, num);
  326. err:
  327. BN_CTX_end(ctx);
  328. BN_CTX_free(ctx);
  329. OPENSSL_clear_free(buf, num);
  330. return r;
  331. }
  332. static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
  333. unsigned char *to, RSA *rsa, int padding)
  334. {
  335. BIGNUM *f, *ret;
  336. int j, num = 0, r = -1;
  337. unsigned char *buf = NULL;
  338. BN_CTX *ctx = NULL;
  339. int local_blinding = 0;
  340. /*
  341. * Used only if the blinding structure is shared. A non-NULL unblind
  342. * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
  343. * the unblinding factor outside the blinding structure.
  344. */
  345. BIGNUM *unblind = NULL;
  346. BN_BLINDING *blinding = NULL;
  347. if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
  348. goto err;
  349. BN_CTX_start(ctx);
  350. f = BN_CTX_get(ctx);
  351. ret = BN_CTX_get(ctx);
  352. num = BN_num_bytes(rsa->n);
  353. buf = OPENSSL_malloc(num);
  354. if (ret == NULL || buf == NULL) {
  355. RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
  356. goto err;
  357. }
  358. /*
  359. * This check was for equality but PGP does evil things and chops off the
  360. * top '0' bytes
  361. */
  362. if (flen > num) {
  363. RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT,
  364. RSA_R_DATA_GREATER_THAN_MOD_LEN);
  365. goto err;
  366. }
  367. /* make data into a big number */
  368. if (BN_bin2bn(from, (int)flen, f) == NULL)
  369. goto err;
  370. if (BN_ucmp(f, rsa->n) >= 0) {
  371. RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT,
  372. RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  373. goto err;
  374. }
  375. if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
  376. blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
  377. if (blinding == NULL) {
  378. RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR);
  379. goto err;
  380. }
  381. }
  382. if (blinding != NULL) {
  383. if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
  384. RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
  385. goto err;
  386. }
  387. if (!rsa_blinding_convert(blinding, f, unblind, ctx))
  388. goto err;
  389. }
  390. /* do the decrypt */
  391. if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
  392. (rsa->version == RSA_ASN1_VERSION_MULTI) ||
  393. ((rsa->p != NULL) &&
  394. (rsa->q != NULL) &&
  395. (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
  396. if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
  397. goto err;
  398. } else {
  399. BIGNUM *d = BN_new();
  400. if (d == NULL) {
  401. RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
  402. goto err;
  403. }
  404. if (rsa->d == NULL) {
  405. RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_MISSING_PRIVATE_KEY);
  406. BN_free(d);
  407. goto err;
  408. }
  409. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  410. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
  411. if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
  412. rsa->n, ctx)) {
  413. BN_free(d);
  414. goto err;
  415. }
  416. if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
  417. rsa->_method_mod_n)) {
  418. BN_free(d);
  419. goto err;
  420. }
  421. /* We MUST free d before any further use of rsa->d */
  422. BN_free(d);
  423. }
  424. if (blinding)
  425. if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
  426. goto err;
  427. j = BN_bn2binpad(ret, buf, num);
  428. if (j < 0)
  429. goto err;
  430. switch (padding) {
  431. case RSA_PKCS1_PADDING:
  432. r = RSA_padding_check_PKCS1_type_2(to, num, buf, j, num);
  433. break;
  434. case RSA_PKCS1_OAEP_PADDING:
  435. r = RSA_padding_check_PKCS1_OAEP(to, num, buf, j, num, NULL, 0);
  436. break;
  437. #ifndef FIPS_MODULE
  438. case RSA_SSLV23_PADDING:
  439. r = RSA_padding_check_SSLv23(to, num, buf, j, num);
  440. break;
  441. #endif
  442. case RSA_NO_PADDING:
  443. memcpy(to, buf, (r = j));
  444. break;
  445. default:
  446. RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
  447. goto err;
  448. }
  449. #ifndef FIPS_MODULE
  450. /*
  451. * This trick doesn't work in the FIPS provider because libcrypto manages
  452. * the error stack. Instead we opt not to put an error on the stack at all
  453. * in case of padding failure in the FIPS provider.
  454. */
  455. RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_PADDING_CHECK_FAILED);
  456. err_clear_last_constant_time(1 & ~constant_time_msb(r));
  457. #endif
  458. err:
  459. BN_CTX_end(ctx);
  460. BN_CTX_free(ctx);
  461. OPENSSL_clear_free(buf, num);
  462. return r;
  463. }
  464. /* signature verification */
  465. static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,
  466. unsigned char *to, RSA *rsa, int padding)
  467. {
  468. BIGNUM *f, *ret;
  469. int i, num = 0, r = -1;
  470. unsigned char *buf = NULL;
  471. BN_CTX *ctx = NULL;
  472. if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
  473. RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_MODULUS_TOO_LARGE);
  474. return -1;
  475. }
  476. if (BN_ucmp(rsa->n, rsa->e) <= 0) {
  477. RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
  478. return -1;
  479. }
  480. /* for large moduli, enforce exponent limit */
  481. if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
  482. if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
  483. RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
  484. return -1;
  485. }
  486. }
  487. if ((ctx = BN_CTX_new_ex(rsa->libctx)) == NULL)
  488. goto err;
  489. BN_CTX_start(ctx);
  490. f = BN_CTX_get(ctx);
  491. ret = BN_CTX_get(ctx);
  492. num = BN_num_bytes(rsa->n);
  493. buf = OPENSSL_malloc(num);
  494. if (ret == NULL || buf == NULL) {
  495. RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, ERR_R_MALLOC_FAILURE);
  496. goto err;
  497. }
  498. /*
  499. * This check was for equality but PGP does evil things and chops off the
  500. * top '0' bytes
  501. */
  502. if (flen > num) {
  503. RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_DATA_GREATER_THAN_MOD_LEN);
  504. goto err;
  505. }
  506. if (BN_bin2bn(from, flen, f) == NULL)
  507. goto err;
  508. if (BN_ucmp(f, rsa->n) >= 0) {
  509. RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT,
  510. RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  511. goto err;
  512. }
  513. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
  514. if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
  515. rsa->n, ctx))
  516. goto err;
  517. if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
  518. rsa->_method_mod_n))
  519. goto err;
  520. if ((padding == RSA_X931_PADDING) && ((bn_get_words(ret)[0] & 0xf) != 12))
  521. if (!BN_sub(ret, rsa->n, ret))
  522. goto err;
  523. i = BN_bn2binpad(ret, buf, num);
  524. if (i < 0)
  525. goto err;
  526. switch (padding) {
  527. case RSA_PKCS1_PADDING:
  528. r = RSA_padding_check_PKCS1_type_1(to, num, buf, i, num);
  529. break;
  530. case RSA_X931_PADDING:
  531. r = RSA_padding_check_X931(to, num, buf, i, num);
  532. break;
  533. case RSA_NO_PADDING:
  534. memcpy(to, buf, (r = i));
  535. break;
  536. default:
  537. RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
  538. goto err;
  539. }
  540. if (r < 0)
  541. RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_PADDING_CHECK_FAILED);
  542. err:
  543. BN_CTX_end(ctx);
  544. BN_CTX_free(ctx);
  545. OPENSSL_clear_free(buf, num);
  546. return r;
  547. }
  548. static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
  549. {
  550. BIGNUM *r1, *m1, *vrfy;
  551. int ret = 0, smooth = 0;
  552. #ifndef FIPS_MODULE
  553. BIGNUM *r2, *m[RSA_MAX_PRIME_NUM - 2];
  554. int i, ex_primes = 0;
  555. RSA_PRIME_INFO *pinfo;
  556. #endif
  557. BN_CTX_start(ctx);
  558. r1 = BN_CTX_get(ctx);
  559. #ifndef FIPS_MODULE
  560. r2 = BN_CTX_get(ctx);
  561. #endif
  562. m1 = BN_CTX_get(ctx);
  563. vrfy = BN_CTX_get(ctx);
  564. if (vrfy == NULL)
  565. goto err;
  566. #ifndef FIPS_MODULE
  567. if (rsa->version == RSA_ASN1_VERSION_MULTI
  568. && ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0
  569. || ex_primes > RSA_MAX_PRIME_NUM - 2))
  570. goto err;
  571. #endif
  572. if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
  573. BIGNUM *factor = BN_new();
  574. if (factor == NULL)
  575. goto err;
  576. /*
  577. * Make sure BN_mod_inverse in Montgomery initialization uses the
  578. * BN_FLG_CONSTTIME flag
  579. */
  580. if (!(BN_with_flags(factor, rsa->p, BN_FLG_CONSTTIME),
  581. BN_MONT_CTX_set_locked(&rsa->_method_mod_p, rsa->lock,
  582. factor, ctx))
  583. || !(BN_with_flags(factor, rsa->q, BN_FLG_CONSTTIME),
  584. BN_MONT_CTX_set_locked(&rsa->_method_mod_q, rsa->lock,
  585. factor, ctx))) {
  586. BN_free(factor);
  587. goto err;
  588. }
  589. #ifndef FIPS_MODULE
  590. for (i = 0; i < ex_primes; i++) {
  591. pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
  592. BN_with_flags(factor, pinfo->r, BN_FLG_CONSTTIME);
  593. if (!BN_MONT_CTX_set_locked(&pinfo->m, rsa->lock, factor, ctx)) {
  594. BN_free(factor);
  595. goto err;
  596. }
  597. }
  598. #endif
  599. /*
  600. * We MUST free |factor| before any further use of the prime factors
  601. */
  602. BN_free(factor);
  603. smooth = (rsa->meth->bn_mod_exp == BN_mod_exp_mont)
  604. #ifndef FIPS_MODULE
  605. && (ex_primes == 0)
  606. #endif
  607. && (BN_num_bits(rsa->q) == BN_num_bits(rsa->p));
  608. }
  609. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
  610. if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
  611. rsa->n, ctx))
  612. goto err;
  613. if (smooth) {
  614. /*
  615. * Conversion from Montgomery domain, a.k.a. Montgomery reduction,
  616. * accepts values in [0-m*2^w) range. w is m's bit width rounded up
  617. * to limb width. So that at the very least if |I| is fully reduced,
  618. * i.e. less than p*q, we can count on from-to round to perform
  619. * below modulo operations on |I|. Unlike BN_mod it's constant time.
  620. */
  621. if (/* m1 = I moq q */
  622. !bn_from_mont_fixed_top(m1, I, rsa->_method_mod_q, ctx)
  623. || !bn_to_mont_fixed_top(m1, m1, rsa->_method_mod_q, ctx)
  624. /* m1 = m1^dmq1 mod q */
  625. || !BN_mod_exp_mont_consttime(m1, m1, rsa->dmq1, rsa->q, ctx,
  626. rsa->_method_mod_q)
  627. /* r1 = I mod p */
  628. || !bn_from_mont_fixed_top(r1, I, rsa->_method_mod_p, ctx)
  629. || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)
  630. /* r1 = r1^dmp1 mod p */
  631. || !BN_mod_exp_mont_consttime(r1, r1, rsa->dmp1, rsa->p, ctx,
  632. rsa->_method_mod_p)
  633. /* r1 = (r1 - m1) mod p */
  634. /*
  635. * bn_mod_sub_fixed_top is not regular modular subtraction,
  636. * it can tolerate subtrahend to be larger than modulus, but
  637. * not bit-wise wider. This makes up for uncommon q>p case,
  638. * when |m1| can be larger than |rsa->p|.
  639. */
  640. || !bn_mod_sub_fixed_top(r1, r1, m1, rsa->p)
  641. /* r1 = r1 * iqmp mod p */
  642. || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)
  643. || !bn_mul_mont_fixed_top(r1, r1, rsa->iqmp, rsa->_method_mod_p,
  644. ctx)
  645. /* r0 = r1 * q + m1 */
  646. || !bn_mul_fixed_top(r0, r1, rsa->q, ctx)
  647. || !bn_mod_add_fixed_top(r0, r0, m1, rsa->n))
  648. goto err;
  649. goto tail;
  650. }
  651. /* compute I mod q */
  652. {
  653. BIGNUM *c = BN_new();
  654. if (c == NULL)
  655. goto err;
  656. BN_with_flags(c, I, BN_FLG_CONSTTIME);
  657. if (!BN_mod(r1, c, rsa->q, ctx)) {
  658. BN_free(c);
  659. goto err;
  660. }
  661. {
  662. BIGNUM *dmq1 = BN_new();
  663. if (dmq1 == NULL) {
  664. BN_free(c);
  665. goto err;
  666. }
  667. BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
  668. /* compute r1^dmq1 mod q */
  669. if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,
  670. rsa->_method_mod_q)) {
  671. BN_free(c);
  672. BN_free(dmq1);
  673. goto err;
  674. }
  675. /* We MUST free dmq1 before any further use of rsa->dmq1 */
  676. BN_free(dmq1);
  677. }
  678. /* compute I mod p */
  679. if (!BN_mod(r1, c, rsa->p, ctx)) {
  680. BN_free(c);
  681. goto err;
  682. }
  683. /* We MUST free c before any further use of I */
  684. BN_free(c);
  685. }
  686. {
  687. BIGNUM *dmp1 = BN_new();
  688. if (dmp1 == NULL)
  689. goto err;
  690. BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
  691. /* compute r1^dmp1 mod p */
  692. if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,
  693. rsa->_method_mod_p)) {
  694. BN_free(dmp1);
  695. goto err;
  696. }
  697. /* We MUST free dmp1 before any further use of rsa->dmp1 */
  698. BN_free(dmp1);
  699. }
  700. #ifndef FIPS_MODULE
  701. /*
  702. * calculate m_i in multi-prime case
  703. *
  704. * TODO:
  705. * 1. squash the following two loops and calculate |m_i| there.
  706. * 2. remove cc and reuse |c|.
  707. * 3. remove |dmq1| and |dmp1| in previous block and use |di|.
  708. *
  709. * If these things are done, the code will be more readable.
  710. */
  711. if (ex_primes > 0) {
  712. BIGNUM *di = BN_new(), *cc = BN_new();
  713. if (cc == NULL || di == NULL) {
  714. BN_free(cc);
  715. BN_free(di);
  716. goto err;
  717. }
  718. for (i = 0; i < ex_primes; i++) {
  719. /* prepare m_i */
  720. if ((m[i] = BN_CTX_get(ctx)) == NULL) {
  721. BN_free(cc);
  722. BN_free(di);
  723. goto err;
  724. }
  725. pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
  726. /* prepare c and d_i */
  727. BN_with_flags(cc, I, BN_FLG_CONSTTIME);
  728. BN_with_flags(di, pinfo->d, BN_FLG_CONSTTIME);
  729. if (!BN_mod(r1, cc, pinfo->r, ctx)) {
  730. BN_free(cc);
  731. BN_free(di);
  732. goto err;
  733. }
  734. /* compute r1 ^ d_i mod r_i */
  735. if (!rsa->meth->bn_mod_exp(m[i], r1, di, pinfo->r, ctx, pinfo->m)) {
  736. BN_free(cc);
  737. BN_free(di);
  738. goto err;
  739. }
  740. }
  741. BN_free(cc);
  742. BN_free(di);
  743. }
  744. #endif
  745. if (!BN_sub(r0, r0, m1))
  746. goto err;
  747. /*
  748. * This will help stop the size of r0 increasing, which does affect the
  749. * multiply if it optimised for a power of 2 size
  750. */
  751. if (BN_is_negative(r0))
  752. if (!BN_add(r0, r0, rsa->p))
  753. goto err;
  754. if (!BN_mul(r1, r0, rsa->iqmp, ctx))
  755. goto err;
  756. {
  757. BIGNUM *pr1 = BN_new();
  758. if (pr1 == NULL)
  759. goto err;
  760. BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
  761. if (!BN_mod(r0, pr1, rsa->p, ctx)) {
  762. BN_free(pr1);
  763. goto err;
  764. }
  765. /* We MUST free pr1 before any further use of r1 */
  766. BN_free(pr1);
  767. }
  768. /*
  769. * If p < q it is occasionally possible for the correction of adding 'p'
  770. * if r0 is negative above to leave the result still negative. This can
  771. * break the private key operations: the following second correction
  772. * should *always* correct this rare occurrence. This will *never* happen
  773. * with OpenSSL generated keys because they ensure p > q [steve]
  774. */
  775. if (BN_is_negative(r0))
  776. if (!BN_add(r0, r0, rsa->p))
  777. goto err;
  778. if (!BN_mul(r1, r0, rsa->q, ctx))
  779. goto err;
  780. if (!BN_add(r0, r1, m1))
  781. goto err;
  782. #ifndef FIPS_MODULE
  783. /* add m_i to m in multi-prime case */
  784. if (ex_primes > 0) {
  785. BIGNUM *pr2 = BN_new();
  786. if (pr2 == NULL)
  787. goto err;
  788. for (i = 0; i < ex_primes; i++) {
  789. pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
  790. if (!BN_sub(r1, m[i], r0)) {
  791. BN_free(pr2);
  792. goto err;
  793. }
  794. if (!BN_mul(r2, r1, pinfo->t, ctx)) {
  795. BN_free(pr2);
  796. goto err;
  797. }
  798. BN_with_flags(pr2, r2, BN_FLG_CONSTTIME);
  799. if (!BN_mod(r1, pr2, pinfo->r, ctx)) {
  800. BN_free(pr2);
  801. goto err;
  802. }
  803. if (BN_is_negative(r1))
  804. if (!BN_add(r1, r1, pinfo->r)) {
  805. BN_free(pr2);
  806. goto err;
  807. }
  808. if (!BN_mul(r1, r1, pinfo->pp, ctx)) {
  809. BN_free(pr2);
  810. goto err;
  811. }
  812. if (!BN_add(r0, r0, r1)) {
  813. BN_free(pr2);
  814. goto err;
  815. }
  816. }
  817. BN_free(pr2);
  818. }
  819. #endif
  820. tail:
  821. if (rsa->e && rsa->n) {
  822. if (rsa->meth->bn_mod_exp == BN_mod_exp_mont) {
  823. if (!BN_mod_exp_mont(vrfy, r0, rsa->e, rsa->n, ctx,
  824. rsa->_method_mod_n))
  825. goto err;
  826. } else {
  827. bn_correct_top(r0);
  828. if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
  829. rsa->_method_mod_n))
  830. goto err;
  831. }
  832. /*
  833. * If 'I' was greater than (or equal to) rsa->n, the operation will
  834. * be equivalent to using 'I mod n'. However, the result of the
  835. * verify will *always* be less than 'n' so we don't check for
  836. * absolute equality, just congruency.
  837. */
  838. if (!BN_sub(vrfy, vrfy, I))
  839. goto err;
  840. if (BN_is_zero(vrfy)) {
  841. bn_correct_top(r0);
  842. ret = 1;
  843. goto err; /* not actually error */
  844. }
  845. if (!BN_mod(vrfy, vrfy, rsa->n, ctx))
  846. goto err;
  847. if (BN_is_negative(vrfy))
  848. if (!BN_add(vrfy, vrfy, rsa->n))
  849. goto err;
  850. if (!BN_is_zero(vrfy)) {
  851. /*
  852. * 'I' and 'vrfy' aren't congruent mod n. Don't leak
  853. * miscalculated CRT output, just do a raw (slower) mod_exp and
  854. * return that instead.
  855. */
  856. BIGNUM *d = BN_new();
  857. if (d == NULL)
  858. goto err;
  859. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  860. if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,
  861. rsa->_method_mod_n)) {
  862. BN_free(d);
  863. goto err;
  864. }
  865. /* We MUST free d before any further use of rsa->d */
  866. BN_free(d);
  867. }
  868. }
  869. /*
  870. * It's unfortunate that we have to bn_correct_top(r0). What hopefully
  871. * saves the day is that correction is highly unlike, and private key
  872. * operations are customarily performed on blinded message. Which means
  873. * that attacker won't observe correlation with chosen plaintext.
  874. * Secondly, remaining code would still handle it in same computational
  875. * time and even conceal memory access pattern around corrected top.
  876. */
  877. bn_correct_top(r0);
  878. ret = 1;
  879. err:
  880. BN_CTX_end(ctx);
  881. return ret;
  882. }
  883. static int rsa_ossl_init(RSA *rsa)
  884. {
  885. rsa->flags |= RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE;
  886. return 1;
  887. }
  888. static int rsa_ossl_finish(RSA *rsa)
  889. {
  890. #ifndef FIPS_MODULE
  891. int i;
  892. RSA_PRIME_INFO *pinfo;
  893. for (i = 0; i < sk_RSA_PRIME_INFO_num(rsa->prime_infos); i++) {
  894. pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
  895. BN_MONT_CTX_free(pinfo->m);
  896. }
  897. #endif
  898. BN_MONT_CTX_free(rsa->_method_mod_n);
  899. BN_MONT_CTX_free(rsa->_method_mod_p);
  900. BN_MONT_CTX_free(rsa->_method_mod_q);
  901. return 1;
  902. }