ec_kmeth.c 10 KB

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