rsa_ossl.c 30 KB

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