2
0

ec_pmeth.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*
  2. * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. /*
  10. * ECDH and ECDSA low level APIs are deprecated for public use, but still ok
  11. * for internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdio.h>
  15. #include "internal/cryptlib.h"
  16. #include <openssl/asn1t.h>
  17. #include <openssl/x509.h>
  18. #include <openssl/ec.h>
  19. #include "ec_local.h"
  20. #include <openssl/evp.h>
  21. #include "crypto/evp.h"
  22. /* EC pkey context structure */
  23. typedef struct {
  24. /* Key and paramgen group */
  25. EC_GROUP *gen_group;
  26. /* message digest */
  27. const EVP_MD *md;
  28. /* Duplicate key if custom cofactor needed */
  29. EC_KEY *co_key;
  30. /* Cofactor mode */
  31. signed char cofactor_mode;
  32. /* KDF (if any) to use for ECDH */
  33. char kdf_type;
  34. /* Message digest to use for key derivation */
  35. const EVP_MD *kdf_md;
  36. /* User key material */
  37. unsigned char *kdf_ukm;
  38. size_t kdf_ukmlen;
  39. /* KDF output length */
  40. size_t kdf_outlen;
  41. } EC_PKEY_CTX;
  42. static int pkey_ec_init(EVP_PKEY_CTX *ctx)
  43. {
  44. EC_PKEY_CTX *dctx;
  45. if ((dctx = OPENSSL_zalloc(sizeof(*dctx))) == NULL)
  46. return 0;
  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, const 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 != NULL) {
  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. ctx->data = NULL;
  91. }
  92. }
  93. static int pkey_ec_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  94. const unsigned char *tbs, size_t tbslen)
  95. {
  96. int ret, type;
  97. unsigned int sltmp;
  98. EC_PKEY_CTX *dctx = ctx->data;
  99. /*
  100. * Discard const. Its marked as const because this may be a cached copy of
  101. * the "real" key. These calls don't make any modifications that need to
  102. * be reflected back in the "original" key.
  103. */
  104. EC_KEY *ec = (EC_KEY *)EVP_PKEY_get0_EC_KEY(ctx->pkey);
  105. const int sig_sz = ECDSA_size(ec);
  106. /* ensure cast to size_t is safe */
  107. if (!ossl_assert(sig_sz > 0))
  108. return 0;
  109. if (sig == NULL) {
  110. *siglen = (size_t)sig_sz;
  111. return 1;
  112. }
  113. if (*siglen < (size_t)sig_sz) {
  114. ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
  115. return 0;
  116. }
  117. type = (dctx->md != NULL) ? EVP_MD_get_type(dctx->md) : NID_sha1;
  118. ret = ECDSA_sign(type, tbs, tbslen, sig, &sltmp, ec);
  119. if (ret <= 0)
  120. return ret;
  121. *siglen = (size_t)sltmp;
  122. return 1;
  123. }
  124. static int pkey_ec_verify(EVP_PKEY_CTX *ctx,
  125. const unsigned char *sig, size_t siglen,
  126. const unsigned char *tbs, size_t tbslen)
  127. {
  128. int ret, type;
  129. EC_PKEY_CTX *dctx = ctx->data;
  130. /*
  131. * Discard const. Its marked as const because this may be a cached copy of
  132. * the "real" key. These calls don't make any modifications that need to
  133. * be reflected back in the "original" key.
  134. */
  135. EC_KEY *ec = (EC_KEY *)EVP_PKEY_get0_EC_KEY(ctx->pkey);
  136. if (dctx->md)
  137. type = EVP_MD_get_type(dctx->md);
  138. else
  139. type = NID_sha1;
  140. ret = ECDSA_verify(type, tbs, tbslen, sig, siglen, ec);
  141. return ret;
  142. }
  143. #ifndef OPENSSL_NO_EC
  144. static int pkey_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
  145. {
  146. int ret;
  147. size_t outlen;
  148. const EC_POINT *pubkey = NULL;
  149. EC_KEY *eckey;
  150. const EC_KEY *eckeypub;
  151. EC_PKEY_CTX *dctx = ctx->data;
  152. if (ctx->pkey == NULL || ctx->peerkey == NULL) {
  153. ERR_raise(ERR_LIB_EC, EC_R_KEYS_NOT_SET);
  154. return 0;
  155. }
  156. eckeypub = EVP_PKEY_get0_EC_KEY(ctx->peerkey);
  157. if (eckeypub == NULL) {
  158. ERR_raise(ERR_LIB_EC, EC_R_KEYS_NOT_SET);
  159. return 0;
  160. }
  161. eckey = dctx->co_key ? dctx->co_key
  162. : (EC_KEY *)EVP_PKEY_get0_EC_KEY(ctx->pkey);
  163. if (!key) {
  164. const EC_GROUP *group;
  165. group = EC_KEY_get0_group(eckey);
  166. if (group == NULL)
  167. return 0;
  168. *keylen = (EC_GROUP_get_degree(group) + 7) / 8;
  169. return 1;
  170. }
  171. pubkey = EC_KEY_get0_public_key(eckeypub);
  172. /*
  173. * NB: unlike PKCS#3 DH, if *outlen is less than maximum size this is not
  174. * an error, the result is truncated.
  175. */
  176. outlen = *keylen;
  177. ret = ECDH_compute_key(key, outlen, pubkey, eckey, 0);
  178. if (ret <= 0)
  179. return 0;
  180. *keylen = ret;
  181. return 1;
  182. }
  183. static int pkey_ec_kdf_derive(EVP_PKEY_CTX *ctx,
  184. unsigned char *key, size_t *keylen)
  185. {
  186. EC_PKEY_CTX *dctx = ctx->data;
  187. unsigned char *ktmp = NULL;
  188. size_t ktmplen;
  189. int rv = 0;
  190. if (dctx->kdf_type == EVP_PKEY_ECDH_KDF_NONE)
  191. return pkey_ec_derive(ctx, key, keylen);
  192. if (!key) {
  193. *keylen = dctx->kdf_outlen;
  194. return 1;
  195. }
  196. if (*keylen != dctx->kdf_outlen)
  197. return 0;
  198. if (!pkey_ec_derive(ctx, NULL, &ktmplen))
  199. return 0;
  200. if ((ktmp = OPENSSL_malloc(ktmplen)) == NULL)
  201. return 0;
  202. if (!pkey_ec_derive(ctx, ktmp, &ktmplen))
  203. goto err;
  204. /* Do KDF stuff */
  205. if (!ossl_ecdh_kdf_X9_63(key, *keylen, ktmp, ktmplen,
  206. dctx->kdf_ukm, dctx->kdf_ukmlen, dctx->kdf_md,
  207. ctx->libctx, ctx->propquery))
  208. goto err;
  209. rv = 1;
  210. err:
  211. OPENSSL_clear_free(ktmp, ktmplen);
  212. return rv;
  213. }
  214. #endif
  215. static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  216. {
  217. EC_PKEY_CTX *dctx = ctx->data;
  218. EC_GROUP *group;
  219. switch (type) {
  220. case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
  221. group = EC_GROUP_new_by_curve_name(p1);
  222. if (group == NULL) {
  223. ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
  224. return 0;
  225. }
  226. EC_GROUP_free(dctx->gen_group);
  227. dctx->gen_group = group;
  228. return 1;
  229. case EVP_PKEY_CTRL_EC_PARAM_ENC:
  230. if (!dctx->gen_group) {
  231. ERR_raise(ERR_LIB_EC, EC_R_NO_PARAMETERS_SET);
  232. return 0;
  233. }
  234. EC_GROUP_set_asn1_flag(dctx->gen_group, p1);
  235. return 1;
  236. #ifndef OPENSSL_NO_EC
  237. case EVP_PKEY_CTRL_EC_ECDH_COFACTOR:
  238. if (p1 == -2) {
  239. if (dctx->cofactor_mode != -1)
  240. return dctx->cofactor_mode;
  241. else {
  242. const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ctx->pkey);
  243. return EC_KEY_get_flags(ec_key) & EC_FLAG_COFACTOR_ECDH ? 1 : 0;
  244. }
  245. } else if (p1 < -1 || p1 > 1)
  246. return -2;
  247. dctx->cofactor_mode = p1;
  248. if (p1 != -1) {
  249. EC_KEY *ec_key = (EC_KEY *)EVP_PKEY_get0_EC_KEY(ctx->pkey);
  250. /*
  251. * We discarded the "const" above. This will only work if the key is
  252. * a "real" legacy key, and not a cached copy of a provided key
  253. */
  254. if (evp_pkey_is_provided(ctx->pkey)) {
  255. ERR_raise(ERR_LIB_EC, ERR_R_UNSUPPORTED);
  256. return 0;
  257. }
  258. if (!ec_key->group)
  259. return -2;
  260. /* If cofactor is 1 cofactor mode does nothing */
  261. if (BN_is_one(ec_key->group->cofactor))
  262. return 1;
  263. if (!dctx->co_key) {
  264. dctx->co_key = EC_KEY_dup(ec_key);
  265. if (!dctx->co_key)
  266. return 0;
  267. }
  268. if (p1)
  269. EC_KEY_set_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH);
  270. else
  271. EC_KEY_clear_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH);
  272. } else {
  273. EC_KEY_free(dctx->co_key);
  274. dctx->co_key = NULL;
  275. }
  276. return 1;
  277. #endif
  278. case EVP_PKEY_CTRL_EC_KDF_TYPE:
  279. if (p1 == -2)
  280. return dctx->kdf_type;
  281. if (p1 != EVP_PKEY_ECDH_KDF_NONE && p1 != EVP_PKEY_ECDH_KDF_X9_63)
  282. return -2;
  283. dctx->kdf_type = p1;
  284. return 1;
  285. case EVP_PKEY_CTRL_EC_KDF_MD:
  286. dctx->kdf_md = p2;
  287. return 1;
  288. case EVP_PKEY_CTRL_GET_EC_KDF_MD:
  289. *(const EVP_MD **)p2 = dctx->kdf_md;
  290. return 1;
  291. case EVP_PKEY_CTRL_EC_KDF_OUTLEN:
  292. if (p1 <= 0)
  293. return -2;
  294. dctx->kdf_outlen = (size_t)p1;
  295. return 1;
  296. case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN:
  297. *(int *)p2 = dctx->kdf_outlen;
  298. return 1;
  299. case EVP_PKEY_CTRL_EC_KDF_UKM:
  300. OPENSSL_free(dctx->kdf_ukm);
  301. dctx->kdf_ukm = p2;
  302. if (p2)
  303. dctx->kdf_ukmlen = p1;
  304. else
  305. dctx->kdf_ukmlen = 0;
  306. return 1;
  307. case EVP_PKEY_CTRL_GET_EC_KDF_UKM:
  308. *(unsigned char **)p2 = dctx->kdf_ukm;
  309. return dctx->kdf_ukmlen;
  310. case EVP_PKEY_CTRL_MD:
  311. if (EVP_MD_get_type((const EVP_MD *)p2) != NID_sha1 &&
  312. EVP_MD_get_type((const EVP_MD *)p2) != NID_ecdsa_with_SHA1 &&
  313. EVP_MD_get_type((const EVP_MD *)p2) != NID_sha224 &&
  314. EVP_MD_get_type((const EVP_MD *)p2) != NID_sha256 &&
  315. EVP_MD_get_type((const EVP_MD *)p2) != NID_sha384 &&
  316. EVP_MD_get_type((const EVP_MD *)p2) != NID_sha512 &&
  317. EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_224 &&
  318. EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_256 &&
  319. EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_384 &&
  320. EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_512 &&
  321. EVP_MD_get_type((const EVP_MD *)p2) != NID_sm3) {
  322. ERR_raise(ERR_LIB_EC, EC_R_INVALID_DIGEST_TYPE);
  323. return 0;
  324. }
  325. dctx->md = p2;
  326. return 1;
  327. case EVP_PKEY_CTRL_GET_MD:
  328. *(const EVP_MD **)p2 = dctx->md;
  329. return 1;
  330. case EVP_PKEY_CTRL_PEER_KEY:
  331. /* Default behaviour is OK */
  332. case EVP_PKEY_CTRL_DIGESTINIT:
  333. case EVP_PKEY_CTRL_PKCS7_SIGN:
  334. case EVP_PKEY_CTRL_CMS_SIGN:
  335. return 1;
  336. default:
  337. return -2;
  338. }
  339. }
  340. static int pkey_ec_ctrl_str(EVP_PKEY_CTX *ctx,
  341. const char *type, const char *value)
  342. {
  343. if (strcmp(type, "ec_paramgen_curve") == 0) {
  344. int nid;
  345. nid = EC_curve_nist2nid(value);
  346. if (nid == NID_undef)
  347. nid = OBJ_sn2nid(value);
  348. if (nid == NID_undef)
  349. nid = OBJ_ln2nid(value);
  350. if (nid == NID_undef) {
  351. ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
  352. return 0;
  353. }
  354. return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid);
  355. } else if (strcmp(type, "ec_param_enc") == 0) {
  356. int param_enc;
  357. if (strcmp(value, "explicit") == 0)
  358. param_enc = 0;
  359. else if (strcmp(value, "named_curve") == 0)
  360. param_enc = OPENSSL_EC_NAMED_CURVE;
  361. else
  362. return -2;
  363. return EVP_PKEY_CTX_set_ec_param_enc(ctx, param_enc);
  364. } else if (strcmp(type, "ecdh_kdf_md") == 0) {
  365. const EVP_MD *md;
  366. if ((md = EVP_get_digestbyname(value)) == NULL) {
  367. ERR_raise(ERR_LIB_EC, EC_R_INVALID_DIGEST);
  368. return 0;
  369. }
  370. return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, md);
  371. } else if (strcmp(type, "ecdh_cofactor_mode") == 0) {
  372. int co_mode;
  373. co_mode = atoi(value);
  374. return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, co_mode);
  375. }
  376. return -2;
  377. }
  378. static int pkey_ec_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  379. {
  380. EC_KEY *ec = NULL;
  381. EC_PKEY_CTX *dctx = ctx->data;
  382. int ret;
  383. if (dctx->gen_group == NULL) {
  384. ERR_raise(ERR_LIB_EC, EC_R_NO_PARAMETERS_SET);
  385. return 0;
  386. }
  387. ec = EC_KEY_new();
  388. if (ec == NULL)
  389. return 0;
  390. if (!(ret = EC_KEY_set_group(ec, dctx->gen_group))
  391. || !ossl_assert(ret = EVP_PKEY_assign_EC_KEY(pkey, ec)))
  392. EC_KEY_free(ec);
  393. return ret;
  394. }
  395. static int pkey_ec_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  396. {
  397. EC_KEY *ec = NULL;
  398. EC_PKEY_CTX *dctx = ctx->data;
  399. int ret;
  400. if (ctx->pkey == NULL && dctx->gen_group == NULL) {
  401. ERR_raise(ERR_LIB_EC, EC_R_NO_PARAMETERS_SET);
  402. return 0;
  403. }
  404. ec = EC_KEY_new();
  405. if (ec == NULL)
  406. return 0;
  407. if (!ossl_assert(EVP_PKEY_assign_EC_KEY(pkey, ec))) {
  408. EC_KEY_free(ec);
  409. return 0;
  410. }
  411. /* Note: if error is returned, we count on caller to free pkey->pkey.ec */
  412. if (ctx->pkey != NULL)
  413. ret = EVP_PKEY_copy_parameters(pkey, ctx->pkey);
  414. else
  415. ret = EC_KEY_set_group(ec, dctx->gen_group);
  416. return ret ? EC_KEY_generate_key(ec) : 0;
  417. }
  418. static const EVP_PKEY_METHOD ec_pkey_meth = {
  419. EVP_PKEY_EC,
  420. 0,
  421. pkey_ec_init,
  422. pkey_ec_copy,
  423. pkey_ec_cleanup,
  424. 0,
  425. pkey_ec_paramgen,
  426. 0,
  427. pkey_ec_keygen,
  428. 0,
  429. pkey_ec_sign,
  430. 0,
  431. pkey_ec_verify,
  432. 0, 0,
  433. 0, 0, 0, 0,
  434. 0,
  435. 0,
  436. 0,
  437. 0,
  438. 0,
  439. #ifndef OPENSSL_NO_EC
  440. pkey_ec_kdf_derive,
  441. #else
  442. 0,
  443. #endif
  444. pkey_ec_ctrl,
  445. pkey_ec_ctrl_str
  446. };
  447. const EVP_PKEY_METHOD *ossl_ec_pkey_method(void)
  448. {
  449. return &ec_pkey_meth;
  450. }