p_rsa.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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 "../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. static int pkey_rsa_init(EVP_PKEY_CTX *ctx) {
  89. RSA_PKEY_CTX *rctx;
  90. rctx = OPENSSL_malloc(sizeof(RSA_PKEY_CTX));
  91. if (!rctx) {
  92. return 0;
  93. }
  94. OPENSSL_memset(rctx, 0, sizeof(RSA_PKEY_CTX));
  95. rctx->nbits = 2048;
  96. rctx->pad_mode = RSA_PKCS1_PADDING;
  97. rctx->saltlen = -2;
  98. ctx->data = rctx;
  99. return 1;
  100. }
  101. static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) {
  102. RSA_PKEY_CTX *dctx, *sctx;
  103. if (!pkey_rsa_init(dst)) {
  104. return 0;
  105. }
  106. sctx = src->data;
  107. dctx = dst->data;
  108. dctx->nbits = sctx->nbits;
  109. if (sctx->pub_exp) {
  110. dctx->pub_exp = BN_dup(sctx->pub_exp);
  111. if (!dctx->pub_exp) {
  112. return 0;
  113. }
  114. }
  115. dctx->pad_mode = sctx->pad_mode;
  116. dctx->md = sctx->md;
  117. dctx->mgf1md = sctx->mgf1md;
  118. if (sctx->oaep_label) {
  119. OPENSSL_free(dctx->oaep_label);
  120. dctx->oaep_label = BUF_memdup(sctx->oaep_label, sctx->oaep_labellen);
  121. if (!dctx->oaep_label) {
  122. return 0;
  123. }
  124. dctx->oaep_labellen = sctx->oaep_labellen;
  125. }
  126. return 1;
  127. }
  128. static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx) {
  129. RSA_PKEY_CTX *rctx = ctx->data;
  130. if (rctx == NULL) {
  131. return;
  132. }
  133. BN_free(rctx->pub_exp);
  134. OPENSSL_free(rctx->tbuf);
  135. OPENSSL_free(rctx->oaep_label);
  136. OPENSSL_free(rctx);
  137. }
  138. static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk) {
  139. if (ctx->tbuf) {
  140. return 1;
  141. }
  142. ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey));
  143. if (!ctx->tbuf) {
  144. return 0;
  145. }
  146. return 1;
  147. }
  148. static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *siglen,
  149. const uint8_t *tbs, size_t tbslen) {
  150. RSA_PKEY_CTX *rctx = ctx->data;
  151. RSA *rsa = ctx->pkey->pkey.rsa;
  152. const size_t key_len = EVP_PKEY_size(ctx->pkey);
  153. if (!sig) {
  154. *siglen = key_len;
  155. return 1;
  156. }
  157. if (*siglen < key_len) {
  158. OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
  159. return 0;
  160. }
  161. if (rctx->md) {
  162. unsigned int out_len;
  163. if (tbslen != EVP_MD_size(rctx->md)) {
  164. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_DIGEST_LENGTH);
  165. return 0;
  166. }
  167. if (EVP_MD_type(rctx->md) == NID_mdc2) {
  168. OPENSSL_PUT_ERROR(EVP, EVP_R_NO_MDC2_SUPPORT);
  169. return 0;
  170. }
  171. switch (rctx->pad_mode) {
  172. case RSA_PKCS1_PADDING:
  173. if (!RSA_sign(EVP_MD_type(rctx->md), tbs, tbslen, sig, &out_len, rsa)) {
  174. return 0;
  175. }
  176. *siglen = out_len;
  177. return 1;
  178. case RSA_PKCS1_PSS_PADDING:
  179. if (!setup_tbuf(rctx, ctx) ||
  180. !RSA_padding_add_PKCS1_PSS_mgf1(rsa, rctx->tbuf, tbs, rctx->md,
  181. rctx->mgf1md, rctx->saltlen) ||
  182. !RSA_sign_raw(rsa, siglen, sig, *siglen, rctx->tbuf, key_len,
  183. RSA_NO_PADDING)) {
  184. return 0;
  185. }
  186. return 1;
  187. default:
  188. return 0;
  189. }
  190. }
  191. return RSA_sign_raw(rsa, siglen, sig, *siglen, tbs, tbslen, rctx->pad_mode);
  192. }
  193. static int pkey_rsa_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig,
  194. size_t siglen, const uint8_t *tbs,
  195. size_t tbslen) {
  196. RSA_PKEY_CTX *rctx = ctx->data;
  197. RSA *rsa = ctx->pkey->pkey.rsa;
  198. size_t rslen;
  199. const size_t key_len = EVP_PKEY_size(ctx->pkey);
  200. if (rctx->md) {
  201. switch (rctx->pad_mode) {
  202. case RSA_PKCS1_PADDING:
  203. return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen, sig, siglen, rsa);
  204. case RSA_PKCS1_PSS_PADDING:
  205. if (tbslen != EVP_MD_size(rctx->md)) {
  206. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_DIGEST_LENGTH);
  207. return 0;
  208. }
  209. if (!setup_tbuf(rctx, ctx) ||
  210. !RSA_verify_raw(rsa, &rslen, rctx->tbuf, key_len, sig, siglen,
  211. RSA_NO_PADDING) ||
  212. !RSA_verify_PKCS1_PSS_mgf1(rsa, tbs, rctx->md, rctx->mgf1md,
  213. rctx->tbuf, rctx->saltlen)) {
  214. return 0;
  215. }
  216. return 1;
  217. default:
  218. return 0;
  219. }
  220. }
  221. if (!setup_tbuf(rctx, ctx) ||
  222. !RSA_verify_raw(rsa, &rslen, rctx->tbuf, key_len, sig, siglen,
  223. rctx->pad_mode) ||
  224. rslen != tbslen ||
  225. CRYPTO_memcmp(tbs, rctx->tbuf, rslen) != 0) {
  226. return 0;
  227. }
  228. return 1;
  229. }
  230. static int pkey_rsa_verify_recover(EVP_PKEY_CTX *ctx, uint8_t *out,
  231. size_t *out_len, const uint8_t *sig,
  232. size_t sig_len) {
  233. RSA_PKEY_CTX *rctx = ctx->data;
  234. RSA *rsa = ctx->pkey->pkey.rsa;
  235. const size_t key_len = EVP_PKEY_size(ctx->pkey);
  236. if (out == NULL) {
  237. *out_len = key_len;
  238. return 1;
  239. }
  240. if (*out_len < key_len) {
  241. OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
  242. return 0;
  243. }
  244. if (!setup_tbuf(rctx, ctx)) {
  245. return 0;
  246. }
  247. if (rctx->md == NULL) {
  248. const int ret = RSA_public_decrypt(sig_len, sig, rctx->tbuf,
  249. ctx->pkey->pkey.rsa, rctx->pad_mode);
  250. if (ret < 0) {
  251. return 0;
  252. }
  253. *out_len = ret;
  254. OPENSSL_memcpy(out, rctx->tbuf, *out_len);
  255. return 1;
  256. }
  257. if (rctx->pad_mode != RSA_PKCS1_PADDING) {
  258. return 0;
  259. }
  260. uint8_t *asn1_prefix;
  261. size_t asn1_prefix_len;
  262. int asn1_prefix_allocated;
  263. if (!RSA_add_pkcs1_prefix(&asn1_prefix, &asn1_prefix_len,
  264. &asn1_prefix_allocated, EVP_MD_type(rctx->md), NULL,
  265. 0)) {
  266. return 0;
  267. }
  268. size_t rslen;
  269. int ok = 1;
  270. if (!RSA_verify_raw(rsa, &rslen, rctx->tbuf, key_len, sig, sig_len,
  271. RSA_PKCS1_PADDING) ||
  272. rslen < asn1_prefix_len ||
  273. CRYPTO_memcmp(rctx->tbuf, asn1_prefix, asn1_prefix_len) != 0) {
  274. ok = 0;
  275. }
  276. if (asn1_prefix_allocated) {
  277. OPENSSL_free(asn1_prefix);
  278. }
  279. if (!ok) {
  280. return 0;
  281. }
  282. const size_t result_len = rslen - asn1_prefix_len;
  283. if (result_len != EVP_MD_size(rctx->md)) {
  284. return 0;
  285. }
  286. if (out != NULL) {
  287. OPENSSL_memcpy(out, rctx->tbuf + asn1_prefix_len, result_len);
  288. }
  289. *out_len = result_len;
  290. return 1;
  291. }
  292. static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *outlen,
  293. const uint8_t *in, size_t inlen) {
  294. RSA_PKEY_CTX *rctx = ctx->data;
  295. RSA *rsa = ctx->pkey->pkey.rsa;
  296. const size_t key_len = EVP_PKEY_size(ctx->pkey);
  297. if (!out) {
  298. *outlen = key_len;
  299. return 1;
  300. }
  301. if (*outlen < key_len) {
  302. OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
  303. return 0;
  304. }
  305. if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
  306. if (!setup_tbuf(rctx, ctx) ||
  307. !RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, key_len, in, inlen,
  308. rctx->oaep_label, rctx->oaep_labellen,
  309. rctx->md, rctx->mgf1md) ||
  310. !RSA_encrypt(rsa, outlen, out, *outlen, rctx->tbuf, key_len,
  311. RSA_NO_PADDING)) {
  312. return 0;
  313. }
  314. return 1;
  315. }
  316. return RSA_encrypt(rsa, outlen, out, *outlen, in, inlen, rctx->pad_mode);
  317. }
  318. static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx, uint8_t *out,
  319. size_t *outlen, const uint8_t *in,
  320. size_t inlen) {
  321. RSA_PKEY_CTX *rctx = ctx->data;
  322. RSA *rsa = ctx->pkey->pkey.rsa;
  323. const size_t key_len = EVP_PKEY_size(ctx->pkey);
  324. if (!out) {
  325. *outlen = key_len;
  326. return 1;
  327. }
  328. if (*outlen < key_len) {
  329. OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
  330. return 0;
  331. }
  332. if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
  333. size_t plaintext_len;
  334. int message_len;
  335. if (!setup_tbuf(rctx, ctx) ||
  336. !RSA_decrypt(rsa, &plaintext_len, rctx->tbuf, key_len, in, inlen,
  337. RSA_NO_PADDING)) {
  338. return 0;
  339. }
  340. message_len = RSA_padding_check_PKCS1_OAEP_mgf1(
  341. out, key_len, rctx->tbuf, plaintext_len, rctx->oaep_label,
  342. rctx->oaep_labellen, rctx->md, rctx->mgf1md);
  343. if (message_len < 0) {
  344. return 0;
  345. }
  346. *outlen = message_len;
  347. return 1;
  348. }
  349. return RSA_decrypt(rsa, outlen, out, key_len, in, inlen, rctx->pad_mode);
  350. }
  351. static int check_padding_md(const EVP_MD *md, int padding) {
  352. if (!md) {
  353. return 1;
  354. }
  355. if (padding == RSA_NO_PADDING) {
  356. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PADDING_MODE);
  357. return 0;
  358. }
  359. return 1;
  360. }
  361. static int is_known_padding(int padding_mode) {
  362. switch (padding_mode) {
  363. case RSA_PKCS1_PADDING:
  364. case RSA_NO_PADDING:
  365. case RSA_PKCS1_OAEP_PADDING:
  366. case RSA_PKCS1_PSS_PADDING:
  367. return 1;
  368. default:
  369. return 0;
  370. }
  371. }
  372. static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) {
  373. RSA_PKEY_CTX *rctx = ctx->data;
  374. switch (type) {
  375. case EVP_PKEY_CTRL_RSA_PADDING:
  376. if (!is_known_padding(p1) || !check_padding_md(rctx->md, p1) ||
  377. (p1 == RSA_PKCS1_PSS_PADDING &&
  378. 0 == (ctx->operation & (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY))) ||
  379. (p1 == RSA_PKCS1_OAEP_PADDING &&
  380. 0 == (ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))) {
  381. OPENSSL_PUT_ERROR(EVP, EVP_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
  382. return 0;
  383. }
  384. if ((p1 == RSA_PKCS1_PSS_PADDING || p1 == RSA_PKCS1_OAEP_PADDING) &&
  385. rctx->md == NULL) {
  386. rctx->md = EVP_sha1();
  387. }
  388. rctx->pad_mode = p1;
  389. return 1;
  390. case EVP_PKEY_CTRL_GET_RSA_PADDING:
  391. *(int *)p2 = rctx->pad_mode;
  392. return 1;
  393. case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
  394. case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
  395. if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
  396. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PSS_SALTLEN);
  397. return 0;
  398. }
  399. if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN) {
  400. *(int *)p2 = rctx->saltlen;
  401. } else {
  402. if (p1 < -2) {
  403. return 0;
  404. }
  405. rctx->saltlen = p1;
  406. }
  407. return 1;
  408. case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
  409. if (p1 < 256) {
  410. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_KEYBITS);
  411. return 0;
  412. }
  413. rctx->nbits = p1;
  414. return 1;
  415. case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
  416. if (!p2) {
  417. return 0;
  418. }
  419. BN_free(rctx->pub_exp);
  420. rctx->pub_exp = p2;
  421. return 1;
  422. case EVP_PKEY_CTRL_RSA_OAEP_MD:
  423. case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
  424. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  425. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PADDING_MODE);
  426. return 0;
  427. }
  428. if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD) {
  429. *(const EVP_MD **)p2 = rctx->md;
  430. } else {
  431. rctx->md = p2;
  432. }
  433. return 1;
  434. case EVP_PKEY_CTRL_MD:
  435. if (!check_padding_md(p2, rctx->pad_mode)) {
  436. return 0;
  437. }
  438. rctx->md = p2;
  439. return 1;
  440. case EVP_PKEY_CTRL_GET_MD:
  441. *(const EVP_MD **)p2 = rctx->md;
  442. return 1;
  443. case EVP_PKEY_CTRL_RSA_MGF1_MD:
  444. case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
  445. if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING &&
  446. rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  447. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_MGF1_MD);
  448. return 0;
  449. }
  450. if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
  451. if (rctx->mgf1md) {
  452. *(const EVP_MD **)p2 = rctx->mgf1md;
  453. } else {
  454. *(const EVP_MD **)p2 = rctx->md;
  455. }
  456. } else {
  457. rctx->mgf1md = p2;
  458. }
  459. return 1;
  460. case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
  461. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  462. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PADDING_MODE);
  463. return 0;
  464. }
  465. OPENSSL_free(rctx->oaep_label);
  466. if (p2 && p1 > 0) {
  467. rctx->oaep_label = p2;
  468. rctx->oaep_labellen = p1;
  469. } else {
  470. rctx->oaep_label = NULL;
  471. rctx->oaep_labellen = 0;
  472. }
  473. return 1;
  474. case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
  475. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  476. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PADDING_MODE);
  477. return 0;
  478. }
  479. CBS_init((CBS *)p2, rctx->oaep_label, rctx->oaep_labellen);
  480. return 1;
  481. default:
  482. OPENSSL_PUT_ERROR(EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  483. return 0;
  484. }
  485. }
  486. static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
  487. RSA *rsa = NULL;
  488. RSA_PKEY_CTX *rctx = ctx->data;
  489. if (!rctx->pub_exp) {
  490. rctx->pub_exp = BN_new();
  491. if (!rctx->pub_exp || !BN_set_word(rctx->pub_exp, RSA_F4)) {
  492. return 0;
  493. }
  494. }
  495. rsa = RSA_new();
  496. if (!rsa) {
  497. return 0;
  498. }
  499. if (!RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, NULL)) {
  500. RSA_free(rsa);
  501. return 0;
  502. }
  503. EVP_PKEY_assign_RSA(pkey, rsa);
  504. return 1;
  505. }
  506. const EVP_PKEY_METHOD rsa_pkey_meth = {
  507. EVP_PKEY_RSA,
  508. pkey_rsa_init,
  509. pkey_rsa_copy,
  510. pkey_rsa_cleanup,
  511. pkey_rsa_keygen,
  512. pkey_rsa_sign,
  513. pkey_rsa_verify,
  514. pkey_rsa_verify_recover,
  515. pkey_rsa_encrypt,
  516. pkey_rsa_decrypt,
  517. 0 /* derive */,
  518. pkey_rsa_ctrl,
  519. };
  520. int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int padding) {
  521. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, -1, EVP_PKEY_CTRL_RSA_PADDING,
  522. padding, NULL);
  523. }
  524. int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *out_padding) {
  525. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, -1, EVP_PKEY_CTRL_GET_RSA_PADDING,
  526. 0, out_padding);
  527. }
  528. int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int salt_len) {
  529. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  530. (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY),
  531. EVP_PKEY_CTRL_RSA_PSS_SALTLEN, salt_len, NULL);
  532. }
  533. int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int *out_salt_len) {
  534. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  535. (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY),
  536. EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN, 0, out_salt_len);
  537. }
  538. int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, int bits) {
  539. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
  540. EVP_PKEY_CTRL_RSA_KEYGEN_BITS, bits, NULL);
  541. }
  542. int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *e) {
  543. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
  544. EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, e);
  545. }
  546. int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
  547. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  548. EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)md);
  549. }
  550. int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
  551. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  552. EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void*) out_md);
  553. }
  554. int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
  555. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  556. EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
  557. EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void*) md);
  558. }
  559. int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
  560. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  561. EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
  562. EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void*) out_md);
  563. }
  564. int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, uint8_t *label,
  565. size_t label_len) {
  566. if (label_len > INT_MAX) {
  567. return 0;
  568. }
  569. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  570. EVP_PKEY_CTRL_RSA_OAEP_LABEL, (int)label_len,
  571. (void *)label);
  572. }
  573. int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx,
  574. const uint8_t **out_label) {
  575. CBS label;
  576. if (!EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  577. EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, 0, &label)) {
  578. return -1;
  579. }
  580. if (CBS_len(&label) > INT_MAX) {
  581. OPENSSL_PUT_ERROR(EVP, ERR_R_OVERFLOW);
  582. return -1;
  583. }
  584. *out_label = CBS_data(&label);
  585. return (int)CBS_len(&label);
  586. }