rsa_ossl.c 29 KB

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