ecx_kmgmt.c 34 KB

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