ec_kmeth.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * Copyright 2015-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. * 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. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  73. return NULL;
  74. }
  75. ret->libctx = libctx;
  76. if (propq != NULL) {
  77. ret->propq = OPENSSL_strdup(propq);
  78. if (ret->propq == NULL) {
  79. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  80. goto err;
  81. }
  82. }
  83. ret->references = 1;
  84. ret->lock = CRYPTO_THREAD_lock_new();
  85. if (ret->lock == NULL) {
  86. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  87. goto err;
  88. }
  89. ret->meth = EC_KEY_get_default_method();
  90. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  91. if (engine != NULL) {
  92. if (!ENGINE_init(engine)) {
  93. ERR_raise(ERR_LIB_EC, ERR_R_ENGINE_LIB);
  94. goto err;
  95. }
  96. ret->engine = engine;
  97. } else
  98. ret->engine = ENGINE_get_default_EC();
  99. if (ret->engine != NULL) {
  100. ret->meth = ENGINE_get_EC(ret->engine);
  101. if (ret->meth == NULL) {
  102. ERR_raise(ERR_LIB_EC, ERR_R_ENGINE_LIB);
  103. goto err;
  104. }
  105. }
  106. #endif
  107. ret->version = 1;
  108. ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
  109. /* No ex_data inside the FIPS provider */
  110. #ifndef FIPS_MODULE
  111. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data)) {
  112. goto err;
  113. }
  114. #endif
  115. if (ret->meth->init != NULL && ret->meth->init(ret) == 0) {
  116. ERR_raise(ERR_LIB_EC, ERR_R_INIT_FAIL);
  117. goto err;
  118. }
  119. return ret;
  120. err:
  121. EC_KEY_free(ret);
  122. return NULL;
  123. }
  124. #ifndef FIPS_MODULE
  125. EC_KEY *EC_KEY_new_method(ENGINE *engine)
  126. {
  127. return ossl_ec_key_new_method_int(NULL, NULL, engine);
  128. }
  129. #endif
  130. int ECDH_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,
  131. const EC_KEY *eckey,
  132. void *(*KDF) (const void *in, size_t inlen, void *out,
  133. size_t *outlen))
  134. {
  135. unsigned char *sec = NULL;
  136. size_t seclen;
  137. if (eckey->meth->compute_key == NULL) {
  138. ERR_raise(ERR_LIB_EC, EC_R_OPERATION_NOT_SUPPORTED);
  139. return 0;
  140. }
  141. if (outlen > INT_MAX) {
  142. ERR_raise(ERR_LIB_EC, EC_R_INVALID_OUTPUT_LENGTH);
  143. return 0;
  144. }
  145. if (!eckey->meth->compute_key(&sec, &seclen, pub_key, eckey))
  146. return 0;
  147. if (KDF != NULL) {
  148. KDF(sec, seclen, out, &outlen);
  149. } else {
  150. if (outlen > seclen)
  151. outlen = seclen;
  152. memcpy(out, sec, outlen);
  153. }
  154. OPENSSL_clear_free(sec, seclen);
  155. return outlen;
  156. }
  157. EC_KEY_METHOD *EC_KEY_METHOD_new(const EC_KEY_METHOD *meth)
  158. {
  159. EC_KEY_METHOD *ret = OPENSSL_zalloc(sizeof(*meth));
  160. if (ret == NULL)
  161. return NULL;
  162. if (meth != NULL)
  163. *ret = *meth;
  164. ret->flags |= EC_KEY_METHOD_DYNAMIC;
  165. return ret;
  166. }
  167. void EC_KEY_METHOD_free(EC_KEY_METHOD *meth)
  168. {
  169. if (meth->flags & EC_KEY_METHOD_DYNAMIC)
  170. OPENSSL_free(meth);
  171. }
  172. void EC_KEY_METHOD_set_init(EC_KEY_METHOD *meth,
  173. int (*init)(EC_KEY *key),
  174. void (*finish)(EC_KEY *key),
  175. int (*copy)(EC_KEY *dest, const EC_KEY *src),
  176. int (*set_group)(EC_KEY *key, const EC_GROUP *grp),
  177. int (*set_private)(EC_KEY *key,
  178. const BIGNUM *priv_key),
  179. int (*set_public)(EC_KEY *key,
  180. const EC_POINT *pub_key))
  181. {
  182. meth->init = init;
  183. meth->finish = finish;
  184. meth->copy = copy;
  185. meth->set_group = set_group;
  186. meth->set_private = set_private;
  187. meth->set_public = set_public;
  188. }
  189. void EC_KEY_METHOD_set_keygen(EC_KEY_METHOD *meth,
  190. int (*keygen)(EC_KEY *key))
  191. {
  192. meth->keygen = keygen;
  193. }
  194. void EC_KEY_METHOD_set_compute_key(EC_KEY_METHOD *meth,
  195. int (*ckey)(unsigned char **psec,
  196. size_t *pseclen,
  197. const EC_POINT *pub_key,
  198. const EC_KEY *ecdh))
  199. {
  200. meth->compute_key = ckey;
  201. }
  202. void EC_KEY_METHOD_set_sign(EC_KEY_METHOD *meth,
  203. int (*sign)(int type, const unsigned char *dgst,
  204. int dlen, unsigned char *sig,
  205. unsigned int *siglen,
  206. const BIGNUM *kinv, const BIGNUM *r,
  207. EC_KEY *eckey),
  208. int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
  209. BIGNUM **kinvp, BIGNUM **rp),
  210. ECDSA_SIG *(*sign_sig)(const unsigned char *dgst,
  211. int dgst_len,
  212. const BIGNUM *in_kinv,
  213. const BIGNUM *in_r,
  214. EC_KEY *eckey))
  215. {
  216. meth->sign = sign;
  217. meth->sign_setup = sign_setup;
  218. meth->sign_sig = sign_sig;
  219. }
  220. void EC_KEY_METHOD_set_verify(EC_KEY_METHOD *meth,
  221. int (*verify)(int type, const unsigned
  222. char *dgst, int dgst_len,
  223. const unsigned char *sigbuf,
  224. int sig_len, EC_KEY *eckey),
  225. int (*verify_sig)(const unsigned char *dgst,
  226. int dgst_len,
  227. const ECDSA_SIG *sig,
  228. EC_KEY *eckey))
  229. {
  230. meth->verify = verify;
  231. meth->verify_sig = verify_sig;
  232. }
  233. void EC_KEY_METHOD_get_init(const EC_KEY_METHOD *meth,
  234. int (**pinit)(EC_KEY *key),
  235. void (**pfinish)(EC_KEY *key),
  236. int (**pcopy)(EC_KEY *dest, const EC_KEY *src),
  237. int (**pset_group)(EC_KEY *key,
  238. const EC_GROUP *grp),
  239. int (**pset_private)(EC_KEY *key,
  240. const BIGNUM *priv_key),
  241. int (**pset_public)(EC_KEY *key,
  242. const EC_POINT *pub_key))
  243. {
  244. if (pinit != NULL)
  245. *pinit = meth->init;
  246. if (pfinish != NULL)
  247. *pfinish = meth->finish;
  248. if (pcopy != NULL)
  249. *pcopy = meth->copy;
  250. if (pset_group != NULL)
  251. *pset_group = meth->set_group;
  252. if (pset_private != NULL)
  253. *pset_private = meth->set_private;
  254. if (pset_public != NULL)
  255. *pset_public = meth->set_public;
  256. }
  257. void EC_KEY_METHOD_get_keygen(const EC_KEY_METHOD *meth,
  258. int (**pkeygen)(EC_KEY *key))
  259. {
  260. if (pkeygen != NULL)
  261. *pkeygen = meth->keygen;
  262. }
  263. void EC_KEY_METHOD_get_compute_key(const EC_KEY_METHOD *meth,
  264. int (**pck)(unsigned char **pout,
  265. size_t *poutlen,
  266. const EC_POINT *pub_key,
  267. const EC_KEY *ecdh))
  268. {
  269. if (pck != NULL)
  270. *pck = meth->compute_key;
  271. }
  272. void EC_KEY_METHOD_get_sign(const EC_KEY_METHOD *meth,
  273. int (**psign)(int type, const unsigned char *dgst,
  274. int dlen, unsigned char *sig,
  275. unsigned int *siglen,
  276. const BIGNUM *kinv, const BIGNUM *r,
  277. EC_KEY *eckey),
  278. int (**psign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
  279. BIGNUM **kinvp, BIGNUM **rp),
  280. ECDSA_SIG *(**psign_sig)(const unsigned char *dgst,
  281. int dgst_len,
  282. const BIGNUM *in_kinv,
  283. const BIGNUM *in_r,
  284. EC_KEY *eckey))
  285. {
  286. if (psign != NULL)
  287. *psign = meth->sign;
  288. if (psign_setup != NULL)
  289. *psign_setup = meth->sign_setup;
  290. if (psign_sig != NULL)
  291. *psign_sig = meth->sign_sig;
  292. }
  293. void EC_KEY_METHOD_get_verify(const EC_KEY_METHOD *meth,
  294. int (**pverify)(int type, const unsigned
  295. char *dgst, int dgst_len,
  296. const unsigned char *sigbuf,
  297. int sig_len, EC_KEY *eckey),
  298. int (**pverify_sig)(const unsigned char *dgst,
  299. int dgst_len,
  300. const ECDSA_SIG *sig,
  301. EC_KEY *eckey))
  302. {
  303. if (pverify != NULL)
  304. *pverify = meth->verify;
  305. if (pverify_sig != NULL)
  306. *pverify_sig = meth->verify_sig;
  307. }