ec_pmeth.c 12 KB

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