Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

612 строки
18 KiB

  1. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  2. * project 2006.
  3. */
  4. /* ====================================================================
  5. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. All advertising materials mentioning features or use of this
  20. * software must display the following acknowledgment:
  21. * "This product includes software developed by the OpenSSL Project
  22. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  23. *
  24. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  25. * endorse or promote products derived from this software without
  26. * prior written permission. For written permission, please contact
  27. * licensing@OpenSSL.org.
  28. *
  29. * 5. Products derived from this software may not be called "OpenSSL"
  30. * nor may "OpenSSL" appear in their names without prior written
  31. * permission of the OpenSSL Project.
  32. *
  33. * 6. Redistributions of any form whatsoever must retain the following
  34. * acknowledgment:
  35. * "This product includes software developed by the OpenSSL Project
  36. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  39. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  41. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  47. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  49. * OF THE POSSIBILITY OF SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This product includes cryptographic software written by Eric Young
  53. * (eay@cryptsoft.com). This product includes software written by Tim
  54. * Hudson (tjh@cryptsoft.com). */
  55. #include <openssl/evp.h>
  56. #include <openssl/bn.h>
  57. #include <openssl/buf.h>
  58. #include <openssl/digest.h>
  59. #include <openssl/err.h>
  60. #include <openssl/mem.h>
  61. #include <openssl/obj.h>
  62. #include <openssl/rsa.h>
  63. #include "../rsa/internal.h"
  64. #include "internal.h"
  65. typedef struct {
  66. /* Key gen parameters */
  67. int nbits;
  68. BIGNUM *pub_exp;
  69. /* RSA padding mode */
  70. int pad_mode;
  71. /* message digest */
  72. const EVP_MD *md;
  73. /* message digest for MGF1 */
  74. const EVP_MD *mgf1md;
  75. /* PSS salt length */
  76. int saltlen;
  77. /* tbuf is a buffer which is either NULL, or is the size of the RSA modulus.
  78. * It's used to store the output of RSA operations. */
  79. uint8_t *tbuf;
  80. /* OAEP label */
  81. uint8_t *oaep_label;
  82. size_t oaep_labellen;
  83. } RSA_PKEY_CTX;
  84. static int pkey_rsa_init(EVP_PKEY_CTX *ctx) {
  85. RSA_PKEY_CTX *rctx;
  86. rctx = OPENSSL_malloc(sizeof(RSA_PKEY_CTX));
  87. if (!rctx) {
  88. return 0;
  89. }
  90. memset(rctx, 0, sizeof(RSA_PKEY_CTX));
  91. rctx->nbits = 2048;
  92. rctx->pad_mode = RSA_PKCS1_PADDING;
  93. rctx->saltlen = -2;
  94. ctx->data = rctx;
  95. return 1;
  96. }
  97. static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) {
  98. RSA_PKEY_CTX *dctx, *sctx;
  99. if (!pkey_rsa_init(dst)) {
  100. return 0;
  101. }
  102. sctx = src->data;
  103. dctx = dst->data;
  104. dctx->nbits = sctx->nbits;
  105. if (sctx->pub_exp) {
  106. dctx->pub_exp = BN_dup(sctx->pub_exp);
  107. if (!dctx->pub_exp) {
  108. return 0;
  109. }
  110. }
  111. dctx->pad_mode = sctx->pad_mode;
  112. dctx->md = sctx->md;
  113. dctx->mgf1md = sctx->mgf1md;
  114. if (sctx->oaep_label) {
  115. if (dctx->oaep_label) {
  116. OPENSSL_free(dctx->oaep_label);
  117. }
  118. dctx->oaep_label = BUF_memdup(sctx->oaep_label, sctx->oaep_labellen);
  119. if (!dctx->oaep_label) {
  120. return 0;
  121. }
  122. dctx->oaep_labellen = sctx->oaep_labellen;
  123. }
  124. return 1;
  125. }
  126. static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx) {
  127. RSA_PKEY_CTX *rctx = ctx->data;
  128. if (rctx == NULL) {
  129. return;
  130. }
  131. if (rctx->pub_exp) {
  132. BN_free(rctx->pub_exp);
  133. }
  134. if (rctx->tbuf) {
  135. OPENSSL_free(rctx->tbuf);
  136. }
  137. if (rctx->oaep_label) {
  138. OPENSSL_free(rctx->oaep_label);
  139. }
  140. OPENSSL_free(rctx);
  141. }
  142. static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk) {
  143. if (ctx->tbuf) {
  144. return 1;
  145. }
  146. ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey));
  147. if (!ctx->tbuf) {
  148. return 0;
  149. }
  150. return 1;
  151. }
  152. static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *siglen,
  153. const uint8_t *tbs, size_t tbslen) {
  154. int ret;
  155. RSA_PKEY_CTX *rctx = ctx->data;
  156. RSA *rsa = ctx->pkey->pkey.rsa;
  157. if (!sig) {
  158. *siglen = RSA_size(rsa);
  159. return 1;
  160. } else if (*siglen < (size_t)RSA_size(rsa)) {
  161. OPENSSL_PUT_ERROR(EVP, pkey_rsa_sign, EVP_R_BUFFER_TOO_SMALL);
  162. return 0;
  163. }
  164. if (rctx->md) {
  165. if (tbslen != EVP_MD_size(rctx->md)) {
  166. OPENSSL_PUT_ERROR(EVP, pkey_rsa_sign, EVP_R_INVALID_DIGEST_LENGTH);
  167. return -1;
  168. }
  169. if (EVP_MD_type(rctx->md) == NID_mdc2) {
  170. OPENSSL_PUT_ERROR(EVP, pkey_rsa_sign, EVP_R_NO_MDC2_SUPPORT);
  171. ret = -1;
  172. } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
  173. unsigned int sltmp;
  174. ret = RSA_sign(EVP_MD_type(rctx->md), tbs, tbslen, sig, &sltmp, rsa);
  175. if (ret <= 0) {
  176. return ret;
  177. }
  178. ret = sltmp;
  179. } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
  180. if (!setup_tbuf(rctx, ctx) ||
  181. !RSA_padding_add_PKCS1_PSS_mgf1(rsa, rctx->tbuf, tbs, rctx->md,
  182. rctx->mgf1md, rctx->saltlen)) {
  183. return -1;
  184. }
  185. /* TODO(fork): don't use private_encrypt. */
  186. ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf, sig, rsa,
  187. RSA_NO_PADDING);
  188. } else {
  189. return -1;
  190. }
  191. } else {
  192. /* TODO(fork): don't use private_encrypt. */
  193. ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
  194. rctx->pad_mode);
  195. }
  196. if (ret < 0) {
  197. return ret;
  198. }
  199. *siglen = ret;
  200. return 1;
  201. }
  202. static int pkey_rsa_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig,
  203. size_t siglen, const uint8_t *tbs,
  204. size_t tbslen) {
  205. RSA_PKEY_CTX *rctx = ctx->data;
  206. RSA *rsa = ctx->pkey->pkey.rsa;
  207. size_t rslen;
  208. if (rctx->md) {
  209. if (rctx->pad_mode == RSA_PKCS1_PADDING) {
  210. return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen, sig, siglen, rsa);
  211. }
  212. if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
  213. int ret;
  214. if (!setup_tbuf(rctx, ctx)) {
  215. return -1;
  216. }
  217. /* TODO(fork): don't use public_decrypt. */
  218. ret = RSA_public_decrypt(siglen, sig, rctx->tbuf, rsa, RSA_NO_PADDING);
  219. if (ret <= 0) {
  220. return 0;
  221. }
  222. ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs, rctx->md, rctx->mgf1md,
  223. rctx->tbuf, rctx->saltlen);
  224. if (ret <= 0) {
  225. return 0;
  226. }
  227. return 1;
  228. } else {
  229. return -1;
  230. }
  231. } else {
  232. if (!setup_tbuf(rctx, ctx)) {
  233. return -1;
  234. }
  235. rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf, rsa, rctx->pad_mode);
  236. if (rslen == 0) {
  237. return 0;
  238. }
  239. }
  240. if (rslen != tbslen || CRYPTO_memcmp(tbs, rctx->tbuf, rslen) != 0) {
  241. return 0;
  242. }
  243. return 1;
  244. }
  245. static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *outlen,
  246. const uint8_t *in, size_t inlen) {
  247. int ret;
  248. RSA_PKEY_CTX *rctx = ctx->data;
  249. RSA *rsa = ctx->pkey->pkey.rsa;
  250. if (!out) {
  251. *outlen = RSA_size(rsa);
  252. return 1;
  253. } else if (*outlen < (size_t)RSA_size(rsa)) {
  254. OPENSSL_PUT_ERROR(EVP, pkey_rsa_encrypt, EVP_R_BUFFER_TOO_SMALL);
  255. return 0;
  256. }
  257. if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
  258. int klen = RSA_size(rsa);
  259. if (!setup_tbuf(rctx, ctx)) {
  260. return -1;
  261. }
  262. if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen, in, inlen,
  263. rctx->oaep_label, rctx->oaep_labellen,
  264. rctx->md, rctx->mgf1md)) {
  265. return -1;
  266. }
  267. ret = RSA_public_encrypt(klen, rctx->tbuf, out, rsa, RSA_NO_PADDING);
  268. } else {
  269. ret = RSA_public_encrypt(inlen, in, out, rsa, rctx->pad_mode);
  270. }
  271. if (ret < 0) {
  272. return ret;
  273. }
  274. *outlen = ret;
  275. return 1;
  276. }
  277. static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx, uint8_t *out,
  278. size_t *outlen, const uint8_t *in,
  279. size_t inlen) {
  280. int ret;
  281. RSA_PKEY_CTX *rctx = ctx->data;
  282. RSA *rsa = ctx->pkey->pkey.rsa;
  283. if (!out) {
  284. *outlen = RSA_size(rsa);
  285. return 1;
  286. } else if (*outlen < (size_t)RSA_size(rsa)) {
  287. OPENSSL_PUT_ERROR(EVP, pkey_rsa_decrypt, EVP_R_BUFFER_TOO_SMALL);
  288. return 0;
  289. }
  290. if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
  291. if (!setup_tbuf(rctx, ctx)) {
  292. return -1;
  293. }
  294. ret = RSA_private_decrypt(inlen, in, rctx->tbuf, rsa, RSA_NO_PADDING);
  295. if (ret <= 0) {
  296. return ret;
  297. }
  298. ret = RSA_padding_check_PKCS1_OAEP_mgf1(
  299. out, ret, rctx->tbuf, ret, rctx->oaep_label,
  300. rctx->oaep_labellen, rctx->md, rctx->mgf1md);
  301. } else {
  302. ret = RSA_private_decrypt(inlen, in, out, rsa, rctx->pad_mode);
  303. }
  304. if (ret < 0) {
  305. return ret;
  306. }
  307. *outlen = ret;
  308. return 1;
  309. }
  310. static int check_padding_md(const EVP_MD *md, int padding) {
  311. if (!md) {
  312. return 1;
  313. }
  314. if (padding == RSA_NO_PADDING) {
  315. OPENSSL_PUT_ERROR(EVP, check_padding_md, EVP_R_INVALID_PADDING_MODE);
  316. return 0;
  317. }
  318. return 1;
  319. }
  320. static int is_known_padding(int padding_mode) {
  321. switch (padding_mode) {
  322. case RSA_PKCS1_PADDING:
  323. case RSA_NO_PADDING:
  324. case RSA_PKCS1_OAEP_PADDING:
  325. case RSA_PKCS1_PSS_PADDING:
  326. return 1;
  327. default:
  328. return 0;
  329. }
  330. }
  331. static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) {
  332. RSA_PKEY_CTX *rctx = ctx->data;
  333. switch (type) {
  334. case EVP_PKEY_CTRL_RSA_PADDING:
  335. if (!is_known_padding(p1) || !check_padding_md(rctx->md, p1) ||
  336. (p1 == RSA_PKCS1_PSS_PADDING &&
  337. 0 == (ctx->operation & (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY))) ||
  338. (p1 == RSA_PKCS1_OAEP_PADDING &&
  339. 0 == (ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))) {
  340. OPENSSL_PUT_ERROR(EVP, pkey_rsa_ctrl,
  341. EVP_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
  342. return -2;
  343. }
  344. if ((p1 == RSA_PKCS1_PSS_PADDING || p1 == RSA_PKCS1_OAEP_PADDING) &&
  345. rctx->md == NULL) {
  346. rctx->md = EVP_sha1();
  347. }
  348. rctx->pad_mode = p1;
  349. return 1;
  350. case EVP_PKEY_CTRL_GET_RSA_PADDING:
  351. *(int *)p2 = rctx->pad_mode;
  352. return 1;
  353. case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
  354. case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
  355. if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
  356. OPENSSL_PUT_ERROR(EVP, pkey_rsa_ctrl, EVP_R_INVALID_PSS_SALTLEN);
  357. return -2;
  358. }
  359. if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN) {
  360. *(int *)p2 = rctx->saltlen;
  361. } else {
  362. if (p1 < -2) {
  363. return -2;
  364. }
  365. rctx->saltlen = p1;
  366. }
  367. return 1;
  368. case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
  369. if (p1 < 256) {
  370. OPENSSL_PUT_ERROR(EVP, pkey_rsa_ctrl, EVP_R_INVALID_KEYBITS);
  371. return -2;
  372. }
  373. rctx->nbits = p1;
  374. return 1;
  375. case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
  376. if (!p2) {
  377. return -2;
  378. }
  379. BN_free(rctx->pub_exp);
  380. rctx->pub_exp = p2;
  381. return 1;
  382. case EVP_PKEY_CTRL_RSA_OAEP_MD:
  383. case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
  384. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  385. OPENSSL_PUT_ERROR(EVP, pkey_rsa_ctrl, EVP_R_INVALID_PADDING_MODE);
  386. return -2;
  387. }
  388. if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD) {
  389. *(const EVP_MD **)p2 = rctx->md;
  390. } else {
  391. rctx->md = p2;
  392. }
  393. return 1;
  394. case EVP_PKEY_CTRL_MD:
  395. if (!check_padding_md(p2, rctx->pad_mode)) {
  396. return 0;
  397. }
  398. rctx->md = p2;
  399. return 1;
  400. case EVP_PKEY_CTRL_GET_MD:
  401. *(const EVP_MD **)p2 = rctx->md;
  402. return 1;
  403. case EVP_PKEY_CTRL_RSA_MGF1_MD:
  404. case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
  405. if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING &&
  406. rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  407. OPENSSL_PUT_ERROR(EVP, pkey_rsa_ctrl, EVP_R_INVALID_MGF1_MD);
  408. return -2;
  409. }
  410. if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
  411. if (rctx->mgf1md) {
  412. *(const EVP_MD **)p2 = rctx->mgf1md;
  413. } else {
  414. *(const EVP_MD **)p2 = rctx->md;
  415. }
  416. } else {
  417. rctx->mgf1md = p2;
  418. }
  419. return 1;
  420. case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
  421. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  422. OPENSSL_PUT_ERROR(EVP, pkey_rsa_ctrl, EVP_R_INVALID_PADDING_MODE);
  423. return -2;
  424. }
  425. if (rctx->oaep_label) {
  426. OPENSSL_free(rctx->oaep_label);
  427. }
  428. if (p2 && p1 > 0) {
  429. /* TODO(fork): this seems wrong. Shouldn't it take a copy of the
  430. * buffer? */
  431. rctx->oaep_label = p2;
  432. rctx->oaep_labellen = p1;
  433. } else {
  434. rctx->oaep_label = NULL;
  435. rctx->oaep_labellen = 0;
  436. }
  437. return 1;
  438. case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
  439. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  440. OPENSSL_PUT_ERROR(EVP, pkey_rsa_ctrl, EVP_R_INVALID_PADDING_MODE);
  441. return -2;
  442. }
  443. *(uint8_t **)p2 = rctx->oaep_label;
  444. return rctx->oaep_labellen;
  445. case EVP_PKEY_CTRL_DIGESTINIT:
  446. return 1;
  447. default:
  448. return -2;
  449. }
  450. }
  451. static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
  452. RSA *rsa = NULL;
  453. RSA_PKEY_CTX *rctx = ctx->data;
  454. int ret;
  455. if (!rctx->pub_exp) {
  456. rctx->pub_exp = BN_new();
  457. if (!rctx->pub_exp || !BN_set_word(rctx->pub_exp, RSA_F4))
  458. return 0;
  459. }
  460. rsa = RSA_new();
  461. if (!rsa) {
  462. return 0;
  463. }
  464. ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, NULL);
  465. if (ret > 0) {
  466. EVP_PKEY_assign_RSA(pkey, rsa);
  467. } else {
  468. RSA_free(rsa);
  469. }
  470. return ret;
  471. }
  472. const EVP_PKEY_METHOD rsa_pkey_meth = {
  473. EVP_PKEY_RSA, 0 /* flags */, pkey_rsa_init,
  474. pkey_rsa_copy, pkey_rsa_cleanup, 0 /* paramgen_init */,
  475. 0 /* paramgen */, 0 /* keygen_init */, pkey_rsa_keygen,
  476. 0 /* sign_init */, pkey_rsa_sign, 0 /* verify_init */,
  477. pkey_rsa_verify, 0 /* signctx_init */, 0 /* signctx */,
  478. 0 /* verifyctx_init */, 0 /* verifyctx */, 0 /* encrypt_init */,
  479. pkey_rsa_encrypt, 0 /* decrypt_init */, pkey_rsa_decrypt,
  480. 0 /* derive_init */, 0 /* derive */, pkey_rsa_ctrl,
  481. };
  482. int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int padding) {
  483. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, -1, EVP_PKEY_CTRL_RSA_PADDING,
  484. padding, NULL);
  485. }
  486. int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *out_padding) {
  487. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, -1, EVP_PKEY_CTRL_GET_RSA_PADDING,
  488. 0, out_padding);
  489. }
  490. int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int salt_len) {
  491. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  492. (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY),
  493. EVP_PKEY_CTRL_RSA_PSS_SALTLEN, salt_len, NULL);
  494. }
  495. int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int *out_salt_len) {
  496. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  497. (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY),
  498. EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN, 0, out_salt_len);
  499. }
  500. int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, int bits) {
  501. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
  502. EVP_PKEY_CTRL_RSA_KEYGEN_BITS, bits, NULL);
  503. }
  504. int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *e) {
  505. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
  506. EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, e);
  507. }
  508. int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
  509. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  510. EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)md);
  511. }
  512. int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
  513. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  514. EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void*) out_md);
  515. }
  516. int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
  517. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  518. EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
  519. EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void*) md);
  520. }
  521. int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
  522. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  523. EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
  524. EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void*) out_md);
  525. }
  526. int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, const uint8_t *label,
  527. size_t label_len) {
  528. int label_len_int = label_len;
  529. if (((size_t) label_len_int) != label_len) {
  530. return -2;
  531. }
  532. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  533. EVP_PKEY_CTRL_RSA_OAEP_LABEL, label_len,
  534. (void *)label);
  535. }
  536. int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx,
  537. const uint8_t **out_label) {
  538. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  539. EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, 0, (void *) out_label);
  540. }