p_rsa.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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 <limits.h>
  57. #include <string.h>
  58. #include <openssl/bn.h>
  59. #include <openssl/buf.h>
  60. #include <openssl/bytestring.h>
  61. #include <openssl/digest.h>
  62. #include <openssl/err.h>
  63. #include <openssl/mem.h>
  64. #include <openssl/nid.h>
  65. #include <openssl/rsa.h>
  66. #include "../internal.h"
  67. #include "../fipsmodule/rsa/internal.h"
  68. #include "internal.h"
  69. typedef struct {
  70. // Key gen parameters
  71. int nbits;
  72. BIGNUM *pub_exp;
  73. // RSA padding mode
  74. int pad_mode;
  75. // message digest
  76. const EVP_MD *md;
  77. // message digest for MGF1
  78. const EVP_MD *mgf1md;
  79. // PSS salt length
  80. int saltlen;
  81. // tbuf is a buffer which is either NULL, or is the size of the RSA modulus.
  82. // It's used to store the output of RSA operations.
  83. uint8_t *tbuf;
  84. // OAEP label
  85. uint8_t *oaep_label;
  86. size_t oaep_labellen;
  87. } RSA_PKEY_CTX;
  88. typedef struct {
  89. uint8_t *data;
  90. size_t len;
  91. } RSA_OAEP_LABEL_PARAMS;
  92. static int pkey_rsa_init(EVP_PKEY_CTX *ctx) {
  93. RSA_PKEY_CTX *rctx;
  94. rctx = OPENSSL_malloc(sizeof(RSA_PKEY_CTX));
  95. if (!rctx) {
  96. return 0;
  97. }
  98. OPENSSL_memset(rctx, 0, sizeof(RSA_PKEY_CTX));
  99. rctx->nbits = 2048;
  100. rctx->pad_mode = RSA_PKCS1_PADDING;
  101. rctx->saltlen = -2;
  102. ctx->data = rctx;
  103. return 1;
  104. }
  105. static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) {
  106. RSA_PKEY_CTX *dctx, *sctx;
  107. if (!pkey_rsa_init(dst)) {
  108. return 0;
  109. }
  110. sctx = src->data;
  111. dctx = dst->data;
  112. dctx->nbits = sctx->nbits;
  113. if (sctx->pub_exp) {
  114. dctx->pub_exp = BN_dup(sctx->pub_exp);
  115. if (!dctx->pub_exp) {
  116. return 0;
  117. }
  118. }
  119. dctx->pad_mode = sctx->pad_mode;
  120. dctx->md = sctx->md;
  121. dctx->mgf1md = sctx->mgf1md;
  122. if (sctx->oaep_label) {
  123. OPENSSL_free(dctx->oaep_label);
  124. dctx->oaep_label = BUF_memdup(sctx->oaep_label, sctx->oaep_labellen);
  125. if (!dctx->oaep_label) {
  126. return 0;
  127. }
  128. dctx->oaep_labellen = sctx->oaep_labellen;
  129. }
  130. return 1;
  131. }
  132. static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx) {
  133. RSA_PKEY_CTX *rctx = ctx->data;
  134. if (rctx == NULL) {
  135. return;
  136. }
  137. BN_free(rctx->pub_exp);
  138. OPENSSL_free(rctx->tbuf);
  139. OPENSSL_free(rctx->oaep_label);
  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. RSA_PKEY_CTX *rctx = ctx->data;
  155. RSA *rsa = ctx->pkey->pkey.rsa;
  156. const size_t key_len = EVP_PKEY_size(ctx->pkey);
  157. if (!sig) {
  158. *siglen = key_len;
  159. return 1;
  160. }
  161. if (*siglen < key_len) {
  162. OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
  163. return 0;
  164. }
  165. if (rctx->md) {
  166. unsigned out_len;
  167. switch (rctx->pad_mode) {
  168. case RSA_PKCS1_PADDING:
  169. if (!RSA_sign(EVP_MD_type(rctx->md), tbs, tbslen, sig, &out_len, rsa)) {
  170. return 0;
  171. }
  172. *siglen = out_len;
  173. return 1;
  174. case RSA_PKCS1_PSS_PADDING:
  175. return RSA_sign_pss_mgf1(rsa, siglen, sig, *siglen, tbs, tbslen,
  176. rctx->md, rctx->mgf1md, rctx->saltlen);
  177. default:
  178. return 0;
  179. }
  180. }
  181. return RSA_sign_raw(rsa, siglen, sig, *siglen, tbs, tbslen, rctx->pad_mode);
  182. }
  183. static int pkey_rsa_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig,
  184. size_t siglen, const uint8_t *tbs,
  185. size_t tbslen) {
  186. RSA_PKEY_CTX *rctx = ctx->data;
  187. RSA *rsa = ctx->pkey->pkey.rsa;
  188. if (rctx->md) {
  189. switch (rctx->pad_mode) {
  190. case RSA_PKCS1_PADDING:
  191. return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen, sig, siglen, rsa);
  192. case RSA_PKCS1_PSS_PADDING:
  193. return RSA_verify_pss_mgf1(rsa, tbs, tbslen, rctx->md, rctx->mgf1md,
  194. rctx->saltlen, sig, siglen);
  195. default:
  196. return 0;
  197. }
  198. }
  199. size_t rslen;
  200. const size_t key_len = EVP_PKEY_size(ctx->pkey);
  201. if (!setup_tbuf(rctx, ctx) ||
  202. !RSA_verify_raw(rsa, &rslen, rctx->tbuf, key_len, sig, siglen,
  203. rctx->pad_mode) ||
  204. rslen != tbslen ||
  205. CRYPTO_memcmp(tbs, rctx->tbuf, rslen) != 0) {
  206. return 0;
  207. }
  208. return 1;
  209. }
  210. static int pkey_rsa_verify_recover(EVP_PKEY_CTX *ctx, uint8_t *out,
  211. size_t *out_len, const uint8_t *sig,
  212. size_t sig_len) {
  213. RSA_PKEY_CTX *rctx = ctx->data;
  214. RSA *rsa = ctx->pkey->pkey.rsa;
  215. const size_t key_len = EVP_PKEY_size(ctx->pkey);
  216. if (out == NULL) {
  217. *out_len = key_len;
  218. return 1;
  219. }
  220. if (*out_len < key_len) {
  221. OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
  222. return 0;
  223. }
  224. if (rctx->md == NULL) {
  225. return RSA_verify_raw(rsa, out_len, out, *out_len, sig, sig_len,
  226. rctx->pad_mode);
  227. }
  228. if (rctx->pad_mode != RSA_PKCS1_PADDING) {
  229. return 0;
  230. }
  231. // Assemble the encoded hash, using a placeholder hash value.
  232. static const uint8_t kDummyHash[EVP_MAX_MD_SIZE] = {0};
  233. const size_t hash_len = EVP_MD_size(rctx->md);
  234. uint8_t *asn1_prefix;
  235. size_t asn1_prefix_len;
  236. int asn1_prefix_allocated;
  237. if (!setup_tbuf(rctx, ctx) ||
  238. !RSA_add_pkcs1_prefix(&asn1_prefix, &asn1_prefix_len,
  239. &asn1_prefix_allocated, EVP_MD_type(rctx->md),
  240. kDummyHash, hash_len)) {
  241. return 0;
  242. }
  243. size_t rslen;
  244. int ok = 1;
  245. if (!RSA_verify_raw(rsa, &rslen, rctx->tbuf, key_len, sig, sig_len,
  246. RSA_PKCS1_PADDING) ||
  247. rslen != asn1_prefix_len ||
  248. // Compare all but the hash suffix.
  249. CRYPTO_memcmp(rctx->tbuf, asn1_prefix, asn1_prefix_len - hash_len) != 0) {
  250. ok = 0;
  251. }
  252. if (asn1_prefix_allocated) {
  253. OPENSSL_free(asn1_prefix);
  254. }
  255. if (!ok) {
  256. return 0;
  257. }
  258. if (out != NULL) {
  259. OPENSSL_memcpy(out, rctx->tbuf + rslen - hash_len, hash_len);
  260. }
  261. *out_len = hash_len;
  262. return 1;
  263. }
  264. static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *outlen,
  265. const uint8_t *in, size_t inlen) {
  266. RSA_PKEY_CTX *rctx = ctx->data;
  267. RSA *rsa = ctx->pkey->pkey.rsa;
  268. const size_t key_len = EVP_PKEY_size(ctx->pkey);
  269. if (!out) {
  270. *outlen = key_len;
  271. return 1;
  272. }
  273. if (*outlen < key_len) {
  274. OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
  275. return 0;
  276. }
  277. if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
  278. if (!setup_tbuf(rctx, ctx) ||
  279. !RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, key_len, in, inlen,
  280. rctx->oaep_label, rctx->oaep_labellen,
  281. rctx->md, rctx->mgf1md) ||
  282. !RSA_encrypt(rsa, outlen, out, *outlen, rctx->tbuf, key_len,
  283. RSA_NO_PADDING)) {
  284. return 0;
  285. }
  286. return 1;
  287. }
  288. return RSA_encrypt(rsa, outlen, out, *outlen, in, inlen, rctx->pad_mode);
  289. }
  290. static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx, uint8_t *out,
  291. size_t *outlen, const uint8_t *in,
  292. size_t inlen) {
  293. RSA_PKEY_CTX *rctx = ctx->data;
  294. RSA *rsa = ctx->pkey->pkey.rsa;
  295. const size_t key_len = EVP_PKEY_size(ctx->pkey);
  296. if (!out) {
  297. *outlen = key_len;
  298. return 1;
  299. }
  300. if (*outlen < key_len) {
  301. OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
  302. return 0;
  303. }
  304. if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
  305. size_t padded_len;
  306. if (!setup_tbuf(rctx, ctx) ||
  307. !RSA_decrypt(rsa, &padded_len, rctx->tbuf, key_len, in, inlen,
  308. RSA_NO_PADDING) ||
  309. !RSA_padding_check_PKCS1_OAEP_mgf1(
  310. out, outlen, key_len, rctx->tbuf, padded_len, rctx->oaep_label,
  311. rctx->oaep_labellen, rctx->md, rctx->mgf1md)) {
  312. return 0;
  313. }
  314. return 1;
  315. }
  316. return RSA_decrypt(rsa, outlen, out, key_len, in, inlen, rctx->pad_mode);
  317. }
  318. static int check_padding_md(const EVP_MD *md, int padding) {
  319. if (!md) {
  320. return 1;
  321. }
  322. if (padding == RSA_NO_PADDING) {
  323. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PADDING_MODE);
  324. return 0;
  325. }
  326. return 1;
  327. }
  328. static int is_known_padding(int padding_mode) {
  329. switch (padding_mode) {
  330. case RSA_PKCS1_PADDING:
  331. case RSA_NO_PADDING:
  332. case RSA_PKCS1_OAEP_PADDING:
  333. case RSA_PKCS1_PSS_PADDING:
  334. return 1;
  335. default:
  336. return 0;
  337. }
  338. }
  339. static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) {
  340. RSA_PKEY_CTX *rctx = ctx->data;
  341. switch (type) {
  342. case EVP_PKEY_CTRL_RSA_PADDING:
  343. if (!is_known_padding(p1) || !check_padding_md(rctx->md, p1) ||
  344. (p1 == RSA_PKCS1_PSS_PADDING &&
  345. 0 == (ctx->operation & (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY))) ||
  346. (p1 == RSA_PKCS1_OAEP_PADDING &&
  347. 0 == (ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))) {
  348. OPENSSL_PUT_ERROR(EVP, EVP_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
  349. return 0;
  350. }
  351. if ((p1 == RSA_PKCS1_PSS_PADDING || p1 == RSA_PKCS1_OAEP_PADDING) &&
  352. rctx->md == NULL) {
  353. rctx->md = EVP_sha1();
  354. }
  355. rctx->pad_mode = p1;
  356. return 1;
  357. case EVP_PKEY_CTRL_GET_RSA_PADDING:
  358. *(int *)p2 = rctx->pad_mode;
  359. return 1;
  360. case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
  361. case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
  362. if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
  363. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PSS_SALTLEN);
  364. return 0;
  365. }
  366. if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN) {
  367. *(int *)p2 = rctx->saltlen;
  368. } else {
  369. if (p1 < -2) {
  370. return 0;
  371. }
  372. rctx->saltlen = p1;
  373. }
  374. return 1;
  375. case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
  376. if (p1 < 256) {
  377. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_KEYBITS);
  378. return 0;
  379. }
  380. rctx->nbits = p1;
  381. return 1;
  382. case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
  383. if (!p2) {
  384. return 0;
  385. }
  386. BN_free(rctx->pub_exp);
  387. rctx->pub_exp = p2;
  388. return 1;
  389. case EVP_PKEY_CTRL_RSA_OAEP_MD:
  390. case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
  391. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  392. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PADDING_MODE);
  393. return 0;
  394. }
  395. if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD) {
  396. *(const EVP_MD **)p2 = rctx->md;
  397. } else {
  398. rctx->md = p2;
  399. }
  400. return 1;
  401. case EVP_PKEY_CTRL_MD:
  402. if (!check_padding_md(p2, rctx->pad_mode)) {
  403. return 0;
  404. }
  405. rctx->md = p2;
  406. return 1;
  407. case EVP_PKEY_CTRL_GET_MD:
  408. *(const EVP_MD **)p2 = rctx->md;
  409. return 1;
  410. case EVP_PKEY_CTRL_RSA_MGF1_MD:
  411. case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
  412. if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING &&
  413. rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  414. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_MGF1_MD);
  415. return 0;
  416. }
  417. if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
  418. if (rctx->mgf1md) {
  419. *(const EVP_MD **)p2 = rctx->mgf1md;
  420. } else {
  421. *(const EVP_MD **)p2 = rctx->md;
  422. }
  423. } else {
  424. rctx->mgf1md = p2;
  425. }
  426. return 1;
  427. case EVP_PKEY_CTRL_RSA_OAEP_LABEL: {
  428. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  429. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PADDING_MODE);
  430. return 0;
  431. }
  432. OPENSSL_free(rctx->oaep_label);
  433. RSA_OAEP_LABEL_PARAMS *params = p2;
  434. rctx->oaep_label = params->data;
  435. rctx->oaep_labellen = params->len;
  436. return 1;
  437. }
  438. case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
  439. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  440. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PADDING_MODE);
  441. return 0;
  442. }
  443. CBS_init((CBS *)p2, rctx->oaep_label, rctx->oaep_labellen);
  444. return 1;
  445. default:
  446. OPENSSL_PUT_ERROR(EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  447. return 0;
  448. }
  449. }
  450. static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
  451. RSA *rsa = NULL;
  452. RSA_PKEY_CTX *rctx = ctx->data;
  453. if (!rctx->pub_exp) {
  454. rctx->pub_exp = BN_new();
  455. if (!rctx->pub_exp || !BN_set_word(rctx->pub_exp, RSA_F4)) {
  456. return 0;
  457. }
  458. }
  459. rsa = RSA_new();
  460. if (!rsa) {
  461. return 0;
  462. }
  463. if (!RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, NULL)) {
  464. RSA_free(rsa);
  465. return 0;
  466. }
  467. EVP_PKEY_assign_RSA(pkey, rsa);
  468. return 1;
  469. }
  470. const EVP_PKEY_METHOD rsa_pkey_meth = {
  471. EVP_PKEY_RSA,
  472. pkey_rsa_init,
  473. pkey_rsa_copy,
  474. pkey_rsa_cleanup,
  475. pkey_rsa_keygen,
  476. pkey_rsa_sign,
  477. NULL /* sign_message */,
  478. pkey_rsa_verify,
  479. NULL /* verify_message */,
  480. pkey_rsa_verify_recover,
  481. pkey_rsa_encrypt,
  482. pkey_rsa_decrypt,
  483. 0 /* derive */,
  484. pkey_rsa_ctrl,
  485. };
  486. int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int padding) {
  487. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, -1, EVP_PKEY_CTRL_RSA_PADDING,
  488. padding, NULL);
  489. }
  490. int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *out_padding) {
  491. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, -1, EVP_PKEY_CTRL_GET_RSA_PADDING,
  492. 0, out_padding);
  493. }
  494. int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int salt_len) {
  495. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  496. (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY),
  497. EVP_PKEY_CTRL_RSA_PSS_SALTLEN, salt_len, NULL);
  498. }
  499. int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int *out_salt_len) {
  500. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  501. (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY),
  502. EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN, 0, out_salt_len);
  503. }
  504. int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, int bits) {
  505. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
  506. EVP_PKEY_CTRL_RSA_KEYGEN_BITS, bits, NULL);
  507. }
  508. int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *e) {
  509. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
  510. EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, e);
  511. }
  512. int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
  513. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  514. EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)md);
  515. }
  516. int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
  517. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  518. EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void*) out_md);
  519. }
  520. int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
  521. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  522. EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
  523. EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void*) md);
  524. }
  525. int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
  526. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  527. EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
  528. EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void*) out_md);
  529. }
  530. int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, uint8_t *label,
  531. size_t label_len) {
  532. RSA_OAEP_LABEL_PARAMS params = {label, label_len};
  533. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  534. EVP_PKEY_CTRL_RSA_OAEP_LABEL, 0, &params);
  535. }
  536. int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx,
  537. const uint8_t **out_label) {
  538. CBS label;
  539. if (!EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  540. EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, 0, &label)) {
  541. return -1;
  542. }
  543. if (CBS_len(&label) > INT_MAX) {
  544. OPENSSL_PUT_ERROR(EVP, ERR_R_OVERFLOW);
  545. return -1;
  546. }
  547. *out_label = CBS_data(&label);
  548. return (int)CBS_len(&label);
  549. }