rsa_ossl.c 29 KB

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