ec_pmeth.c 8.8 KB

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