2
0

ec_pmeth.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  2. * project 2006.
  3. */
  4. /* ====================================================================
  5. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. All advertising materials mentioning features or use of this
  20. * software must display the following acknowledgment:
  21. * "This product includes software developed by the OpenSSL Project
  22. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  23. *
  24. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  25. * endorse or promote products derived from this software without
  26. * prior written permission. For written permission, please contact
  27. * licensing@OpenSSL.org.
  28. *
  29. * 5. Products derived from this software may not be called "OpenSSL"
  30. * nor may "OpenSSL" appear in their names without prior written
  31. * permission of the OpenSSL Project.
  32. *
  33. * 6. Redistributions of any form whatsoever must retain the following
  34. * acknowledgment:
  35. * "This product includes software developed by the OpenSSL Project
  36. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  39. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  41. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  47. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  49. * OF THE POSSIBILITY OF SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This product includes cryptographic software written by Eric Young
  53. * (eay@cryptsoft.com). This product includes software written by Tim
  54. * Hudson (tjh@cryptsoft.com).
  55. *
  56. */
  57. #include <stdio.h>
  58. #include "cryptlib.h"
  59. #include <openssl/asn1t.h>
  60. #include <openssl/x509.h>
  61. #include <openssl/ec.h>
  62. #include <openssl/ecdsa.h>
  63. #include <openssl/evp.h>
  64. #include "evp_locl.h"
  65. /* EC pkey context structure */
  66. typedef struct
  67. {
  68. /* Key and paramgen group */
  69. EC_GROUP *gen_group;
  70. /* message digest */
  71. const EVP_MD *md;
  72. } EC_PKEY_CTX;
  73. static int pkey_ec_init(EVP_PKEY_CTX *ctx)
  74. {
  75. EC_PKEY_CTX *dctx;
  76. dctx = OPENSSL_malloc(sizeof(EC_PKEY_CTX));
  77. if (!dctx)
  78. return 0;
  79. dctx->gen_group = NULL;
  80. dctx->md = NULL;
  81. ctx->data = dctx;
  82. return 1;
  83. }
  84. static int pkey_ec_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
  85. {
  86. EC_PKEY_CTX *dctx, *sctx;
  87. if (!pkey_ec_init(dst))
  88. return 0;
  89. sctx = src->data;
  90. dctx = dst->data;
  91. if (sctx->gen_group)
  92. {
  93. dctx->gen_group = EC_GROUP_dup(sctx->gen_group);
  94. if (!dctx->gen_group)
  95. return 0;
  96. }
  97. dctx->md = sctx->md;
  98. return 1;
  99. }
  100. static void pkey_ec_cleanup(EVP_PKEY_CTX *ctx)
  101. {
  102. EC_PKEY_CTX *dctx = ctx->data;
  103. if (dctx)
  104. {
  105. if (dctx->gen_group)
  106. EC_GROUP_free(dctx->gen_group);
  107. OPENSSL_free(dctx);
  108. }
  109. }
  110. static int pkey_ec_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  111. const unsigned char *tbs, size_t tbslen)
  112. {
  113. int ret, type;
  114. unsigned int sltmp;
  115. EC_PKEY_CTX *dctx = ctx->data;
  116. EC_KEY *ec = ctx->pkey->pkey.ec;
  117. if (!sig)
  118. {
  119. *siglen = ECDSA_size(ec);
  120. return 1;
  121. }
  122. else if(*siglen < (size_t)ECDSA_size(ec))
  123. {
  124. ECerr(EC_F_PKEY_EC_SIGN, EC_R_BUFFER_TOO_SMALL);
  125. return 0;
  126. }
  127. if (dctx->md)
  128. type = EVP_MD_type(dctx->md);
  129. else
  130. type = NID_sha1;
  131. ret = ECDSA_sign(type, tbs, tbslen, sig, &sltmp, ec);
  132. if (ret <= 0)
  133. return ret;
  134. *siglen = (size_t)sltmp;
  135. return 1;
  136. }
  137. static int pkey_ec_verify(EVP_PKEY_CTX *ctx,
  138. const unsigned char *sig, size_t siglen,
  139. const unsigned char *tbs, size_t tbslen)
  140. {
  141. int ret, type;
  142. EC_PKEY_CTX *dctx = ctx->data;
  143. EC_KEY *ec = ctx->pkey->pkey.ec;
  144. if (dctx->md)
  145. type = EVP_MD_type(dctx->md);
  146. else
  147. type = NID_sha1;
  148. ret = ECDSA_verify(type, tbs, tbslen, sig, siglen, ec);
  149. return ret;
  150. }
  151. static int pkey_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
  152. {
  153. int ret;
  154. size_t outlen;
  155. const EC_POINT *pubkey = NULL;
  156. if (!ctx->pkey || !ctx->peerkey)
  157. {
  158. ECerr(EC_F_PKEY_EC_DERIVE, EC_R_KEYS_NOT_SET);
  159. return 0;
  160. }
  161. if (!key)
  162. {
  163. const EC_GROUP *group;
  164. group = EC_KEY_get0_group(ctx->pkey->pkey.ec);
  165. *keylen = (EC_GROUP_get_degree(group) + 7)/8;
  166. return 1;
  167. }
  168. pubkey = EC_KEY_get0_public_key(ctx->peerkey->pkey.ec);
  169. /* NB: unlike PKS#3 DH, if *outlen is less than maximum size this is
  170. * not an error, the result is truncated.
  171. */
  172. outlen = *keylen;
  173. ret = ECDH_compute_key(key, outlen, pubkey, ctx->pkey->pkey.ec, 0);
  174. if (ret < 0)
  175. return ret;
  176. *keylen = ret;
  177. return 1;
  178. }
  179. static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  180. {
  181. EC_PKEY_CTX *dctx = ctx->data;
  182. EC_GROUP *group;
  183. switch (type)
  184. {
  185. case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
  186. group = EC_GROUP_new_by_curve_name(p1);
  187. if (group == NULL)
  188. {
  189. ECerr(EC_F_PKEY_EC_CTRL, EC_R_INVALID_CURVE);
  190. return 0;
  191. }
  192. if (dctx->gen_group)
  193. EC_GROUP_free(dctx->gen_group);
  194. dctx->gen_group = group;
  195. return 1;
  196. case EVP_PKEY_CTRL_MD:
  197. if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 &&
  198. EVP_MD_type((const EVP_MD *)p2) != NID_ecdsa_with_SHA1 &&
  199. EVP_MD_type((const EVP_MD *)p2) != NID_sha224 &&
  200. EVP_MD_type((const EVP_MD *)p2) != NID_sha256 &&
  201. EVP_MD_type((const EVP_MD *)p2) != NID_sha384 &&
  202. EVP_MD_type((const EVP_MD *)p2) != NID_sha512)
  203. {
  204. ECerr(EC_F_PKEY_EC_CTRL, EC_R_INVALID_DIGEST_TYPE);
  205. return 0;
  206. }
  207. dctx->md = p2;
  208. return 1;
  209. case EVP_PKEY_CTRL_PEER_KEY:
  210. /* Default behaviour is OK */
  211. case EVP_PKEY_CTRL_DIGESTINIT:
  212. case EVP_PKEY_CTRL_PKCS7_SIGN:
  213. case EVP_PKEY_CTRL_CMS_SIGN:
  214. return 1;
  215. default:
  216. return -2;
  217. }
  218. }
  219. static int pkey_ec_ctrl_str(EVP_PKEY_CTX *ctx,
  220. const char *type, const char *value)
  221. {
  222. if (!strcmp(type, "ec_paramgen_curve"))
  223. {
  224. int nid;
  225. nid = OBJ_sn2nid(value);
  226. if (nid == NID_undef)
  227. nid = OBJ_ln2nid(value);
  228. if (nid == NID_undef)
  229. {
  230. ECerr(EC_F_PKEY_EC_CTRL_STR, EC_R_INVALID_CURVE);
  231. return 0;
  232. }
  233. return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid);
  234. }
  235. return -2;
  236. }
  237. static int pkey_ec_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  238. {
  239. EC_KEY *ec = NULL;
  240. EC_PKEY_CTX *dctx = ctx->data;
  241. int ret = 0;
  242. if (dctx->gen_group == NULL)
  243. {
  244. ECerr(EC_F_PKEY_EC_PARAMGEN, EC_R_NO_PARAMETERS_SET);
  245. return 0;
  246. }
  247. ec = EC_KEY_new();
  248. if (!ec)
  249. return 0;
  250. ret = EC_KEY_set_group(ec, dctx->gen_group);
  251. if (ret)
  252. EVP_PKEY_assign_EC_KEY(pkey, ec);
  253. else
  254. EC_KEY_free(ec);
  255. return ret;
  256. }
  257. static int pkey_ec_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  258. {
  259. EC_KEY *ec = NULL;
  260. if (ctx->pkey == NULL)
  261. {
  262. ECerr(EC_F_PKEY_EC_KEYGEN, EC_R_NO_PARAMETERS_SET);
  263. return 0;
  264. }
  265. ec = EC_KEY_new();
  266. if (!ec)
  267. return 0;
  268. EVP_PKEY_assign_EC_KEY(pkey, ec);
  269. /* Note: if error return, pkey is freed by parent routine */
  270. if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
  271. return 0;
  272. return EC_KEY_generate_key(pkey->pkey.ec);
  273. }
  274. const EVP_PKEY_METHOD ec_pkey_meth =
  275. {
  276. EVP_PKEY_EC,
  277. 0,
  278. pkey_ec_init,
  279. pkey_ec_copy,
  280. pkey_ec_cleanup,
  281. 0,
  282. pkey_ec_paramgen,
  283. 0,
  284. pkey_ec_keygen,
  285. 0,
  286. pkey_ec_sign,
  287. 0,
  288. pkey_ec_verify,
  289. 0,0,
  290. 0,0,0,0,
  291. 0,0,
  292. 0,0,
  293. 0,
  294. pkey_ec_derive,
  295. pkey_ec_ctrl,
  296. pkey_ec_ctrl_str
  297. };