ecx_kmgmt.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  1. /*
  2. * Copyright 2020-2025 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #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_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. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_MANDATORY_DIGEST, NULL, 0),
  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. } else {
  419. return NULL;
  420. }
  421. if (!ecx_gen_set_params(gctx, params)) {
  422. ecx_gen_cleanup(gctx);
  423. gctx = NULL;
  424. }
  425. return gctx;
  426. }
  427. static void *x25519_gen_init(void *provctx, int selection,
  428. const OSSL_PARAM params[])
  429. {
  430. return ecx_gen_init(provctx, selection, params, ECX_KEY_TYPE_X25519);
  431. }
  432. static void *x448_gen_init(void *provctx, int selection,
  433. const OSSL_PARAM params[])
  434. {
  435. return ecx_gen_init(provctx, selection, params, ECX_KEY_TYPE_X448);
  436. }
  437. static void *ed25519_gen_init(void *provctx, int selection,
  438. const OSSL_PARAM params[])
  439. {
  440. return ecx_gen_init(provctx, selection, params, ECX_KEY_TYPE_ED25519);
  441. }
  442. static void *ed448_gen_init(void *provctx, int selection,
  443. const OSSL_PARAM params[])
  444. {
  445. return ecx_gen_init(provctx, selection, params, ECX_KEY_TYPE_ED448);
  446. }
  447. static int ecx_gen_set_params(void *genctx, const OSSL_PARAM params[])
  448. {
  449. struct ecx_gen_ctx *gctx = genctx;
  450. const OSSL_PARAM *p;
  451. if (gctx == NULL)
  452. return 0;
  453. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);
  454. if (p != NULL) {
  455. const char *groupname = NULL;
  456. /*
  457. * We optionally allow setting a group name - but each algorithm only
  458. * support one such name, so all we do is verify that it is the one we
  459. * expected.
  460. */
  461. switch (gctx->type) {
  462. case ECX_KEY_TYPE_X25519:
  463. groupname = "x25519";
  464. break;
  465. case ECX_KEY_TYPE_X448:
  466. groupname = "x448";
  467. break;
  468. default:
  469. /* We only support this for key exchange at the moment */
  470. break;
  471. }
  472. if (p->data_type != OSSL_PARAM_UTF8_STRING
  473. || groupname == NULL
  474. || OPENSSL_strcasecmp(p->data, groupname) != 0) {
  475. ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
  476. return 0;
  477. }
  478. }
  479. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PROPERTIES);
  480. if (p != NULL) {
  481. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  482. return 0;
  483. OPENSSL_free(gctx->propq);
  484. gctx->propq = OPENSSL_strdup(p->data);
  485. if (gctx->propq == NULL)
  486. return 0;
  487. }
  488. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DHKEM_IKM);
  489. if (p != NULL) {
  490. if (p->data_size != 0 && p->data != NULL) {
  491. OPENSSL_free(gctx->dhkem_ikm);
  492. gctx->dhkem_ikm = NULL;
  493. if (!OSSL_PARAM_get_octet_string(p, (void **)&gctx->dhkem_ikm, 0,
  494. &gctx->dhkem_ikmlen))
  495. return 0;
  496. }
  497. }
  498. return 1;
  499. }
  500. static const OSSL_PARAM *ecx_gen_settable_params(ossl_unused void *genctx,
  501. ossl_unused void *provctx)
  502. {
  503. static OSSL_PARAM settable[] = {
  504. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),
  505. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
  506. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_DHKEM_IKM, NULL, 0),
  507. OSSL_PARAM_END
  508. };
  509. return settable;
  510. }
  511. #ifdef FIPS_MODULE
  512. /*
  513. * Refer: FIPS 140-3 IG 10.3.A Additional Comment 1
  514. * Perform a pairwise test for EDDSA by signing and verifying signature.
  515. *
  516. * The parameter `self_test` is used to indicate whether to create OSSL_SELF_TEST
  517. * instance.
  518. */
  519. static int ecd_fips140_pairwise_test(const ECX_KEY *ecx, int type, int self_test)
  520. {
  521. int ret = 0;
  522. OSSL_SELF_TEST *st = NULL;
  523. OSSL_CALLBACK *cb = NULL;
  524. void *cbarg = NULL;
  525. unsigned char msg[16] = {0};
  526. size_t msg_len = sizeof(msg);
  527. unsigned char sig[ED448_SIGSIZE] = {0};
  528. int is_ed25519 = (type == ECX_KEY_TYPE_ED25519) ? 1 : 0;
  529. int operation_result = 0;
  530. /*
  531. * The functions `OSSL_SELF_TEST_*` will return directly if parameter `st`
  532. * is NULL.
  533. */
  534. if (self_test) {
  535. OSSL_SELF_TEST_get_callback(ecx->libctx, &cb, &cbarg);
  536. st = OSSL_SELF_TEST_new(cb, cbarg);
  537. if (st == NULL)
  538. return 0;
  539. }
  540. OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_PCT,
  541. OSSL_SELF_TEST_DESC_PCT_EDDSA);
  542. if (is_ed25519)
  543. operation_result = ossl_ed25519_sign(sig, msg, msg_len, ecx->pubkey,
  544. ecx->privkey, 0, 0, 0, NULL, 0,
  545. ecx->libctx, ecx->propq);
  546. else
  547. operation_result = ossl_ed448_sign(ecx->libctx, sig, msg, msg_len,
  548. ecx->pubkey, ecx->privkey, NULL, 0,
  549. 0, ecx->propq);
  550. if (operation_result != 1)
  551. goto err;
  552. OSSL_SELF_TEST_oncorrupt_byte(st, sig);
  553. if (is_ed25519)
  554. operation_result = ossl_ed25519_verify(msg, msg_len, sig, ecx->pubkey,
  555. 0, 0, 0, NULL, 0, ecx->libctx,
  556. ecx->propq);
  557. else
  558. operation_result = ossl_ed448_verify(ecx->libctx, msg, msg_len, sig,
  559. ecx->pubkey, NULL, 0, 0, ecx->propq);
  560. if (operation_result != 1)
  561. goto err;
  562. ret = 1;
  563. err:
  564. OSSL_SELF_TEST_onend(st, ret);
  565. OSSL_SELF_TEST_free(st);
  566. return ret;
  567. }
  568. #endif
  569. static void *ecx_gen(struct ecx_gen_ctx *gctx)
  570. {
  571. ECX_KEY *key;
  572. unsigned char *privkey;
  573. if (gctx == NULL)
  574. return NULL;
  575. if ((key = ossl_ecx_key_new(gctx->libctx, gctx->type, 0,
  576. gctx->propq)) == NULL) {
  577. ERR_raise(ERR_LIB_PROV, ERR_R_EC_LIB);
  578. return NULL;
  579. }
  580. /* If we're doing parameter generation then we just return a blank key */
  581. if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
  582. return key;
  583. if ((privkey = ossl_ecx_key_allocate_privkey(key)) == NULL) {
  584. ERR_raise(ERR_LIB_PROV, ERR_R_EC_LIB);
  585. goto err;
  586. }
  587. #ifndef FIPS_MODULE
  588. if (gctx->dhkem_ikm != NULL && gctx->dhkem_ikmlen != 0) {
  589. if (gctx->type == ECX_KEY_TYPE_ED25519
  590. || gctx->type == ECX_KEY_TYPE_ED448)
  591. goto err;
  592. if (!ossl_ecx_dhkem_derive_private(key, privkey,
  593. gctx->dhkem_ikm, gctx->dhkem_ikmlen))
  594. goto err;
  595. } else
  596. #endif
  597. {
  598. if (RAND_priv_bytes_ex(gctx->libctx, privkey, key->keylen, 0) <= 0)
  599. goto err;
  600. }
  601. switch (gctx->type) {
  602. case ECX_KEY_TYPE_X25519:
  603. privkey[0] &= 248;
  604. privkey[X25519_KEYLEN - 1] &= 127;
  605. privkey[X25519_KEYLEN - 1] |= 64;
  606. ossl_x25519_public_from_private(key->pubkey, privkey);
  607. break;
  608. case ECX_KEY_TYPE_X448:
  609. privkey[0] &= 252;
  610. privkey[X448_KEYLEN - 1] |= 128;
  611. ossl_x448_public_from_private(key->pubkey, privkey);
  612. break;
  613. case ECX_KEY_TYPE_ED25519:
  614. if (!ossl_ed25519_public_from_private(gctx->libctx, key->pubkey, privkey,
  615. gctx->propq))
  616. goto err;
  617. break;
  618. case ECX_KEY_TYPE_ED448:
  619. if (!ossl_ed448_public_from_private(gctx->libctx, key->pubkey, privkey,
  620. gctx->propq))
  621. goto err;
  622. break;
  623. }
  624. key->haspubkey = 1;
  625. return key;
  626. err:
  627. ossl_ecx_key_free(key);
  628. return NULL;
  629. }
  630. static void *x25519_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
  631. {
  632. struct ecx_gen_ctx *gctx = genctx;
  633. if (!ossl_prov_is_running())
  634. return 0;
  635. #ifdef S390X_EC_ASM
  636. if (OPENSSL_s390xcap_P.pcc[1] & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_X25519))
  637. return s390x_ecx_keygen25519(gctx);
  638. #endif
  639. return ecx_gen(gctx);
  640. }
  641. static void *x448_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
  642. {
  643. struct ecx_gen_ctx *gctx = genctx;
  644. if (!ossl_prov_is_running())
  645. return 0;
  646. #ifdef S390X_EC_ASM
  647. if (OPENSSL_s390xcap_P.pcc[1] & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_X448))
  648. return s390x_ecx_keygen448(gctx);
  649. #endif
  650. return ecx_gen(gctx);
  651. }
  652. static void *ed25519_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
  653. {
  654. ECX_KEY *key = NULL;
  655. struct ecx_gen_ctx *gctx = genctx;
  656. if (!ossl_prov_is_running())
  657. return 0;
  658. #ifdef S390X_EC_ASM
  659. if (OPENSSL_s390xcap_P.pcc[1] & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_ED25519)
  660. && OPENSSL_s390xcap_P.kdsa[0] & S390X_CAPBIT(S390X_EDDSA_SIGN_ED25519)
  661. && OPENSSL_s390xcap_P.kdsa[0]
  662. & S390X_CAPBIT(S390X_EDDSA_VERIFY_ED25519)) {
  663. key = s390x_ecd_keygen25519(gctx);
  664. } else
  665. #endif
  666. {
  667. key = ecx_gen(gctx);
  668. }
  669. #ifdef FIPS_MODULE
  670. /* Exit if keygen failed OR we are doing parameter generation (blank key) */
  671. if (!key || ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0))
  672. return key;
  673. if (ecd_fips140_pairwise_test(key, ECX_KEY_TYPE_ED25519, 1) != 1) {
  674. ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT);
  675. ossl_ecx_key_free(key);
  676. return NULL;
  677. }
  678. #endif
  679. return key;
  680. }
  681. static void *ed448_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
  682. {
  683. ECX_KEY *key = NULL;
  684. struct ecx_gen_ctx *gctx = genctx;
  685. if (!ossl_prov_is_running())
  686. return 0;
  687. #ifdef S390X_EC_ASM
  688. if (OPENSSL_s390xcap_P.pcc[1] & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_ED448)
  689. && OPENSSL_s390xcap_P.kdsa[0] & S390X_CAPBIT(S390X_EDDSA_SIGN_ED448)
  690. && OPENSSL_s390xcap_P.kdsa[0] & S390X_CAPBIT(S390X_EDDSA_VERIFY_ED448)) {
  691. key = s390x_ecd_keygen448(gctx);
  692. } else
  693. #endif
  694. {
  695. key = ecx_gen(gctx);
  696. }
  697. #ifdef FIPS_MODULE
  698. /* Exit if keygen failed OR we are doing parameter generation (blank key) */
  699. if (!key || ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0))
  700. return key;
  701. if (ecd_fips140_pairwise_test(key, ECX_KEY_TYPE_ED448, 1) != 1) {
  702. ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT);
  703. ossl_ecx_key_free(key);
  704. return NULL;
  705. }
  706. #endif
  707. return key;
  708. }
  709. static void ecx_gen_cleanup(void *genctx)
  710. {
  711. struct ecx_gen_ctx *gctx = genctx;
  712. if (gctx == NULL)
  713. return;
  714. OPENSSL_clear_free(gctx->dhkem_ikm, gctx->dhkem_ikmlen);
  715. OPENSSL_free(gctx->propq);
  716. OPENSSL_free(gctx);
  717. }
  718. void *ecx_load(const void *reference, size_t reference_sz)
  719. {
  720. ECX_KEY *key = NULL;
  721. if (ossl_prov_is_running() && reference_sz == sizeof(key)) {
  722. /* The contents of the reference is the address to our object */
  723. key = *(ECX_KEY **)reference;
  724. /* We grabbed, so we detach it */
  725. *(ECX_KEY **)reference = NULL;
  726. return key;
  727. }
  728. return NULL;
  729. }
  730. static void *ecx_dup(const void *keydata_from, int selection)
  731. {
  732. if (ossl_prov_is_running())
  733. return ossl_ecx_key_dup(keydata_from, selection);
  734. return NULL;
  735. }
  736. static int ecx_key_pairwise_check(const ECX_KEY *ecx, int type)
  737. {
  738. uint8_t pub[64];
  739. switch (type) {
  740. case ECX_KEY_TYPE_X25519:
  741. ossl_x25519_public_from_private(pub, ecx->privkey);
  742. break;
  743. case ECX_KEY_TYPE_X448:
  744. ossl_x448_public_from_private(pub, ecx->privkey);
  745. break;
  746. default:
  747. return 0;
  748. }
  749. return CRYPTO_memcmp(ecx->pubkey, pub, ecx->keylen) == 0;
  750. }
  751. #ifdef FIPS_MODULE
  752. static int ecd_key_pairwise_check(const ECX_KEY *ecx, int type)
  753. {
  754. return ecd_fips140_pairwise_test(ecx, type, 0);
  755. }
  756. #else
  757. static int ecd_key_pairwise_check(const ECX_KEY *ecx, int type)
  758. {
  759. uint8_t pub[64];
  760. switch (type) {
  761. case ECX_KEY_TYPE_ED25519:
  762. if (!ossl_ed25519_public_from_private(ecx->libctx, pub, ecx->privkey,
  763. ecx->propq))
  764. return 0;
  765. break;
  766. case ECX_KEY_TYPE_ED448:
  767. if (!ossl_ed448_public_from_private(ecx->libctx, pub, ecx->privkey,
  768. ecx->propq))
  769. return 0;
  770. break;
  771. default:
  772. return 0;
  773. }
  774. return CRYPTO_memcmp(ecx->pubkey, pub, ecx->keylen) == 0;
  775. }
  776. #endif
  777. static int ecx_validate(const void *keydata, int selection, int type, size_t keylen)
  778. {
  779. const ECX_KEY *ecx = keydata;
  780. int ok = keylen == ecx->keylen;
  781. if (!ossl_prov_is_running())
  782. return 0;
  783. if ((selection & ECX_POSSIBLE_SELECTIONS) == 0)
  784. return 1; /* nothing to validate */
  785. if (!ok) {
  786. ERR_raise(ERR_LIB_PROV, PROV_R_ALGORITHM_MISMATCH);
  787. return 0;
  788. }
  789. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  790. ok = ok && ecx->haspubkey;
  791. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  792. ok = ok && ecx->privkey != NULL;
  793. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != OSSL_KEYMGMT_SELECT_KEYPAIR)
  794. return ok;
  795. if (type == ECX_KEY_TYPE_ED25519 || type == ECX_KEY_TYPE_ED448)
  796. ok = ok && ecd_key_pairwise_check(ecx, type);
  797. else
  798. ok = ok && ecx_key_pairwise_check(ecx, type);
  799. return ok;
  800. }
  801. static int x25519_validate(const void *keydata, int selection, int checktype)
  802. {
  803. return ecx_validate(keydata, selection, ECX_KEY_TYPE_X25519, X25519_KEYLEN);
  804. }
  805. static int x448_validate(const void *keydata, int selection, int checktype)
  806. {
  807. return ecx_validate(keydata, selection, ECX_KEY_TYPE_X448, X448_KEYLEN);
  808. }
  809. static int ed25519_validate(const void *keydata, int selection, int checktype)
  810. {
  811. return ecx_validate(keydata, selection, ECX_KEY_TYPE_ED25519, ED25519_KEYLEN);
  812. }
  813. static int ed448_validate(const void *keydata, int selection, int checktype)
  814. {
  815. return ecx_validate(keydata, selection, ECX_KEY_TYPE_ED448, ED448_KEYLEN);
  816. }
  817. #define MAKE_KEYMGMT_FUNCTIONS(alg) \
  818. const OSSL_DISPATCH ossl_##alg##_keymgmt_functions[] = { \
  819. { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))alg##_new_key }, \
  820. { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ossl_ecx_key_free }, \
  821. { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))alg##_get_params }, \
  822. { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))alg##_gettable_params }, \
  823. { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))alg##_set_params }, \
  824. { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))alg##_settable_params }, \
  825. { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ecx_has }, \
  826. { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ecx_match }, \
  827. { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))alg##_validate }, \
  828. { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ecx_import }, \
  829. { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ecx_imexport_types }, \
  830. { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ecx_export }, \
  831. { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ecx_imexport_types }, \
  832. { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))alg##_gen_init }, \
  833. { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))ecx_gen_set_params }, \
  834. { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, \
  835. (void (*)(void))ecx_gen_settable_params }, \
  836. { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))alg##_gen }, \
  837. { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))ecx_gen_cleanup }, \
  838. { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))ecx_load }, \
  839. { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))ecx_dup }, \
  840. OSSL_DISPATCH_END \
  841. };
  842. MAKE_KEYMGMT_FUNCTIONS(x25519)
  843. MAKE_KEYMGMT_FUNCTIONS(x448)
  844. MAKE_KEYMGMT_FUNCTIONS(ed25519)
  845. MAKE_KEYMGMT_FUNCTIONS(ed448)
  846. #ifdef S390X_EC_ASM
  847. # include "s390x_arch.h"
  848. static void *s390x_ecx_keygen25519(struct ecx_gen_ctx *gctx)
  849. {
  850. static const unsigned char generator[] = {
  851. 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  852. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  853. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  854. };
  855. ECX_KEY *key = ossl_ecx_key_new(gctx->libctx, ECX_KEY_TYPE_X25519, 1,
  856. gctx->propq);
  857. unsigned char *privkey = NULL, *pubkey;
  858. if (key == NULL) {
  859. ERR_raise(ERR_LIB_PROV, ERR_R_EC_LIB);
  860. goto err;
  861. }
  862. /* If we're doing parameter generation then we just return a blank key */
  863. if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
  864. return key;
  865. pubkey = key->pubkey;
  866. privkey = ossl_ecx_key_allocate_privkey(key);
  867. if (privkey == NULL) {
  868. ERR_raise(ERR_LIB_PROV, ERR_R_EC_LIB);
  869. goto err;
  870. }
  871. #ifndef FIPS_MODULE
  872. if (gctx->dhkem_ikm != NULL && gctx->dhkem_ikmlen != 0) {
  873. if (gctx->type != ECX_KEY_TYPE_X25519)
  874. goto err;
  875. if (!ossl_ecx_dhkem_derive_private(key, privkey,
  876. gctx->dhkem_ikm, gctx->dhkem_ikmlen))
  877. goto err;
  878. } else
  879. #endif
  880. {
  881. if (RAND_priv_bytes_ex(gctx->libctx, privkey, X25519_KEYLEN, 0) <= 0)
  882. goto err;
  883. }
  884. privkey[0] &= 248;
  885. privkey[31] &= 127;
  886. privkey[31] |= 64;
  887. if (s390x_x25519_mul(pubkey, generator, privkey) != 1)
  888. goto err;
  889. key->haspubkey = 1;
  890. return key;
  891. err:
  892. ossl_ecx_key_free(key);
  893. return NULL;
  894. }
  895. static void *s390x_ecx_keygen448(struct ecx_gen_ctx *gctx)
  896. {
  897. static const unsigned char generator[] = {
  898. 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  899. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  900. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  901. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  902. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  903. };
  904. ECX_KEY *key = ossl_ecx_key_new(gctx->libctx, ECX_KEY_TYPE_X448, 1,
  905. gctx->propq);
  906. unsigned char *privkey = NULL, *pubkey;
  907. if (key == NULL) {
  908. ERR_raise(ERR_LIB_PROV, ERR_R_EC_LIB);
  909. goto err;
  910. }
  911. /* If we're doing parameter generation then we just return a blank key */
  912. if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
  913. return key;
  914. pubkey = key->pubkey;
  915. privkey = ossl_ecx_key_allocate_privkey(key);
  916. if (privkey == NULL) {
  917. ERR_raise(ERR_LIB_PROV, ERR_R_EC_LIB);
  918. goto err;
  919. }
  920. #ifndef FIPS_MODULE
  921. if (gctx->dhkem_ikm != NULL && gctx->dhkem_ikmlen != 0) {
  922. if (gctx->type != ECX_KEY_TYPE_X448)
  923. goto err;
  924. if (!ossl_ecx_dhkem_derive_private(key, privkey,
  925. gctx->dhkem_ikm, gctx->dhkem_ikmlen))
  926. goto err;
  927. } else
  928. #endif
  929. {
  930. if (RAND_priv_bytes_ex(gctx->libctx, privkey, X448_KEYLEN, 0) <= 0)
  931. goto err;
  932. }
  933. privkey[0] &= 252;
  934. privkey[55] |= 128;
  935. if (s390x_x448_mul(pubkey, generator, privkey) != 1)
  936. goto err;
  937. key->haspubkey = 1;
  938. return key;
  939. err:
  940. ossl_ecx_key_free(key);
  941. return NULL;
  942. }
  943. static void *s390x_ecd_keygen25519(struct ecx_gen_ctx *gctx)
  944. {
  945. static const unsigned char generator_x[] = {
  946. 0x1a, 0xd5, 0x25, 0x8f, 0x60, 0x2d, 0x56, 0xc9, 0xb2, 0xa7, 0x25, 0x95,
  947. 0x60, 0xc7, 0x2c, 0x69, 0x5c, 0xdc, 0xd6, 0xfd, 0x31, 0xe2, 0xa4, 0xc0,
  948. 0xfe, 0x53, 0x6e, 0xcd, 0xd3, 0x36, 0x69, 0x21
  949. };
  950. static const unsigned char generator_y[] = {
  951. 0x58, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  952. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  953. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  954. };
  955. unsigned char x_dst[32], buff[SHA512_DIGEST_LENGTH];
  956. ECX_KEY *key = ossl_ecx_key_new(gctx->libctx, ECX_KEY_TYPE_ED25519, 1,
  957. gctx->propq);
  958. unsigned char *privkey = NULL, *pubkey;
  959. unsigned int sz;
  960. EVP_MD *sha = NULL;
  961. int j;
  962. if (key == NULL) {
  963. ERR_raise(ERR_LIB_PROV, ERR_R_EC_LIB);
  964. goto err;
  965. }
  966. /* If we're doing parameter generation then we just return a blank key */
  967. if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
  968. return key;
  969. pubkey = key->pubkey;
  970. privkey = ossl_ecx_key_allocate_privkey(key);
  971. if (privkey == NULL) {
  972. ERR_raise(ERR_LIB_PROV, ERR_R_EC_LIB);
  973. goto err;
  974. }
  975. if (RAND_priv_bytes_ex(gctx->libctx, privkey, ED25519_KEYLEN, 0) <= 0)
  976. goto err;
  977. sha = EVP_MD_fetch(gctx->libctx, "SHA512", gctx->propq);
  978. if (sha == NULL)
  979. goto err;
  980. j = EVP_Digest(privkey, 32, buff, &sz, sha, NULL);
  981. EVP_MD_free(sha);
  982. if (!j)
  983. goto err;
  984. buff[0] &= 248;
  985. buff[31] &= 63;
  986. buff[31] |= 64;
  987. if (s390x_ed25519_mul(x_dst, pubkey,
  988. generator_x, generator_y, buff) != 1)
  989. goto err;
  990. pubkey[31] |= ((x_dst[0] & 0x01) << 7);
  991. key->haspubkey = 1;
  992. return key;
  993. err:
  994. ossl_ecx_key_free(key);
  995. return NULL;
  996. }
  997. static void *s390x_ecd_keygen448(struct ecx_gen_ctx *gctx)
  998. {
  999. static const unsigned char generator_x[] = {
  1000. 0x5e, 0xc0, 0x0c, 0xc7, 0x2b, 0xa8, 0x26, 0x26, 0x8e, 0x93, 0x00, 0x8b,
  1001. 0xe1, 0x80, 0x3b, 0x43, 0x11, 0x65, 0xb6, 0x2a, 0xf7, 0x1a, 0xae, 0x12,
  1002. 0x64, 0xa4, 0xd3, 0xa3, 0x24, 0xe3, 0x6d, 0xea, 0x67, 0x17, 0x0f, 0x47,
  1003. 0x70, 0x65, 0x14, 0x9e, 0xda, 0x36, 0xbf, 0x22, 0xa6, 0x15, 0x1d, 0x22,
  1004. 0xed, 0x0d, 0xed, 0x6b, 0xc6, 0x70, 0x19, 0x4f, 0x00
  1005. };
  1006. static const unsigned char generator_y[] = {
  1007. 0x14, 0xfa, 0x30, 0xf2, 0x5b, 0x79, 0x08, 0x98, 0xad, 0xc8, 0xd7, 0x4e,
  1008. 0x2c, 0x13, 0xbd, 0xfd, 0xc4, 0x39, 0x7c, 0xe6, 0x1c, 0xff, 0xd3, 0x3a,
  1009. 0xd7, 0xc2, 0xa0, 0x05, 0x1e, 0x9c, 0x78, 0x87, 0x40, 0x98, 0xa3, 0x6c,
  1010. 0x73, 0x73, 0xea, 0x4b, 0x62, 0xc7, 0xc9, 0x56, 0x37, 0x20, 0x76, 0x88,
  1011. 0x24, 0xbc, 0xb6, 0x6e, 0x71, 0x46, 0x3f, 0x69, 0x00
  1012. };
  1013. unsigned char x_dst[57], buff[114];
  1014. ECX_KEY *key = ossl_ecx_key_new(gctx->libctx, ECX_KEY_TYPE_ED448, 1,
  1015. gctx->propq);
  1016. unsigned char *privkey = NULL, *pubkey;
  1017. EVP_MD_CTX *hashctx = NULL;
  1018. EVP_MD *shake = NULL;
  1019. if (key == NULL) {
  1020. ERR_raise(ERR_LIB_PROV, ERR_R_EC_LIB);
  1021. goto err;
  1022. }
  1023. /* If we're doing parameter generation then we just return a blank key */
  1024. if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
  1025. return key;
  1026. pubkey = key->pubkey;
  1027. privkey = ossl_ecx_key_allocate_privkey(key);
  1028. if (privkey == NULL) {
  1029. ERR_raise(ERR_LIB_PROV, ERR_R_EC_LIB);
  1030. goto err;
  1031. }
  1032. shake = EVP_MD_fetch(gctx->libctx, "SHAKE256", gctx->propq);
  1033. if (shake == NULL)
  1034. goto err;
  1035. if (RAND_priv_bytes_ex(gctx->libctx, privkey, ED448_KEYLEN, 0) <= 0)
  1036. goto err;
  1037. hashctx = EVP_MD_CTX_new();
  1038. if (hashctx == NULL)
  1039. goto err;
  1040. if (EVP_DigestInit_ex(hashctx, shake, NULL) != 1)
  1041. goto err;
  1042. if (EVP_DigestUpdate(hashctx, privkey, 57) != 1)
  1043. goto err;
  1044. if (EVP_DigestFinalXOF(hashctx, buff, sizeof(buff)) != 1)
  1045. goto err;
  1046. buff[0] &= -4;
  1047. buff[55] |= 0x80;
  1048. buff[56] = 0;
  1049. if (s390x_ed448_mul(x_dst, pubkey,
  1050. generator_x, generator_y, buff) != 1)
  1051. goto err;
  1052. pubkey[56] |= ((x_dst[0] & 0x01) << 7);
  1053. EVP_MD_CTX_free(hashctx);
  1054. EVP_MD_free(shake);
  1055. key->haspubkey = 1;
  1056. return key;
  1057. err:
  1058. ossl_ecx_key_free(key);
  1059. EVP_MD_CTX_free(hashctx);
  1060. EVP_MD_free(shake);
  1061. return NULL;
  1062. }
  1063. #endif