rsa_ossl.c 27 KB

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