rsa_ossl.c 29 KB

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