rsa_kem.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * Copyright 2020-2021 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 "e_os.h" /* strcasecmp */
  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 "prov/provider_ctx.h"
  25. #include "prov/implementations.h"
  26. #include "prov/securitycheck.h"
  27. static OSSL_FUNC_kem_newctx_fn rsakem_newctx;
  28. static OSSL_FUNC_kem_encapsulate_init_fn rsakem_encapsulate_init;
  29. static OSSL_FUNC_kem_encapsulate_fn rsakem_generate;
  30. static OSSL_FUNC_kem_decapsulate_init_fn rsakem_decapsulate_init;
  31. static OSSL_FUNC_kem_decapsulate_fn rsakem_recover;
  32. static OSSL_FUNC_kem_freectx_fn rsakem_freectx;
  33. static OSSL_FUNC_kem_dupctx_fn rsakem_dupctx;
  34. static OSSL_FUNC_kem_get_ctx_params_fn rsakem_get_ctx_params;
  35. static OSSL_FUNC_kem_gettable_ctx_params_fn rsakem_gettable_ctx_params;
  36. static OSSL_FUNC_kem_set_ctx_params_fn rsakem_set_ctx_params;
  37. static OSSL_FUNC_kem_settable_ctx_params_fn rsakem_settable_ctx_params;
  38. /*
  39. * Only the KEM for RSASVE as defined in SP800-56b r2 is implemented
  40. * currently.
  41. */
  42. #define KEM_OP_UNDEFINED -1
  43. #define KEM_OP_RSASVE 0
  44. /*
  45. * What's passed as an actual key is defined by the KEYMGMT interface.
  46. * We happen to know that our KEYMGMT simply passes RSA structures, so
  47. * we use that here too.
  48. */
  49. typedef struct {
  50. OSSL_LIB_CTX *libctx;
  51. RSA *rsa;
  52. int op;
  53. } PROV_RSA_CTX;
  54. static const OSSL_ITEM rsakem_opname_id_map[] = {
  55. { KEM_OP_RSASVE, OSSL_KEM_PARAM_OPERATION_RSASVE },
  56. };
  57. static int name2id(const char *name, const OSSL_ITEM *map, size_t sz)
  58. {
  59. size_t i;
  60. if (name == NULL)
  61. return -1;
  62. for (i = 0; i < sz; ++i) {
  63. if (strcasecmp(map[i].ptr, name) == 0)
  64. return map[i].id;
  65. }
  66. return -1;
  67. }
  68. static int rsakem_opname2id(const char *name)
  69. {
  70. return name2id(name, rsakem_opname_id_map, OSSL_NELEM(rsakem_opname_id_map));
  71. }
  72. static void *rsakem_newctx(void *provctx)
  73. {
  74. PROV_RSA_CTX *prsactx = OPENSSL_zalloc(sizeof(PROV_RSA_CTX));
  75. if (prsactx == NULL)
  76. return NULL;
  77. prsactx->libctx = PROV_LIBCTX_OF(provctx);
  78. prsactx->op = KEM_OP_UNDEFINED;
  79. return prsactx;
  80. }
  81. static void rsakem_freectx(void *vprsactx)
  82. {
  83. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  84. RSA_free(prsactx->rsa);
  85. OPENSSL_free(prsactx);
  86. }
  87. static void *rsakem_dupctx(void *vprsactx)
  88. {
  89. PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx;
  90. PROV_RSA_CTX *dstctx;
  91. dstctx = OPENSSL_zalloc(sizeof(*srcctx));
  92. if (dstctx == NULL)
  93. return NULL;
  94. *dstctx = *srcctx;
  95. if (dstctx->rsa != NULL && !RSA_up_ref(dstctx->rsa)) {
  96. OPENSSL_free(dstctx);
  97. return NULL;
  98. }
  99. return dstctx;
  100. }
  101. static int rsakem_init(void *vprsactx, void *vrsa,
  102. const OSSL_PARAM params[], int operation)
  103. {
  104. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  105. if (prsactx == NULL || vrsa == NULL)
  106. return 0;
  107. if (!ossl_rsa_check_key(vrsa, operation))
  108. return 0;
  109. if (!RSA_up_ref(vrsa))
  110. return 0;
  111. RSA_free(prsactx->rsa);
  112. prsactx->rsa = vrsa;
  113. return rsakem_set_ctx_params(prsactx, params);
  114. }
  115. static int rsakem_encapsulate_init(void *vprsactx, void *vrsa,
  116. const OSSL_PARAM params[])
  117. {
  118. return rsakem_init(vprsactx, vrsa, params, EVP_PKEY_OP_ENCAPSULATE);
  119. }
  120. static int rsakem_decapsulate_init(void *vprsactx, void *vrsa,
  121. const OSSL_PARAM params[])
  122. {
  123. return rsakem_init(vprsactx, vrsa, params, EVP_PKEY_OP_DECAPSULATE);
  124. }
  125. static int rsakem_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
  126. {
  127. PROV_RSA_CTX *ctx = (PROV_RSA_CTX *)vprsactx;
  128. return ctx != NULL;
  129. }
  130. static const OSSL_PARAM known_gettable_rsakem_ctx_params[] = {
  131. OSSL_PARAM_END
  132. };
  133. static const OSSL_PARAM *rsakem_gettable_ctx_params(ossl_unused void *vprsactx,
  134. ossl_unused void *provctx)
  135. {
  136. return known_gettable_rsakem_ctx_params;
  137. }
  138. static int rsakem_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
  139. {
  140. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  141. const OSSL_PARAM *p;
  142. int op;
  143. if (prsactx == NULL)
  144. return 0;
  145. if (params == NULL)
  146. return 1;
  147. p = OSSL_PARAM_locate_const(params, OSSL_KEM_PARAM_OPERATION);
  148. if (p != NULL) {
  149. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  150. return 0;
  151. op = rsakem_opname2id(p->data);
  152. if (op < 0)
  153. return 0;
  154. prsactx->op = op;
  155. }
  156. return 1;
  157. }
  158. static const OSSL_PARAM known_settable_rsakem_ctx_params[] = {
  159. OSSL_PARAM_utf8_string(OSSL_KEM_PARAM_OPERATION, NULL, 0),
  160. OSSL_PARAM_END
  161. };
  162. static const OSSL_PARAM *rsakem_settable_ctx_params(ossl_unused void *vprsactx,
  163. ossl_unused void *provctx)
  164. {
  165. return known_settable_rsakem_ctx_params;
  166. }
  167. /*
  168. * NIST.SP.800-56Br2
  169. * 7.2.1.2 RSASVE Generate Operation (RSASVE.GENERATE).
  170. *
  171. * Generate a random in the range 1 < z < (n – 1)
  172. */
  173. static int rsasve_gen_rand_bytes(RSA *rsa_pub,
  174. unsigned char *out, int outlen)
  175. {
  176. int ret = 0;
  177. BN_CTX *bnctx;
  178. BIGNUM *z, *nminus3;
  179. bnctx = BN_CTX_secure_new_ex(ossl_rsa_get0_libctx(rsa_pub));
  180. if (bnctx == NULL)
  181. return 0;
  182. /*
  183. * Generate a random in the range 1 < z < (n – 1).
  184. * Since BN_priv_rand_range_ex() returns a value in range 0 <= r < max
  185. * We can achieve this by adding 2.. but then we need to subtract 3 from
  186. * the upper bound i.e: 2 + (0 <= r < (n - 3))
  187. */
  188. BN_CTX_start(bnctx);
  189. nminus3 = BN_CTX_get(bnctx);
  190. z = BN_CTX_get(bnctx);
  191. ret = (z != NULL
  192. && (BN_copy(nminus3, RSA_get0_n(rsa_pub)) != NULL)
  193. && BN_sub_word(nminus3, 3)
  194. && BN_priv_rand_range_ex(z, nminus3, bnctx)
  195. && BN_add_word(z, 2)
  196. && (BN_bn2binpad(z, out, outlen) == outlen));
  197. BN_CTX_end(bnctx);
  198. BN_CTX_free(bnctx);
  199. return ret;
  200. }
  201. /*
  202. * NIST.SP.800-56Br2
  203. * 7.2.1.2 RSASVE Generate Operation (RSASVE.GENERATE).
  204. */
  205. static int rsasve_generate(PROV_RSA_CTX *prsactx,
  206. unsigned char *out, size_t *outlen,
  207. unsigned char *secret, size_t *secretlen)
  208. {
  209. int ret;
  210. size_t nlen;
  211. /* Step (1): nlen = Ceil(len(n)/8) */
  212. nlen = RSA_size(prsactx->rsa);
  213. if (out == NULL) {
  214. if (nlen == 0) {
  215. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
  216. return 0;
  217. }
  218. if (outlen == NULL && secretlen == NULL)
  219. return 0;
  220. if (outlen != NULL)
  221. *outlen = nlen;
  222. if (secretlen != NULL)
  223. *secretlen = nlen;
  224. return 1;
  225. }
  226. /*
  227. * Step (2): Generate a random byte string z of nlen bytes where
  228. * 1 < z < n - 1
  229. */
  230. if (!rsasve_gen_rand_bytes(prsactx->rsa, secret, nlen))
  231. return 0;
  232. /* Step(3): out = RSAEP((n,e), z) */
  233. ret = RSA_public_encrypt(nlen, secret, out, prsactx->rsa, RSA_NO_PADDING);
  234. if (ret) {
  235. ret = 1;
  236. if (outlen != NULL)
  237. *outlen = nlen;
  238. if (secretlen != NULL)
  239. *secretlen = nlen;
  240. } else {
  241. OPENSSL_cleanse(secret, nlen);
  242. }
  243. return ret;
  244. }
  245. /*
  246. * NIST.SP.800-56Br2
  247. * 7.2.1.3 RSASVE Recovery Operation (RSASVE.RECOVER).
  248. */
  249. static int rsasve_recover(PROV_RSA_CTX *prsactx,
  250. unsigned char *out, size_t *outlen,
  251. const unsigned char *in, size_t inlen)
  252. {
  253. size_t nlen;
  254. /* Step (1): get the byte length of n */
  255. nlen = RSA_size(prsactx->rsa);
  256. if (out == NULL) {
  257. if (nlen == 0) {
  258. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
  259. return 0;
  260. }
  261. *outlen = nlen;
  262. return 1;
  263. }
  264. /* Step (2): check the input ciphertext 'inlen' matches the nlen */
  265. if (inlen != nlen) {
  266. ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
  267. return 0;
  268. }
  269. /* Step (3): out = RSADP((n,d), in) */
  270. return (RSA_private_decrypt(inlen, in, out, prsactx->rsa, RSA_NO_PADDING) > 0);
  271. }
  272. static int rsakem_generate(void *vprsactx, unsigned char *out, size_t *outlen,
  273. unsigned char *secret, size_t *secretlen)
  274. {
  275. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  276. switch (prsactx->op) {
  277. case KEM_OP_RSASVE:
  278. return rsasve_generate(prsactx, out, outlen, secret, secretlen);
  279. default:
  280. return -2;
  281. }
  282. }
  283. static int rsakem_recover(void *vprsactx, unsigned char *out, size_t *outlen,
  284. const unsigned char *in, size_t inlen)
  285. {
  286. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  287. switch (prsactx->op) {
  288. case KEM_OP_RSASVE:
  289. return rsasve_recover(prsactx, out, outlen, in, inlen);
  290. default:
  291. return -2;
  292. }
  293. }
  294. const OSSL_DISPATCH ossl_rsa_asym_kem_functions[] = {
  295. { OSSL_FUNC_KEM_NEWCTX, (void (*)(void))rsakem_newctx },
  296. { OSSL_FUNC_KEM_ENCAPSULATE_INIT,
  297. (void (*)(void))rsakem_encapsulate_init },
  298. { OSSL_FUNC_KEM_ENCAPSULATE, (void (*)(void))rsakem_generate },
  299. { OSSL_FUNC_KEM_DECAPSULATE_INIT,
  300. (void (*)(void))rsakem_decapsulate_init },
  301. { OSSL_FUNC_KEM_DECAPSULATE, (void (*)(void))rsakem_recover },
  302. { OSSL_FUNC_KEM_FREECTX, (void (*)(void))rsakem_freectx },
  303. { OSSL_FUNC_KEM_DUPCTX, (void (*)(void))rsakem_dupctx },
  304. { OSSL_FUNC_KEM_GET_CTX_PARAMS,
  305. (void (*)(void))rsakem_get_ctx_params },
  306. { OSSL_FUNC_KEM_GETTABLE_CTX_PARAMS,
  307. (void (*)(void))rsakem_gettable_ctx_params },
  308. { OSSL_FUNC_KEM_SET_CTX_PARAMS,
  309. (void (*)(void))rsakem_set_ctx_params },
  310. { OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS,
  311. (void (*)(void))rsakem_settable_ctx_params },
  312. { 0, NULL }
  313. };