rsa_lib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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 <stdio.h>
  10. #include <openssl/crypto.h>
  11. #include "internal/cryptlib.h"
  12. #include "internal/refcount.h"
  13. #include "crypto/bn.h"
  14. #include <openssl/engine.h>
  15. #include <openssl/evp.h>
  16. #include "crypto/evp.h"
  17. #include "rsa_local.h"
  18. RSA *RSA_new(void)
  19. {
  20. return RSA_new_method(NULL);
  21. }
  22. const RSA_METHOD *RSA_get_method(const RSA *rsa)
  23. {
  24. return rsa->meth;
  25. }
  26. int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
  27. {
  28. /*
  29. * NB: The caller is specifically setting a method, so it's not up to us
  30. * to deal with which ENGINE it comes from.
  31. */
  32. const RSA_METHOD *mtmp;
  33. mtmp = rsa->meth;
  34. if (mtmp->finish)
  35. mtmp->finish(rsa);
  36. #ifndef OPENSSL_NO_ENGINE
  37. ENGINE_finish(rsa->engine);
  38. rsa->engine = NULL;
  39. #endif
  40. rsa->meth = meth;
  41. if (meth->init)
  42. meth->init(rsa);
  43. return 1;
  44. }
  45. RSA *RSA_new_method(ENGINE *engine)
  46. {
  47. RSA *ret = OPENSSL_zalloc(sizeof(*ret));
  48. if (ret == NULL) {
  49. RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
  50. return NULL;
  51. }
  52. ret->references = 1;
  53. ret->lock = CRYPTO_THREAD_lock_new();
  54. if (ret->lock == NULL) {
  55. RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
  56. OPENSSL_free(ret);
  57. return NULL;
  58. }
  59. ret->meth = RSA_get_default_method();
  60. #ifndef OPENSSL_NO_ENGINE
  61. ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
  62. if (engine) {
  63. if (!ENGINE_init(engine)) {
  64. RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
  65. goto err;
  66. }
  67. ret->engine = engine;
  68. } else {
  69. ret->engine = ENGINE_get_default_RSA();
  70. }
  71. if (ret->engine) {
  72. ret->meth = ENGINE_get_RSA(ret->engine);
  73. if (ret->meth == NULL) {
  74. RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
  75. goto err;
  76. }
  77. }
  78. #endif
  79. ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
  80. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
  81. goto err;
  82. }
  83. if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
  84. RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_INIT_FAIL);
  85. goto err;
  86. }
  87. return ret;
  88. err:
  89. RSA_free(ret);
  90. return NULL;
  91. }
  92. void RSA_free(RSA *r)
  93. {
  94. int i;
  95. if (r == NULL)
  96. return;
  97. CRYPTO_DOWN_REF(&r->references, &i, r->lock);
  98. REF_PRINT_COUNT("RSA", r);
  99. if (i > 0)
  100. return;
  101. REF_ASSERT_ISNT(i < 0);
  102. if (r->meth != NULL && r->meth->finish != NULL)
  103. r->meth->finish(r);
  104. #ifndef OPENSSL_NO_ENGINE
  105. ENGINE_finish(r->engine);
  106. #endif
  107. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
  108. CRYPTO_THREAD_lock_free(r->lock);
  109. BN_free(r->n);
  110. BN_free(r->e);
  111. BN_clear_free(r->d);
  112. BN_clear_free(r->p);
  113. BN_clear_free(r->q);
  114. BN_clear_free(r->dmp1);
  115. BN_clear_free(r->dmq1);
  116. BN_clear_free(r->iqmp);
  117. RSA_PSS_PARAMS_free(r->pss);
  118. sk_RSA_PRIME_INFO_pop_free(r->prime_infos, rsa_multip_info_free);
  119. BN_BLINDING_free(r->blinding);
  120. BN_BLINDING_free(r->mt_blinding);
  121. OPENSSL_free(r->bignum_data);
  122. OPENSSL_free(r);
  123. }
  124. int RSA_up_ref(RSA *r)
  125. {
  126. int i;
  127. if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
  128. return 0;
  129. REF_PRINT_COUNT("RSA", r);
  130. REF_ASSERT_ISNT(i < 2);
  131. return i > 1 ? 1 : 0;
  132. }
  133. int RSA_set_ex_data(RSA *r, int idx, void *arg)
  134. {
  135. return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
  136. }
  137. void *RSA_get_ex_data(const RSA *r, int idx)
  138. {
  139. return CRYPTO_get_ex_data(&r->ex_data, idx);
  140. }
  141. int RSA_security_bits(const RSA *rsa)
  142. {
  143. int bits = BN_num_bits(rsa->n);
  144. if (rsa->version == RSA_ASN1_VERSION_MULTI) {
  145. /* This ought to mean that we have private key at hand. */
  146. int ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos);
  147. if (ex_primes <= 0 || (ex_primes + 2) > rsa_multip_cap(bits))
  148. return 0;
  149. }
  150. return BN_security_bits(bits, -1);
  151. }
  152. int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
  153. {
  154. /* If the fields n and e in r are NULL, the corresponding input
  155. * parameters MUST be non-NULL for n and e. d may be
  156. * left NULL (in case only the public key is used).
  157. */
  158. if ((r->n == NULL && n == NULL)
  159. || (r->e == NULL && e == NULL))
  160. return 0;
  161. if (n != NULL) {
  162. BN_free(r->n);
  163. r->n = n;
  164. }
  165. if (e != NULL) {
  166. BN_free(r->e);
  167. r->e = e;
  168. }
  169. if (d != NULL) {
  170. BN_clear_free(r->d);
  171. r->d = d;
  172. BN_set_flags(r->d, BN_FLG_CONSTTIME);
  173. }
  174. return 1;
  175. }
  176. int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
  177. {
  178. /* If the fields p and q in r are NULL, the corresponding input
  179. * parameters MUST be non-NULL.
  180. */
  181. if ((r->p == NULL && p == NULL)
  182. || (r->q == NULL && q == NULL))
  183. return 0;
  184. if (p != NULL) {
  185. BN_clear_free(r->p);
  186. r->p = p;
  187. BN_set_flags(r->p, BN_FLG_CONSTTIME);
  188. }
  189. if (q != NULL) {
  190. BN_clear_free(r->q);
  191. r->q = q;
  192. BN_set_flags(r->q, BN_FLG_CONSTTIME);
  193. }
  194. return 1;
  195. }
  196. int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
  197. {
  198. /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
  199. * parameters MUST be non-NULL.
  200. */
  201. if ((r->dmp1 == NULL && dmp1 == NULL)
  202. || (r->dmq1 == NULL && dmq1 == NULL)
  203. || (r->iqmp == NULL && iqmp == NULL))
  204. return 0;
  205. if (dmp1 != NULL) {
  206. BN_clear_free(r->dmp1);
  207. r->dmp1 = dmp1;
  208. BN_set_flags(r->dmp1, BN_FLG_CONSTTIME);
  209. }
  210. if (dmq1 != NULL) {
  211. BN_clear_free(r->dmq1);
  212. r->dmq1 = dmq1;
  213. BN_set_flags(r->dmq1, BN_FLG_CONSTTIME);
  214. }
  215. if (iqmp != NULL) {
  216. BN_clear_free(r->iqmp);
  217. r->iqmp = iqmp;
  218. BN_set_flags(r->iqmp, BN_FLG_CONSTTIME);
  219. }
  220. return 1;
  221. }
  222. /*
  223. * Is it better to export RSA_PRIME_INFO structure
  224. * and related functions to let user pass a triplet?
  225. */
  226. int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[],
  227. BIGNUM *coeffs[], int pnum)
  228. {
  229. STACK_OF(RSA_PRIME_INFO) *prime_infos, *old = NULL;
  230. RSA_PRIME_INFO *pinfo;
  231. int i;
  232. if (primes == NULL || exps == NULL || coeffs == NULL || pnum == 0)
  233. return 0;
  234. prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
  235. if (prime_infos == NULL)
  236. return 0;
  237. if (r->prime_infos != NULL)
  238. old = r->prime_infos;
  239. for (i = 0; i < pnum; i++) {
  240. pinfo = rsa_multip_info_new();
  241. if (pinfo == NULL)
  242. goto err;
  243. if (primes[i] != NULL && exps[i] != NULL && coeffs[i] != NULL) {
  244. BN_clear_free(pinfo->r);
  245. BN_clear_free(pinfo->d);
  246. BN_clear_free(pinfo->t);
  247. pinfo->r = primes[i];
  248. pinfo->d = exps[i];
  249. pinfo->t = coeffs[i];
  250. BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
  251. BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
  252. BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
  253. } else {
  254. rsa_multip_info_free(pinfo);
  255. goto err;
  256. }
  257. (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
  258. }
  259. r->prime_infos = prime_infos;
  260. if (!rsa_multip_calc_product(r)) {
  261. r->prime_infos = old;
  262. goto err;
  263. }
  264. if (old != NULL) {
  265. /*
  266. * This is hard to deal with, since the old infos could
  267. * also be set by this function and r, d, t should not
  268. * be freed in that case. So currently, stay consistent
  269. * with other *set0* functions: just free it...
  270. */
  271. sk_RSA_PRIME_INFO_pop_free(old, rsa_multip_info_free);
  272. }
  273. r->version = RSA_ASN1_VERSION_MULTI;
  274. return 1;
  275. err:
  276. /* r, d, t should not be freed */
  277. sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
  278. return 0;
  279. }
  280. void RSA_get0_key(const RSA *r,
  281. const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
  282. {
  283. if (n != NULL)
  284. *n = r->n;
  285. if (e != NULL)
  286. *e = r->e;
  287. if (d != NULL)
  288. *d = r->d;
  289. }
  290. void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
  291. {
  292. if (p != NULL)
  293. *p = r->p;
  294. if (q != NULL)
  295. *q = r->q;
  296. }
  297. int RSA_get_multi_prime_extra_count(const RSA *r)
  298. {
  299. int pnum;
  300. pnum = sk_RSA_PRIME_INFO_num(r->prime_infos);
  301. if (pnum <= 0)
  302. pnum = 0;
  303. return pnum;
  304. }
  305. int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[])
  306. {
  307. int pnum, i;
  308. RSA_PRIME_INFO *pinfo;
  309. if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
  310. return 0;
  311. /*
  312. * return other primes
  313. * it's caller's responsibility to allocate oth_primes[pnum]
  314. */
  315. for (i = 0; i < pnum; i++) {
  316. pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
  317. primes[i] = pinfo->r;
  318. }
  319. return 1;
  320. }
  321. void RSA_get0_crt_params(const RSA *r,
  322. const BIGNUM **dmp1, const BIGNUM **dmq1,
  323. const BIGNUM **iqmp)
  324. {
  325. if (dmp1 != NULL)
  326. *dmp1 = r->dmp1;
  327. if (dmq1 != NULL)
  328. *dmq1 = r->dmq1;
  329. if (iqmp != NULL)
  330. *iqmp = r->iqmp;
  331. }
  332. int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[],
  333. const BIGNUM *coeffs[])
  334. {
  335. int pnum;
  336. if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
  337. return 0;
  338. /* return other primes */
  339. if (exps != NULL || coeffs != NULL) {
  340. RSA_PRIME_INFO *pinfo;
  341. int i;
  342. /* it's the user's job to guarantee the buffer length */
  343. for (i = 0; i < pnum; i++) {
  344. pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
  345. if (exps != NULL)
  346. exps[i] = pinfo->d;
  347. if (coeffs != NULL)
  348. coeffs[i] = pinfo->t;
  349. }
  350. }
  351. return 1;
  352. }
  353. const BIGNUM *RSA_get0_n(const RSA *r)
  354. {
  355. return r->n;
  356. }
  357. const BIGNUM *RSA_get0_e(const RSA *r)
  358. {
  359. return r->e;
  360. }
  361. const BIGNUM *RSA_get0_d(const RSA *r)
  362. {
  363. return r->d;
  364. }
  365. const BIGNUM *RSA_get0_p(const RSA *r)
  366. {
  367. return r->p;
  368. }
  369. const BIGNUM *RSA_get0_q(const RSA *r)
  370. {
  371. return r->q;
  372. }
  373. const BIGNUM *RSA_get0_dmp1(const RSA *r)
  374. {
  375. return r->dmp1;
  376. }
  377. const BIGNUM *RSA_get0_dmq1(const RSA *r)
  378. {
  379. return r->dmq1;
  380. }
  381. const BIGNUM *RSA_get0_iqmp(const RSA *r)
  382. {
  383. return r->iqmp;
  384. }
  385. const RSA_PSS_PARAMS *RSA_get0_pss_params(const RSA *r)
  386. {
  387. return r->pss;
  388. }
  389. void RSA_clear_flags(RSA *r, int flags)
  390. {
  391. r->flags &= ~flags;
  392. }
  393. int RSA_test_flags(const RSA *r, int flags)
  394. {
  395. return r->flags & flags;
  396. }
  397. void RSA_set_flags(RSA *r, int flags)
  398. {
  399. r->flags |= flags;
  400. }
  401. int RSA_get_version(RSA *r)
  402. {
  403. /* { two-prime(0), multi(1) } */
  404. return r->version;
  405. }
  406. ENGINE *RSA_get0_engine(const RSA *r)
  407. {
  408. return r->engine;
  409. }
  410. int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
  411. {
  412. /* If key type not RSA or RSA-PSS return error */
  413. if (ctx != NULL && ctx->pmeth != NULL
  414. && ctx->pmeth->pkey_id != EVP_PKEY_RSA
  415. && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
  416. return -1;
  417. return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
  418. }