ec_kmgmt.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  1. /*
  2. * Copyright 2020-2023 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/ECDSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <string.h>
  15. #include <openssl/core_dispatch.h>
  16. #include <openssl/core_names.h>
  17. #include <openssl/bn.h>
  18. #include <openssl/err.h>
  19. #include <openssl/objects.h>
  20. #include <openssl/proverr.h>
  21. #include "crypto/bn.h"
  22. #include "crypto/ec.h"
  23. #include "prov/implementations.h"
  24. #include "prov/providercommon.h"
  25. #include "prov/provider_ctx.h"
  26. #include "internal/param_build_set.h"
  27. #ifndef FIPS_MODULE
  28. # ifndef OPENSSL_NO_SM2
  29. # include "crypto/sm2.h"
  30. # endif
  31. #endif
  32. static OSSL_FUNC_keymgmt_new_fn ec_newdata;
  33. static OSSL_FUNC_keymgmt_gen_init_fn ec_gen_init;
  34. static OSSL_FUNC_keymgmt_gen_set_template_fn ec_gen_set_template;
  35. static OSSL_FUNC_keymgmt_gen_set_params_fn ec_gen_set_params;
  36. static OSSL_FUNC_keymgmt_gen_settable_params_fn ec_gen_settable_params;
  37. static OSSL_FUNC_keymgmt_gen_fn ec_gen;
  38. static OSSL_FUNC_keymgmt_gen_cleanup_fn ec_gen_cleanup;
  39. static OSSL_FUNC_keymgmt_load_fn ec_load;
  40. static OSSL_FUNC_keymgmt_free_fn ec_freedata;
  41. static OSSL_FUNC_keymgmt_get_params_fn ec_get_params;
  42. static OSSL_FUNC_keymgmt_gettable_params_fn ec_gettable_params;
  43. static OSSL_FUNC_keymgmt_set_params_fn ec_set_params;
  44. static OSSL_FUNC_keymgmt_settable_params_fn ec_settable_params;
  45. static OSSL_FUNC_keymgmt_has_fn ec_has;
  46. static OSSL_FUNC_keymgmt_match_fn ec_match;
  47. static OSSL_FUNC_keymgmt_validate_fn ec_validate;
  48. static OSSL_FUNC_keymgmt_import_fn ec_import;
  49. static OSSL_FUNC_keymgmt_import_types_fn ec_import_types;
  50. static OSSL_FUNC_keymgmt_export_fn ec_export;
  51. static OSSL_FUNC_keymgmt_export_types_fn ec_export_types;
  52. static OSSL_FUNC_keymgmt_query_operation_name_fn ec_query_operation_name;
  53. static OSSL_FUNC_keymgmt_dup_fn ec_dup;
  54. #ifndef FIPS_MODULE
  55. # ifndef OPENSSL_NO_SM2
  56. static OSSL_FUNC_keymgmt_new_fn sm2_newdata;
  57. static OSSL_FUNC_keymgmt_gen_init_fn sm2_gen_init;
  58. static OSSL_FUNC_keymgmt_gen_fn sm2_gen;
  59. static OSSL_FUNC_keymgmt_get_params_fn sm2_get_params;
  60. static OSSL_FUNC_keymgmt_gettable_params_fn sm2_gettable_params;
  61. static OSSL_FUNC_keymgmt_settable_params_fn sm2_settable_params;
  62. static OSSL_FUNC_keymgmt_import_fn sm2_import;
  63. static OSSL_FUNC_keymgmt_query_operation_name_fn sm2_query_operation_name;
  64. static OSSL_FUNC_keymgmt_validate_fn sm2_validate;
  65. # endif
  66. #endif
  67. #define EC_DEFAULT_MD "SHA256"
  68. #define EC_POSSIBLE_SELECTIONS \
  69. (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS)
  70. #define SM2_DEFAULT_MD "SM3"
  71. static
  72. const char *ec_query_operation_name(int operation_id)
  73. {
  74. switch (operation_id) {
  75. case OSSL_OP_KEYEXCH:
  76. return "ECDH";
  77. case OSSL_OP_SIGNATURE:
  78. return "ECDSA";
  79. }
  80. return NULL;
  81. }
  82. #ifndef FIPS_MODULE
  83. # ifndef OPENSSL_NO_SM2
  84. static
  85. const char *sm2_query_operation_name(int operation_id)
  86. {
  87. switch (operation_id) {
  88. case OSSL_OP_SIGNATURE:
  89. return "SM2";
  90. }
  91. return NULL;
  92. }
  93. # endif
  94. #endif
  95. /*
  96. * Callers of key_to_params MUST make sure that domparams_to_params is also
  97. * called!
  98. *
  99. * This function only exports the bare keypair, domain parameters and other
  100. * parameters are exported separately.
  101. */
  102. static ossl_inline
  103. int key_to_params(const EC_KEY *eckey, OSSL_PARAM_BLD *tmpl,
  104. OSSL_PARAM params[], int include_private,
  105. unsigned char **pub_key)
  106. {
  107. BIGNUM *x = NULL, *y = NULL;
  108. const BIGNUM *priv_key = NULL;
  109. const EC_POINT *pub_point = NULL;
  110. const EC_GROUP *ecg = NULL;
  111. size_t pub_key_len = 0;
  112. int ret = 0;
  113. BN_CTX *bnctx = NULL;
  114. if (eckey == NULL
  115. || (ecg = EC_KEY_get0_group(eckey)) == NULL)
  116. return 0;
  117. priv_key = EC_KEY_get0_private_key(eckey);
  118. pub_point = EC_KEY_get0_public_key(eckey);
  119. if (pub_point != NULL) {
  120. OSSL_PARAM *p = NULL, *px = NULL, *py = NULL;
  121. /*
  122. * EC_POINT_point2buf() can generate random numbers in some
  123. * implementations so we need to ensure we use the correct libctx.
  124. */
  125. bnctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(eckey));
  126. if (bnctx == NULL)
  127. goto err;
  128. /* If we are doing a get then check first before decoding the point */
  129. if (tmpl == NULL) {
  130. p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_PUB_KEY);
  131. px = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_EC_PUB_X);
  132. py = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_EC_PUB_Y);
  133. }
  134. if (p != NULL || tmpl != NULL) {
  135. /* convert pub_point to a octet string according to the SECG standard */
  136. point_conversion_form_t format = EC_KEY_get_conv_form(eckey);
  137. if ((pub_key_len = EC_POINT_point2buf(ecg, pub_point,
  138. format,
  139. pub_key, bnctx)) == 0
  140. || !ossl_param_build_set_octet_string(tmpl, p,
  141. OSSL_PKEY_PARAM_PUB_KEY,
  142. *pub_key, pub_key_len))
  143. goto err;
  144. }
  145. if (px != NULL || py != NULL) {
  146. if (px != NULL) {
  147. x = BN_CTX_get(bnctx);
  148. if (x == NULL)
  149. goto err;
  150. }
  151. if (py != NULL) {
  152. y = BN_CTX_get(bnctx);
  153. if (y == NULL)
  154. goto err;
  155. }
  156. if (!EC_POINT_get_affine_coordinates(ecg, pub_point, x, y, bnctx))
  157. goto err;
  158. if (px != NULL
  159. && !ossl_param_build_set_bn(tmpl, px,
  160. OSSL_PKEY_PARAM_EC_PUB_X, x))
  161. goto err;
  162. if (py != NULL
  163. && !ossl_param_build_set_bn(tmpl, py,
  164. OSSL_PKEY_PARAM_EC_PUB_Y, y))
  165. goto err;
  166. }
  167. }
  168. if (priv_key != NULL && include_private) {
  169. size_t sz;
  170. int ecbits;
  171. /*
  172. * Key import/export should never leak the bit length of the secret
  173. * scalar in the key.
  174. *
  175. * For this reason, on export we use padded BIGNUMs with fixed length.
  176. *
  177. * When importing we also should make sure that, even if short lived,
  178. * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
  179. * soon as possible, so that any processing of this BIGNUM might opt for
  180. * constant time implementations in the backend.
  181. *
  182. * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
  183. * to preallocate the BIGNUM internal buffer to a fixed public size big
  184. * enough that operations performed during the processing never trigger
  185. * a realloc which would leak the size of the scalar through memory
  186. * accesses.
  187. *
  188. * Fixed Length
  189. * ------------
  190. *
  191. * The order of the large prime subgroup of the curve is our choice for
  192. * a fixed public size, as that is generally the upper bound for
  193. * generating a private key in EC cryptosystems and should fit all valid
  194. * secret scalars.
  195. *
  196. * For padding on export we just use the bit length of the order
  197. * converted to bytes (rounding up).
  198. *
  199. * For preallocating the BIGNUM storage we look at the number of "words"
  200. * required for the internal representation of the order, and we
  201. * preallocate 2 extra "words" in case any of the subsequent processing
  202. * might temporarily overflow the order length.
  203. */
  204. ecbits = EC_GROUP_order_bits(ecg);
  205. if (ecbits <= 0)
  206. goto err;
  207. sz = (ecbits + 7) / 8;
  208. if (!ossl_param_build_set_bn_pad(tmpl, params,
  209. OSSL_PKEY_PARAM_PRIV_KEY,
  210. priv_key, sz))
  211. goto err;
  212. }
  213. ret = 1;
  214. err:
  215. BN_CTX_free(bnctx);
  216. return ret;
  217. }
  218. static ossl_inline
  219. int otherparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl,
  220. OSSL_PARAM params[])
  221. {
  222. int ecdh_cofactor_mode = 0, group_check = 0;
  223. const char *name = NULL;
  224. point_conversion_form_t format;
  225. if (ec == NULL)
  226. return 0;
  227. format = EC_KEY_get_conv_form(ec);
  228. name = ossl_ec_pt_format_id2name((int)format);
  229. if (name != NULL
  230. && !ossl_param_build_set_utf8_string(tmpl, params,
  231. OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
  232. name))
  233. return 0;
  234. group_check = EC_KEY_get_flags(ec) & EC_FLAG_CHECK_NAMED_GROUP_MASK;
  235. name = ossl_ec_check_group_type_id2name(group_check);
  236. if (name != NULL
  237. && !ossl_param_build_set_utf8_string(tmpl, params,
  238. OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE,
  239. name))
  240. return 0;
  241. if ((EC_KEY_get_enc_flags(ec) & EC_PKEY_NO_PUBKEY) != 0
  242. && !ossl_param_build_set_int(tmpl, params,
  243. OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, 0))
  244. return 0;
  245. ecdh_cofactor_mode =
  246. (EC_KEY_get_flags(ec) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
  247. return ossl_param_build_set_int(tmpl, params,
  248. OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,
  249. ecdh_cofactor_mode);
  250. }
  251. static
  252. void *ec_newdata(void *provctx)
  253. {
  254. if (!ossl_prov_is_running())
  255. return NULL;
  256. return EC_KEY_new_ex(PROV_LIBCTX_OF(provctx), NULL);
  257. }
  258. #ifndef FIPS_MODULE
  259. # ifndef OPENSSL_NO_SM2
  260. static
  261. void *sm2_newdata(void *provctx)
  262. {
  263. if (!ossl_prov_is_running())
  264. return NULL;
  265. return EC_KEY_new_by_curve_name_ex(PROV_LIBCTX_OF(provctx), NULL, NID_sm2);
  266. }
  267. # endif
  268. #endif
  269. static
  270. void ec_freedata(void *keydata)
  271. {
  272. EC_KEY_free(keydata);
  273. }
  274. static
  275. int ec_has(const void *keydata, int selection)
  276. {
  277. const EC_KEY *ec = keydata;
  278. int ok = 1;
  279. if (!ossl_prov_is_running() || ec == NULL)
  280. return 0;
  281. if ((selection & EC_POSSIBLE_SELECTIONS) == 0)
  282. return 1; /* the selection is not missing */
  283. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  284. ok = ok && (EC_KEY_get0_public_key(ec) != NULL);
  285. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  286. ok = ok && (EC_KEY_get0_private_key(ec) != NULL);
  287. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
  288. ok = ok && (EC_KEY_get0_group(ec) != NULL);
  289. /*
  290. * We consider OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS to always be
  291. * available, so no extra check is needed other than the previous one
  292. * against EC_POSSIBLE_SELECTIONS.
  293. */
  294. return ok;
  295. }
  296. static int ec_match(const void *keydata1, const void *keydata2, int selection)
  297. {
  298. const EC_KEY *ec1 = keydata1;
  299. const EC_KEY *ec2 = keydata2;
  300. const EC_GROUP *group_a = EC_KEY_get0_group(ec1);
  301. const EC_GROUP *group_b = EC_KEY_get0_group(ec2);
  302. BN_CTX *ctx = NULL;
  303. int ok = 1;
  304. if (!ossl_prov_is_running())
  305. return 0;
  306. ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(ec1));
  307. if (ctx == NULL)
  308. return 0;
  309. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
  310. ok = ok && group_a != NULL && group_b != NULL
  311. && EC_GROUP_cmp(group_a, group_b, ctx) == 0;
  312. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
  313. int key_checked = 0;
  314. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
  315. const EC_POINT *pa = EC_KEY_get0_public_key(ec1);
  316. const EC_POINT *pb = EC_KEY_get0_public_key(ec2);
  317. if (pa != NULL && pb != NULL) {
  318. ok = ok && EC_POINT_cmp(group_b, pa, pb, ctx) == 0;
  319. key_checked = 1;
  320. }
  321. }
  322. if (!key_checked
  323. && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  324. const BIGNUM *pa = EC_KEY_get0_private_key(ec1);
  325. const BIGNUM *pb = EC_KEY_get0_private_key(ec2);
  326. if (pa != NULL && pb != NULL) {
  327. ok = ok && BN_cmp(pa, pb) == 0;
  328. key_checked = 1;
  329. }
  330. }
  331. ok = ok && key_checked;
  332. }
  333. BN_CTX_free(ctx);
  334. return ok;
  335. }
  336. static int common_check_sm2(const EC_KEY *ec, int sm2_wanted)
  337. {
  338. const EC_GROUP *ecg = NULL;
  339. /*
  340. * sm2_wanted: import the keys or domparams only on SM2 Curve
  341. * !sm2_wanted: import the keys or domparams only not on SM2 Curve
  342. */
  343. if ((ecg = EC_KEY_get0_group(ec)) == NULL
  344. || (sm2_wanted ^ (EC_GROUP_get_curve_name(ecg) == NID_sm2)))
  345. return 0;
  346. return 1;
  347. }
  348. static
  349. int common_import(void *keydata, int selection, const OSSL_PARAM params[],
  350. int sm2_wanted)
  351. {
  352. EC_KEY *ec = keydata;
  353. int ok = 1;
  354. if (!ossl_prov_is_running() || ec == NULL)
  355. return 0;
  356. /*
  357. * In this implementation, we can export/import only keydata in the
  358. * following combinations:
  359. * - domain parameters (+optional other params)
  360. * - public key with associated domain parameters (+optional other params)
  361. * - private key with associated domain parameters and optional public key
  362. * (+optional other params)
  363. *
  364. * This means:
  365. * - domain parameters must always be requested
  366. * - private key must be requested alongside public key
  367. * - other parameters are always optional
  368. */
  369. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0)
  370. return 0;
  371. ok = ok && ossl_ec_group_fromdata(ec, params);
  372. if (!common_check_sm2(ec, sm2_wanted))
  373. return 0;
  374. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
  375. int include_private =
  376. selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
  377. ok = ok && ossl_ec_key_fromdata(ec, params, include_private);
  378. }
  379. if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
  380. ok = ok && ossl_ec_key_otherparams_fromdata(ec, params);
  381. return ok;
  382. }
  383. static
  384. int ec_import(void *keydata, int selection, const OSSL_PARAM params[])
  385. {
  386. return common_import(keydata, selection, params, 0);
  387. }
  388. #ifndef FIPS_MODULE
  389. # ifndef OPENSSL_NO_SM2
  390. static
  391. int sm2_import(void *keydata, int selection, const OSSL_PARAM params[])
  392. {
  393. return common_import(keydata, selection, params, 1);
  394. }
  395. # endif
  396. #endif
  397. static
  398. int ec_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
  399. void *cbarg)
  400. {
  401. EC_KEY *ec = keydata;
  402. OSSL_PARAM_BLD *tmpl = NULL;
  403. OSSL_PARAM *params = NULL;
  404. unsigned char *pub_key = NULL, *genbuf = NULL;
  405. BN_CTX *bnctx = NULL;
  406. int ok = 1;
  407. if (!ossl_prov_is_running() || ec == NULL)
  408. return 0;
  409. /*
  410. * In this implementation, we can export/import only keydata in the
  411. * following combinations:
  412. * - domain parameters (+optional other params)
  413. * - public key with associated domain parameters (+optional other params)
  414. * - private key with associated public key and domain parameters
  415. * (+optional other params)
  416. *
  417. * This means:
  418. * - domain parameters must always be requested
  419. * - private key must be requested alongside public key
  420. * - other parameters are always optional
  421. */
  422. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0)
  423. return 0;
  424. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
  425. && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0)
  426. return 0;
  427. tmpl = OSSL_PARAM_BLD_new();
  428. if (tmpl == NULL)
  429. return 0;
  430. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
  431. bnctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(ec));
  432. if (bnctx == NULL) {
  433. ok = 0;
  434. goto end;
  435. }
  436. BN_CTX_start(bnctx);
  437. ok = ok && ossl_ec_group_todata(EC_KEY_get0_group(ec), tmpl, NULL,
  438. ossl_ec_key_get_libctx(ec),
  439. ossl_ec_key_get0_propq(ec),
  440. bnctx, &genbuf);
  441. }
  442. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
  443. int include_private =
  444. selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
  445. ok = ok && key_to_params(ec, tmpl, NULL, include_private, &pub_key);
  446. }
  447. if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
  448. ok = ok && otherparams_to_params(ec, tmpl, NULL);
  449. if (!ok || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
  450. ok = 0;
  451. goto end;
  452. }
  453. ok = param_cb(params, cbarg);
  454. OSSL_PARAM_free(params);
  455. end:
  456. OSSL_PARAM_BLD_free(tmpl);
  457. OPENSSL_free(pub_key);
  458. OPENSSL_free(genbuf);
  459. BN_CTX_end(bnctx);
  460. BN_CTX_free(bnctx);
  461. return ok;
  462. }
  463. /* IMEXPORT = IMPORT + EXPORT */
  464. # define EC_IMEXPORTABLE_DOM_PARAMETERS \
  465. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0), \
  466. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0), \
  467. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, NULL, 0),\
  468. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_FIELD_TYPE, NULL, 0), \
  469. OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_P, NULL, 0), \
  470. OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_A, NULL, 0), \
  471. OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_B, NULL, 0), \
  472. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_GENERATOR, NULL, 0), \
  473. OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_ORDER, NULL, 0), \
  474. OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_COFACTOR, NULL, 0), \
  475. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0), \
  476. OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS, NULL)
  477. # define EC_IMEXPORTABLE_PUBLIC_KEY \
  478. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
  479. # define EC_IMEXPORTABLE_PRIVATE_KEY \
  480. OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
  481. # define EC_IMEXPORTABLE_OTHER_PARAMETERS \
  482. OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL), \
  483. OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, NULL)
  484. /*
  485. * Include all the possible combinations of OSSL_PARAM arrays for
  486. * ec_imexport_types().
  487. *
  488. * They are in a separate file as it is ~100 lines of unreadable and
  489. * uninteresting machine generated stuff.
  490. */
  491. #include "ec_kmgmt_imexport.inc"
  492. static ossl_inline
  493. const OSSL_PARAM *ec_imexport_types(int selection)
  494. {
  495. int type_select = 0;
  496. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  497. type_select += 1;
  498. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  499. type_select += 2;
  500. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
  501. type_select += 4;
  502. if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
  503. type_select += 8;
  504. return ec_types[type_select];
  505. }
  506. static
  507. const OSSL_PARAM *ec_import_types(int selection)
  508. {
  509. return ec_imexport_types(selection);
  510. }
  511. static
  512. const OSSL_PARAM *ec_export_types(int selection)
  513. {
  514. return ec_imexport_types(selection);
  515. }
  516. static int ec_get_ecm_params(const EC_GROUP *group, OSSL_PARAM params[])
  517. {
  518. #ifdef OPENSSL_NO_EC2M
  519. return 1;
  520. #else
  521. int ret = 0, m;
  522. unsigned int k1 = 0, k2 = 0, k3 = 0;
  523. int basis_nid;
  524. const char *basis_name = NULL;
  525. int fid = EC_GROUP_get_field_type(group);
  526. if (fid != NID_X9_62_characteristic_two_field)
  527. return 1;
  528. basis_nid = EC_GROUP_get_basis_type(group);
  529. if (basis_nid == NID_X9_62_tpBasis)
  530. basis_name = SN_X9_62_tpBasis;
  531. else if (basis_nid == NID_X9_62_ppBasis)
  532. basis_name = SN_X9_62_ppBasis;
  533. else
  534. goto err;
  535. m = EC_GROUP_get_degree(group);
  536. if (!ossl_param_build_set_int(NULL, params, OSSL_PKEY_PARAM_EC_CHAR2_M, m)
  537. || !ossl_param_build_set_utf8_string(NULL, params,
  538. OSSL_PKEY_PARAM_EC_CHAR2_TYPE,
  539. basis_name))
  540. goto err;
  541. if (basis_nid == NID_X9_62_tpBasis) {
  542. if (!EC_GROUP_get_trinomial_basis(group, &k1)
  543. || !ossl_param_build_set_int(NULL, params,
  544. OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS,
  545. (int)k1))
  546. goto err;
  547. } else {
  548. if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3)
  549. || !ossl_param_build_set_int(NULL, params,
  550. OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, (int)k1)
  551. || !ossl_param_build_set_int(NULL, params,
  552. OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, (int)k2)
  553. || !ossl_param_build_set_int(NULL, params,
  554. OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, (int)k3))
  555. goto err;
  556. }
  557. ret = 1;
  558. err:
  559. return ret;
  560. #endif /* OPENSSL_NO_EC2M */
  561. }
  562. static
  563. int common_get_params(void *key, OSSL_PARAM params[], int sm2)
  564. {
  565. int ret = 0;
  566. EC_KEY *eck = key;
  567. const EC_GROUP *ecg = NULL;
  568. OSSL_PARAM *p;
  569. unsigned char *pub_key = NULL, *genbuf = NULL;
  570. OSSL_LIB_CTX *libctx;
  571. const char *propq;
  572. BN_CTX *bnctx = NULL;
  573. ecg = EC_KEY_get0_group(eck);
  574. if (ecg == NULL) {
  575. ERR_raise(ERR_LIB_PROV, PROV_R_NO_PARAMETERS_SET);
  576. return 0;
  577. }
  578. libctx = ossl_ec_key_get_libctx(eck);
  579. propq = ossl_ec_key_get0_propq(eck);
  580. bnctx = BN_CTX_new_ex(libctx);
  581. if (bnctx == NULL)
  582. return 0;
  583. BN_CTX_start(bnctx);
  584. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
  585. && !OSSL_PARAM_set_int(p, ECDSA_size(eck)))
  586. goto err;
  587. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
  588. && !OSSL_PARAM_set_int(p, EC_GROUP_order_bits(ecg)))
  589. goto err;
  590. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL) {
  591. int ecbits, sec_bits;
  592. ecbits = EC_GROUP_order_bits(ecg);
  593. /*
  594. * The following estimates are based on the values published
  595. * in Table 2 of "NIST Special Publication 800-57 Part 1 Revision 4"
  596. * at http://dx.doi.org/10.6028/NIST.SP.800-57pt1r4 .
  597. *
  598. * Note that the above reference explicitly categorizes algorithms in a
  599. * discrete set of values {80, 112, 128, 192, 256}, and that it is
  600. * relevant only for NIST approved Elliptic Curves, while OpenSSL
  601. * applies the same logic also to other curves.
  602. *
  603. * Classifications produced by other standardazing bodies might differ,
  604. * so the results provided for "bits of security" by this provider are
  605. * to be considered merely indicative, and it is the users'
  606. * responsibility to compare these values against the normative
  607. * references that may be relevant for their intent and purposes.
  608. */
  609. if (ecbits >= 512)
  610. sec_bits = 256;
  611. else if (ecbits >= 384)
  612. sec_bits = 192;
  613. else if (ecbits >= 256)
  614. sec_bits = 128;
  615. else if (ecbits >= 224)
  616. sec_bits = 112;
  617. else if (ecbits >= 160)
  618. sec_bits = 80;
  619. else
  620. sec_bits = ecbits / 2;
  621. if (!OSSL_PARAM_set_int(p, sec_bits))
  622. goto err;
  623. }
  624. if ((p = OSSL_PARAM_locate(params,
  625. OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS))
  626. != NULL) {
  627. int explicitparams = EC_KEY_decoded_from_explicit_params(eck);
  628. if (explicitparams < 0
  629. || !OSSL_PARAM_set_int(p, explicitparams))
  630. goto err;
  631. }
  632. if (!sm2) {
  633. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
  634. && !OSSL_PARAM_set_utf8_string(p, EC_DEFAULT_MD))
  635. goto err;
  636. } else {
  637. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
  638. && !OSSL_PARAM_set_utf8_string(p, SM2_DEFAULT_MD))
  639. goto err;
  640. }
  641. /* SM2 doesn't support this PARAM */
  642. if (!sm2) {
  643. p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
  644. if (p != NULL) {
  645. int ecdh_cofactor_mode = 0;
  646. ecdh_cofactor_mode =
  647. (EC_KEY_get_flags(eck) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
  648. if (!OSSL_PARAM_set_int(p, ecdh_cofactor_mode))
  649. goto err;
  650. }
  651. }
  652. if ((p = OSSL_PARAM_locate(params,
  653. OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY)) != NULL) {
  654. const EC_POINT *ecp = EC_KEY_get0_public_key(key);
  655. if (ecp == NULL) {
  656. ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
  657. goto err;
  658. }
  659. p->return_size = EC_POINT_point2oct(ecg, ecp,
  660. POINT_CONVERSION_UNCOMPRESSED,
  661. p->data, p->data_size, bnctx);
  662. if (p->return_size == 0)
  663. goto err;
  664. }
  665. ret = ec_get_ecm_params(ecg, params)
  666. && ossl_ec_group_todata(ecg, NULL, params, libctx, propq, bnctx,
  667. &genbuf)
  668. && key_to_params(eck, NULL, params, 1, &pub_key)
  669. && otherparams_to_params(eck, NULL, params);
  670. err:
  671. OPENSSL_free(genbuf);
  672. OPENSSL_free(pub_key);
  673. BN_CTX_end(bnctx);
  674. BN_CTX_free(bnctx);
  675. return ret;
  676. }
  677. static
  678. int ec_get_params(void *key, OSSL_PARAM params[])
  679. {
  680. return common_get_params(key, params, 0);
  681. }
  682. #ifndef OPENSSL_NO_EC2M
  683. # define EC2M_GETTABLE_DOM_PARAMS \
  684. OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_M, NULL), \
  685. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_CHAR2_TYPE, NULL, 0), \
  686. OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS, NULL), \
  687. OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, NULL), \
  688. OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, NULL), \
  689. OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, NULL),
  690. #else
  691. # define EC2M_GETTABLE_DOM_PARAMS
  692. #endif
  693. static const OSSL_PARAM ec_known_gettable_params[] = {
  694. OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
  695. OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
  696. OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
  697. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0),
  698. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
  699. OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS, NULL),
  700. EC_IMEXPORTABLE_DOM_PARAMETERS,
  701. EC2M_GETTABLE_DOM_PARAMS
  702. EC_IMEXPORTABLE_PUBLIC_KEY,
  703. OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_PUB_X, NULL, 0),
  704. OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_PUB_Y, NULL, 0),
  705. EC_IMEXPORTABLE_PRIVATE_KEY,
  706. EC_IMEXPORTABLE_OTHER_PARAMETERS,
  707. OSSL_PARAM_END
  708. };
  709. static
  710. const OSSL_PARAM *ec_gettable_params(void *provctx)
  711. {
  712. return ec_known_gettable_params;
  713. }
  714. static const OSSL_PARAM ec_known_settable_params[] = {
  715. OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
  716. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
  717. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0),
  718. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, NULL, 0),
  719. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0),
  720. OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, NULL),
  721. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE, NULL, 0),
  722. OSSL_PARAM_END
  723. };
  724. static
  725. const OSSL_PARAM *ec_settable_params(void *provctx)
  726. {
  727. return ec_known_settable_params;
  728. }
  729. static
  730. int ec_set_params(void *key, const OSSL_PARAM params[])
  731. {
  732. EC_KEY *eck = key;
  733. const OSSL_PARAM *p;
  734. if (key == NULL)
  735. return 0;
  736. if (params == NULL)
  737. return 1;
  738. if (!ossl_ec_group_set_params((EC_GROUP *)EC_KEY_get0_group(key), params))
  739. return 0;
  740. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY);
  741. if (p != NULL) {
  742. BN_CTX *ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(key));
  743. int ret = 1;
  744. if (ctx == NULL
  745. || p->data_type != OSSL_PARAM_OCTET_STRING
  746. || !EC_KEY_oct2key(key, p->data, p->data_size, ctx))
  747. ret = 0;
  748. BN_CTX_free(ctx);
  749. if (!ret)
  750. return 0;
  751. }
  752. return ossl_ec_key_otherparams_fromdata(eck, params);
  753. }
  754. #ifndef FIPS_MODULE
  755. # ifndef OPENSSL_NO_SM2
  756. static
  757. int sm2_get_params(void *key, OSSL_PARAM params[])
  758. {
  759. return common_get_params(key, params, 1);
  760. }
  761. static const OSSL_PARAM sm2_known_gettable_params[] = {
  762. OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
  763. OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
  764. OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
  765. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0),
  766. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
  767. OSSL_PARAM_int(OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS, NULL),
  768. EC_IMEXPORTABLE_DOM_PARAMETERS,
  769. EC_IMEXPORTABLE_PUBLIC_KEY,
  770. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_X, NULL, 0),
  771. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_PUB_Y, NULL, 0),
  772. EC_IMEXPORTABLE_PRIVATE_KEY,
  773. OSSL_PARAM_END
  774. };
  775. static
  776. const OSSL_PARAM *sm2_gettable_params(ossl_unused void *provctx)
  777. {
  778. return sm2_known_gettable_params;
  779. }
  780. static const OSSL_PARAM sm2_known_settable_params[] = {
  781. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
  782. OSSL_PARAM_END
  783. };
  784. static
  785. const OSSL_PARAM *sm2_settable_params(ossl_unused void *provctx)
  786. {
  787. return sm2_known_settable_params;
  788. }
  789. static
  790. int sm2_validate(const void *keydata, int selection, int checktype)
  791. {
  792. const EC_KEY *eck = keydata;
  793. int ok = 1;
  794. BN_CTX *ctx = NULL;
  795. if (!ossl_prov_is_running())
  796. return 0;
  797. if ((selection & EC_POSSIBLE_SELECTIONS) == 0)
  798. return 1; /* nothing to validate */
  799. ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(eck));
  800. if (ctx == NULL)
  801. return 0;
  802. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
  803. ok = ok && EC_GROUP_check(EC_KEY_get0_group(eck), ctx);
  804. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
  805. if (checktype == OSSL_KEYMGMT_VALIDATE_QUICK_CHECK)
  806. ok = ok && ossl_ec_key_public_check_quick(eck, ctx);
  807. else
  808. ok = ok && ossl_ec_key_public_check(eck, ctx);
  809. }
  810. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  811. ok = ok && ossl_sm2_key_private_check(eck);
  812. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR)
  813. ok = ok && ossl_ec_key_pairwise_check(eck, ctx);
  814. BN_CTX_free(ctx);
  815. return ok;
  816. }
  817. # endif
  818. #endif
  819. static
  820. int ec_validate(const void *keydata, int selection, int checktype)
  821. {
  822. const EC_KEY *eck = keydata;
  823. int ok = 1;
  824. BN_CTX *ctx = NULL;
  825. if (!ossl_prov_is_running())
  826. return 0;
  827. if ((selection & EC_POSSIBLE_SELECTIONS) == 0)
  828. return 1; /* nothing to validate */
  829. ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(eck));
  830. if (ctx == NULL)
  831. return 0;
  832. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
  833. int flags = EC_KEY_get_flags(eck);
  834. if ((flags & EC_FLAG_CHECK_NAMED_GROUP) != 0)
  835. ok = ok && EC_GROUP_check_named_curve(EC_KEY_get0_group(eck),
  836. (flags & EC_FLAG_CHECK_NAMED_GROUP_NIST) != 0, ctx) > 0;
  837. else
  838. ok = ok && EC_GROUP_check(EC_KEY_get0_group(eck), ctx);
  839. }
  840. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
  841. if (checktype == OSSL_KEYMGMT_VALIDATE_QUICK_CHECK)
  842. ok = ok && ossl_ec_key_public_check_quick(eck, ctx);
  843. else
  844. ok = ok && ossl_ec_key_public_check(eck, ctx);
  845. }
  846. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  847. ok = ok && ossl_ec_key_private_check(eck);
  848. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR)
  849. ok = ok && ossl_ec_key_pairwise_check(eck, ctx);
  850. BN_CTX_free(ctx);
  851. return ok;
  852. }
  853. struct ec_gen_ctx {
  854. OSSL_LIB_CTX *libctx;
  855. char *group_name;
  856. char *encoding;
  857. char *pt_format;
  858. char *group_check;
  859. char *field_type;
  860. BIGNUM *p, *a, *b, *order, *cofactor;
  861. unsigned char *gen, *seed;
  862. size_t gen_len, seed_len;
  863. int selection;
  864. int ecdh_mode;
  865. EC_GROUP *gen_group;
  866. unsigned char *dhkem_ikm;
  867. size_t dhkem_ikmlen;
  868. };
  869. static void *ec_gen_init(void *provctx, int selection,
  870. const OSSL_PARAM params[])
  871. {
  872. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
  873. struct ec_gen_ctx *gctx = NULL;
  874. if (!ossl_prov_is_running() || (selection & (EC_POSSIBLE_SELECTIONS)) == 0)
  875. return NULL;
  876. if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
  877. gctx->libctx = libctx;
  878. gctx->selection = selection;
  879. gctx->ecdh_mode = 0;
  880. if (!ec_gen_set_params(gctx, params)) {
  881. OPENSSL_free(gctx);
  882. gctx = NULL;
  883. }
  884. }
  885. return gctx;
  886. }
  887. #ifndef FIPS_MODULE
  888. # ifndef OPENSSL_NO_SM2
  889. static void *sm2_gen_init(void *provctx, int selection,
  890. const OSSL_PARAM params[])
  891. {
  892. struct ec_gen_ctx *gctx = ec_gen_init(provctx, selection, params);
  893. if (gctx != NULL) {
  894. if (gctx->group_name != NULL)
  895. return gctx;
  896. if ((gctx->group_name = OPENSSL_strdup("sm2")) != NULL)
  897. return gctx;
  898. ec_gen_cleanup(gctx);
  899. }
  900. return NULL;
  901. }
  902. # endif
  903. #endif
  904. static int ec_gen_set_group(void *genctx, const EC_GROUP *src)
  905. {
  906. struct ec_gen_ctx *gctx = genctx;
  907. EC_GROUP *group;
  908. group = EC_GROUP_dup(src);
  909. if (group == NULL) {
  910. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CURVE);
  911. return 0;
  912. }
  913. EC_GROUP_free(gctx->gen_group);
  914. gctx->gen_group = group;
  915. return 1;
  916. }
  917. static int ec_gen_set_template(void *genctx, void *templ)
  918. {
  919. struct ec_gen_ctx *gctx = genctx;
  920. EC_KEY *ec = templ;
  921. const EC_GROUP *ec_group;
  922. if (!ossl_prov_is_running() || gctx == NULL || ec == NULL)
  923. return 0;
  924. if ((ec_group = EC_KEY_get0_group(ec)) == NULL)
  925. return 0;
  926. return ec_gen_set_group(gctx, ec_group);
  927. }
  928. #define COPY_INT_PARAM(params, key, val) \
  929. p = OSSL_PARAM_locate_const(params, key); \
  930. if (p != NULL && !OSSL_PARAM_get_int(p, &val)) \
  931. goto err;
  932. #define COPY_UTF8_PARAM(params, key, val) \
  933. p = OSSL_PARAM_locate_const(params, key); \
  934. if (p != NULL) { \
  935. if (p->data_type != OSSL_PARAM_UTF8_STRING) \
  936. goto err; \
  937. OPENSSL_free(val); \
  938. val = OPENSSL_strdup(p->data); \
  939. if (val == NULL) \
  940. goto err; \
  941. }
  942. #define COPY_OCTET_PARAM(params, key, val, len) \
  943. p = OSSL_PARAM_locate_const(params, key); \
  944. if (p != NULL) { \
  945. if (p->data_type != OSSL_PARAM_OCTET_STRING) \
  946. goto err; \
  947. OPENSSL_free(val); \
  948. len = p->data_size; \
  949. val = OPENSSL_memdup(p->data, p->data_size); \
  950. if (val == NULL) \
  951. goto err; \
  952. }
  953. #define COPY_BN_PARAM(params, key, bn) \
  954. p = OSSL_PARAM_locate_const(params, key); \
  955. if (p != NULL) { \
  956. if (bn == NULL) \
  957. bn = BN_new(); \
  958. if (bn == NULL || !OSSL_PARAM_get_BN(p, &bn)) \
  959. goto err; \
  960. }
  961. static int ec_gen_set_params(void *genctx, const OSSL_PARAM params[])
  962. {
  963. int ret = 0;
  964. struct ec_gen_ctx *gctx = genctx;
  965. const OSSL_PARAM *p;
  966. EC_GROUP *group = NULL;
  967. COPY_INT_PARAM(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, gctx->ecdh_mode);
  968. COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_GROUP_NAME, gctx->group_name);
  969. COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE, gctx->field_type);
  970. COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_ENCODING, gctx->encoding);
  971. COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, gctx->pt_format);
  972. COPY_UTF8_PARAM(params, OSSL_PKEY_PARAM_EC_GROUP_CHECK_TYPE, gctx->group_check);
  973. COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_P, gctx->p);
  974. COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_A, gctx->a);
  975. COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_B, gctx->b);
  976. COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_ORDER, gctx->order);
  977. COPY_BN_PARAM(params, OSSL_PKEY_PARAM_EC_COFACTOR, gctx->cofactor);
  978. COPY_OCTET_PARAM(params, OSSL_PKEY_PARAM_EC_SEED, gctx->seed, gctx->seed_len);
  979. COPY_OCTET_PARAM(params, OSSL_PKEY_PARAM_EC_GENERATOR, gctx->gen,
  980. gctx->gen_len);
  981. COPY_OCTET_PARAM(params, OSSL_PKEY_PARAM_DHKEM_IKM, gctx->dhkem_ikm,
  982. gctx->dhkem_ikmlen);
  983. ret = 1;
  984. err:
  985. EC_GROUP_free(group);
  986. return ret;
  987. }
  988. static int ec_gen_set_group_from_params(struct ec_gen_ctx *gctx)
  989. {
  990. int ret = 0;
  991. OSSL_PARAM_BLD *bld;
  992. OSSL_PARAM *params = NULL;
  993. EC_GROUP *group = NULL;
  994. bld = OSSL_PARAM_BLD_new();
  995. if (bld == NULL)
  996. return 0;
  997. if (gctx->encoding != NULL
  998. && !OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_EC_ENCODING,
  999. gctx->encoding, 0))
  1000. goto err;
  1001. if (gctx->pt_format != NULL
  1002. && !OSSL_PARAM_BLD_push_utf8_string(bld,
  1003. OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
  1004. gctx->pt_format, 0))
  1005. goto err;
  1006. if (gctx->group_name != NULL) {
  1007. if (!OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME,
  1008. gctx->group_name, 0))
  1009. goto err;
  1010. /* Ignore any other parameters if there is a group name */
  1011. goto build;
  1012. } else if (gctx->field_type != NULL) {
  1013. if (!OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_EC_FIELD_TYPE,
  1014. gctx->field_type, 0))
  1015. goto err;
  1016. } else {
  1017. goto err;
  1018. }
  1019. if (gctx->p == NULL
  1020. || gctx->a == NULL
  1021. || gctx->b == NULL
  1022. || gctx->order == NULL
  1023. || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_P, gctx->p)
  1024. || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_A, gctx->a)
  1025. || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_B, gctx->b)
  1026. || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_ORDER, gctx->order))
  1027. goto err;
  1028. if (gctx->cofactor != NULL
  1029. && !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_COFACTOR,
  1030. gctx->cofactor))
  1031. goto err;
  1032. if (gctx->seed != NULL
  1033. && !OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_EC_SEED,
  1034. gctx->seed, gctx->seed_len))
  1035. goto err;
  1036. if (gctx->gen == NULL
  1037. || !OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_EC_GENERATOR,
  1038. gctx->gen, gctx->gen_len))
  1039. goto err;
  1040. build:
  1041. params = OSSL_PARAM_BLD_to_param(bld);
  1042. if (params == NULL)
  1043. goto err;
  1044. group = EC_GROUP_new_from_params(params, gctx->libctx, NULL);
  1045. if (group == NULL)
  1046. goto err;
  1047. EC_GROUP_free(gctx->gen_group);
  1048. gctx->gen_group = group;
  1049. ret = 1;
  1050. err:
  1051. OSSL_PARAM_free(params);
  1052. OSSL_PARAM_BLD_free(bld);
  1053. return ret;
  1054. }
  1055. static const OSSL_PARAM *ec_gen_settable_params(ossl_unused void *genctx,
  1056. ossl_unused void *provctx)
  1057. {
  1058. static OSSL_PARAM settable[] = {
  1059. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),
  1060. OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
  1061. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_ENCODING, NULL, 0),
  1062. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, NULL, 0),
  1063. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_EC_FIELD_TYPE, NULL, 0),
  1064. OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_P, NULL, 0),
  1065. OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_A, NULL, 0),
  1066. OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_B, NULL, 0),
  1067. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_GENERATOR, NULL, 0),
  1068. OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_ORDER, NULL, 0),
  1069. OSSL_PARAM_BN(OSSL_PKEY_PARAM_EC_COFACTOR, NULL, 0),
  1070. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_EC_SEED, NULL, 0),
  1071. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_DHKEM_IKM, NULL, 0),
  1072. OSSL_PARAM_END
  1073. };
  1074. return settable;
  1075. }
  1076. static int ec_gen_assign_group(EC_KEY *ec, EC_GROUP *group)
  1077. {
  1078. if (group == NULL) {
  1079. ERR_raise(ERR_LIB_PROV, PROV_R_NO_PARAMETERS_SET);
  1080. return 0;
  1081. }
  1082. return EC_KEY_set_group(ec, group) > 0;
  1083. }
  1084. /*
  1085. * The callback arguments (osslcb & cbarg) are not used by EC_KEY generation
  1086. */
  1087. static void *ec_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
  1088. {
  1089. struct ec_gen_ctx *gctx = genctx;
  1090. EC_KEY *ec = NULL;
  1091. int ret = 0;
  1092. if (!ossl_prov_is_running()
  1093. || gctx == NULL
  1094. || (ec = EC_KEY_new_ex(gctx->libctx, NULL)) == NULL)
  1095. return NULL;
  1096. if (gctx->gen_group == NULL) {
  1097. if (!ec_gen_set_group_from_params(gctx))
  1098. goto err;
  1099. } else {
  1100. if (gctx->encoding != NULL) {
  1101. int flags = ossl_ec_encoding_name2id(gctx->encoding);
  1102. if (flags < 0)
  1103. goto err;
  1104. EC_GROUP_set_asn1_flag(gctx->gen_group, flags);
  1105. }
  1106. if (gctx->pt_format != NULL) {
  1107. int format = ossl_ec_pt_format_name2id(gctx->pt_format);
  1108. if (format < 0)
  1109. goto err;
  1110. EC_GROUP_set_point_conversion_form(gctx->gen_group, format);
  1111. }
  1112. }
  1113. /* We must always assign a group, no matter what */
  1114. ret = ec_gen_assign_group(ec, gctx->gen_group);
  1115. /* Whether you want it or not, you get a keypair, not just one half */
  1116. if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
  1117. #ifndef FIPS_MODULE
  1118. if (gctx->dhkem_ikm != NULL && gctx->dhkem_ikmlen != 0)
  1119. ret = ret && ossl_ec_generate_key_dhkem(ec, gctx->dhkem_ikm,
  1120. gctx->dhkem_ikmlen);
  1121. else
  1122. #endif
  1123. ret = ret && EC_KEY_generate_key(ec);
  1124. }
  1125. if (gctx->ecdh_mode != -1)
  1126. ret = ret && ossl_ec_set_ecdh_cofactor_mode(ec, gctx->ecdh_mode);
  1127. if (gctx->group_check != NULL)
  1128. ret = ret && ossl_ec_set_check_group_type_from_name(ec,
  1129. gctx->group_check);
  1130. if (ret)
  1131. return ec;
  1132. err:
  1133. /* Something went wrong, throw the key away */
  1134. EC_KEY_free(ec);
  1135. return NULL;
  1136. }
  1137. #ifndef FIPS_MODULE
  1138. # ifndef OPENSSL_NO_SM2
  1139. /*
  1140. * The callback arguments (osslcb & cbarg) are not used by EC_KEY generation
  1141. */
  1142. static void *sm2_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
  1143. {
  1144. struct ec_gen_ctx *gctx = genctx;
  1145. EC_KEY *ec = NULL;
  1146. int ret = 1;
  1147. if (gctx == NULL
  1148. || (ec = EC_KEY_new_ex(gctx->libctx, NULL)) == NULL)
  1149. return NULL;
  1150. if (gctx->gen_group == NULL) {
  1151. if (!ec_gen_set_group_from_params(gctx))
  1152. goto err;
  1153. } else {
  1154. if (gctx->encoding) {
  1155. int flags = ossl_ec_encoding_name2id(gctx->encoding);
  1156. if (flags < 0)
  1157. goto err;
  1158. EC_GROUP_set_asn1_flag(gctx->gen_group, flags);
  1159. }
  1160. if (gctx->pt_format != NULL) {
  1161. int format = ossl_ec_pt_format_name2id(gctx->pt_format);
  1162. if (format < 0)
  1163. goto err;
  1164. EC_GROUP_set_point_conversion_form(gctx->gen_group, format);
  1165. }
  1166. }
  1167. /* We must always assign a group, no matter what */
  1168. ret = ec_gen_assign_group(ec, gctx->gen_group);
  1169. /* Whether you want it or not, you get a keypair, not just one half */
  1170. if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
  1171. ret = ret && EC_KEY_generate_key(ec);
  1172. if (ret)
  1173. return ec;
  1174. err:
  1175. /* Something went wrong, throw the key away */
  1176. EC_KEY_free(ec);
  1177. return NULL;
  1178. }
  1179. # endif
  1180. #endif
  1181. static void ec_gen_cleanup(void *genctx)
  1182. {
  1183. struct ec_gen_ctx *gctx = genctx;
  1184. if (gctx == NULL)
  1185. return;
  1186. OPENSSL_clear_free(gctx->dhkem_ikm, gctx->dhkem_ikmlen);
  1187. EC_GROUP_free(gctx->gen_group);
  1188. BN_free(gctx->p);
  1189. BN_free(gctx->a);
  1190. BN_free(gctx->b);
  1191. BN_free(gctx->order);
  1192. BN_free(gctx->cofactor);
  1193. OPENSSL_free(gctx->group_name);
  1194. OPENSSL_free(gctx->field_type);
  1195. OPENSSL_free(gctx->pt_format);
  1196. OPENSSL_free(gctx->encoding);
  1197. OPENSSL_free(gctx->seed);
  1198. OPENSSL_free(gctx->gen);
  1199. OPENSSL_free(gctx);
  1200. }
  1201. static void *common_load(const void *reference, size_t reference_sz,
  1202. int sm2_wanted)
  1203. {
  1204. EC_KEY *ec = NULL;
  1205. if (ossl_prov_is_running() && reference_sz == sizeof(ec)) {
  1206. /* The contents of the reference is the address to our object */
  1207. ec = *(EC_KEY **)reference;
  1208. if (!common_check_sm2(ec, sm2_wanted))
  1209. return NULL;
  1210. /* We grabbed, so we detach it */
  1211. *(EC_KEY **)reference = NULL;
  1212. return ec;
  1213. }
  1214. return NULL;
  1215. }
  1216. static void *ec_load(const void *reference, size_t reference_sz)
  1217. {
  1218. return common_load(reference, reference_sz, 0);
  1219. }
  1220. #ifndef FIPS_MODULE
  1221. # ifndef OPENSSL_NO_SM2
  1222. static void *sm2_load(const void *reference, size_t reference_sz)
  1223. {
  1224. return common_load(reference, reference_sz, 1);
  1225. }
  1226. # endif
  1227. #endif
  1228. static void *ec_dup(const void *keydata_from, int selection)
  1229. {
  1230. if (ossl_prov_is_running())
  1231. return ossl_ec_key_dup(keydata_from, selection);
  1232. return NULL;
  1233. }
  1234. const OSSL_DISPATCH ossl_ec_keymgmt_functions[] = {
  1235. { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))ec_newdata },
  1236. { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))ec_gen_init },
  1237. { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE,
  1238. (void (*)(void))ec_gen_set_template },
  1239. { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))ec_gen_set_params },
  1240. { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
  1241. (void (*)(void))ec_gen_settable_params },
  1242. { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))ec_gen },
  1243. { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))ec_gen_cleanup },
  1244. { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))ec_load },
  1245. { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ec_freedata },
  1246. { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))ec_get_params },
  1247. { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))ec_gettable_params },
  1248. { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))ec_set_params },
  1249. { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))ec_settable_params },
  1250. { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ec_has },
  1251. { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ec_match },
  1252. { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))ec_validate },
  1253. { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ec_import },
  1254. { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ec_import_types },
  1255. { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ec_export },
  1256. { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ec_export_types },
  1257. { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
  1258. (void (*)(void))ec_query_operation_name },
  1259. { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))ec_dup },
  1260. OSSL_DISPATCH_END
  1261. };
  1262. #ifndef FIPS_MODULE
  1263. # ifndef OPENSSL_NO_SM2
  1264. const OSSL_DISPATCH ossl_sm2_keymgmt_functions[] = {
  1265. { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))sm2_newdata },
  1266. { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))sm2_gen_init },
  1267. { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE,
  1268. (void (*)(void))ec_gen_set_template },
  1269. { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))ec_gen_set_params },
  1270. { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
  1271. (void (*)(void))ec_gen_settable_params },
  1272. { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))sm2_gen },
  1273. { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))ec_gen_cleanup },
  1274. { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))sm2_load },
  1275. { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ec_freedata },
  1276. { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))sm2_get_params },
  1277. { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))sm2_gettable_params },
  1278. { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))ec_set_params },
  1279. { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))sm2_settable_params },
  1280. { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ec_has },
  1281. { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ec_match },
  1282. { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))sm2_validate },
  1283. { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))sm2_import },
  1284. { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ec_import_types },
  1285. { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ec_export },
  1286. { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ec_export_types },
  1287. { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
  1288. (void (*)(void))sm2_query_operation_name },
  1289. { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))ec_dup },
  1290. OSSL_DISPATCH_END
  1291. };
  1292. # endif
  1293. #endif