ec_pmeth.c 13 KB

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