ec_kmeth.c 11 KB

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