1
0

rsa_kem.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. /*
  10. * RSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include "internal/nelem.h"
  15. #include <openssl/crypto.h>
  16. #include <openssl/evp.h>
  17. #include <openssl/core_dispatch.h>
  18. #include <openssl/core_names.h>
  19. #include <openssl/rsa.h>
  20. #include <openssl/params.h>
  21. #include <openssl/err.h>
  22. #include "crypto/rsa.h"
  23. #include <openssl/proverr.h>
  24. #include "internal/nelem.h"
  25. #include "prov/provider_ctx.h"
  26. #include "prov/implementations.h"
  27. #include "prov/securitycheck.h"
  28. static OSSL_FUNC_kem_newctx_fn rsakem_newctx;
  29. static OSSL_FUNC_kem_encapsulate_init_fn rsakem_encapsulate_init;
  30. static OSSL_FUNC_kem_encapsulate_fn rsakem_generate;
  31. static OSSL_FUNC_kem_decapsulate_init_fn rsakem_decapsulate_init;
  32. static OSSL_FUNC_kem_decapsulate_fn rsakem_recover;
  33. static OSSL_FUNC_kem_freectx_fn rsakem_freectx;
  34. static OSSL_FUNC_kem_dupctx_fn rsakem_dupctx;
  35. static OSSL_FUNC_kem_get_ctx_params_fn rsakem_get_ctx_params;
  36. static OSSL_FUNC_kem_gettable_ctx_params_fn rsakem_gettable_ctx_params;
  37. static OSSL_FUNC_kem_set_ctx_params_fn rsakem_set_ctx_params;
  38. static OSSL_FUNC_kem_settable_ctx_params_fn rsakem_settable_ctx_params;
  39. /*
  40. * Only the KEM for RSASVE as defined in SP800-56b r2 is implemented
  41. * currently.
  42. */
  43. #define KEM_OP_UNDEFINED -1
  44. #define KEM_OP_RSASVE 0
  45. /*
  46. * What's passed as an actual key is defined by the KEYMGMT interface.
  47. * We happen to know that our KEYMGMT simply passes RSA structures, so
  48. * we use that here too.
  49. */
  50. typedef struct {
  51. OSSL_LIB_CTX *libctx;
  52. RSA *rsa;
  53. int op;
  54. } PROV_RSA_CTX;
  55. static const OSSL_ITEM rsakem_opname_id_map[] = {
  56. { KEM_OP_RSASVE, OSSL_KEM_PARAM_OPERATION_RSASVE },
  57. };
  58. static int name2id(const char *name, const OSSL_ITEM *map, size_t sz)
  59. {
  60. size_t i;
  61. if (name == NULL)
  62. return -1;
  63. for (i = 0; i < sz; ++i) {
  64. if (OPENSSL_strcasecmp(map[i].ptr, name) == 0)
  65. return map[i].id;
  66. }
  67. return -1;
  68. }
  69. static int rsakem_opname2id(const char *name)
  70. {
  71. return name2id(name, rsakem_opname_id_map, OSSL_NELEM(rsakem_opname_id_map));
  72. }
  73. static void *rsakem_newctx(void *provctx)
  74. {
  75. PROV_RSA_CTX *prsactx = OPENSSL_zalloc(sizeof(PROV_RSA_CTX));
  76. if (prsactx == NULL)
  77. return NULL;
  78. prsactx->libctx = PROV_LIBCTX_OF(provctx);
  79. prsactx->op = KEM_OP_UNDEFINED;
  80. return prsactx;
  81. }
  82. static void rsakem_freectx(void *vprsactx)
  83. {
  84. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  85. RSA_free(prsactx->rsa);
  86. OPENSSL_free(prsactx);
  87. }
  88. static void *rsakem_dupctx(void *vprsactx)
  89. {
  90. PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx;
  91. PROV_RSA_CTX *dstctx;
  92. dstctx = OPENSSL_zalloc(sizeof(*srcctx));
  93. if (dstctx == NULL)
  94. return NULL;
  95. *dstctx = *srcctx;
  96. if (dstctx->rsa != NULL && !RSA_up_ref(dstctx->rsa)) {
  97. OPENSSL_free(dstctx);
  98. return NULL;
  99. }
  100. return dstctx;
  101. }
  102. static int rsakem_init(void *vprsactx, void *vrsa,
  103. const OSSL_PARAM params[], int operation)
  104. {
  105. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  106. if (prsactx == NULL || vrsa == NULL)
  107. return 0;
  108. if (!ossl_rsa_check_key(prsactx->libctx, vrsa, operation))
  109. return 0;
  110. if (!RSA_up_ref(vrsa))
  111. return 0;
  112. RSA_free(prsactx->rsa);
  113. prsactx->rsa = vrsa;
  114. return rsakem_set_ctx_params(prsactx, params);
  115. }
  116. static int rsakem_encapsulate_init(void *vprsactx, void *vrsa,
  117. const OSSL_PARAM params[])
  118. {
  119. return rsakem_init(vprsactx, vrsa, params, EVP_PKEY_OP_ENCAPSULATE);
  120. }
  121. static int rsakem_decapsulate_init(void *vprsactx, void *vrsa,
  122. const OSSL_PARAM params[])
  123. {
  124. return rsakem_init(vprsactx, vrsa, params, EVP_PKEY_OP_DECAPSULATE);
  125. }
  126. static int rsakem_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
  127. {
  128. PROV_RSA_CTX *ctx = (PROV_RSA_CTX *)vprsactx;
  129. return ctx != NULL;
  130. }
  131. static const OSSL_PARAM known_gettable_rsakem_ctx_params[] = {
  132. OSSL_PARAM_END
  133. };
  134. static const OSSL_PARAM *rsakem_gettable_ctx_params(ossl_unused void *vprsactx,
  135. ossl_unused void *provctx)
  136. {
  137. return known_gettable_rsakem_ctx_params;
  138. }
  139. static int rsakem_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
  140. {
  141. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  142. const OSSL_PARAM *p;
  143. int op;
  144. if (prsactx == NULL)
  145. return 0;
  146. if (params == NULL)
  147. return 1;
  148. p = OSSL_PARAM_locate_const(params, OSSL_KEM_PARAM_OPERATION);
  149. if (p != NULL) {
  150. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  151. return 0;
  152. op = rsakem_opname2id(p->data);
  153. if (op < 0)
  154. return 0;
  155. prsactx->op = op;
  156. }
  157. return 1;
  158. }
  159. static const OSSL_PARAM known_settable_rsakem_ctx_params[] = {
  160. OSSL_PARAM_utf8_string(OSSL_KEM_PARAM_OPERATION, NULL, 0),
  161. OSSL_PARAM_END
  162. };
  163. static const OSSL_PARAM *rsakem_settable_ctx_params(ossl_unused void *vprsactx,
  164. ossl_unused void *provctx)
  165. {
  166. return known_settable_rsakem_ctx_params;
  167. }
  168. /*
  169. * NIST.SP.800-56Br2
  170. * 7.2.1.2 RSASVE Generate Operation (RSASVE.GENERATE).
  171. *
  172. * Generate a random in the range 1 < z < (n – 1)
  173. */
  174. static int rsasve_gen_rand_bytes(RSA *rsa_pub,
  175. unsigned char *out, int outlen)
  176. {
  177. int ret = 0;
  178. BN_CTX *bnctx;
  179. BIGNUM *z, *nminus3;
  180. bnctx = BN_CTX_secure_new_ex(ossl_rsa_get0_libctx(rsa_pub));
  181. if (bnctx == NULL)
  182. return 0;
  183. /*
  184. * Generate a random in the range 1 < z < (n – 1).
  185. * Since BN_priv_rand_range_ex() returns a value in range 0 <= r < max
  186. * We can achieve this by adding 2.. but then we need to subtract 3 from
  187. * the upper bound i.e: 2 + (0 <= r < (n - 3))
  188. */
  189. BN_CTX_start(bnctx);
  190. nminus3 = BN_CTX_get(bnctx);
  191. z = BN_CTX_get(bnctx);
  192. ret = (z != NULL
  193. && (BN_copy(nminus3, RSA_get0_n(rsa_pub)) != NULL)
  194. && BN_sub_word(nminus3, 3)
  195. && BN_priv_rand_range_ex(z, nminus3, 0, bnctx)
  196. && BN_add_word(z, 2)
  197. && (BN_bn2binpad(z, out, outlen) == outlen));
  198. BN_CTX_end(bnctx);
  199. BN_CTX_free(bnctx);
  200. return ret;
  201. }
  202. /*
  203. * NIST.SP.800-56Br2
  204. * 7.2.1.2 RSASVE Generate Operation (RSASVE.GENERATE).
  205. */
  206. static int rsasve_generate(PROV_RSA_CTX *prsactx,
  207. unsigned char *out, size_t *outlen,
  208. unsigned char *secret, size_t *secretlen)
  209. {
  210. int ret;
  211. size_t nlen;
  212. /* Step (1): nlen = Ceil(len(n)/8) */
  213. nlen = RSA_size(prsactx->rsa);
  214. if (out == NULL) {
  215. if (nlen == 0) {
  216. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
  217. return 0;
  218. }
  219. if (outlen == NULL && secretlen == NULL)
  220. return 0;
  221. if (outlen != NULL)
  222. *outlen = nlen;
  223. if (secretlen != NULL)
  224. *secretlen = nlen;
  225. return 1;
  226. }
  227. /*
  228. * If outlen is specified, then it must report the length
  229. * of the out buffer on input so that we can confirm
  230. * its size is sufficent for encapsulation
  231. */
  232. if (outlen != NULL && *outlen < nlen) {
  233. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH);
  234. return 0;
  235. }
  236. /*
  237. * Step (2): Generate a random byte string z of nlen bytes where
  238. * 1 < z < n - 1
  239. */
  240. if (!rsasve_gen_rand_bytes(prsactx->rsa, secret, nlen))
  241. return 0;
  242. /* Step(3): out = RSAEP((n,e), z) */
  243. ret = RSA_public_encrypt(nlen, secret, out, prsactx->rsa, RSA_NO_PADDING);
  244. if (ret) {
  245. ret = 1;
  246. if (outlen != NULL)
  247. *outlen = nlen;
  248. if (secretlen != NULL)
  249. *secretlen = nlen;
  250. } else {
  251. OPENSSL_cleanse(secret, nlen);
  252. }
  253. return ret;
  254. }
  255. /**
  256. * rsasve_recover - Recovers a secret value from ciphertext using an RSA
  257. * private key. Once, recovered, the secret value is considered to be a
  258. * shared secret. Algorithm is preformed as per
  259. * NIST SP 800-56B Rev 2
  260. * 7.2.1.3 RSASVE Recovery Operation (RSASVE.RECOVER).
  261. *
  262. * This function performs RSA decryption using the private key from the
  263. * provided RSA context (`prsactx`). It takes the input ciphertext, decrypts
  264. * it, and writes the decrypted message to the output buffer.
  265. *
  266. * @prsactx: The RSA context containing the private key.
  267. * @out: The output buffer to store the decrypted message.
  268. * @outlen: On input, the size of the output buffer. On successful
  269. * completion, the actual length of the decrypted message.
  270. * @in: The input buffer containing the ciphertext to be decrypted.
  271. * @inlen: The length of the input ciphertext in bytes.
  272. *
  273. * Returns 1 on success, or 0 on error. In case of error, appropriate
  274. * error messages are raised using the ERR_raise function.
  275. */
  276. static int rsasve_recover(PROV_RSA_CTX *prsactx,
  277. unsigned char *out, size_t *outlen,
  278. const unsigned char *in, size_t inlen)
  279. {
  280. size_t nlen;
  281. int ret;
  282. /* Step (1): get the byte length of n */
  283. nlen = RSA_size(prsactx->rsa);
  284. if (out == NULL) {
  285. if (nlen == 0) {
  286. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
  287. return 0;
  288. }
  289. *outlen = nlen;
  290. return 1;
  291. }
  292. /*
  293. * Step (2): check the input ciphertext 'inlen' matches the nlen
  294. * and that outlen is at least nlen bytes
  295. */
  296. if (inlen != nlen) {
  297. ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
  298. return 0;
  299. }
  300. /*
  301. * If outlen is specified, then it must report the length
  302. * of the out buffer, so that we can confirm that it is of
  303. * sufficient size to hold the output of decapsulation
  304. */
  305. if (outlen != NULL && *outlen < nlen) {
  306. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH);
  307. return 0;
  308. }
  309. /* Step (3): out = RSADP((n,d), in) */
  310. ret = RSA_private_decrypt(inlen, in, out, prsactx->rsa, RSA_NO_PADDING);
  311. if (ret > 0 && outlen != NULL)
  312. *outlen = ret;
  313. return ret > 0;
  314. }
  315. static int rsakem_generate(void *vprsactx, unsigned char *out, size_t *outlen,
  316. unsigned char *secret, size_t *secretlen)
  317. {
  318. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  319. switch (prsactx->op) {
  320. case KEM_OP_RSASVE:
  321. return rsasve_generate(prsactx, out, outlen, secret, secretlen);
  322. default:
  323. return -2;
  324. }
  325. }
  326. static int rsakem_recover(void *vprsactx, unsigned char *out, size_t *outlen,
  327. const unsigned char *in, size_t inlen)
  328. {
  329. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  330. switch (prsactx->op) {
  331. case KEM_OP_RSASVE:
  332. return rsasve_recover(prsactx, out, outlen, in, inlen);
  333. default:
  334. return -2;
  335. }
  336. }
  337. const OSSL_DISPATCH ossl_rsa_asym_kem_functions[] = {
  338. { OSSL_FUNC_KEM_NEWCTX, (void (*)(void))rsakem_newctx },
  339. { OSSL_FUNC_KEM_ENCAPSULATE_INIT,
  340. (void (*)(void))rsakem_encapsulate_init },
  341. { OSSL_FUNC_KEM_ENCAPSULATE, (void (*)(void))rsakem_generate },
  342. { OSSL_FUNC_KEM_DECAPSULATE_INIT,
  343. (void (*)(void))rsakem_decapsulate_init },
  344. { OSSL_FUNC_KEM_DECAPSULATE, (void (*)(void))rsakem_recover },
  345. { OSSL_FUNC_KEM_FREECTX, (void (*)(void))rsakem_freectx },
  346. { OSSL_FUNC_KEM_DUPCTX, (void (*)(void))rsakem_dupctx },
  347. { OSSL_FUNC_KEM_GET_CTX_PARAMS,
  348. (void (*)(void))rsakem_get_ctx_params },
  349. { OSSL_FUNC_KEM_GETTABLE_CTX_PARAMS,
  350. (void (*)(void))rsakem_gettable_ctx_params },
  351. { OSSL_FUNC_KEM_SET_CTX_PARAMS,
  352. (void (*)(void))rsakem_set_ctx_params },
  353. { OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS,
  354. (void (*)(void))rsakem_settable_ctx_params },
  355. OSSL_DISPATCH_END
  356. };