2
0

rsa_kem.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * Copyright 2020-2022 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. * Step (2): Generate a random byte string z of nlen bytes where
  229. * 1 < z < n - 1
  230. */
  231. if (!rsasve_gen_rand_bytes(prsactx->rsa, secret, nlen))
  232. return 0;
  233. /* Step(3): out = RSAEP((n,e), z) */
  234. ret = RSA_public_encrypt(nlen, secret, out, prsactx->rsa, RSA_NO_PADDING);
  235. if (ret) {
  236. ret = 1;
  237. if (outlen != NULL)
  238. *outlen = nlen;
  239. if (secretlen != NULL)
  240. *secretlen = nlen;
  241. } else {
  242. OPENSSL_cleanse(secret, nlen);
  243. }
  244. return ret;
  245. }
  246. /*
  247. * NIST.SP.800-56Br2
  248. * 7.2.1.3 RSASVE Recovery Operation (RSASVE.RECOVER).
  249. */
  250. static int rsasve_recover(PROV_RSA_CTX *prsactx,
  251. unsigned char *out, size_t *outlen,
  252. const unsigned char *in, size_t inlen)
  253. {
  254. size_t nlen;
  255. /* Step (1): get the byte length of n */
  256. nlen = RSA_size(prsactx->rsa);
  257. if (out == NULL) {
  258. if (nlen == 0) {
  259. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
  260. return 0;
  261. }
  262. *outlen = nlen;
  263. return 1;
  264. }
  265. /* Step (2): check the input ciphertext 'inlen' matches the nlen */
  266. if (inlen != nlen) {
  267. ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
  268. return 0;
  269. }
  270. /* Step (3): out = RSADP((n,d), in) */
  271. return (RSA_private_decrypt(inlen, in, out, prsactx->rsa, RSA_NO_PADDING) > 0);
  272. }
  273. static int rsakem_generate(void *vprsactx, unsigned char *out, size_t *outlen,
  274. unsigned char *secret, size_t *secretlen)
  275. {
  276. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  277. switch (prsactx->op) {
  278. case KEM_OP_RSASVE:
  279. return rsasve_generate(prsactx, out, outlen, secret, secretlen);
  280. default:
  281. return -2;
  282. }
  283. }
  284. static int rsakem_recover(void *vprsactx, unsigned char *out, size_t *outlen,
  285. const unsigned char *in, size_t inlen)
  286. {
  287. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  288. switch (prsactx->op) {
  289. case KEM_OP_RSASVE:
  290. return rsasve_recover(prsactx, out, outlen, in, inlen);
  291. default:
  292. return -2;
  293. }
  294. }
  295. const OSSL_DISPATCH ossl_rsa_asym_kem_functions[] = {
  296. { OSSL_FUNC_KEM_NEWCTX, (void (*)(void))rsakem_newctx },
  297. { OSSL_FUNC_KEM_ENCAPSULATE_INIT,
  298. (void (*)(void))rsakem_encapsulate_init },
  299. { OSSL_FUNC_KEM_ENCAPSULATE, (void (*)(void))rsakem_generate },
  300. { OSSL_FUNC_KEM_DECAPSULATE_INIT,
  301. (void (*)(void))rsakem_decapsulate_init },
  302. { OSSL_FUNC_KEM_DECAPSULATE, (void (*)(void))rsakem_recover },
  303. { OSSL_FUNC_KEM_FREECTX, (void (*)(void))rsakem_freectx },
  304. { OSSL_FUNC_KEM_DUPCTX, (void (*)(void))rsakem_dupctx },
  305. { OSSL_FUNC_KEM_GET_CTX_PARAMS,
  306. (void (*)(void))rsakem_get_ctx_params },
  307. { OSSL_FUNC_KEM_GETTABLE_CTX_PARAMS,
  308. (void (*)(void))rsakem_gettable_ctx_params },
  309. { OSSL_FUNC_KEM_SET_CTX_PARAMS,
  310. (void (*)(void))rsakem_set_ctx_params },
  311. { OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS,
  312. (void (*)(void))rsakem_settable_ctx_params },
  313. OSSL_DISPATCH_END
  314. };