ec_pmeth.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /* Written by Dr Stephen N Henson (shenson@bigfoot.com) 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 void pkey_ec_cleanup(EVP_PKEY_CTX *ctx)
  85. {
  86. EC_PKEY_CTX *dctx = ctx->data;
  87. if (dctx)
  88. {
  89. if (dctx->gen_group)
  90. EC_GROUP_free(dctx->gen_group);
  91. OPENSSL_free(dctx);
  92. }
  93. }
  94. static int pkey_ec_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, int *siglen,
  95. const unsigned char *tbs, int tbslen)
  96. {
  97. int ret, type;
  98. unsigned int sltmp;
  99. EC_PKEY_CTX *dctx = ctx->data;
  100. EC_KEY *ec = ctx->pkey->pkey.ec;
  101. if (!sig)
  102. {
  103. *siglen = ECDSA_size(ec);
  104. return 1;
  105. }
  106. else if(*siglen < ECDSA_size(ec))
  107. {
  108. ECerr(EC_F_PKEY_EC_SIGN, EC_R_BUFFER_TOO_SMALL);
  109. return 0;
  110. }
  111. if (dctx->md)
  112. type = EVP_MD_type(dctx->md);
  113. else
  114. type = NID_sha1;
  115. ret = ECDSA_sign(type, tbs, tbslen, sig, &sltmp, ec);
  116. if (ret < 0)
  117. return ret;
  118. *siglen = sltmp;
  119. return 1;
  120. }
  121. static int pkey_ec_verify(EVP_PKEY_CTX *ctx,
  122. const unsigned char *sig, int siglen,
  123. const unsigned char *tbs, int tbslen)
  124. {
  125. int ret, type;
  126. EC_PKEY_CTX *dctx = ctx->data;
  127. EC_KEY *ec = ctx->pkey->pkey.ec;
  128. if (dctx->md)
  129. type = EVP_MD_type(dctx->md);
  130. else
  131. type = NID_sha1;
  132. ret = ECDSA_verify(type, tbs, tbslen, sig, siglen, ec);
  133. return ret;
  134. }
  135. static int pkey_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key, int *keylen)
  136. {
  137. int ret;
  138. size_t outlen;
  139. const EC_POINT *pubkey = NULL;
  140. if (!ctx->pkey || !ctx->peerkey)
  141. {
  142. ECerr(EC_F_PKEY_EC_DERIVE, EC_R_KEYS_NOT_SET);
  143. return 0;
  144. }
  145. if (!key)
  146. {
  147. const EC_GROUP *group;
  148. group = EC_KEY_get0_group(ctx->pkey->pkey.ec);
  149. *keylen = (EC_GROUP_get_degree(group) + 7)/8;
  150. return 1;
  151. }
  152. pubkey = EC_KEY_get0_public_key(ctx->peerkey->pkey.ec);
  153. /* NB: unlike PKS#3 DH, if *outlen is less than maximum size this is
  154. * not an error, the result is truncated.
  155. */
  156. outlen = *keylen;
  157. ret = ECDH_compute_key(key, outlen, pubkey, ctx->pkey->pkey.ec, 0);
  158. if (ret < 0)
  159. return ret;
  160. *keylen = ret;
  161. return 1;
  162. }
  163. static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  164. {
  165. EC_PKEY_CTX *dctx = ctx->data;
  166. EC_GROUP *group;
  167. switch (type)
  168. {
  169. case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
  170. group = EC_GROUP_new_by_curve_name(p1);
  171. if (group == NULL)
  172. {
  173. ECerr(EC_F_PKEY_EC_CTRL, EC_R_INVALID_CURVE);
  174. return 0;
  175. }
  176. if (dctx->gen_group)
  177. EC_GROUP_free(dctx->gen_group);
  178. dctx->gen_group = group;
  179. return 1;
  180. case EVP_PKEY_CTRL_MD:
  181. if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1)
  182. {
  183. ECerr(EC_F_PKEY_EC_CTRL, EC_R_INVALID_DIGEST_TYPE);
  184. return 0;
  185. }
  186. dctx->md = p2;
  187. return 1;
  188. case EVP_PKEY_CTRL_PEER_KEY:
  189. /* Default behaviour is OK */
  190. return 1;
  191. default:
  192. return -2;
  193. }
  194. }
  195. static int pkey_ec_ctrl_str(EVP_PKEY_CTX *ctx,
  196. const char *type, const char *value)
  197. {
  198. if (!strcmp(type, "ec_paramgen_curve"))
  199. {
  200. int nid;
  201. nid = OBJ_sn2nid(value);
  202. if (nid == NID_undef)
  203. nid = OBJ_ln2nid(value);
  204. if (nid == NID_undef)
  205. {
  206. ECerr(EC_F_PKEY_EC_CTRL_STR, EC_R_INVALID_CURVE);
  207. return 0;
  208. }
  209. return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid);
  210. }
  211. return -2;
  212. }
  213. static int pkey_ec_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  214. {
  215. EC_KEY *ec = NULL;
  216. EC_PKEY_CTX *dctx = ctx->data;
  217. int ret = 0;
  218. if (dctx->gen_group == NULL)
  219. {
  220. ECerr(EC_F_PKEY_EC_PARAMGEN, EC_R_NO_PARAMETERS_SET);
  221. return 0;
  222. }
  223. ec = EC_KEY_new();
  224. if (!ec)
  225. return 0;
  226. ret = EC_KEY_set_group(ec, dctx->gen_group);
  227. if (ret)
  228. EVP_PKEY_assign_EC_KEY(pkey, ec);
  229. else
  230. EC_KEY_free(ec);
  231. return ret;
  232. }
  233. static int pkey_ec_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  234. {
  235. EC_KEY *ec = NULL;
  236. if (ctx->pkey == NULL)
  237. {
  238. ECerr(EC_F_PKEY_EC_KEYGEN, EC_R_NO_PARAMETERS_SET);
  239. return 0;
  240. }
  241. ec = EC_KEY_new();
  242. if (!ec)
  243. return 0;
  244. EVP_PKEY_assign_EC_KEY(pkey, ec);
  245. /* Note: if error return, pkey is freed by parent routine */
  246. if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
  247. return 0;
  248. return EC_KEY_generate_key(pkey->pkey.ec);
  249. }
  250. const EVP_PKEY_METHOD ec_pkey_meth =
  251. {
  252. EVP_PKEY_EC,
  253. 0,
  254. pkey_ec_init,
  255. pkey_ec_cleanup,
  256. 0,
  257. pkey_ec_paramgen,
  258. 0,
  259. pkey_ec_keygen,
  260. 0,
  261. pkey_ec_sign,
  262. 0,
  263. pkey_ec_verify,
  264. 0,0,
  265. 0,0,0,0,
  266. 0,0,
  267. 0,0,
  268. 0,
  269. pkey_ec_derive,
  270. pkey_ec_ctrl,
  271. pkey_ec_ctrl_str
  272. };