ec_pmeth.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. * Written by Dr Stephen N Henson (steve@openssl.org) 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. * licensing@OpenSSL.org.
  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. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include <stdio.h>
  59. #include "internal/cryptlib.h"
  60. #include <openssl/asn1t.h>
  61. #include <openssl/x509.h>
  62. #include <openssl/ec.h>
  63. #include "ec_lcl.h"
  64. #include <openssl/ecdsa.h>
  65. #include <openssl/evp.h>
  66. #include "internal/evp_int.h"
  67. /* EC pkey context structure */
  68. typedef struct {
  69. /* Key and paramgen group */
  70. EC_GROUP *gen_group;
  71. /* message digest */
  72. const EVP_MD *md;
  73. /* Duplicate key if custom cofactor needed */
  74. EC_KEY *co_key;
  75. /* Cofactor mode */
  76. signed char cofactor_mode;
  77. /* KDF (if any) to use for ECDH */
  78. char kdf_type;
  79. /* Message digest to use for key derivation */
  80. const EVP_MD *kdf_md;
  81. /* User key material */
  82. unsigned char *kdf_ukm;
  83. size_t kdf_ukmlen;
  84. /* KDF output length */
  85. size_t kdf_outlen;
  86. } EC_PKEY_CTX;
  87. static int pkey_ec_init(EVP_PKEY_CTX *ctx)
  88. {
  89. EC_PKEY_CTX *dctx;
  90. dctx = OPENSSL_malloc(sizeof(*dctx));
  91. if (!dctx)
  92. return 0;
  93. dctx->gen_group = NULL;
  94. dctx->md = NULL;
  95. dctx->cofactor_mode = -1;
  96. dctx->co_key = NULL;
  97. dctx->kdf_type = EVP_PKEY_ECDH_KDF_NONE;
  98. dctx->kdf_md = NULL;
  99. dctx->kdf_outlen = 0;
  100. dctx->kdf_ukm = NULL;
  101. dctx->kdf_ukmlen = 0;
  102. ctx->data = dctx;
  103. return 1;
  104. }
  105. static int pkey_ec_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
  106. {
  107. EC_PKEY_CTX *dctx, *sctx;
  108. if (!pkey_ec_init(dst))
  109. return 0;
  110. sctx = src->data;
  111. dctx = dst->data;
  112. if (sctx->gen_group) {
  113. dctx->gen_group = EC_GROUP_dup(sctx->gen_group);
  114. if (!dctx->gen_group)
  115. return 0;
  116. }
  117. dctx->md = sctx->md;
  118. if (sctx->co_key) {
  119. dctx->co_key = EC_KEY_dup(sctx->co_key);
  120. if (!dctx->co_key)
  121. return 0;
  122. }
  123. dctx->kdf_type = sctx->kdf_type;
  124. dctx->kdf_md = sctx->kdf_md;
  125. dctx->kdf_outlen = sctx->kdf_outlen;
  126. if (sctx->kdf_ukm) {
  127. dctx->kdf_ukm = BUF_memdup(sctx->kdf_ukm, sctx->kdf_ukmlen);
  128. if (!dctx->kdf_ukm)
  129. return 0;
  130. } else
  131. dctx->kdf_ukm = NULL;
  132. dctx->kdf_ukmlen = sctx->kdf_ukmlen;
  133. return 1;
  134. }
  135. static void pkey_ec_cleanup(EVP_PKEY_CTX *ctx)
  136. {
  137. EC_PKEY_CTX *dctx = ctx->data;
  138. if (dctx) {
  139. EC_GROUP_free(dctx->gen_group);
  140. EC_KEY_free(dctx->co_key);
  141. OPENSSL_free(dctx->kdf_ukm);
  142. OPENSSL_free(dctx);
  143. }
  144. }
  145. static int pkey_ec_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  146. const unsigned char *tbs, size_t tbslen)
  147. {
  148. int ret, type;
  149. unsigned int sltmp;
  150. EC_PKEY_CTX *dctx = ctx->data;
  151. EC_KEY *ec = ctx->pkey->pkey.ec;
  152. if (!sig) {
  153. *siglen = ECDSA_size(ec);
  154. return 1;
  155. } else if (*siglen < (size_t)ECDSA_size(ec)) {
  156. ECerr(EC_F_PKEY_EC_SIGN, EC_R_BUFFER_TOO_SMALL);
  157. return 0;
  158. }
  159. if (dctx->md)
  160. type = EVP_MD_type(dctx->md);
  161. else
  162. type = NID_sha1;
  163. ret = ECDSA_sign(type, tbs, tbslen, sig, &sltmp, ec);
  164. if (ret <= 0)
  165. return ret;
  166. *siglen = (size_t)sltmp;
  167. return 1;
  168. }
  169. static int pkey_ec_verify(EVP_PKEY_CTX *ctx,
  170. const unsigned char *sig, size_t siglen,
  171. const unsigned char *tbs, size_t tbslen)
  172. {
  173. int ret, type;
  174. EC_PKEY_CTX *dctx = ctx->data;
  175. EC_KEY *ec = ctx->pkey->pkey.ec;
  176. if (dctx->md)
  177. type = EVP_MD_type(dctx->md);
  178. else
  179. type = NID_sha1;
  180. ret = ECDSA_verify(type, tbs, tbslen, sig, siglen, ec);
  181. return ret;
  182. }
  183. #ifndef OPENSSL_NO_EC
  184. static int pkey_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
  185. size_t *keylen)
  186. {
  187. int ret;
  188. size_t outlen;
  189. const EC_POINT *pubkey = NULL;
  190. EC_KEY *eckey;
  191. EC_PKEY_CTX *dctx = ctx->data;
  192. if (!ctx->pkey || !ctx->peerkey) {
  193. ECerr(EC_F_PKEY_EC_DERIVE, EC_R_KEYS_NOT_SET);
  194. return 0;
  195. }
  196. eckey = dctx->co_key ? dctx->co_key : ctx->pkey->pkey.ec;
  197. if (!key) {
  198. const EC_GROUP *group;
  199. group = EC_KEY_get0_group(eckey);
  200. *keylen = (EC_GROUP_get_degree(group) + 7) / 8;
  201. return 1;
  202. }
  203. pubkey = EC_KEY_get0_public_key(ctx->peerkey->pkey.ec);
  204. /*
  205. * NB: unlike PKCS#3 DH, if *outlen is less than maximum size this is not
  206. * an error, the result is truncated.
  207. */
  208. outlen = *keylen;
  209. ret = ECDH_compute_key(key, outlen, pubkey, eckey, 0);
  210. if (ret <= 0)
  211. return 0;
  212. *keylen = ret;
  213. return 1;
  214. }
  215. static int pkey_ec_kdf_derive(EVP_PKEY_CTX *ctx,
  216. unsigned char *key, size_t *keylen)
  217. {
  218. EC_PKEY_CTX *dctx = ctx->data;
  219. unsigned char *ktmp = NULL;
  220. size_t ktmplen;
  221. int rv = 0;
  222. if (dctx->kdf_type == EVP_PKEY_ECDH_KDF_NONE)
  223. return pkey_ec_derive(ctx, key, keylen);
  224. if (!key) {
  225. *keylen = dctx->kdf_outlen;
  226. return 1;
  227. }
  228. if (*keylen != dctx->kdf_outlen)
  229. return 0;
  230. if (!pkey_ec_derive(ctx, NULL, &ktmplen))
  231. return 0;
  232. ktmp = OPENSSL_malloc(ktmplen);
  233. if (!ktmp)
  234. return 0;
  235. if (!pkey_ec_derive(ctx, ktmp, &ktmplen))
  236. goto err;
  237. /* Do KDF stuff */
  238. if (!ECDH_KDF_X9_62(key, *keylen, ktmp, ktmplen,
  239. dctx->kdf_ukm, dctx->kdf_ukmlen, dctx->kdf_md))
  240. goto err;
  241. rv = 1;
  242. err:
  243. OPENSSL_clear_free(ktmp, ktmplen);
  244. return rv;
  245. }
  246. #endif
  247. static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  248. {
  249. EC_PKEY_CTX *dctx = ctx->data;
  250. EC_GROUP *group;
  251. switch (type) {
  252. case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
  253. group = EC_GROUP_new_by_curve_name(p1);
  254. if (group == NULL) {
  255. ECerr(EC_F_PKEY_EC_CTRL, EC_R_INVALID_CURVE);
  256. return 0;
  257. }
  258. EC_GROUP_free(dctx->gen_group);
  259. dctx->gen_group = group;
  260. return 1;
  261. case EVP_PKEY_CTRL_EC_PARAM_ENC:
  262. if (!dctx->gen_group) {
  263. ECerr(EC_F_PKEY_EC_CTRL, EC_R_NO_PARAMETERS_SET);
  264. return 0;
  265. }
  266. EC_GROUP_set_asn1_flag(dctx->gen_group, p1);
  267. return 1;
  268. #ifndef OPENSSL_NO_EC
  269. case EVP_PKEY_CTRL_EC_ECDH_COFACTOR:
  270. if (p1 == -2) {
  271. if (dctx->cofactor_mode != -1)
  272. return dctx->cofactor_mode;
  273. else {
  274. EC_KEY *ec_key = ctx->pkey->pkey.ec;
  275. return EC_KEY_get_flags(ec_key) & EC_FLAG_COFACTOR_ECDH ? 1 :
  276. 0;
  277. }
  278. } else if (p1 < -1 || p1 > 1)
  279. return -2;
  280. dctx->cofactor_mode = p1;
  281. if (p1 != -1) {
  282. EC_KEY *ec_key = ctx->pkey->pkey.ec;
  283. if (!ec_key->group)
  284. return -2;
  285. /* If cofactor is 1 cofactor mode does nothing */
  286. if (BN_is_one(ec_key->group->cofactor))
  287. return 1;
  288. if (!dctx->co_key) {
  289. dctx->co_key = EC_KEY_dup(ec_key);
  290. if (!dctx->co_key)
  291. return 0;
  292. }
  293. if (p1)
  294. EC_KEY_set_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH);
  295. else
  296. EC_KEY_clear_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH);
  297. } else {
  298. EC_KEY_free(dctx->co_key);
  299. dctx->co_key = NULL;
  300. }
  301. return 1;
  302. #endif
  303. case EVP_PKEY_CTRL_EC_KDF_TYPE:
  304. if (p1 == -2)
  305. return dctx->kdf_type;
  306. if (p1 != EVP_PKEY_ECDH_KDF_NONE && p1 != EVP_PKEY_ECDH_KDF_X9_62)
  307. return -2;
  308. dctx->kdf_type = p1;
  309. return 1;
  310. case EVP_PKEY_CTRL_EC_KDF_MD:
  311. dctx->kdf_md = p2;
  312. return 1;
  313. case EVP_PKEY_CTRL_GET_EC_KDF_MD:
  314. *(const EVP_MD **)p2 = dctx->kdf_md;
  315. return 1;
  316. case EVP_PKEY_CTRL_EC_KDF_OUTLEN:
  317. if (p1 <= 0)
  318. return -2;
  319. dctx->kdf_outlen = (size_t)p1;
  320. return 1;
  321. case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN:
  322. *(int *)p2 = dctx->kdf_outlen;
  323. return 1;
  324. case EVP_PKEY_CTRL_EC_KDF_UKM:
  325. OPENSSL_free(dctx->kdf_ukm);
  326. dctx->kdf_ukm = p2;
  327. if (p2)
  328. dctx->kdf_ukmlen = p1;
  329. else
  330. dctx->kdf_ukmlen = 0;
  331. return 1;
  332. case EVP_PKEY_CTRL_GET_EC_KDF_UKM:
  333. *(unsigned char **)p2 = dctx->kdf_ukm;
  334. return dctx->kdf_ukmlen;
  335. case EVP_PKEY_CTRL_MD:
  336. if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 &&
  337. EVP_MD_type((const EVP_MD *)p2) != NID_ecdsa_with_SHA1 &&
  338. EVP_MD_type((const EVP_MD *)p2) != NID_sha224 &&
  339. EVP_MD_type((const EVP_MD *)p2) != NID_sha256 &&
  340. EVP_MD_type((const EVP_MD *)p2) != NID_sha384 &&
  341. EVP_MD_type((const EVP_MD *)p2) != NID_sha512) {
  342. ECerr(EC_F_PKEY_EC_CTRL, EC_R_INVALID_DIGEST_TYPE);
  343. return 0;
  344. }
  345. dctx->md = p2;
  346. return 1;
  347. case EVP_PKEY_CTRL_GET_MD:
  348. *(const EVP_MD **)p2 = dctx->md;
  349. return 1;
  350. case EVP_PKEY_CTRL_PEER_KEY:
  351. /* Default behaviour is OK */
  352. case EVP_PKEY_CTRL_DIGESTINIT:
  353. case EVP_PKEY_CTRL_PKCS7_SIGN:
  354. case EVP_PKEY_CTRL_CMS_SIGN:
  355. return 1;
  356. default:
  357. return -2;
  358. }
  359. }
  360. static int pkey_ec_ctrl_str(EVP_PKEY_CTX *ctx,
  361. const char *type, const char *value)
  362. {
  363. if (strcmp(type, "ec_paramgen_curve") == 0) {
  364. int nid;
  365. nid = EC_curve_nist2nid(value);
  366. if (nid == NID_undef)
  367. nid = OBJ_sn2nid(value);
  368. if (nid == NID_undef)
  369. nid = OBJ_ln2nid(value);
  370. if (nid == NID_undef) {
  371. ECerr(EC_F_PKEY_EC_CTRL_STR, EC_R_INVALID_CURVE);
  372. return 0;
  373. }
  374. return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid);
  375. } else if (strcmp(type, "ec_param_enc") == 0) {
  376. int param_enc;
  377. if (strcmp(value, "explicit") == 0)
  378. param_enc = 0;
  379. else if (strcmp(value, "named_curve") == 0)
  380. param_enc = OPENSSL_EC_NAMED_CURVE;
  381. else
  382. return -2;
  383. return EVP_PKEY_CTX_set_ec_param_enc(ctx, param_enc);
  384. } else if (strcmp(type, "ecdh_kdf_md") == 0) {
  385. const EVP_MD *md;
  386. if ((md = EVP_get_digestbyname(value)) == NULL) {
  387. ECerr(EC_F_PKEY_EC_CTRL_STR, EC_R_INVALID_DIGEST);
  388. return 0;
  389. }
  390. return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, md);
  391. } else if (strcmp(type, "ecdh_cofactor_mode") == 0) {
  392. int co_mode;
  393. co_mode = atoi(value);
  394. return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, co_mode);
  395. }
  396. return -2;
  397. }
  398. static int pkey_ec_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  399. {
  400. EC_KEY *ec = NULL;
  401. EC_PKEY_CTX *dctx = ctx->data;
  402. int ret = 0;
  403. if (dctx->gen_group == NULL) {
  404. ECerr(EC_F_PKEY_EC_PARAMGEN, EC_R_NO_PARAMETERS_SET);
  405. return 0;
  406. }
  407. ec = EC_KEY_new();
  408. if (!ec)
  409. return 0;
  410. ret = EC_KEY_set_group(ec, dctx->gen_group);
  411. if (ret)
  412. EVP_PKEY_assign_EC_KEY(pkey, ec);
  413. else
  414. EC_KEY_free(ec);
  415. return ret;
  416. }
  417. static int pkey_ec_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  418. {
  419. EC_KEY *ec = NULL;
  420. EC_PKEY_CTX *dctx = ctx->data;
  421. if (ctx->pkey == NULL && dctx->gen_group == NULL) {
  422. ECerr(EC_F_PKEY_EC_KEYGEN, EC_R_NO_PARAMETERS_SET);
  423. return 0;
  424. }
  425. ec = EC_KEY_new();
  426. if (!ec)
  427. return 0;
  428. EVP_PKEY_assign_EC_KEY(pkey, ec);
  429. if (ctx->pkey) {
  430. /* Note: if error return, pkey is freed by parent routine */
  431. if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
  432. return 0;
  433. } else {
  434. if (!EC_KEY_set_group(ec, dctx->gen_group))
  435. return 0;
  436. }
  437. return EC_KEY_generate_key(pkey->pkey.ec);
  438. }
  439. const EVP_PKEY_METHOD ec_pkey_meth = {
  440. EVP_PKEY_EC,
  441. 0,
  442. pkey_ec_init,
  443. pkey_ec_copy,
  444. pkey_ec_cleanup,
  445. 0,
  446. pkey_ec_paramgen,
  447. 0,
  448. pkey_ec_keygen,
  449. 0,
  450. pkey_ec_sign,
  451. 0,
  452. pkey_ec_verify,
  453. 0, 0,
  454. 0, 0, 0, 0,
  455. 0, 0,
  456. 0, 0,
  457. 0,
  458. #ifndef OPENSSL_NO_EC
  459. pkey_ec_kdf_derive,
  460. #else
  461. 0,
  462. #endif
  463. pkey_ec_ctrl,
  464. pkey_ec_ctrl_str
  465. };