ec_kmgmt.c 51 KB

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