ecx_kmgmt.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. /*
  2. * Copyright 2020-2022 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. #include <assert.h>
  10. #include <string.h>
  11. #include <openssl/core_dispatch.h>
  12. #include <openssl/core_names.h>
  13. #include <openssl/params.h>
  14. #include <openssl/err.h>
  15. #include <openssl/proverr.h>
  16. #include <openssl/evp.h>
  17. #include <openssl/rand.h>
  18. #include "internal/param_build_set.h"
  19. #include <openssl/param_build.h>
  20. #include "crypto/ecx.h"
  21. #include "prov/implementations.h"
  22. #include "prov/providercommon.h"
  23. #include "prov/provider_ctx.h"
  24. #include "prov/ecx.h"
  25. #ifdef S390X_EC_ASM
  26. # include "s390x_arch.h"
  27. # include <openssl/sha.h> /* For SHA512_DIGEST_LENGTH */
  28. #endif
  29. static OSSL_FUNC_keymgmt_new_fn x25519_new_key;
  30. static OSSL_FUNC_keymgmt_new_fn x448_new_key;
  31. static OSSL_FUNC_keymgmt_new_fn ed25519_new_key;
  32. static OSSL_FUNC_keymgmt_new_fn ed448_new_key;
  33. static OSSL_FUNC_keymgmt_gen_init_fn x25519_gen_init;
  34. static OSSL_FUNC_keymgmt_gen_init_fn x448_gen_init;
  35. static OSSL_FUNC_keymgmt_gen_init_fn ed25519_gen_init;
  36. static OSSL_FUNC_keymgmt_gen_init_fn ed448_gen_init;
  37. static OSSL_FUNC_keymgmt_gen_fn x25519_gen;
  38. static OSSL_FUNC_keymgmt_gen_fn x448_gen;
  39. static OSSL_FUNC_keymgmt_gen_fn ed25519_gen;
  40. static OSSL_FUNC_keymgmt_gen_fn ed448_gen;
  41. static OSSL_FUNC_keymgmt_gen_cleanup_fn ecx_gen_cleanup;
  42. static OSSL_FUNC_keymgmt_gen_set_params_fn ecx_gen_set_params;
  43. static OSSL_FUNC_keymgmt_gen_settable_params_fn ecx_gen_settable_params;
  44. static OSSL_FUNC_keymgmt_load_fn ecx_load;
  45. static OSSL_FUNC_keymgmt_get_params_fn x25519_get_params;
  46. static OSSL_FUNC_keymgmt_get_params_fn x448_get_params;
  47. static OSSL_FUNC_keymgmt_get_params_fn ed25519_get_params;
  48. static OSSL_FUNC_keymgmt_get_params_fn ed448_get_params;
  49. static OSSL_FUNC_keymgmt_gettable_params_fn x25519_gettable_params;
  50. static OSSL_FUNC_keymgmt_gettable_params_fn x448_gettable_params;
  51. static OSSL_FUNC_keymgmt_gettable_params_fn ed25519_gettable_params;
  52. static OSSL_FUNC_keymgmt_gettable_params_fn ed448_gettable_params;
  53. static OSSL_FUNC_keymgmt_set_params_fn x25519_set_params;
  54. static OSSL_FUNC_keymgmt_set_params_fn x448_set_params;
  55. static OSSL_FUNC_keymgmt_set_params_fn ed25519_set_params;
  56. static OSSL_FUNC_keymgmt_set_params_fn ed448_set_params;
  57. static OSSL_FUNC_keymgmt_settable_params_fn x25519_settable_params;
  58. static OSSL_FUNC_keymgmt_settable_params_fn x448_settable_params;
  59. static OSSL_FUNC_keymgmt_settable_params_fn ed25519_settable_params;
  60. static OSSL_FUNC_keymgmt_settable_params_fn ed448_settable_params;
  61. static OSSL_FUNC_keymgmt_has_fn ecx_has;
  62. static OSSL_FUNC_keymgmt_match_fn ecx_match;
  63. static OSSL_FUNC_keymgmt_validate_fn x25519_validate;
  64. static OSSL_FUNC_keymgmt_validate_fn x448_validate;
  65. static OSSL_FUNC_keymgmt_validate_fn ed25519_validate;
  66. static OSSL_FUNC_keymgmt_validate_fn ed448_validate;
  67. static OSSL_FUNC_keymgmt_import_fn ecx_import;
  68. static OSSL_FUNC_keymgmt_import_types_fn ecx_imexport_types;
  69. static OSSL_FUNC_keymgmt_export_fn ecx_export;
  70. static OSSL_FUNC_keymgmt_export_types_fn ecx_imexport_types;
  71. static OSSL_FUNC_keymgmt_dup_fn ecx_dup;
  72. #define ECX_POSSIBLE_SELECTIONS (OSSL_KEYMGMT_SELECT_KEYPAIR)
  73. struct ecx_gen_ctx {
  74. OSSL_LIB_CTX *libctx;
  75. char *propq;
  76. ECX_KEY_TYPE type;
  77. int selection;
  78. unsigned char *dhkem_ikm;
  79. size_t dhkem_ikmlen;
  80. };
  81. #ifdef S390X_EC_ASM
  82. static void *s390x_ecx_keygen25519(struct ecx_gen_ctx *gctx);
  83. static void *s390x_ecx_keygen448(struct ecx_gen_ctx *gctx);
  84. static void *s390x_ecd_keygen25519(struct ecx_gen_ctx *gctx);
  85. static void *s390x_ecd_keygen448(struct ecx_gen_ctx *gctx);
  86. #endif
  87. static void *x25519_new_key(void *provctx)
  88. {
  89. if (!ossl_prov_is_running())
  90. return 0;
  91. return ossl_ecx_key_new(PROV_LIBCTX_OF(provctx), ECX_KEY_TYPE_X25519, 0,
  92. NULL);
  93. }
  94. static void *x448_new_key(void *provctx)
  95. {
  96. if (!ossl_prov_is_running())
  97. return 0;
  98. return ossl_ecx_key_new(PROV_LIBCTX_OF(provctx), ECX_KEY_TYPE_X448, 0,
  99. NULL);
  100. }
  101. static void *ed25519_new_key(void *provctx)
  102. {
  103. if (!ossl_prov_is_running())
  104. return 0;
  105. return ossl_ecx_key_new(PROV_LIBCTX_OF(provctx), ECX_KEY_TYPE_ED25519, 0,
  106. NULL);
  107. }
  108. static void *ed448_new_key(void *provctx)
  109. {
  110. if (!ossl_prov_is_running())
  111. return 0;
  112. return ossl_ecx_key_new(PROV_LIBCTX_OF(provctx), ECX_KEY_TYPE_ED448, 0,
  113. NULL);
  114. }
  115. static int ecx_has(const void *keydata, int selection)
  116. {
  117. const ECX_KEY *key = keydata;
  118. int ok = 0;
  119. if (ossl_prov_is_running() && key != NULL) {
  120. /*
  121. * ECX keys always have all the parameters they need (i.e. none).
  122. * Therefore we always return with 1, if asked about parameters.
  123. */
  124. ok = 1;
  125. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  126. ok = ok && key->haspubkey;
  127. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  128. ok = ok && key->privkey != NULL;
  129. }
  130. return ok;
  131. }
  132. static int ecx_match(const void *keydata1, const void *keydata2, int selection)
  133. {
  134. const ECX_KEY *key1 = keydata1;
  135. const ECX_KEY *key2 = keydata2;
  136. int ok = 1;
  137. if (!ossl_prov_is_running())
  138. return 0;
  139. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
  140. ok = ok && key1->type == key2->type;
  141. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
  142. int key_checked = 0;
  143. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
  144. const unsigned char *pa = key1->haspubkey ? key1->pubkey : NULL;
  145. const unsigned char *pb = key2->haspubkey ? key2->pubkey : NULL;
  146. size_t pal = key1->keylen;
  147. size_t pbl = key2->keylen;
  148. if (pa != NULL && pb != NULL) {
  149. ok = ok
  150. && key1->type == key2->type
  151. && pal == pbl
  152. && CRYPTO_memcmp(pa, pb, pal) == 0;
  153. key_checked = 1;
  154. }
  155. }
  156. if (!key_checked
  157. && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  158. const unsigned char *pa = key1->privkey;
  159. const unsigned char *pb = key2->privkey;
  160. size_t pal = key1->keylen;
  161. size_t pbl = key2->keylen;
  162. if (pa != NULL && pb != NULL) {
  163. ok = ok
  164. && key1->type == key2->type
  165. && pal == pbl
  166. && CRYPTO_memcmp(pa, pb, pal) == 0;
  167. key_checked = 1;
  168. }
  169. }
  170. ok = ok && key_checked;
  171. }
  172. return ok;
  173. }
  174. static int ecx_import(void *keydata, int selection, const OSSL_PARAM params[])
  175. {
  176. ECX_KEY *key = keydata;
  177. int ok = 1;
  178. int include_private;
  179. if (!ossl_prov_is_running() || key == NULL)
  180. return 0;
  181. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
  182. return 0;
  183. include_private = selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
  184. ok = ok && ossl_ecx_key_fromdata(key, params, include_private);
  185. return ok;
  186. }
  187. static int key_to_params(ECX_KEY *key, OSSL_PARAM_BLD *tmpl,
  188. OSSL_PARAM params[], int include_private)
  189. {
  190. if (key == NULL)
  191. return 0;
  192. if (!ossl_param_build_set_octet_string(tmpl, params,
  193. OSSL_PKEY_PARAM_PUB_KEY,
  194. key->pubkey, key->keylen))
  195. return 0;
  196. if (include_private
  197. && key->privkey != NULL
  198. && !ossl_param_build_set_octet_string(tmpl, params,
  199. OSSL_PKEY_PARAM_PRIV_KEY,
  200. key->privkey, key->keylen))
  201. return 0;
  202. return 1;
  203. }
  204. static int ecx_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
  205. void *cbarg)
  206. {
  207. ECX_KEY *key = keydata;
  208. OSSL_PARAM_BLD *tmpl;
  209. OSSL_PARAM *params = NULL;
  210. int ret = 0;
  211. if (!ossl_prov_is_running() || key == NULL)
  212. return 0;
  213. tmpl = OSSL_PARAM_BLD_new();
  214. if (tmpl == NULL)
  215. return 0;
  216. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
  217. int include_private = ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0);
  218. if (!key_to_params(key, tmpl, NULL, include_private))
  219. goto err;
  220. }
  221. params = OSSL_PARAM_BLD_to_param(tmpl);
  222. if (params == NULL)
  223. goto err;
  224. ret = param_cb(params, cbarg);
  225. OSSL_PARAM_free(params);
  226. err:
  227. OSSL_PARAM_BLD_free(tmpl);
  228. return ret;
  229. }
  230. #define ECX_KEY_TYPES() \
  231. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0), \
  232. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
  233. static const OSSL_PARAM ecx_key_types[] = {
  234. ECX_KEY_TYPES(),
  235. OSSL_PARAM_END
  236. };
  237. static const OSSL_PARAM *ecx_imexport_types(int selection)
  238. {
  239. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
  240. return ecx_key_types;
  241. return NULL;
  242. }
  243. static int ecx_get_params(void *key, OSSL_PARAM params[], int bits, int secbits,
  244. int size)
  245. {
  246. ECX_KEY *ecx = key;
  247. OSSL_PARAM *p;
  248. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
  249. && !OSSL_PARAM_set_int(p, bits))
  250. return 0;
  251. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
  252. && !OSSL_PARAM_set_int(p, secbits))
  253. return 0;
  254. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
  255. && !OSSL_PARAM_set_int(p, size))
  256. return 0;
  257. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY)) != NULL
  258. && (ecx->type == ECX_KEY_TYPE_X25519
  259. || ecx->type == ECX_KEY_TYPE_X448)) {
  260. if (!OSSL_PARAM_set_octet_string(p, ecx->pubkey, ecx->keylen))
  261. return 0;
  262. }
  263. return key_to_params(ecx, NULL, params, 1);
  264. }
  265. static int ed_get_params(void *key, OSSL_PARAM params[])
  266. {
  267. OSSL_PARAM *p;
  268. if ((p = OSSL_PARAM_locate(params,
  269. OSSL_PKEY_PARAM_MANDATORY_DIGEST)) != NULL
  270. && !OSSL_PARAM_set_utf8_string(p, ""))
  271. return 0;
  272. return 1;
  273. }
  274. static int x25519_get_params(void *key, OSSL_PARAM params[])
  275. {
  276. return ecx_get_params(key, params, X25519_BITS, X25519_SECURITY_BITS,
  277. X25519_KEYLEN);
  278. }
  279. static int x448_get_params(void *key, OSSL_PARAM params[])
  280. {
  281. return ecx_get_params(key, params, X448_BITS, X448_SECURITY_BITS,
  282. X448_KEYLEN);
  283. }
  284. static int ed25519_get_params(void *key, OSSL_PARAM params[])
  285. {
  286. return ecx_get_params(key, params, ED25519_BITS, ED25519_SECURITY_BITS,
  287. ED25519_SIGSIZE)
  288. && ed_get_params(key, params);
  289. }
  290. static int ed448_get_params(void *key, OSSL_PARAM params[])
  291. {
  292. return ecx_get_params(key, params, ED448_BITS, ED448_SECURITY_BITS,
  293. ED448_SIGSIZE)
  294. && ed_get_params(key, params);
  295. }
  296. static const OSSL_PARAM ecx_gettable_params[] = {
  297. OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
  298. OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
  299. OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
  300. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_MANDATORY_DIGEST, NULL, 0),
  301. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
  302. ECX_KEY_TYPES(),
  303. OSSL_PARAM_END
  304. };
  305. static const OSSL_PARAM ed_gettable_params[] = {
  306. OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
  307. OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
  308. OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
  309. ECX_KEY_TYPES(),
  310. OSSL_PARAM_END
  311. };
  312. static const OSSL_PARAM *x25519_gettable_params(void *provctx)
  313. {
  314. return ecx_gettable_params;
  315. }
  316. static const OSSL_PARAM *x448_gettable_params(void *provctx)
  317. {
  318. return ecx_gettable_params;
  319. }
  320. static const OSSL_PARAM *ed25519_gettable_params(void *provctx)
  321. {
  322. return ed_gettable_params;
  323. }
  324. static const OSSL_PARAM *ed448_gettable_params(void *provctx)
  325. {
  326. return ed_gettable_params;
  327. }
  328. static int set_property_query(ECX_KEY *ecxkey, const char *propq)
  329. {
  330. OPENSSL_free(ecxkey->propq);
  331. ecxkey->propq = NULL;
  332. if (propq != NULL) {
  333. ecxkey->propq = OPENSSL_strdup(propq);
  334. if (ecxkey->propq == NULL)
  335. return 0;
  336. }
  337. return 1;
  338. }
  339. static int ecx_set_params(void *key, const OSSL_PARAM params[])
  340. {
  341. ECX_KEY *ecxkey = key;
  342. const OSSL_PARAM *p;
  343. if (params == NULL)
  344. return 1;
  345. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY);
  346. if (p != NULL) {
  347. void *buf = ecxkey->pubkey;
  348. if (p->data_size != ecxkey->keylen
  349. || !OSSL_PARAM_get_octet_string(p, &buf, sizeof(ecxkey->pubkey),
  350. NULL))
  351. return 0;
  352. OPENSSL_clear_free(ecxkey->privkey, ecxkey->keylen);
  353. ecxkey->privkey = NULL;
  354. ecxkey->haspubkey = 1;
  355. }
  356. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PROPERTIES);
  357. if (p != NULL) {
  358. if (p->data_type != OSSL_PARAM_UTF8_STRING
  359. || !set_property_query(ecxkey, p->data))
  360. return 0;
  361. }
  362. return 1;
  363. }
  364. static int x25519_set_params(void *key, const OSSL_PARAM params[])
  365. {
  366. return ecx_set_params(key, params);
  367. }
  368. static int x448_set_params(void *key, const OSSL_PARAM params[])
  369. {
  370. return ecx_set_params(key, params);
  371. }
  372. static int ed25519_set_params(void *key, const OSSL_PARAM params[])
  373. {
  374. return 1;
  375. }
  376. static int ed448_set_params(void *key, const OSSL_PARAM params[])
  377. {
  378. return 1;
  379. }
  380. static const OSSL_PARAM ecx_settable_params[] = {
  381. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
  382. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_PROPERTIES, NULL, 0),
  383. OSSL_PARAM_END
  384. };
  385. static const OSSL_PARAM ed_settable_params[] = {
  386. OSSL_PARAM_END
  387. };
  388. static const OSSL_PARAM *x25519_settable_params(void *provctx)
  389. {
  390. return ecx_settable_params;
  391. }
  392. static const OSSL_PARAM *x448_settable_params(void *provctx)
  393. {
  394. return ecx_settable_params;
  395. }
  396. static const OSSL_PARAM *ed25519_settable_params(void *provctx)
  397. {
  398. return ed_settable_params;
  399. }
  400. static const OSSL_PARAM *ed448_settable_params(void *provctx)
  401. {
  402. return ed_settable_params;
  403. }
  404. static void *ecx_gen_init(void *provctx, int selection,
  405. const OSSL_PARAM params[], ECX_KEY_TYPE type)
  406. {
  407. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
  408. struct ecx_gen_ctx *gctx = NULL;
  409. if (!ossl_prov_is_running())
  410. return NULL;
  411. if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
  412. gctx->libctx = libctx;
  413. gctx->type = type;
  414. gctx->selection = selection;
  415. }
  416. if (!ecx_gen_set_params(gctx, params)) {
  417. OPENSSL_free(gctx);
  418. gctx = NULL;
  419. }
  420. return gctx;
  421. }
  422. static void *x25519_gen_init(void *provctx, int selection,
  423. const OSSL_PARAM params[])
  424. {
  425. return ecx_gen_init(provctx, selection, params, ECX_KEY_TYPE_X25519);
  426. }
  427. static void *x448_gen_init(void *provctx, int selection,
  428. const OSSL_PARAM params[])
  429. {
  430. return ecx_gen_init(provctx, selection, params, ECX_KEY_TYPE_X448);
  431. }
  432. static void *ed25519_gen_init(void *provctx, int selection,
  433. const OSSL_PARAM params[])
  434. {
  435. return ecx_gen_init(provctx, selection, params, ECX_KEY_TYPE_ED25519);
  436. }
  437. static void *ed448_gen_init(void *provctx, int selection,
  438. const OSSL_PARAM params[])
  439. {
  440. return ecx_gen_init(provctx, selection, params, ECX_KEY_TYPE_ED448);
  441. }
  442. static int ecx_gen_set_params(void *genctx, const OSSL_PARAM params[])
  443. {
  444. struct ecx_gen_ctx *gctx = genctx;
  445. const OSSL_PARAM *p;
  446. if (gctx == NULL)
  447. return 0;
  448. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);
  449. if (p != NULL) {
  450. const char *groupname = NULL;
  451. /*
  452. * We optionally allow setting a group name - but each algorithm only
  453. * support one such name, so all we do is verify that it is the one we
  454. * expected.
  455. */
  456. switch (gctx->type) {
  457. case ECX_KEY_TYPE_X25519:
  458. groupname = "x25519";
  459. break;
  460. case ECX_KEY_TYPE_X448:
  461. groupname = "x448";
  462. break;
  463. default:
  464. /* We only support this for key exchange at the moment */
  465. break;
  466. }
  467. if (p->data_type != OSSL_PARAM_UTF8_STRING
  468. || groupname == NULL
  469. || OPENSSL_strcasecmp(p->data, groupname) != 0) {
  470. ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
  471. return 0;
  472. }
  473. }
  474. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PROPERTIES);
  475. if (p != NULL) {
  476. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  477. return 0;
  478. OPENSSL_free(gctx->propq);
  479. gctx->propq = OPENSSL_strdup(p->data);
  480. if (gctx->propq == NULL)
  481. return 0;
  482. }
  483. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DHKEM_IKM);
  484. if (p != NULL) {
  485. if (p->data_size != 0 && p->data != NULL) {
  486. OPENSSL_free(gctx->dhkem_ikm);
  487. gctx->dhkem_ikm = NULL;
  488. if (!OSSL_PARAM_get_octet_string(p, (void **)&gctx->dhkem_ikm, 0,
  489. &gctx->dhkem_ikmlen))
  490. return 0;
  491. }
  492. }
  493. return 1;
  494. }
  495. static const OSSL_PARAM *ecx_gen_settable_params(ossl_unused void *genctx,
  496. ossl_unused void *provctx)
  497. {
  498. static OSSL_PARAM settable[] = {
  499. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),
  500. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
  501. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_DHKEM_IKM, NULL, 0),
  502. OSSL_PARAM_END
  503. };
  504. return settable;
  505. }
  506. static void *ecx_gen(struct ecx_gen_ctx *gctx)
  507. {
  508. ECX_KEY *key;
  509. unsigned char *privkey;
  510. if (gctx == NULL)
  511. return NULL;
  512. if ((key = ossl_ecx_key_new(gctx->libctx, gctx->type, 0,
  513. gctx->propq)) == NULL) {
  514. ERR_raise(ERR_LIB_PROV, ERR_R_EC_LIB);
  515. return NULL;
  516. }
  517. /* If we're doing parameter generation then we just return a blank key */
  518. if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
  519. return key;
  520. if ((privkey = ossl_ecx_key_allocate_privkey(key)) == NULL) {
  521. ERR_raise(ERR_LIB_PROV, ERR_R_EC_LIB);
  522. goto err;
  523. }
  524. #ifndef FIPS_MODULE
  525. if (gctx->dhkem_ikm != NULL && gctx->dhkem_ikmlen != 0) {
  526. if (gctx->type == ECX_KEY_TYPE_ED25519
  527. || gctx->type == ECX_KEY_TYPE_ED448)
  528. goto err;
  529. if (!ossl_ecx_dhkem_derive_private(key, privkey,
  530. gctx->dhkem_ikm, gctx->dhkem_ikmlen))
  531. goto err;
  532. } else
  533. #endif
  534. {
  535. if (RAND_priv_bytes_ex(gctx->libctx, privkey, key->keylen, 0) <= 0)
  536. goto err;
  537. }
  538. switch (gctx->type) {
  539. case ECX_KEY_TYPE_X25519:
  540. privkey[0] &= 248;
  541. privkey[X25519_KEYLEN - 1] &= 127;
  542. privkey[X25519_KEYLEN - 1] |= 64;
  543. ossl_x25519_public_from_private(key->pubkey, privkey);
  544. break;
  545. case ECX_KEY_TYPE_X448:
  546. privkey[0] &= 252;
  547. privkey[X448_KEYLEN - 1] |= 128;
  548. ossl_x448_public_from_private(key->pubkey, privkey);
  549. break;
  550. case ECX_KEY_TYPE_ED25519:
  551. if (!ossl_ed25519_public_from_private(gctx->libctx, key->pubkey, privkey,
  552. gctx->propq))
  553. goto err;
  554. break;
  555. case ECX_KEY_TYPE_ED448:
  556. if (!ossl_ed448_public_from_private(gctx->libctx, key->pubkey, privkey,
  557. gctx->propq))
  558. goto err;
  559. break;
  560. }
  561. key->haspubkey = 1;
  562. return key;
  563. err:
  564. ossl_ecx_key_free(key);
  565. return NULL;
  566. }
  567. static void *x25519_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
  568. {
  569. struct ecx_gen_ctx *gctx = genctx;
  570. if (!ossl_prov_is_running())
  571. return 0;
  572. #ifdef S390X_EC_ASM
  573. if (OPENSSL_s390xcap_P.pcc[1] & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_X25519))
  574. return s390x_ecx_keygen25519(gctx);
  575. #endif
  576. return ecx_gen(gctx);
  577. }
  578. static void *x448_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
  579. {
  580. struct ecx_gen_ctx *gctx = genctx;
  581. if (!ossl_prov_is_running())
  582. return 0;
  583. #ifdef S390X_EC_ASM
  584. if (OPENSSL_s390xcap_P.pcc[1] & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_X448))
  585. return s390x_ecx_keygen448(gctx);
  586. #endif
  587. return ecx_gen(gctx);
  588. }
  589. static void *ed25519_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
  590. {
  591. struct ecx_gen_ctx *gctx = genctx;
  592. if (!ossl_prov_is_running())
  593. return 0;
  594. #ifdef S390X_EC_ASM
  595. if (OPENSSL_s390xcap_P.pcc[1] & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_ED25519)
  596. && OPENSSL_s390xcap_P.kdsa[0] & S390X_CAPBIT(S390X_EDDSA_SIGN_ED25519)
  597. && OPENSSL_s390xcap_P.kdsa[0]
  598. & S390X_CAPBIT(S390X_EDDSA_VERIFY_ED25519))
  599. return s390x_ecd_keygen25519(gctx);
  600. #endif
  601. return ecx_gen(gctx);
  602. }
  603. static void *ed448_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
  604. {
  605. struct ecx_gen_ctx *gctx = genctx;
  606. if (!ossl_prov_is_running())
  607. return 0;
  608. #ifdef S390X_EC_ASM
  609. if (OPENSSL_s390xcap_P.pcc[1] & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_ED448)
  610. && OPENSSL_s390xcap_P.kdsa[0] & S390X_CAPBIT(S390X_EDDSA_SIGN_ED448)
  611. && OPENSSL_s390xcap_P.kdsa[0] & S390X_CAPBIT(S390X_EDDSA_VERIFY_ED448))
  612. return s390x_ecd_keygen448(gctx);
  613. #endif
  614. return ecx_gen(gctx);
  615. }
  616. static void ecx_gen_cleanup(void *genctx)
  617. {
  618. struct ecx_gen_ctx *gctx = genctx;
  619. OPENSSL_clear_free(gctx->dhkem_ikm, gctx->dhkem_ikmlen);
  620. OPENSSL_free(gctx->propq);
  621. OPENSSL_free(gctx);
  622. }
  623. void *ecx_load(const void *reference, size_t reference_sz)
  624. {
  625. ECX_KEY *key = NULL;
  626. if (ossl_prov_is_running() && reference_sz == sizeof(key)) {
  627. /* The contents of the reference is the address to our object */
  628. key = *(ECX_KEY **)reference;
  629. /* We grabbed, so we detach it */
  630. *(ECX_KEY **)reference = NULL;
  631. return key;
  632. }
  633. return NULL;
  634. }
  635. static void *ecx_dup(const void *keydata_from, int selection)
  636. {
  637. if (ossl_prov_is_running())
  638. return ossl_ecx_key_dup(keydata_from, selection);
  639. return NULL;
  640. }
  641. static int ecx_key_pairwise_check(const ECX_KEY *ecx, int type)
  642. {
  643. uint8_t pub[64];
  644. switch (type) {
  645. case ECX_KEY_TYPE_X25519:
  646. ossl_x25519_public_from_private(pub, ecx->privkey);
  647. break;
  648. case ECX_KEY_TYPE_X448:
  649. ossl_x448_public_from_private(pub, ecx->privkey);
  650. break;
  651. case ECX_KEY_TYPE_ED25519:
  652. if (!ossl_ed25519_public_from_private(ecx->libctx, pub, ecx->privkey,
  653. ecx->propq))
  654. return 0;
  655. break;
  656. case ECX_KEY_TYPE_ED448:
  657. if (!ossl_ed448_public_from_private(ecx->libctx, pub, ecx->privkey,
  658. ecx->propq))
  659. return 0;
  660. break;
  661. default:
  662. return 0;
  663. }
  664. return CRYPTO_memcmp(ecx->pubkey, pub, ecx->keylen) == 0;
  665. }
  666. static int ecx_validate(const void *keydata, int selection, int type, size_t keylen)
  667. {
  668. const ECX_KEY *ecx = keydata;
  669. int ok = keylen == ecx->keylen;
  670. if (!ossl_prov_is_running())
  671. return 0;
  672. if ((selection & ECX_POSSIBLE_SELECTIONS) == 0)
  673. return 1; /* nothing to validate */
  674. if (!ok) {
  675. ERR_raise(ERR_LIB_PROV, PROV_R_ALGORITHM_MISMATCH);
  676. return 0;
  677. }
  678. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  679. ok = ok && ecx->haspubkey;
  680. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  681. ok = ok && ecx->privkey != NULL;
  682. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR)
  683. ok = ok && ecx_key_pairwise_check(ecx, type);
  684. return ok;
  685. }
  686. static int x25519_validate(const void *keydata, int selection, int checktype)
  687. {
  688. return ecx_validate(keydata, selection, ECX_KEY_TYPE_X25519, X25519_KEYLEN);
  689. }
  690. static int x448_validate(const void *keydata, int selection, int checktype)
  691. {
  692. return ecx_validate(keydata, selection, ECX_KEY_TYPE_X448, X448_KEYLEN);
  693. }
  694. static int ed25519_validate(const void *keydata, int selection, int checktype)
  695. {
  696. return ecx_validate(keydata, selection, ECX_KEY_TYPE_ED25519, ED25519_KEYLEN);
  697. }
  698. static int ed448_validate(const void *keydata, int selection, int checktype)
  699. {
  700. return ecx_validate(keydata, selection, ECX_KEY_TYPE_ED448, ED448_KEYLEN);
  701. }
  702. #define MAKE_KEYMGMT_FUNCTIONS(alg) \
  703. const OSSL_DISPATCH ossl_##alg##_keymgmt_functions[] = { \
  704. { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))alg##_new_key }, \
  705. { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ossl_ecx_key_free }, \
  706. { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))alg##_get_params }, \
  707. { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))alg##_gettable_params }, \
  708. { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))alg##_set_params }, \
  709. { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))alg##_settable_params }, \
  710. { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ecx_has }, \
  711. { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ecx_match }, \
  712. { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))alg##_validate }, \
  713. { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ecx_import }, \
  714. { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ecx_imexport_types }, \
  715. { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ecx_export }, \
  716. { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ecx_imexport_types }, \
  717. { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))alg##_gen_init }, \
  718. { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))ecx_gen_set_params }, \
  719. { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, \
  720. (void (*)(void))ecx_gen_settable_params }, \
  721. { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))alg##_gen }, \
  722. { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))ecx_gen_cleanup }, \
  723. { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))ecx_load }, \
  724. { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))ecx_dup }, \
  725. { 0, NULL } \
  726. };
  727. MAKE_KEYMGMT_FUNCTIONS(x25519)
  728. MAKE_KEYMGMT_FUNCTIONS(x448)
  729. MAKE_KEYMGMT_FUNCTIONS(ed25519)
  730. MAKE_KEYMGMT_FUNCTIONS(ed448)
  731. #ifdef S390X_EC_ASM
  732. # include "s390x_arch.h"
  733. static void *s390x_ecx_keygen25519(struct ecx_gen_ctx *gctx)
  734. {
  735. static const unsigned char generator[] = {
  736. 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  737. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  738. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  739. };
  740. ECX_KEY *key = ossl_ecx_key_new(gctx->libctx, ECX_KEY_TYPE_X25519, 1,
  741. gctx->propq);
  742. unsigned char *privkey = NULL, *pubkey;
  743. if (key == NULL) {
  744. ERR_raise(ERR_LIB_PROV, ERR_R_EC_LIB);
  745. goto err;
  746. }
  747. /* If we're doing parameter generation then we just return a blank key */
  748. if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
  749. return key;
  750. pubkey = key->pubkey;
  751. privkey = ossl_ecx_key_allocate_privkey(key);
  752. if (privkey == NULL) {
  753. ERR_raise(ERR_LIB_PROV, ERR_R_EC_LIB);
  754. goto err;
  755. }
  756. #ifndef FIPS_MODULE
  757. if (gctx->dhkem_ikm != NULL && gctx->dhkem_ikmlen != 0) {
  758. if (gctx->type != ECX_KEY_TYPE_X25519)
  759. goto err;
  760. if (!ossl_ecx_dhkem_derive_private(key, privkey,
  761. gctx->dhkem_ikm, gctx->dhkem_ikmlen))
  762. goto err;
  763. } else
  764. #endif
  765. {
  766. if (RAND_priv_bytes_ex(gctx->libctx, privkey, X25519_KEYLEN, 0) <= 0)
  767. goto err;
  768. }
  769. privkey[0] &= 248;
  770. privkey[31] &= 127;
  771. privkey[31] |= 64;
  772. if (s390x_x25519_mul(pubkey, generator, privkey) != 1)
  773. goto err;
  774. key->haspubkey = 1;
  775. return key;
  776. err:
  777. ossl_ecx_key_free(key);
  778. return NULL;
  779. }
  780. static void *s390x_ecx_keygen448(struct ecx_gen_ctx *gctx)
  781. {
  782. static const unsigned char generator[] = {
  783. 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  784. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  785. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  786. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  787. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  788. };
  789. ECX_KEY *key = ossl_ecx_key_new(gctx->libctx, ECX_KEY_TYPE_X448, 1,
  790. gctx->propq);
  791. unsigned char *privkey = NULL, *pubkey;
  792. if (key == NULL) {
  793. ERR_raise(ERR_LIB_PROV, ERR_R_EC_LIB);
  794. goto err;
  795. }
  796. /* If we're doing parameter generation then we just return a blank key */
  797. if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
  798. return key;
  799. pubkey = key->pubkey;
  800. privkey = ossl_ecx_key_allocate_privkey(key);
  801. if (privkey == NULL) {
  802. ERR_raise(ERR_LIB_PROV, ERR_R_EC_LIB);
  803. goto err;
  804. }
  805. #ifndef FIPS_MODULE
  806. if (gctx->dhkem_ikm != NULL && gctx->dhkem_ikmlen != 0) {
  807. if (gctx->type != ECX_KEY_TYPE_X448)
  808. goto err;
  809. if (!ossl_ecx_dhkem_derive_private(key, privkey,
  810. gctx->dhkem_ikm, gctx->dhkem_ikmlen))
  811. goto err;
  812. } else
  813. #endif
  814. {
  815. if (RAND_priv_bytes_ex(gctx->libctx, privkey, X448_KEYLEN, 0) <= 0)
  816. goto err;
  817. }
  818. privkey[0] &= 252;
  819. privkey[55] |= 128;
  820. if (s390x_x448_mul(pubkey, generator, privkey) != 1)
  821. goto err;
  822. key->haspubkey = 1;
  823. return key;
  824. err:
  825. ossl_ecx_key_free(key);
  826. return NULL;
  827. }
  828. static void *s390x_ecd_keygen25519(struct ecx_gen_ctx *gctx)
  829. {
  830. static const unsigned char generator_x[] = {
  831. 0x1a, 0xd5, 0x25, 0x8f, 0x60, 0x2d, 0x56, 0xc9, 0xb2, 0xa7, 0x25, 0x95,
  832. 0x60, 0xc7, 0x2c, 0x69, 0x5c, 0xdc, 0xd6, 0xfd, 0x31, 0xe2, 0xa4, 0xc0,
  833. 0xfe, 0x53, 0x6e, 0xcd, 0xd3, 0x36, 0x69, 0x21
  834. };
  835. static const unsigned char generator_y[] = {
  836. 0x58, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  837. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  838. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  839. };
  840. unsigned char x_dst[32], buff[SHA512_DIGEST_LENGTH];
  841. ECX_KEY *key = ossl_ecx_key_new(gctx->libctx, ECX_KEY_TYPE_ED25519, 1,
  842. gctx->propq);
  843. unsigned char *privkey = NULL, *pubkey;
  844. unsigned int sz;
  845. EVP_MD *sha = NULL;
  846. int j;
  847. if (key == NULL) {
  848. ERR_raise(ERR_LIB_PROV, ERR_R_EC_LIB);
  849. goto err;
  850. }
  851. /* If we're doing parameter generation then we just return a blank key */
  852. if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
  853. return key;
  854. pubkey = key->pubkey;
  855. privkey = ossl_ecx_key_allocate_privkey(key);
  856. if (privkey == NULL) {
  857. ERR_raise(ERR_LIB_PROV, ERR_R_EC_LIB);
  858. goto err;
  859. }
  860. if (RAND_priv_bytes_ex(gctx->libctx, privkey, ED25519_KEYLEN, 0) <= 0)
  861. goto err;
  862. sha = EVP_MD_fetch(gctx->libctx, "SHA512", gctx->propq);
  863. if (sha == NULL)
  864. goto err;
  865. j = EVP_Digest(privkey, 32, buff, &sz, sha, NULL);
  866. EVP_MD_free(sha);
  867. if (!j)
  868. goto err;
  869. buff[0] &= 248;
  870. buff[31] &= 63;
  871. buff[31] |= 64;
  872. if (s390x_ed25519_mul(x_dst, pubkey,
  873. generator_x, generator_y, buff) != 1)
  874. goto err;
  875. pubkey[31] |= ((x_dst[0] & 0x01) << 7);
  876. key->haspubkey = 1;
  877. return key;
  878. err:
  879. ossl_ecx_key_free(key);
  880. return NULL;
  881. }
  882. static void *s390x_ecd_keygen448(struct ecx_gen_ctx *gctx)
  883. {
  884. static const unsigned char generator_x[] = {
  885. 0x5e, 0xc0, 0x0c, 0xc7, 0x2b, 0xa8, 0x26, 0x26, 0x8e, 0x93, 0x00, 0x8b,
  886. 0xe1, 0x80, 0x3b, 0x43, 0x11, 0x65, 0xb6, 0x2a, 0xf7, 0x1a, 0xae, 0x12,
  887. 0x64, 0xa4, 0xd3, 0xa3, 0x24, 0xe3, 0x6d, 0xea, 0x67, 0x17, 0x0f, 0x47,
  888. 0x70, 0x65, 0x14, 0x9e, 0xda, 0x36, 0xbf, 0x22, 0xa6, 0x15, 0x1d, 0x22,
  889. 0xed, 0x0d, 0xed, 0x6b, 0xc6, 0x70, 0x19, 0x4f, 0x00
  890. };
  891. static const unsigned char generator_y[] = {
  892. 0x14, 0xfa, 0x30, 0xf2, 0x5b, 0x79, 0x08, 0x98, 0xad, 0xc8, 0xd7, 0x4e,
  893. 0x2c, 0x13, 0xbd, 0xfd, 0xc4, 0x39, 0x7c, 0xe6, 0x1c, 0xff, 0xd3, 0x3a,
  894. 0xd7, 0xc2, 0xa0, 0x05, 0x1e, 0x9c, 0x78, 0x87, 0x40, 0x98, 0xa3, 0x6c,
  895. 0x73, 0x73, 0xea, 0x4b, 0x62, 0xc7, 0xc9, 0x56, 0x37, 0x20, 0x76, 0x88,
  896. 0x24, 0xbc, 0xb6, 0x6e, 0x71, 0x46, 0x3f, 0x69, 0x00
  897. };
  898. unsigned char x_dst[57], buff[114];
  899. ECX_KEY *key = ossl_ecx_key_new(gctx->libctx, ECX_KEY_TYPE_ED448, 1,
  900. gctx->propq);
  901. unsigned char *privkey = NULL, *pubkey;
  902. EVP_MD_CTX *hashctx = NULL;
  903. EVP_MD *shake = NULL;
  904. if (key == NULL) {
  905. ERR_raise(ERR_LIB_PROV, ERR_R_EC_LIB);
  906. goto err;
  907. }
  908. /* If we're doing parameter generation then we just return a blank key */
  909. if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
  910. return key;
  911. pubkey = key->pubkey;
  912. privkey = ossl_ecx_key_allocate_privkey(key);
  913. if (privkey == NULL) {
  914. ERR_raise(ERR_LIB_PROV, ERR_R_EC_LIB);
  915. goto err;
  916. }
  917. shake = EVP_MD_fetch(gctx->libctx, "SHAKE256", gctx->propq);
  918. if (shake == NULL)
  919. goto err;
  920. if (RAND_priv_bytes_ex(gctx->libctx, privkey, ED448_KEYLEN, 0) <= 0)
  921. goto err;
  922. hashctx = EVP_MD_CTX_new();
  923. if (hashctx == NULL)
  924. goto err;
  925. if (EVP_DigestInit_ex(hashctx, shake, NULL) != 1)
  926. goto err;
  927. if (EVP_DigestUpdate(hashctx, privkey, 57) != 1)
  928. goto err;
  929. if (EVP_DigestFinalXOF(hashctx, buff, sizeof(buff)) != 1)
  930. goto err;
  931. buff[0] &= -4;
  932. buff[55] |= 0x80;
  933. buff[56] = 0;
  934. if (s390x_ed448_mul(x_dst, pubkey,
  935. generator_x, generator_y, buff) != 1)
  936. goto err;
  937. pubkey[56] |= ((x_dst[0] & 0x01) << 7);
  938. EVP_MD_CTX_free(hashctx);
  939. EVP_MD_free(shake);
  940. key->haspubkey = 1;
  941. return key;
  942. err:
  943. ossl_ecx_key_free(key);
  944. EVP_MD_CTX_free(hashctx);
  945. EVP_MD_free(shake);
  946. return NULL;
  947. }
  948. #endif