ec_pmeth.c 13 KB

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