ec_kmeth.c 11 KB

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