ec_kmeth.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Copyright 2015-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. * ECDH and ECDSA low level APIs are deprecated for public use, but still ok
  11. * for internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <string.h>
  15. #include <openssl/ec.h>
  16. #ifndef FIPS_MODULE
  17. # include <openssl/engine.h>
  18. #endif
  19. #include <openssl/err.h>
  20. #include "ec_local.h"
  21. static const EC_KEY_METHOD openssl_ec_key_method = {
  22. "OpenSSL EC_KEY method",
  23. 0,
  24. 0,0,0,0,0,0,
  25. ossl_ec_key_gen,
  26. ossl_ecdh_compute_key,
  27. ossl_ecdsa_sign,
  28. ossl_ecdsa_sign_setup,
  29. ossl_ecdsa_sign_sig,
  30. ossl_ecdsa_verify,
  31. ossl_ecdsa_verify_sig
  32. };
  33. static const EC_KEY_METHOD *default_ec_key_meth = &openssl_ec_key_method;
  34. const EC_KEY_METHOD *EC_KEY_OpenSSL(void)
  35. {
  36. return &openssl_ec_key_method;
  37. }
  38. const EC_KEY_METHOD *EC_KEY_get_default_method(void)
  39. {
  40. return default_ec_key_meth;
  41. }
  42. void EC_KEY_set_default_method(const EC_KEY_METHOD *meth)
  43. {
  44. if (meth == NULL)
  45. default_ec_key_meth = &openssl_ec_key_method;
  46. else
  47. default_ec_key_meth = meth;
  48. }
  49. const EC_KEY_METHOD *EC_KEY_get_method(const EC_KEY *key)
  50. {
  51. return key->meth;
  52. }
  53. int EC_KEY_set_method(EC_KEY *key, const EC_KEY_METHOD *meth)
  54. {
  55. void (*finish)(EC_KEY *key) = key->meth->finish;
  56. if (finish != NULL)
  57. finish(key);
  58. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  59. ENGINE_finish(key->engine);
  60. key->engine = NULL;
  61. #endif
  62. key->meth = meth;
  63. if (meth->init != NULL)
  64. return meth->init(key);
  65. return 1;
  66. }
  67. EC_KEY *ossl_ec_key_new_method_int(OSSL_LIB_CTX *libctx, const char *propq,
  68. ENGINE *engine)
  69. {
  70. EC_KEY *ret = OPENSSL_zalloc(sizeof(*ret));
  71. if (ret == NULL)
  72. return NULL;
  73. if (!CRYPTO_NEW_REF(&ret->references, 1)) {
  74. OPENSSL_free(ret);
  75. return NULL;
  76. }
  77. ret->libctx = libctx;
  78. if (propq != NULL) {
  79. ret->propq = OPENSSL_strdup(propq);
  80. if (ret->propq == NULL)
  81. goto err;
  82. }
  83. ret->meth = EC_KEY_get_default_method();
  84. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  85. if (engine != NULL) {
  86. if (!ENGINE_init(engine)) {
  87. ERR_raise(ERR_LIB_EC, ERR_R_ENGINE_LIB);
  88. goto err;
  89. }
  90. ret->engine = engine;
  91. } else
  92. ret->engine = ENGINE_get_default_EC();
  93. if (ret->engine != NULL) {
  94. ret->meth = ENGINE_get_EC(ret->engine);
  95. if (ret->meth == NULL) {
  96. ERR_raise(ERR_LIB_EC, ERR_R_ENGINE_LIB);
  97. goto err;
  98. }
  99. }
  100. #endif
  101. ret->version = 1;
  102. ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
  103. /* No ex_data inside the FIPS provider */
  104. #ifndef FIPS_MODULE
  105. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data)) {
  106. ERR_raise(ERR_LIB_EC, ERR_R_CRYPTO_LIB);
  107. goto err;
  108. }
  109. #endif
  110. if (ret->meth->init != NULL && ret->meth->init(ret) == 0) {
  111. ERR_raise(ERR_LIB_EC, ERR_R_INIT_FAIL);
  112. goto err;
  113. }
  114. return ret;
  115. err:
  116. EC_KEY_free(ret);
  117. return NULL;
  118. }
  119. #ifndef FIPS_MODULE
  120. EC_KEY *EC_KEY_new_method(ENGINE *engine)
  121. {
  122. return ossl_ec_key_new_method_int(NULL, NULL, engine);
  123. }
  124. #endif
  125. int ECDH_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,
  126. const EC_KEY *eckey,
  127. void *(*KDF) (const void *in, size_t inlen, void *out,
  128. size_t *outlen))
  129. {
  130. unsigned char *sec = NULL;
  131. size_t seclen;
  132. if (eckey->meth->compute_key == NULL) {
  133. ERR_raise(ERR_LIB_EC, EC_R_OPERATION_NOT_SUPPORTED);
  134. return 0;
  135. }
  136. if (outlen > INT_MAX) {
  137. ERR_raise(ERR_LIB_EC, EC_R_INVALID_OUTPUT_LENGTH);
  138. return 0;
  139. }
  140. if (!eckey->meth->compute_key(&sec, &seclen, pub_key, eckey))
  141. return 0;
  142. if (KDF != NULL) {
  143. KDF(sec, seclen, out, &outlen);
  144. } else {
  145. if (outlen > seclen)
  146. outlen = seclen;
  147. memcpy(out, sec, outlen);
  148. }
  149. OPENSSL_clear_free(sec, seclen);
  150. return outlen;
  151. }
  152. EC_KEY_METHOD *EC_KEY_METHOD_new(const EC_KEY_METHOD *meth)
  153. {
  154. EC_KEY_METHOD *ret = OPENSSL_zalloc(sizeof(*meth));
  155. if (ret == NULL)
  156. return NULL;
  157. if (meth != NULL)
  158. *ret = *meth;
  159. ret->flags |= EC_KEY_METHOD_DYNAMIC;
  160. return ret;
  161. }
  162. void EC_KEY_METHOD_free(EC_KEY_METHOD *meth)
  163. {
  164. if (meth->flags & EC_KEY_METHOD_DYNAMIC)
  165. OPENSSL_free(meth);
  166. }
  167. void EC_KEY_METHOD_set_init(EC_KEY_METHOD *meth,
  168. int (*init)(EC_KEY *key),
  169. void (*finish)(EC_KEY *key),
  170. int (*copy)(EC_KEY *dest, const EC_KEY *src),
  171. int (*set_group)(EC_KEY *key, const EC_GROUP *grp),
  172. int (*set_private)(EC_KEY *key,
  173. const BIGNUM *priv_key),
  174. int (*set_public)(EC_KEY *key,
  175. const EC_POINT *pub_key))
  176. {
  177. meth->init = init;
  178. meth->finish = finish;
  179. meth->copy = copy;
  180. meth->set_group = set_group;
  181. meth->set_private = set_private;
  182. meth->set_public = set_public;
  183. }
  184. void EC_KEY_METHOD_set_keygen(EC_KEY_METHOD *meth,
  185. int (*keygen)(EC_KEY *key))
  186. {
  187. meth->keygen = keygen;
  188. }
  189. void EC_KEY_METHOD_set_compute_key(EC_KEY_METHOD *meth,
  190. int (*ckey)(unsigned char **psec,
  191. size_t *pseclen,
  192. const EC_POINT *pub_key,
  193. const EC_KEY *ecdh))
  194. {
  195. meth->compute_key = ckey;
  196. }
  197. void EC_KEY_METHOD_set_sign(EC_KEY_METHOD *meth,
  198. int (*sign)(int type, const unsigned char *dgst,
  199. int dlen, unsigned char *sig,
  200. unsigned int *siglen,
  201. const BIGNUM *kinv, const BIGNUM *r,
  202. EC_KEY *eckey),
  203. int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
  204. BIGNUM **kinvp, BIGNUM **rp),
  205. ECDSA_SIG *(*sign_sig)(const unsigned char *dgst,
  206. int dgst_len,
  207. const BIGNUM *in_kinv,
  208. const BIGNUM *in_r,
  209. EC_KEY *eckey))
  210. {
  211. meth->sign = sign;
  212. meth->sign_setup = sign_setup;
  213. meth->sign_sig = sign_sig;
  214. }
  215. void EC_KEY_METHOD_set_verify(EC_KEY_METHOD *meth,
  216. int (*verify)(int type, const unsigned
  217. char *dgst, int dgst_len,
  218. const unsigned char *sigbuf,
  219. int sig_len, EC_KEY *eckey),
  220. int (*verify_sig)(const unsigned char *dgst,
  221. int dgst_len,
  222. const ECDSA_SIG *sig,
  223. EC_KEY *eckey))
  224. {
  225. meth->verify = verify;
  226. meth->verify_sig = verify_sig;
  227. }
  228. void EC_KEY_METHOD_get_init(const EC_KEY_METHOD *meth,
  229. int (**pinit)(EC_KEY *key),
  230. void (**pfinish)(EC_KEY *key),
  231. int (**pcopy)(EC_KEY *dest, const EC_KEY *src),
  232. int (**pset_group)(EC_KEY *key,
  233. const EC_GROUP *grp),
  234. int (**pset_private)(EC_KEY *key,
  235. const BIGNUM *priv_key),
  236. int (**pset_public)(EC_KEY *key,
  237. const EC_POINT *pub_key))
  238. {
  239. if (pinit != NULL)
  240. *pinit = meth->init;
  241. if (pfinish != NULL)
  242. *pfinish = meth->finish;
  243. if (pcopy != NULL)
  244. *pcopy = meth->copy;
  245. if (pset_group != NULL)
  246. *pset_group = meth->set_group;
  247. if (pset_private != NULL)
  248. *pset_private = meth->set_private;
  249. if (pset_public != NULL)
  250. *pset_public = meth->set_public;
  251. }
  252. void EC_KEY_METHOD_get_keygen(const EC_KEY_METHOD *meth,
  253. int (**pkeygen)(EC_KEY *key))
  254. {
  255. if (pkeygen != NULL)
  256. *pkeygen = meth->keygen;
  257. }
  258. void EC_KEY_METHOD_get_compute_key(const EC_KEY_METHOD *meth,
  259. int (**pck)(unsigned char **pout,
  260. size_t *poutlen,
  261. const EC_POINT *pub_key,
  262. const EC_KEY *ecdh))
  263. {
  264. if (pck != NULL)
  265. *pck = meth->compute_key;
  266. }
  267. void EC_KEY_METHOD_get_sign(const EC_KEY_METHOD *meth,
  268. int (**psign)(int type, const unsigned char *dgst,
  269. int dlen, unsigned char *sig,
  270. unsigned int *siglen,
  271. const BIGNUM *kinv, const BIGNUM *r,
  272. EC_KEY *eckey),
  273. int (**psign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
  274. BIGNUM **kinvp, BIGNUM **rp),
  275. ECDSA_SIG *(**psign_sig)(const unsigned char *dgst,
  276. int dgst_len,
  277. const BIGNUM *in_kinv,
  278. const BIGNUM *in_r,
  279. EC_KEY *eckey))
  280. {
  281. if (psign != NULL)
  282. *psign = meth->sign;
  283. if (psign_setup != NULL)
  284. *psign_setup = meth->sign_setup;
  285. if (psign_sig != NULL)
  286. *psign_sig = meth->sign_sig;
  287. }
  288. void EC_KEY_METHOD_get_verify(const EC_KEY_METHOD *meth,
  289. int (**pverify)(int type, const unsigned
  290. char *dgst, int dgst_len,
  291. const unsigned char *sigbuf,
  292. int sig_len, EC_KEY *eckey),
  293. int (**pverify_sig)(const unsigned char *dgst,
  294. int dgst_len,
  295. const ECDSA_SIG *sig,
  296. EC_KEY *eckey))
  297. {
  298. if (pverify != NULL)
  299. *pverify = meth->verify;
  300. if (pverify_sig != NULL)
  301. *pverify_sig = meth->verify_sig;
  302. }