ec_pmeth.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /*
  2. * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/asn1t.h>
  12. #include <openssl/x509.h>
  13. #include <openssl/ec.h>
  14. #include "ec_lcl.h"
  15. #include <openssl/evp.h>
  16. #include "internal/evp_int.h"
  17. #if !defined(OPENSSL_NO_SM2)
  18. # include <openssl/sm2.h>
  19. #endif
  20. /* EC pkey context structure */
  21. typedef struct {
  22. /* Key and paramgen group */
  23. EC_GROUP *gen_group;
  24. /* message digest */
  25. const EVP_MD *md;
  26. /* Duplicate key if custom cofactor needed */
  27. EC_KEY *co_key;
  28. /* Cofactor mode */
  29. signed char cofactor_mode;
  30. /* KDF (if any) to use for ECDH */
  31. char kdf_type;
  32. /* Message digest to use for key derivation */
  33. const EVP_MD *kdf_md;
  34. /* User key material */
  35. unsigned char *kdf_ukm;
  36. size_t kdf_ukmlen;
  37. /* KDF output length */
  38. size_t kdf_outlen;
  39. } EC_PKEY_CTX;
  40. static int pkey_ec_init(EVP_PKEY_CTX *ctx)
  41. {
  42. EC_PKEY_CTX *dctx;
  43. if ((dctx = OPENSSL_zalloc(sizeof(*dctx))) == NULL) {
  44. ECerr(EC_F_PKEY_EC_INIT, ERR_R_MALLOC_FAILURE);
  45. return 0;
  46. }
  47. dctx->cofactor_mode = -1;
  48. dctx->kdf_type = EVP_PKEY_ECDH_KDF_NONE;
  49. ctx->data = dctx;
  50. return 1;
  51. }
  52. static int pkey_ec_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
  53. {
  54. EC_PKEY_CTX *dctx, *sctx;
  55. if (!pkey_ec_init(dst))
  56. return 0;
  57. sctx = src->data;
  58. dctx = dst->data;
  59. if (sctx->gen_group) {
  60. dctx->gen_group = EC_GROUP_dup(sctx->gen_group);
  61. if (!dctx->gen_group)
  62. return 0;
  63. }
  64. dctx->md = sctx->md;
  65. if (sctx->co_key) {
  66. dctx->co_key = EC_KEY_dup(sctx->co_key);
  67. if (!dctx->co_key)
  68. return 0;
  69. }
  70. dctx->kdf_type = sctx->kdf_type;
  71. dctx->kdf_md = sctx->kdf_md;
  72. dctx->kdf_outlen = sctx->kdf_outlen;
  73. if (sctx->kdf_ukm) {
  74. dctx->kdf_ukm = OPENSSL_memdup(sctx->kdf_ukm, sctx->kdf_ukmlen);
  75. if (!dctx->kdf_ukm)
  76. return 0;
  77. } else
  78. dctx->kdf_ukm = NULL;
  79. dctx->kdf_ukmlen = sctx->kdf_ukmlen;
  80. return 1;
  81. }
  82. static void pkey_ec_cleanup(EVP_PKEY_CTX *ctx)
  83. {
  84. EC_PKEY_CTX *dctx = ctx->data;
  85. if (dctx) {
  86. EC_GROUP_free(dctx->gen_group);
  87. EC_KEY_free(dctx->co_key);
  88. OPENSSL_free(dctx->kdf_ukm);
  89. OPENSSL_free(dctx);
  90. }
  91. }
  92. static int pkey_ec_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  93. const unsigned char *tbs, size_t tbslen)
  94. {
  95. int ret, type;
  96. unsigned int sltmp;
  97. EC_PKEY_CTX *dctx = ctx->data;
  98. EC_KEY *ec = ctx->pkey->pkey.ec;
  99. const int ec_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
  100. if (!sig) {
  101. *siglen = ECDSA_size(ec);
  102. return 1;
  103. } else if (*siglen < (size_t)ECDSA_size(ec)) {
  104. ECerr(EC_F_PKEY_EC_SIGN, EC_R_BUFFER_TOO_SMALL);
  105. return 0;
  106. }
  107. if (dctx->md)
  108. type = EVP_MD_type(dctx->md);
  109. else
  110. type = NID_sha1;
  111. if (ec_nid == NID_sm2) {
  112. #if defined(OPENSSL_NO_SM2)
  113. ret = -1;
  114. #else
  115. ret = SM2_sign(type, tbs, tbslen, sig, &sltmp, ec);
  116. #endif
  117. } else {
  118. ret = ECDSA_sign(type, tbs, tbslen, sig, &sltmp, ec);
  119. }
  120. if (ret <= 0)
  121. return ret;
  122. *siglen = (size_t)sltmp;
  123. return 1;
  124. }
  125. static int pkey_ec_verify(EVP_PKEY_CTX *ctx,
  126. const unsigned char *sig, size_t siglen,
  127. const unsigned char *tbs, size_t tbslen)
  128. {
  129. int ret, type;
  130. EC_PKEY_CTX *dctx = ctx->data;
  131. EC_KEY *ec = ctx->pkey->pkey.ec;
  132. const int ec_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
  133. if (dctx->md)
  134. type = EVP_MD_type(dctx->md);
  135. else
  136. type = NID_sha1;
  137. if (ec_nid == NID_sm2) {
  138. #if defined(OPENSSL_NO_SM2)
  139. ret = -1;
  140. #else
  141. ret = SM2_verify(type, tbs, tbslen, sig, siglen, ec);
  142. #endif
  143. } else {
  144. ret = ECDSA_verify(type, tbs, tbslen, sig, siglen, ec);
  145. }
  146. return ret;
  147. }
  148. #ifndef OPENSSL_NO_EC
  149. static int pkey_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
  150. {
  151. int ret;
  152. size_t outlen;
  153. const EC_POINT *pubkey = NULL;
  154. EC_KEY *eckey;
  155. EC_PKEY_CTX *dctx = ctx->data;
  156. if (!ctx->pkey || !ctx->peerkey) {
  157. ECerr(EC_F_PKEY_EC_DERIVE, EC_R_KEYS_NOT_SET);
  158. return 0;
  159. }
  160. eckey = dctx->co_key ? dctx->co_key : ctx->pkey->pkey.ec;
  161. if (!key) {
  162. const EC_GROUP *group;
  163. group = EC_KEY_get0_group(eckey);
  164. *keylen = (EC_GROUP_get_degree(group) + 7) / 8;
  165. return 1;
  166. }
  167. pubkey = EC_KEY_get0_public_key(ctx->peerkey->pkey.ec);
  168. /*
  169. * NB: unlike PKCS#3 DH, if *outlen is less than maximum size this is not
  170. * an error, the result is truncated.
  171. */
  172. outlen = *keylen;
  173. ret = ECDH_compute_key(key, outlen, pubkey, eckey, 0);
  174. if (ret <= 0)
  175. return 0;
  176. *keylen = ret;
  177. return 1;
  178. }
  179. static int pkey_ecies_encrypt(EVP_PKEY_CTX *ctx,
  180. unsigned char *out, size_t *outlen,
  181. const unsigned char *in, size_t inlen)
  182. {
  183. int ret;
  184. EC_KEY *ec = ctx->pkey->pkey.ec;
  185. const int ec_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
  186. if (ec_nid == NID_sm2) {
  187. # if defined(OPENSSL_NO_SM2)
  188. ret = -1;
  189. # else
  190. int md_type;
  191. EC_PKEY_CTX *dctx = ctx->data;
  192. if (dctx->md)
  193. md_type = EVP_MD_type(dctx->md);
  194. else if (ec_nid == NID_sm2)
  195. md_type = NID_sm3;
  196. else
  197. md_type = NID_sha256;
  198. if (out == NULL) {
  199. *outlen = SM2_ciphertext_size(ec, EVP_get_digestbynid(md_type),
  200. inlen);
  201. ret = 1;
  202. }
  203. else {
  204. ret = SM2_encrypt(ec, EVP_get_digestbynid(md_type),
  205. in, inlen, out, outlen);
  206. }
  207. # endif
  208. } else {
  209. /* standard ECIES not implemented */
  210. ret = -1;
  211. }
  212. return ret;
  213. }
  214. static int pkey_ecies_decrypt(EVP_PKEY_CTX *ctx,
  215. unsigned char *out, size_t *outlen,
  216. const unsigned char *in, size_t inlen)
  217. {
  218. int ret;
  219. EC_KEY *ec = ctx->pkey->pkey.ec;
  220. const int ec_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
  221. if (ec_nid == NID_sm2) {
  222. # if defined(OPENSSL_NO_SM2)
  223. ret = -1;
  224. # else
  225. int md_type;
  226. EC_PKEY_CTX *dctx = ctx->data;
  227. if (dctx->md)
  228. md_type = EVP_MD_type(dctx->md);
  229. else if (ec_nid == NID_sm2)
  230. md_type = NID_sm3;
  231. else
  232. md_type = NID_sha256;
  233. if (out == NULL) {
  234. *outlen = SM2_plaintext_size(ec, EVP_get_digestbynid(md_type), inlen);
  235. ret = 1;
  236. }
  237. else {
  238. ret = SM2_decrypt(ec, EVP_get_digestbynid(md_type),
  239. in, inlen, out, outlen);
  240. }
  241. # endif
  242. } else {
  243. /* standard ECIES not implemented */
  244. ret = -1;
  245. }
  246. return ret;
  247. }
  248. static int pkey_ec_kdf_derive(EVP_PKEY_CTX *ctx,
  249. unsigned char *key, size_t *keylen)
  250. {
  251. EC_PKEY_CTX *dctx = ctx->data;
  252. unsigned char *ktmp = NULL;
  253. size_t ktmplen;
  254. int rv = 0;
  255. if (dctx->kdf_type == EVP_PKEY_ECDH_KDF_NONE)
  256. return pkey_ec_derive(ctx, key, keylen);
  257. if (!key) {
  258. *keylen = dctx->kdf_outlen;
  259. return 1;
  260. }
  261. if (*keylen != dctx->kdf_outlen)
  262. return 0;
  263. if (!pkey_ec_derive(ctx, NULL, &ktmplen))
  264. return 0;
  265. if ((ktmp = OPENSSL_malloc(ktmplen)) == NULL) {
  266. ECerr(EC_F_PKEY_EC_KDF_DERIVE, ERR_R_MALLOC_FAILURE);
  267. return 0;
  268. }
  269. if (!pkey_ec_derive(ctx, ktmp, &ktmplen))
  270. goto err;
  271. /* Do KDF stuff */
  272. if (!ECDH_KDF_X9_62(key, *keylen, ktmp, ktmplen,
  273. dctx->kdf_ukm, dctx->kdf_ukmlen, dctx->kdf_md))
  274. goto err;
  275. rv = 1;
  276. err:
  277. OPENSSL_clear_free(ktmp, ktmplen);
  278. return rv;
  279. }
  280. #endif
  281. static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  282. {
  283. EC_PKEY_CTX *dctx = ctx->data;
  284. EC_GROUP *group;
  285. switch (type) {
  286. case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
  287. group = EC_GROUP_new_by_curve_name(p1);
  288. if (group == NULL) {
  289. ECerr(EC_F_PKEY_EC_CTRL, EC_R_INVALID_CURVE);
  290. return 0;
  291. }
  292. EC_GROUP_free(dctx->gen_group);
  293. dctx->gen_group = group;
  294. return 1;
  295. case EVP_PKEY_CTRL_EC_PARAM_ENC:
  296. if (!dctx->gen_group) {
  297. ECerr(EC_F_PKEY_EC_CTRL, EC_R_NO_PARAMETERS_SET);
  298. return 0;
  299. }
  300. EC_GROUP_set_asn1_flag(dctx->gen_group, p1);
  301. return 1;
  302. #ifndef OPENSSL_NO_EC
  303. case EVP_PKEY_CTRL_EC_ECDH_COFACTOR:
  304. if (p1 == -2) {
  305. if (dctx->cofactor_mode != -1)
  306. return dctx->cofactor_mode;
  307. else {
  308. EC_KEY *ec_key = ctx->pkey->pkey.ec;
  309. return EC_KEY_get_flags(ec_key) & EC_FLAG_COFACTOR_ECDH ? 1 : 0;
  310. }
  311. } else if (p1 < -1 || p1 > 1)
  312. return -2;
  313. dctx->cofactor_mode = p1;
  314. if (p1 != -1) {
  315. EC_KEY *ec_key = ctx->pkey->pkey.ec;
  316. if (!ec_key->group)
  317. return -2;
  318. /* If cofactor is 1 cofactor mode does nothing */
  319. if (BN_is_one(ec_key->group->cofactor))
  320. return 1;
  321. if (!dctx->co_key) {
  322. dctx->co_key = EC_KEY_dup(ec_key);
  323. if (!dctx->co_key)
  324. return 0;
  325. }
  326. if (p1)
  327. EC_KEY_set_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH);
  328. else
  329. EC_KEY_clear_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH);
  330. } else {
  331. EC_KEY_free(dctx->co_key);
  332. dctx->co_key = NULL;
  333. }
  334. return 1;
  335. #endif
  336. case EVP_PKEY_CTRL_EC_KDF_TYPE:
  337. if (p1 == -2)
  338. return dctx->kdf_type;
  339. if (p1 != EVP_PKEY_ECDH_KDF_NONE && p1 != EVP_PKEY_ECDH_KDF_X9_62)
  340. return -2;
  341. dctx->kdf_type = p1;
  342. return 1;
  343. case EVP_PKEY_CTRL_EC_KDF_MD:
  344. dctx->kdf_md = p2;
  345. return 1;
  346. case EVP_PKEY_CTRL_GET_EC_KDF_MD:
  347. *(const EVP_MD **)p2 = dctx->kdf_md;
  348. return 1;
  349. case EVP_PKEY_CTRL_EC_KDF_OUTLEN:
  350. if (p1 <= 0)
  351. return -2;
  352. dctx->kdf_outlen = (size_t)p1;
  353. return 1;
  354. case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN:
  355. *(int *)p2 = dctx->kdf_outlen;
  356. return 1;
  357. case EVP_PKEY_CTRL_EC_KDF_UKM:
  358. OPENSSL_free(dctx->kdf_ukm);
  359. dctx->kdf_ukm = p2;
  360. if (p2)
  361. dctx->kdf_ukmlen = p1;
  362. else
  363. dctx->kdf_ukmlen = 0;
  364. return 1;
  365. case EVP_PKEY_CTRL_GET_EC_KDF_UKM:
  366. *(unsigned char **)p2 = dctx->kdf_ukm;
  367. return dctx->kdf_ukmlen;
  368. case EVP_PKEY_CTRL_MD:
  369. if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 &&
  370. EVP_MD_type((const EVP_MD *)p2) != NID_ecdsa_with_SHA1 &&
  371. EVP_MD_type((const EVP_MD *)p2) != NID_sha224 &&
  372. EVP_MD_type((const EVP_MD *)p2) != NID_sha256 &&
  373. EVP_MD_type((const EVP_MD *)p2) != NID_sha384 &&
  374. EVP_MD_type((const EVP_MD *)p2) != NID_sha512 &&
  375. EVP_MD_type((const EVP_MD *)p2) != NID_sm3) {
  376. ECerr(EC_F_PKEY_EC_CTRL, EC_R_INVALID_DIGEST_TYPE);
  377. return 0;
  378. }
  379. dctx->md = p2;
  380. return 1;
  381. case EVP_PKEY_CTRL_GET_MD:
  382. *(const EVP_MD **)p2 = dctx->md;
  383. return 1;
  384. case EVP_PKEY_CTRL_PEER_KEY:
  385. /* Default behaviour is OK */
  386. case EVP_PKEY_CTRL_DIGESTINIT:
  387. case EVP_PKEY_CTRL_PKCS7_SIGN:
  388. case EVP_PKEY_CTRL_CMS_SIGN:
  389. return 1;
  390. default:
  391. return -2;
  392. }
  393. }
  394. static int pkey_ec_ctrl_str(EVP_PKEY_CTX *ctx,
  395. const char *type, const char *value)
  396. {
  397. if (strcmp(type, "ec_paramgen_curve") == 0) {
  398. int nid;
  399. nid = EC_curve_nist2nid(value);
  400. if (nid == NID_undef)
  401. nid = OBJ_sn2nid(value);
  402. if (nid == NID_undef)
  403. nid = OBJ_ln2nid(value);
  404. if (nid == NID_undef) {
  405. ECerr(EC_F_PKEY_EC_CTRL_STR, EC_R_INVALID_CURVE);
  406. return 0;
  407. }
  408. return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid);
  409. } else if (strcmp(type, "ec_param_enc") == 0) {
  410. int param_enc;
  411. if (strcmp(value, "explicit") == 0)
  412. param_enc = 0;
  413. else if (strcmp(value, "named_curve") == 0)
  414. param_enc = OPENSSL_EC_NAMED_CURVE;
  415. else
  416. return -2;
  417. return EVP_PKEY_CTX_set_ec_param_enc(ctx, param_enc);
  418. } else if (strcmp(type, "ecdh_kdf_md") == 0) {
  419. const EVP_MD *md;
  420. if ((md = EVP_get_digestbyname(value)) == NULL) {
  421. ECerr(EC_F_PKEY_EC_CTRL_STR, EC_R_INVALID_DIGEST);
  422. return 0;
  423. }
  424. return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, md);
  425. } else if (strcmp(type, "ecdh_cofactor_mode") == 0) {
  426. int co_mode;
  427. co_mode = atoi(value);
  428. return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, co_mode);
  429. }
  430. return -2;
  431. }
  432. static int pkey_ec_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  433. {
  434. EC_KEY *ec = NULL;
  435. EC_PKEY_CTX *dctx = ctx->data;
  436. int ret = 0;
  437. if (dctx->gen_group == NULL) {
  438. ECerr(EC_F_PKEY_EC_PARAMGEN, EC_R_NO_PARAMETERS_SET);
  439. return 0;
  440. }
  441. ec = EC_KEY_new();
  442. if (ec == NULL)
  443. return 0;
  444. ret = EC_KEY_set_group(ec, dctx->gen_group);
  445. if (ret)
  446. EVP_PKEY_assign_EC_KEY(pkey, ec);
  447. else
  448. EC_KEY_free(ec);
  449. return ret;
  450. }
  451. static int pkey_ec_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  452. {
  453. EC_KEY *ec = NULL;
  454. EC_PKEY_CTX *dctx = ctx->data;
  455. if (ctx->pkey == NULL && dctx->gen_group == NULL) {
  456. ECerr(EC_F_PKEY_EC_KEYGEN, EC_R_NO_PARAMETERS_SET);
  457. return 0;
  458. }
  459. ec = EC_KEY_new();
  460. if (!ec)
  461. return 0;
  462. EVP_PKEY_assign_EC_KEY(pkey, ec);
  463. if (ctx->pkey) {
  464. /* Note: if error return, pkey is freed by parent routine */
  465. if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
  466. return 0;
  467. } else {
  468. if (!EC_KEY_set_group(ec, dctx->gen_group))
  469. return 0;
  470. }
  471. return EC_KEY_generate_key(pkey->pkey.ec);
  472. }
  473. const EVP_PKEY_METHOD ec_pkey_meth = {
  474. EVP_PKEY_EC,
  475. 0,
  476. pkey_ec_init,
  477. pkey_ec_copy,
  478. pkey_ec_cleanup,
  479. 0,
  480. pkey_ec_paramgen,
  481. 0,
  482. pkey_ec_keygen,
  483. 0,
  484. pkey_ec_sign,
  485. 0,
  486. pkey_ec_verify,
  487. 0, 0,
  488. 0, 0, 0, 0,
  489. 0,
  490. pkey_ecies_encrypt,
  491. 0,
  492. pkey_ecies_decrypt,
  493. 0,
  494. #ifndef OPENSSL_NO_EC
  495. pkey_ec_kdf_derive,
  496. #else
  497. 0,
  498. #endif
  499. pkey_ec_ctrl,
  500. pkey_ec_ctrl_str
  501. };