ec_pmeth.c 13 KB

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