rsa_ossl.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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. if (ex_primes > 0) {
  692. BIGNUM *di = BN_new(), *cc = BN_new();
  693. if (cc == NULL || di == NULL) {
  694. BN_free(cc);
  695. BN_free(di);
  696. goto err;
  697. }
  698. for (i = 0; i < ex_primes; i++) {
  699. /* prepare m_i */
  700. if ((m[i] = BN_CTX_get(ctx)) == NULL) {
  701. BN_free(cc);
  702. BN_free(di);
  703. goto err;
  704. }
  705. pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
  706. /* prepare c and d_i */
  707. BN_with_flags(cc, I, BN_FLG_CONSTTIME);
  708. BN_with_flags(di, pinfo->d, BN_FLG_CONSTTIME);
  709. if (!BN_mod(r1, cc, pinfo->r, ctx)) {
  710. BN_free(cc);
  711. BN_free(di);
  712. goto err;
  713. }
  714. /* compute r1 ^ d_i mod r_i */
  715. if (!rsa->meth->bn_mod_exp(m[i], r1, di, pinfo->r, ctx, pinfo->m)) {
  716. BN_free(cc);
  717. BN_free(di);
  718. goto err;
  719. }
  720. }
  721. BN_free(cc);
  722. BN_free(di);
  723. }
  724. #endif
  725. if (!BN_sub(r0, r0, m1))
  726. goto err;
  727. /*
  728. * This will help stop the size of r0 increasing, which does affect the
  729. * multiply if it optimised for a power of 2 size
  730. */
  731. if (BN_is_negative(r0))
  732. if (!BN_add(r0, r0, rsa->p))
  733. goto err;
  734. if (!BN_mul(r1, r0, rsa->iqmp, ctx))
  735. goto err;
  736. {
  737. BIGNUM *pr1 = BN_new();
  738. if (pr1 == NULL)
  739. goto err;
  740. BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
  741. if (!BN_mod(r0, pr1, rsa->p, ctx)) {
  742. BN_free(pr1);
  743. goto err;
  744. }
  745. /* We MUST free pr1 before any further use of r1 */
  746. BN_free(pr1);
  747. }
  748. /*
  749. * If p < q it is occasionally possible for the correction of adding 'p'
  750. * if r0 is negative above to leave the result still negative. This can
  751. * break the private key operations: the following second correction
  752. * should *always* correct this rare occurrence. This will *never* happen
  753. * with OpenSSL generated keys because they ensure p > q [steve]
  754. */
  755. if (BN_is_negative(r0))
  756. if (!BN_add(r0, r0, rsa->p))
  757. goto err;
  758. if (!BN_mul(r1, r0, rsa->q, ctx))
  759. goto err;
  760. if (!BN_add(r0, r1, m1))
  761. goto err;
  762. #ifndef FIPS_MODULE
  763. /* add m_i to m in multi-prime case */
  764. if (ex_primes > 0) {
  765. BIGNUM *pr2 = BN_new();
  766. if (pr2 == NULL)
  767. goto err;
  768. for (i = 0; i < ex_primes; i++) {
  769. pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
  770. if (!BN_sub(r1, m[i], r0)) {
  771. BN_free(pr2);
  772. goto err;
  773. }
  774. if (!BN_mul(r2, r1, pinfo->t, ctx)) {
  775. BN_free(pr2);
  776. goto err;
  777. }
  778. BN_with_flags(pr2, r2, BN_FLG_CONSTTIME);
  779. if (!BN_mod(r1, pr2, pinfo->r, ctx)) {
  780. BN_free(pr2);
  781. goto err;
  782. }
  783. if (BN_is_negative(r1))
  784. if (!BN_add(r1, r1, pinfo->r)) {
  785. BN_free(pr2);
  786. goto err;
  787. }
  788. if (!BN_mul(r1, r1, pinfo->pp, ctx)) {
  789. BN_free(pr2);
  790. goto err;
  791. }
  792. if (!BN_add(r0, r0, r1)) {
  793. BN_free(pr2);
  794. goto err;
  795. }
  796. }
  797. BN_free(pr2);
  798. }
  799. #endif
  800. tail:
  801. if (rsa->e && rsa->n) {
  802. if (rsa->meth->bn_mod_exp == BN_mod_exp_mont) {
  803. if (!BN_mod_exp_mont(vrfy, r0, rsa->e, rsa->n, ctx,
  804. rsa->_method_mod_n))
  805. goto err;
  806. } else {
  807. bn_correct_top(r0);
  808. if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
  809. rsa->_method_mod_n))
  810. goto err;
  811. }
  812. /*
  813. * If 'I' was greater than (or equal to) rsa->n, the operation will
  814. * be equivalent to using 'I mod n'. However, the result of the
  815. * verify will *always* be less than 'n' so we don't check for
  816. * absolute equality, just congruency.
  817. */
  818. if (!BN_sub(vrfy, vrfy, I))
  819. goto err;
  820. if (BN_is_zero(vrfy)) {
  821. bn_correct_top(r0);
  822. ret = 1;
  823. goto err; /* not actually error */
  824. }
  825. if (!BN_mod(vrfy, vrfy, rsa->n, ctx))
  826. goto err;
  827. if (BN_is_negative(vrfy))
  828. if (!BN_add(vrfy, vrfy, rsa->n))
  829. goto err;
  830. if (!BN_is_zero(vrfy)) {
  831. /*
  832. * 'I' and 'vrfy' aren't congruent mod n. Don't leak
  833. * miscalculated CRT output, just do a raw (slower) mod_exp and
  834. * return that instead.
  835. */
  836. BIGNUM *d = BN_new();
  837. if (d == NULL)
  838. goto err;
  839. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  840. if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,
  841. rsa->_method_mod_n)) {
  842. BN_free(d);
  843. goto err;
  844. }
  845. /* We MUST free d before any further use of rsa->d */
  846. BN_free(d);
  847. }
  848. }
  849. /*
  850. * It's unfortunate that we have to bn_correct_top(r0). What hopefully
  851. * saves the day is that correction is highly unlike, and private key
  852. * operations are customarily performed on blinded message. Which means
  853. * that attacker won't observe correlation with chosen plaintext.
  854. * Secondly, remaining code would still handle it in same computational
  855. * time and even conceal memory access pattern around corrected top.
  856. */
  857. bn_correct_top(r0);
  858. ret = 1;
  859. err:
  860. BN_CTX_end(ctx);
  861. return ret;
  862. }
  863. static int rsa_ossl_init(RSA *rsa)
  864. {
  865. rsa->flags |= RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE;
  866. return 1;
  867. }
  868. static int rsa_ossl_finish(RSA *rsa)
  869. {
  870. #ifndef FIPS_MODULE
  871. int i;
  872. RSA_PRIME_INFO *pinfo;
  873. for (i = 0; i < sk_RSA_PRIME_INFO_num(rsa->prime_infos); i++) {
  874. pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
  875. BN_MONT_CTX_free(pinfo->m);
  876. }
  877. #endif
  878. BN_MONT_CTX_free(rsa->_method_mod_n);
  879. BN_MONT_CTX_free(rsa->_method_mod_p);
  880. BN_MONT_CTX_free(rsa->_method_mod_q);
  881. return 1;
  882. }