ecx_kmgmt.c 37 KB

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