ec_pmeth.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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 "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 "evp_locl.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(EC_PKEY_CTX));
  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. if (dctx->gen_group)
  140. EC_GROUP_free(dctx->gen_group);
  141. if (dctx->co_key)
  142. EC_KEY_free(dctx->co_key);
  143. if (dctx->kdf_ukm)
  144. OPENSSL_free(dctx->kdf_ukm);
  145. OPENSSL_free(dctx);
  146. }
  147. }
  148. static int pkey_ec_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  149. const unsigned char *tbs, size_t tbslen)
  150. {
  151. int ret, type;
  152. unsigned int sltmp;
  153. EC_PKEY_CTX *dctx = ctx->data;
  154. EC_KEY *ec = ctx->pkey->pkey.ec;
  155. if (!sig) {
  156. *siglen = ECDSA_size(ec);
  157. return 1;
  158. } else if (*siglen < (size_t)ECDSA_size(ec)) {
  159. ECerr(EC_F_PKEY_EC_SIGN, EC_R_BUFFER_TOO_SMALL);
  160. return 0;
  161. }
  162. if (dctx->md)
  163. type = EVP_MD_type(dctx->md);
  164. else
  165. type = NID_sha1;
  166. ret = ECDSA_sign(type, tbs, tbslen, sig, &sltmp, ec);
  167. if (ret <= 0)
  168. return ret;
  169. *siglen = (size_t)sltmp;
  170. return 1;
  171. }
  172. static int pkey_ec_verify(EVP_PKEY_CTX *ctx,
  173. const unsigned char *sig, size_t siglen,
  174. const unsigned char *tbs, size_t tbslen)
  175. {
  176. int ret, type;
  177. EC_PKEY_CTX *dctx = ctx->data;
  178. EC_KEY *ec = ctx->pkey->pkey.ec;
  179. if (dctx->md)
  180. type = EVP_MD_type(dctx->md);
  181. else
  182. type = NID_sha1;
  183. ret = ECDSA_verify(type, tbs, tbslen, sig, siglen, ec);
  184. return ret;
  185. }
  186. #ifndef OPENSSL_NO_ECDH
  187. static int pkey_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
  188. size_t *keylen)
  189. {
  190. int ret;
  191. size_t outlen;
  192. const EC_POINT *pubkey = NULL;
  193. EC_KEY *eckey;
  194. EC_PKEY_CTX *dctx = ctx->data;
  195. if (!ctx->pkey || !ctx->peerkey) {
  196. ECerr(EC_F_PKEY_EC_DERIVE, EC_R_KEYS_NOT_SET);
  197. return 0;
  198. }
  199. eckey = dctx->co_key ? dctx->co_key : ctx->pkey->pkey.ec;
  200. if (!key) {
  201. const EC_GROUP *group;
  202. group = EC_KEY_get0_group(eckey);
  203. *keylen = (EC_GROUP_get_degree(group) + 7) / 8;
  204. return 1;
  205. }
  206. pubkey = EC_KEY_get0_public_key(ctx->peerkey->pkey.ec);
  207. /*
  208. * NB: unlike PKCS#3 DH, if *outlen is less than maximum size this is not
  209. * an error, the result is truncated.
  210. */
  211. outlen = *keylen;
  212. ret = ECDH_compute_key(key, outlen, pubkey, eckey, 0);
  213. if (ret <= 0)
  214. return 0;
  215. *keylen = ret;
  216. return 1;
  217. }
  218. static int pkey_ec_kdf_derive(EVP_PKEY_CTX *ctx,
  219. unsigned char *key, size_t *keylen)
  220. {
  221. EC_PKEY_CTX *dctx = ctx->data;
  222. unsigned char *ktmp = NULL;
  223. size_t ktmplen;
  224. int rv = 0;
  225. if (dctx->kdf_type == EVP_PKEY_ECDH_KDF_NONE)
  226. return pkey_ec_derive(ctx, key, keylen);
  227. if (!key) {
  228. *keylen = dctx->kdf_outlen;
  229. return 1;
  230. }
  231. if (*keylen != dctx->kdf_outlen)
  232. return 0;
  233. if (!pkey_ec_derive(ctx, NULL, &ktmplen))
  234. return 0;
  235. ktmp = OPENSSL_malloc(ktmplen);
  236. if (!ktmp)
  237. return 0;
  238. if (!pkey_ec_derive(ctx, ktmp, &ktmplen))
  239. goto err;
  240. /* Do KDF stuff */
  241. if (!ECDH_KDF_X9_62(key, *keylen, ktmp, ktmplen,
  242. dctx->kdf_ukm, dctx->kdf_ukmlen, dctx->kdf_md))
  243. goto err;
  244. rv = 1;
  245. err:
  246. if (ktmp) {
  247. OPENSSL_cleanse(ktmp, ktmplen);
  248. OPENSSL_free(ktmp);
  249. }
  250. return rv;
  251. }
  252. #endif
  253. static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  254. {
  255. EC_PKEY_CTX *dctx = ctx->data;
  256. EC_GROUP *group;
  257. switch (type) {
  258. case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
  259. group = EC_GROUP_new_by_curve_name(p1);
  260. if (group == NULL) {
  261. ECerr(EC_F_PKEY_EC_CTRL, EC_R_INVALID_CURVE);
  262. return 0;
  263. }
  264. if (dctx->gen_group)
  265. EC_GROUP_free(dctx->gen_group);
  266. dctx->gen_group = group;
  267. return 1;
  268. case EVP_PKEY_CTRL_EC_PARAM_ENC:
  269. if (!dctx->gen_group) {
  270. ECerr(EC_F_PKEY_EC_CTRL, EC_R_NO_PARAMETERS_SET);
  271. return 0;
  272. }
  273. EC_GROUP_set_asn1_flag(dctx->gen_group, p1);
  274. return 1;
  275. #ifndef OPENSSL_NO_ECDH
  276. case EVP_PKEY_CTRL_EC_ECDH_COFACTOR:
  277. if (p1 == -2) {
  278. if (dctx->cofactor_mode != -1)
  279. return dctx->cofactor_mode;
  280. else {
  281. EC_KEY *ec_key = ctx->pkey->pkey.ec;
  282. return EC_KEY_get_flags(ec_key) & EC_FLAG_COFACTOR_ECDH ? 1 :
  283. 0;
  284. }
  285. } else if (p1 < -1 || p1 > 1)
  286. return -2;
  287. dctx->cofactor_mode = p1;
  288. if (p1 != -1) {
  289. EC_KEY *ec_key = ctx->pkey->pkey.ec;
  290. if (!ec_key->group)
  291. return -2;
  292. /* If cofactor is 1 cofactor mode does nothing */
  293. if (BN_is_one(&ec_key->group->cofactor))
  294. return 1;
  295. if (!dctx->co_key) {
  296. dctx->co_key = EC_KEY_dup(ec_key);
  297. if (!dctx->co_key)
  298. return 0;
  299. }
  300. if (p1)
  301. EC_KEY_set_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH);
  302. else
  303. EC_KEY_clear_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH);
  304. } else if (dctx->co_key) {
  305. EC_KEY_free(dctx->co_key);
  306. dctx->co_key = NULL;
  307. }
  308. return 1;
  309. #endif
  310. case EVP_PKEY_CTRL_EC_KDF_TYPE:
  311. if (p1 == -2)
  312. return dctx->kdf_type;
  313. if (p1 != EVP_PKEY_ECDH_KDF_NONE && p1 != EVP_PKEY_ECDH_KDF_X9_62)
  314. return -2;
  315. dctx->kdf_type = p1;
  316. return 1;
  317. case EVP_PKEY_CTRL_EC_KDF_MD:
  318. dctx->kdf_md = p2;
  319. return 1;
  320. case EVP_PKEY_CTRL_GET_EC_KDF_MD:
  321. *(const EVP_MD **)p2 = dctx->kdf_md;
  322. return 1;
  323. case EVP_PKEY_CTRL_EC_KDF_OUTLEN:
  324. if (p1 <= 0)
  325. return -2;
  326. dctx->kdf_outlen = (size_t)p1;
  327. return 1;
  328. case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN:
  329. *(int *)p2 = dctx->kdf_outlen;
  330. return 1;
  331. case EVP_PKEY_CTRL_EC_KDF_UKM:
  332. if (dctx->kdf_ukm)
  333. OPENSSL_free(dctx->kdf_ukm);
  334. dctx->kdf_ukm = p2;
  335. if (p2)
  336. dctx->kdf_ukmlen = p1;
  337. else
  338. dctx->kdf_ukmlen = 0;
  339. return 1;
  340. case EVP_PKEY_CTRL_GET_EC_KDF_UKM:
  341. *(unsigned char **)p2 = dctx->kdf_ukm;
  342. return dctx->kdf_ukmlen;
  343. case EVP_PKEY_CTRL_MD:
  344. if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 &&
  345. EVP_MD_type((const EVP_MD *)p2) != NID_ecdsa_with_SHA1 &&
  346. EVP_MD_type((const EVP_MD *)p2) != NID_sha224 &&
  347. EVP_MD_type((const EVP_MD *)p2) != NID_sha256 &&
  348. EVP_MD_type((const EVP_MD *)p2) != NID_sha384 &&
  349. EVP_MD_type((const EVP_MD *)p2) != NID_sha512) {
  350. ECerr(EC_F_PKEY_EC_CTRL, EC_R_INVALID_DIGEST_TYPE);
  351. return 0;
  352. }
  353. dctx->md = p2;
  354. return 1;
  355. case EVP_PKEY_CTRL_GET_MD:
  356. *(const EVP_MD **)p2 = dctx->md;
  357. return 1;
  358. case EVP_PKEY_CTRL_PEER_KEY:
  359. /* Default behaviour is OK */
  360. case EVP_PKEY_CTRL_DIGESTINIT:
  361. case EVP_PKEY_CTRL_PKCS7_SIGN:
  362. case EVP_PKEY_CTRL_CMS_SIGN:
  363. return 1;
  364. default:
  365. return -2;
  366. }
  367. }
  368. static int pkey_ec_ctrl_str(EVP_PKEY_CTX *ctx,
  369. const char *type, const char *value)
  370. {
  371. if (!strcmp(type, "ec_paramgen_curve")) {
  372. int nid;
  373. nid = EC_curve_nist2nid(value);
  374. if (nid == NID_undef)
  375. nid = OBJ_sn2nid(value);
  376. if (nid == NID_undef)
  377. nid = OBJ_ln2nid(value);
  378. if (nid == NID_undef) {
  379. ECerr(EC_F_PKEY_EC_CTRL_STR, EC_R_INVALID_CURVE);
  380. return 0;
  381. }
  382. return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid);
  383. } else if (!strcmp(type, "ec_param_enc")) {
  384. int param_enc;
  385. if (!strcmp(value, "explicit"))
  386. param_enc = 0;
  387. else if (!strcmp(value, "named_curve"))
  388. param_enc = OPENSSL_EC_NAMED_CURVE;
  389. else
  390. return -2;
  391. return EVP_PKEY_CTX_set_ec_param_enc(ctx, param_enc);
  392. } else if (!strcmp(type, "ecdh_kdf_md")) {
  393. const EVP_MD *md;
  394. if (!(md = EVP_get_digestbyname(value))) {
  395. ECerr(EC_F_PKEY_EC_CTRL_STR, EC_R_INVALID_DIGEST);
  396. return 0;
  397. }
  398. return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, md);
  399. } else if (!strcmp(type, "ecdh_cofactor_mode")) {
  400. int co_mode;
  401. co_mode = atoi(value);
  402. return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, co_mode);
  403. }
  404. return -2;
  405. }
  406. static int pkey_ec_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  407. {
  408. EC_KEY *ec = NULL;
  409. EC_PKEY_CTX *dctx = ctx->data;
  410. int ret = 0;
  411. if (dctx->gen_group == NULL) {
  412. ECerr(EC_F_PKEY_EC_PARAMGEN, EC_R_NO_PARAMETERS_SET);
  413. return 0;
  414. }
  415. ec = EC_KEY_new();
  416. if (!ec)
  417. return 0;
  418. ret = EC_KEY_set_group(ec, dctx->gen_group);
  419. if (ret)
  420. EVP_PKEY_assign_EC_KEY(pkey, ec);
  421. else
  422. EC_KEY_free(ec);
  423. return ret;
  424. }
  425. static int pkey_ec_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  426. {
  427. EC_KEY *ec = NULL;
  428. EC_PKEY_CTX *dctx = ctx->data;
  429. if (ctx->pkey == NULL && dctx->gen_group == NULL) {
  430. ECerr(EC_F_PKEY_EC_KEYGEN, EC_R_NO_PARAMETERS_SET);
  431. return 0;
  432. }
  433. ec = EC_KEY_new();
  434. if (!ec)
  435. return 0;
  436. EVP_PKEY_assign_EC_KEY(pkey, ec);
  437. if (ctx->pkey) {
  438. /* Note: if error return, pkey is freed by parent routine */
  439. if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
  440. return 0;
  441. } else {
  442. if (!EC_KEY_set_group(ec, dctx->gen_group))
  443. return 0;
  444. }
  445. return EC_KEY_generate_key(pkey->pkey.ec);
  446. }
  447. const EVP_PKEY_METHOD ec_pkey_meth = {
  448. EVP_PKEY_EC,
  449. 0,
  450. pkey_ec_init,
  451. pkey_ec_copy,
  452. pkey_ec_cleanup,
  453. 0,
  454. pkey_ec_paramgen,
  455. 0,
  456. pkey_ec_keygen,
  457. 0,
  458. pkey_ec_sign,
  459. 0,
  460. pkey_ec_verify,
  461. 0, 0,
  462. 0, 0, 0, 0,
  463. 0, 0,
  464. 0, 0,
  465. 0,
  466. #ifndef OPENSSL_NO_ECDH
  467. pkey_ec_kdf_derive,
  468. #else
  469. 0,
  470. #endif
  471. pkey_ec_ctrl,
  472. pkey_ec_ctrl_str
  473. };