2
0

ec_kmeth.c 11 KB

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