ecx_meth.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. /*
  2. * Copyright 2006-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * ECDSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdio.h>
  15. #include <openssl/x509.h>
  16. #include <openssl/ec.h>
  17. #include <openssl/core_names.h>
  18. #include <openssl/param_build.h>
  19. #include <openssl/rand.h>
  20. #include "internal/cryptlib.h"
  21. #include "internal/provider.h"
  22. #include "crypto/asn1.h"
  23. #include "crypto/evp.h"
  24. #include "crypto/ecx.h"
  25. #include "ec_local.h"
  26. #include "curve448/curve448_local.h"
  27. #include "ecx_backend.h"
  28. static int ecx_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
  29. {
  30. const ECX_KEY *ecxkey = pkey->pkey.ecx;
  31. unsigned char *penc;
  32. if (ecxkey == NULL) {
  33. ERR_raise(ERR_LIB_EC, EC_R_INVALID_KEY);
  34. return 0;
  35. }
  36. penc = OPENSSL_memdup(ecxkey->pubkey, KEYLEN(pkey));
  37. if (penc == NULL)
  38. return 0;
  39. if (!X509_PUBKEY_set0_param(pk, OBJ_nid2obj(pkey->ameth->pkey_id),
  40. V_ASN1_UNDEF, NULL, penc, KEYLEN(pkey))) {
  41. OPENSSL_free(penc);
  42. ERR_raise(ERR_LIB_EC, ERR_R_X509_LIB);
  43. return 0;
  44. }
  45. return 1;
  46. }
  47. static int ecx_pub_decode(EVP_PKEY *pkey, const X509_PUBKEY *pubkey)
  48. {
  49. const unsigned char *p;
  50. int pklen;
  51. X509_ALGOR *palg;
  52. ECX_KEY *ecx;
  53. int ret = 0;
  54. if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
  55. return 0;
  56. ecx = ossl_ecx_key_op(palg, p, pklen, pkey->ameth->pkey_id,
  57. KEY_OP_PUBLIC, NULL, NULL);
  58. if (ecx != NULL) {
  59. ret = 1;
  60. EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, ecx);
  61. }
  62. return ret;
  63. }
  64. static int ecx_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
  65. {
  66. const ECX_KEY *akey = a->pkey.ecx;
  67. const ECX_KEY *bkey = b->pkey.ecx;
  68. if (akey == NULL || bkey == NULL)
  69. return -2;
  70. return CRYPTO_memcmp(akey->pubkey, bkey->pubkey, KEYLEN(a)) == 0;
  71. }
  72. static int ecx_priv_decode_ex(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8,
  73. OSSL_LIB_CTX *libctx, const char *propq)
  74. {
  75. int ret = 0;
  76. ECX_KEY *ecx = ossl_ecx_key_from_pkcs8(p8, libctx, propq);
  77. if (ecx != NULL) {
  78. ret = 1;
  79. EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, ecx);
  80. }
  81. return ret;
  82. }
  83. static int ecx_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
  84. {
  85. const ECX_KEY *ecxkey = pkey->pkey.ecx;
  86. ASN1_OCTET_STRING oct;
  87. unsigned char *penc = NULL;
  88. int penclen;
  89. if (ecxkey == NULL || ecxkey->privkey == NULL) {
  90. ERR_raise(ERR_LIB_EC, EC_R_INVALID_PRIVATE_KEY);
  91. return 0;
  92. }
  93. oct.data = ecxkey->privkey;
  94. oct.length = KEYLEN(pkey);
  95. oct.flags = 0;
  96. penclen = i2d_ASN1_OCTET_STRING(&oct, &penc);
  97. if (penclen < 0) {
  98. ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
  99. return 0;
  100. }
  101. if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(pkey->ameth->pkey_id), 0,
  102. V_ASN1_UNDEF, NULL, penc, penclen)) {
  103. OPENSSL_clear_free(penc, penclen);
  104. ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
  105. return 0;
  106. }
  107. return 1;
  108. }
  109. static int ecx_size(const EVP_PKEY *pkey)
  110. {
  111. return KEYLEN(pkey);
  112. }
  113. static int ecx_bits(const EVP_PKEY *pkey)
  114. {
  115. if (IS25519(pkey->ameth->pkey_id)) {
  116. return X25519_BITS;
  117. } else if (ISX448(pkey->ameth->pkey_id)) {
  118. return X448_BITS;
  119. } else {
  120. return ED448_BITS;
  121. }
  122. }
  123. static int ecx_security_bits(const EVP_PKEY *pkey)
  124. {
  125. if (IS25519(pkey->ameth->pkey_id)) {
  126. return X25519_SECURITY_BITS;
  127. } else {
  128. return X448_SECURITY_BITS;
  129. }
  130. }
  131. static void ecx_free(EVP_PKEY *pkey)
  132. {
  133. ossl_ecx_key_free(pkey->pkey.ecx);
  134. }
  135. /* "parameters" are always equal */
  136. static int ecx_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
  137. {
  138. return 1;
  139. }
  140. static int ecx_key_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  141. ASN1_PCTX *ctx, ecx_key_op_t op)
  142. {
  143. const ECX_KEY *ecxkey = pkey->pkey.ecx;
  144. const char *nm = OBJ_nid2ln(pkey->ameth->pkey_id);
  145. if (op == KEY_OP_PRIVATE) {
  146. if (ecxkey == NULL || ecxkey->privkey == NULL) {
  147. if (BIO_printf(bp, "%*s<INVALID PRIVATE KEY>\n", indent, "") <= 0)
  148. return 0;
  149. return 1;
  150. }
  151. if (BIO_printf(bp, "%*s%s Private-Key:\n", indent, "", nm) <= 0)
  152. return 0;
  153. if (BIO_printf(bp, "%*spriv:\n", indent, "") <= 0)
  154. return 0;
  155. if (ASN1_buf_print(bp, ecxkey->privkey, KEYLEN(pkey),
  156. indent + 4) == 0)
  157. return 0;
  158. } else {
  159. if (ecxkey == NULL) {
  160. if (BIO_printf(bp, "%*s<INVALID PUBLIC KEY>\n", indent, "") <= 0)
  161. return 0;
  162. return 1;
  163. }
  164. if (BIO_printf(bp, "%*s%s Public-Key:\n", indent, "", nm) <= 0)
  165. return 0;
  166. }
  167. if (BIO_printf(bp, "%*spub:\n", indent, "") <= 0)
  168. return 0;
  169. if (ASN1_buf_print(bp, ecxkey->pubkey, KEYLEN(pkey),
  170. indent + 4) == 0)
  171. return 0;
  172. return 1;
  173. }
  174. static int ecx_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  175. ASN1_PCTX *ctx)
  176. {
  177. return ecx_key_print(bp, pkey, indent, ctx, KEY_OP_PRIVATE);
  178. }
  179. static int ecx_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  180. ASN1_PCTX *ctx)
  181. {
  182. return ecx_key_print(bp, pkey, indent, ctx, KEY_OP_PUBLIC);
  183. }
  184. static int ecx_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
  185. {
  186. switch (op) {
  187. case ASN1_PKEY_CTRL_SET1_TLS_ENCPT: {
  188. ECX_KEY *ecx = ossl_ecx_key_op(NULL, arg2, arg1, pkey->ameth->pkey_id,
  189. KEY_OP_PUBLIC, NULL, NULL);
  190. if (ecx != NULL) {
  191. EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, ecx);
  192. return 1;
  193. }
  194. return 0;
  195. }
  196. case ASN1_PKEY_CTRL_GET1_TLS_ENCPT:
  197. if (pkey->pkey.ecx != NULL) {
  198. unsigned char **ppt = arg2;
  199. *ppt = OPENSSL_memdup(pkey->pkey.ecx->pubkey, KEYLEN(pkey));
  200. if (*ppt != NULL)
  201. return KEYLEN(pkey);
  202. }
  203. return 0;
  204. default:
  205. return -2;
  206. }
  207. }
  208. static int ecd_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
  209. {
  210. switch (op) {
  211. case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
  212. /* We currently only support Pure EdDSA which takes no digest */
  213. *(int *)arg2 = NID_undef;
  214. return 2;
  215. default:
  216. return -2;
  217. }
  218. }
  219. static int ecx_set_priv_key(EVP_PKEY *pkey, const unsigned char *priv,
  220. size_t len)
  221. {
  222. OSSL_LIB_CTX *libctx = NULL;
  223. ECX_KEY *ecx = NULL;
  224. if (pkey->keymgmt != NULL)
  225. libctx = ossl_provider_libctx(EVP_KEYMGMT_get0_provider(pkey->keymgmt));
  226. ecx = ossl_ecx_key_op(NULL, priv, len, pkey->ameth->pkey_id,
  227. KEY_OP_PRIVATE, libctx, NULL);
  228. if (ecx != NULL) {
  229. EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, ecx);
  230. return 1;
  231. }
  232. return 0;
  233. }
  234. static int ecx_set_pub_key(EVP_PKEY *pkey, const unsigned char *pub, size_t len)
  235. {
  236. OSSL_LIB_CTX *libctx = NULL;
  237. ECX_KEY *ecx = NULL;
  238. if (pkey->keymgmt != NULL)
  239. libctx = ossl_provider_libctx(EVP_KEYMGMT_get0_provider(pkey->keymgmt));
  240. ecx = ossl_ecx_key_op(NULL, pub, len, pkey->ameth->pkey_id,
  241. KEY_OP_PUBLIC, libctx, NULL);
  242. if (ecx != NULL) {
  243. EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, ecx);
  244. return 1;
  245. }
  246. return 0;
  247. }
  248. static int ecx_get_priv_key(const EVP_PKEY *pkey, unsigned char *priv,
  249. size_t *len)
  250. {
  251. const ECX_KEY *key = pkey->pkey.ecx;
  252. if (priv == NULL) {
  253. *len = KEYLENID(pkey->ameth->pkey_id);
  254. return 1;
  255. }
  256. if (key == NULL
  257. || key->privkey == NULL
  258. || *len < (size_t)KEYLENID(pkey->ameth->pkey_id))
  259. return 0;
  260. *len = KEYLENID(pkey->ameth->pkey_id);
  261. memcpy(priv, key->privkey, *len);
  262. return 1;
  263. }
  264. static int ecx_get_pub_key(const EVP_PKEY *pkey, unsigned char *pub,
  265. size_t *len)
  266. {
  267. const ECX_KEY *key = pkey->pkey.ecx;
  268. if (pub == NULL) {
  269. *len = KEYLENID(pkey->ameth->pkey_id);
  270. return 1;
  271. }
  272. if (key == NULL
  273. || *len < (size_t)KEYLENID(pkey->ameth->pkey_id))
  274. return 0;
  275. *len = KEYLENID(pkey->ameth->pkey_id);
  276. memcpy(pub, key->pubkey, *len);
  277. return 1;
  278. }
  279. static size_t ecx_pkey_dirty_cnt(const EVP_PKEY *pkey)
  280. {
  281. /*
  282. * We provide no mechanism to "update" an ECX key once it has been set,
  283. * therefore we do not have to maintain a dirty count.
  284. */
  285. return 1;
  286. }
  287. static int ecx_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
  288. OSSL_FUNC_keymgmt_import_fn *importer,
  289. OSSL_LIB_CTX *libctx, const char *propq)
  290. {
  291. const ECX_KEY *key = from->pkey.ecx;
  292. OSSL_PARAM_BLD *tmpl = OSSL_PARAM_BLD_new();
  293. OSSL_PARAM *params = NULL;
  294. int selection = 0;
  295. int rv = 0;
  296. if (tmpl == NULL)
  297. return 0;
  298. /* A key must at least have a public part */
  299. if (!OSSL_PARAM_BLD_push_octet_string(tmpl, OSSL_PKEY_PARAM_PUB_KEY,
  300. key->pubkey, key->keylen))
  301. goto err;
  302. selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
  303. if (key->privkey != NULL) {
  304. if (!OSSL_PARAM_BLD_push_octet_string(tmpl,
  305. OSSL_PKEY_PARAM_PRIV_KEY,
  306. key->privkey, key->keylen))
  307. goto err;
  308. selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
  309. }
  310. params = OSSL_PARAM_BLD_to_param(tmpl);
  311. /* We export, the provider imports */
  312. rv = importer(to_keydata, selection, params);
  313. err:
  314. OSSL_PARAM_BLD_free(tmpl);
  315. OSSL_PARAM_free(params);
  316. return rv;
  317. }
  318. static int ecx_generic_import_from(const OSSL_PARAM params[], void *vpctx,
  319. int keytype)
  320. {
  321. EVP_PKEY_CTX *pctx = vpctx;
  322. EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
  323. ECX_KEY *ecx = ossl_ecx_key_new(pctx->libctx, KEYNID2TYPE(keytype), 0,
  324. pctx->propquery);
  325. if (ecx == NULL) {
  326. ERR_raise(ERR_LIB_DH, ERR_R_EC_LIB);
  327. return 0;
  328. }
  329. if (!ossl_ecx_key_fromdata(ecx, params, 1)
  330. || !EVP_PKEY_assign(pkey, keytype, ecx)) {
  331. ossl_ecx_key_free(ecx);
  332. return 0;
  333. }
  334. return 1;
  335. }
  336. static int ecx_pkey_copy(EVP_PKEY *to, EVP_PKEY *from)
  337. {
  338. ECX_KEY *ecx = from->pkey.ecx, *dupkey = NULL;
  339. int ret;
  340. if (ecx != NULL) {
  341. dupkey = ossl_ecx_key_dup(ecx, OSSL_KEYMGMT_SELECT_ALL);
  342. if (dupkey == NULL)
  343. return 0;
  344. }
  345. ret = EVP_PKEY_assign(to, from->type, dupkey);
  346. if (!ret)
  347. ossl_ecx_key_free(dupkey);
  348. return ret;
  349. }
  350. static int x25519_import_from(const OSSL_PARAM params[], void *vpctx)
  351. {
  352. return ecx_generic_import_from(params, vpctx, EVP_PKEY_X25519);
  353. }
  354. const EVP_PKEY_ASN1_METHOD ossl_ecx25519_asn1_meth = {
  355. EVP_PKEY_X25519,
  356. EVP_PKEY_X25519,
  357. 0,
  358. "X25519",
  359. "OpenSSL X25519 algorithm",
  360. ecx_pub_decode,
  361. ecx_pub_encode,
  362. ecx_pub_cmp,
  363. ecx_pub_print,
  364. NULL,
  365. ecx_priv_encode,
  366. ecx_priv_print,
  367. ecx_size,
  368. ecx_bits,
  369. ecx_security_bits,
  370. 0, 0, 0, 0,
  371. ecx_cmp_parameters,
  372. 0, 0,
  373. ecx_free,
  374. ecx_ctrl,
  375. NULL,
  376. NULL,
  377. NULL,
  378. NULL,
  379. NULL,
  380. NULL,
  381. NULL,
  382. NULL,
  383. ecx_set_priv_key,
  384. ecx_set_pub_key,
  385. ecx_get_priv_key,
  386. ecx_get_pub_key,
  387. ecx_pkey_dirty_cnt,
  388. ecx_pkey_export_to,
  389. x25519_import_from,
  390. ecx_pkey_copy,
  391. ecx_priv_decode_ex
  392. };
  393. static int x448_import_from(const OSSL_PARAM params[], void *vpctx)
  394. {
  395. return ecx_generic_import_from(params, vpctx, EVP_PKEY_X448);
  396. }
  397. const EVP_PKEY_ASN1_METHOD ossl_ecx448_asn1_meth = {
  398. EVP_PKEY_X448,
  399. EVP_PKEY_X448,
  400. 0,
  401. "X448",
  402. "OpenSSL X448 algorithm",
  403. ecx_pub_decode,
  404. ecx_pub_encode,
  405. ecx_pub_cmp,
  406. ecx_pub_print,
  407. NULL,
  408. ecx_priv_encode,
  409. ecx_priv_print,
  410. ecx_size,
  411. ecx_bits,
  412. ecx_security_bits,
  413. 0, 0, 0, 0,
  414. ecx_cmp_parameters,
  415. 0, 0,
  416. ecx_free,
  417. ecx_ctrl,
  418. NULL,
  419. NULL,
  420. NULL,
  421. NULL,
  422. NULL,
  423. NULL,
  424. NULL,
  425. NULL,
  426. ecx_set_priv_key,
  427. ecx_set_pub_key,
  428. ecx_get_priv_key,
  429. ecx_get_pub_key,
  430. ecx_pkey_dirty_cnt,
  431. ecx_pkey_export_to,
  432. x448_import_from,
  433. ecx_pkey_copy,
  434. ecx_priv_decode_ex
  435. };
  436. static int ecd_size25519(const EVP_PKEY *pkey)
  437. {
  438. return ED25519_SIGSIZE;
  439. }
  440. static int ecd_size448(const EVP_PKEY *pkey)
  441. {
  442. return ED448_SIGSIZE;
  443. }
  444. static int ecd_item_verify(EVP_MD_CTX *ctx, const ASN1_ITEM *it,
  445. const void *asn, const X509_ALGOR *sigalg,
  446. const ASN1_BIT_STRING *str, EVP_PKEY *pkey)
  447. {
  448. const ASN1_OBJECT *obj;
  449. int ptype;
  450. int nid;
  451. /* Sanity check: make sure it is ED25519/ED448 with absent parameters */
  452. X509_ALGOR_get0(&obj, &ptype, NULL, sigalg);
  453. nid = OBJ_obj2nid(obj);
  454. if ((nid != NID_ED25519 && nid != NID_ED448) || ptype != V_ASN1_UNDEF) {
  455. ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
  456. return 0;
  457. }
  458. if (!EVP_DigestVerifyInit(ctx, NULL, NULL, NULL, pkey))
  459. return 0;
  460. return 2;
  461. }
  462. static int ecd_item_sign(X509_ALGOR *alg1, X509_ALGOR *alg2, int nid)
  463. {
  464. /* Note that X509_ALGOR_set0(..., ..., V_ASN1_UNDEF, ...) cannot fail */
  465. /* Set algorithms identifiers */
  466. (void)X509_ALGOR_set0(alg1, OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL);
  467. if (alg2 != NULL)
  468. (void)X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL);
  469. /* Algorithm identifiers set: carry on as normal */
  470. return 3;
  471. }
  472. static int ecd_item_sign25519(EVP_MD_CTX *ctx, const ASN1_ITEM *it,
  473. const void *asn,
  474. X509_ALGOR *alg1, X509_ALGOR *alg2,
  475. ASN1_BIT_STRING *str)
  476. {
  477. return ecd_item_sign(alg1, alg2, NID_ED25519);
  478. }
  479. static int ecd_sig_info_set25519(X509_SIG_INFO *siginf, const X509_ALGOR *alg,
  480. const ASN1_STRING *sig)
  481. {
  482. X509_SIG_INFO_set(siginf, NID_undef, NID_ED25519, X25519_SECURITY_BITS,
  483. X509_SIG_INFO_TLS);
  484. return 1;
  485. }
  486. static int ecd_item_sign448(EVP_MD_CTX *ctx, const ASN1_ITEM *it,
  487. const void *asn,
  488. X509_ALGOR *alg1, X509_ALGOR *alg2,
  489. ASN1_BIT_STRING *str)
  490. {
  491. return ecd_item_sign(alg1, alg2, NID_ED448);
  492. }
  493. static int ecd_sig_info_set448(X509_SIG_INFO *siginf, const X509_ALGOR *alg,
  494. const ASN1_STRING *sig)
  495. {
  496. X509_SIG_INFO_set(siginf, NID_undef, NID_ED448, X448_SECURITY_BITS,
  497. X509_SIG_INFO_TLS);
  498. return 1;
  499. }
  500. static int ed25519_import_from(const OSSL_PARAM params[], void *vpctx)
  501. {
  502. return ecx_generic_import_from(params, vpctx, EVP_PKEY_ED25519);
  503. }
  504. const EVP_PKEY_ASN1_METHOD ossl_ed25519_asn1_meth = {
  505. EVP_PKEY_ED25519,
  506. EVP_PKEY_ED25519,
  507. 0,
  508. "ED25519",
  509. "OpenSSL ED25519 algorithm",
  510. ecx_pub_decode,
  511. ecx_pub_encode,
  512. ecx_pub_cmp,
  513. ecx_pub_print,
  514. NULL,
  515. ecx_priv_encode,
  516. ecx_priv_print,
  517. ecd_size25519,
  518. ecx_bits,
  519. ecx_security_bits,
  520. 0, 0, 0, 0,
  521. ecx_cmp_parameters,
  522. 0, 0,
  523. ecx_free,
  524. ecd_ctrl,
  525. NULL,
  526. NULL,
  527. ecd_item_verify,
  528. ecd_item_sign25519,
  529. ecd_sig_info_set25519,
  530. NULL,
  531. NULL,
  532. NULL,
  533. ecx_set_priv_key,
  534. ecx_set_pub_key,
  535. ecx_get_priv_key,
  536. ecx_get_pub_key,
  537. ecx_pkey_dirty_cnt,
  538. ecx_pkey_export_to,
  539. ed25519_import_from,
  540. ecx_pkey_copy,
  541. ecx_priv_decode_ex
  542. };
  543. static int ed448_import_from(const OSSL_PARAM params[], void *vpctx)
  544. {
  545. return ecx_generic_import_from(params, vpctx, EVP_PKEY_ED448);
  546. }
  547. const EVP_PKEY_ASN1_METHOD ossl_ed448_asn1_meth = {
  548. EVP_PKEY_ED448,
  549. EVP_PKEY_ED448,
  550. 0,
  551. "ED448",
  552. "OpenSSL ED448 algorithm",
  553. ecx_pub_decode,
  554. ecx_pub_encode,
  555. ecx_pub_cmp,
  556. ecx_pub_print,
  557. NULL,
  558. ecx_priv_encode,
  559. ecx_priv_print,
  560. ecd_size448,
  561. ecx_bits,
  562. ecx_security_bits,
  563. 0, 0, 0, 0,
  564. ecx_cmp_parameters,
  565. 0, 0,
  566. ecx_free,
  567. ecd_ctrl,
  568. NULL,
  569. NULL,
  570. ecd_item_verify,
  571. ecd_item_sign448,
  572. ecd_sig_info_set448,
  573. NULL,
  574. NULL,
  575. NULL,
  576. ecx_set_priv_key,
  577. ecx_set_pub_key,
  578. ecx_get_priv_key,
  579. ecx_get_pub_key,
  580. ecx_pkey_dirty_cnt,
  581. ecx_pkey_export_to,
  582. ed448_import_from,
  583. ecx_pkey_copy,
  584. ecx_priv_decode_ex
  585. };
  586. static int pkey_ecx_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  587. {
  588. ECX_KEY *ecx = ossl_ecx_key_op(NULL, NULL, 0, ctx->pmeth->pkey_id,
  589. KEY_OP_PUBLIC, NULL, NULL);
  590. if (ecx != NULL) {
  591. EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, ecx);
  592. return 1;
  593. }
  594. return 0;
  595. }
  596. static int validate_ecx_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
  597. size_t *keylen,
  598. const unsigned char **privkey,
  599. const unsigned char **pubkey)
  600. {
  601. const ECX_KEY *ecxkey, *peerkey;
  602. if (ctx->pkey == NULL || ctx->peerkey == NULL) {
  603. ERR_raise(ERR_LIB_EC, EC_R_KEYS_NOT_SET);
  604. return 0;
  605. }
  606. ecxkey = evp_pkey_get_legacy(ctx->pkey);
  607. peerkey = evp_pkey_get_legacy(ctx->peerkey);
  608. if (ecxkey == NULL || ecxkey->privkey == NULL) {
  609. ERR_raise(ERR_LIB_EC, EC_R_INVALID_PRIVATE_KEY);
  610. return 0;
  611. }
  612. if (peerkey == NULL) {
  613. ERR_raise(ERR_LIB_EC, EC_R_INVALID_PEER_KEY);
  614. return 0;
  615. }
  616. *privkey = ecxkey->privkey;
  617. *pubkey = peerkey->pubkey;
  618. return 1;
  619. }
  620. static int pkey_ecx_derive25519(EVP_PKEY_CTX *ctx, unsigned char *key,
  621. size_t *keylen)
  622. {
  623. const unsigned char *privkey, *pubkey;
  624. if (!validate_ecx_derive(ctx, key, keylen, &privkey, &pubkey)
  625. || (key != NULL
  626. && ossl_x25519(key, privkey, pubkey) == 0))
  627. return 0;
  628. *keylen = X25519_KEYLEN;
  629. return 1;
  630. }
  631. static int pkey_ecx_derive448(EVP_PKEY_CTX *ctx, unsigned char *key,
  632. size_t *keylen)
  633. {
  634. const unsigned char *privkey, *pubkey;
  635. if (!validate_ecx_derive(ctx, key, keylen, &privkey, &pubkey)
  636. || (key != NULL
  637. && ossl_x448(key, privkey, pubkey) == 0))
  638. return 0;
  639. *keylen = X448_KEYLEN;
  640. return 1;
  641. }
  642. static int pkey_ecx_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  643. {
  644. /* Only need to handle peer key for derivation */
  645. if (type == EVP_PKEY_CTRL_PEER_KEY)
  646. return 1;
  647. return -2;
  648. }
  649. static const EVP_PKEY_METHOD ecx25519_pkey_meth = {
  650. EVP_PKEY_X25519,
  651. 0, 0, 0, 0, 0, 0, 0,
  652. pkey_ecx_keygen,
  653. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  654. pkey_ecx_derive25519,
  655. pkey_ecx_ctrl,
  656. 0
  657. };
  658. static const EVP_PKEY_METHOD ecx448_pkey_meth = {
  659. EVP_PKEY_X448,
  660. 0, 0, 0, 0, 0, 0, 0,
  661. pkey_ecx_keygen,
  662. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  663. pkey_ecx_derive448,
  664. pkey_ecx_ctrl,
  665. 0
  666. };
  667. static int pkey_ecd_digestsign25519(EVP_MD_CTX *ctx, unsigned char *sig,
  668. size_t *siglen, const unsigned char *tbs,
  669. size_t tbslen)
  670. {
  671. const ECX_KEY *edkey = evp_pkey_get_legacy(EVP_MD_CTX_get_pkey_ctx(ctx)->pkey);
  672. if (edkey == NULL) {
  673. ERR_raise(ERR_LIB_EC, EC_R_INVALID_KEY);
  674. return 0;
  675. }
  676. if (sig == NULL) {
  677. *siglen = ED25519_SIGSIZE;
  678. return 1;
  679. }
  680. if (*siglen < ED25519_SIGSIZE) {
  681. ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
  682. return 0;
  683. }
  684. if (ossl_ed25519_sign(sig, tbs, tbslen, edkey->pubkey, edkey->privkey, NULL,
  685. NULL) == 0)
  686. return 0;
  687. *siglen = ED25519_SIGSIZE;
  688. return 1;
  689. }
  690. static int pkey_ecd_digestsign448(EVP_MD_CTX *ctx, unsigned char *sig,
  691. size_t *siglen, const unsigned char *tbs,
  692. size_t tbslen)
  693. {
  694. const ECX_KEY *edkey = evp_pkey_get_legacy(EVP_MD_CTX_get_pkey_ctx(ctx)->pkey);
  695. if (edkey == NULL) {
  696. ERR_raise(ERR_LIB_EC, EC_R_INVALID_KEY);
  697. return 0;
  698. }
  699. if (sig == NULL) {
  700. *siglen = ED448_SIGSIZE;
  701. return 1;
  702. }
  703. if (*siglen < ED448_SIGSIZE) {
  704. ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
  705. return 0;
  706. }
  707. if (ossl_ed448_sign(edkey->libctx, sig, tbs, tbslen, edkey->pubkey,
  708. edkey->privkey, NULL, 0, edkey->propq) == 0)
  709. return 0;
  710. *siglen = ED448_SIGSIZE;
  711. return 1;
  712. }
  713. static int pkey_ecd_digestverify25519(EVP_MD_CTX *ctx, const unsigned char *sig,
  714. size_t siglen, const unsigned char *tbs,
  715. size_t tbslen)
  716. {
  717. const ECX_KEY *edkey = evp_pkey_get_legacy(EVP_MD_CTX_get_pkey_ctx(ctx)->pkey);
  718. if (edkey == NULL) {
  719. ERR_raise(ERR_LIB_EC, EC_R_INVALID_KEY);
  720. return 0;
  721. }
  722. if (siglen != ED25519_SIGSIZE)
  723. return 0;
  724. return ossl_ed25519_verify(tbs, tbslen, sig, edkey->pubkey,
  725. edkey->libctx, edkey->propq);
  726. }
  727. static int pkey_ecd_digestverify448(EVP_MD_CTX *ctx, const unsigned char *sig,
  728. size_t siglen, const unsigned char *tbs,
  729. size_t tbslen)
  730. {
  731. const ECX_KEY *edkey = evp_pkey_get_legacy(EVP_MD_CTX_get_pkey_ctx(ctx)->pkey);
  732. if (edkey == NULL) {
  733. ERR_raise(ERR_LIB_EC, EC_R_INVALID_KEY);
  734. return 0;
  735. }
  736. if (siglen != ED448_SIGSIZE)
  737. return 0;
  738. return ossl_ed448_verify(edkey->libctx, tbs, tbslen, sig, edkey->pubkey,
  739. NULL, 0, edkey->propq);
  740. }
  741. static int pkey_ecd_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  742. {
  743. switch (type) {
  744. case EVP_PKEY_CTRL_MD:
  745. /* Only NULL allowed as digest */
  746. if (p2 == NULL || (const EVP_MD *)p2 == EVP_md_null())
  747. return 1;
  748. ERR_raise(ERR_LIB_EC, EC_R_INVALID_DIGEST_TYPE);
  749. return 0;
  750. case EVP_PKEY_CTRL_DIGESTINIT:
  751. return 1;
  752. }
  753. return -2;
  754. }
  755. static const EVP_PKEY_METHOD ed25519_pkey_meth = {
  756. EVP_PKEY_ED25519, EVP_PKEY_FLAG_SIGCTX_CUSTOM,
  757. 0, 0, 0, 0, 0, 0,
  758. pkey_ecx_keygen,
  759. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  760. pkey_ecd_ctrl,
  761. 0,
  762. pkey_ecd_digestsign25519,
  763. pkey_ecd_digestverify25519
  764. };
  765. static const EVP_PKEY_METHOD ed448_pkey_meth = {
  766. EVP_PKEY_ED448, EVP_PKEY_FLAG_SIGCTX_CUSTOM,
  767. 0, 0, 0, 0, 0, 0,
  768. pkey_ecx_keygen,
  769. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  770. pkey_ecd_ctrl,
  771. 0,
  772. pkey_ecd_digestsign448,
  773. pkey_ecd_digestverify448
  774. };
  775. #ifdef S390X_EC_ASM
  776. # include "s390x_arch.h"
  777. static int s390x_pkey_ecx_keygen25519(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  778. {
  779. static const unsigned char generator[] = {
  780. 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  781. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  782. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  783. };
  784. ECX_KEY *key = ossl_ecx_key_new(ctx->libctx, ECX_KEY_TYPE_X25519, 1,
  785. ctx->propquery);
  786. unsigned char *privkey = NULL, *pubkey;
  787. if (key == NULL) {
  788. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  789. goto err;
  790. }
  791. pubkey = key->pubkey;
  792. privkey = ossl_ecx_key_allocate_privkey(key);
  793. if (privkey == NULL) {
  794. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  795. goto err;
  796. }
  797. if (RAND_priv_bytes_ex(ctx->libctx, privkey, X25519_KEYLEN, 0) <= 0)
  798. goto err;
  799. privkey[0] &= 248;
  800. privkey[31] &= 127;
  801. privkey[31] |= 64;
  802. if (s390x_x25519_mul(pubkey, generator, privkey) != 1)
  803. goto err;
  804. EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, key);
  805. return 1;
  806. err:
  807. ossl_ecx_key_free(key);
  808. return 0;
  809. }
  810. static int s390x_pkey_ecx_keygen448(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  811. {
  812. static const unsigned char generator[] = {
  813. 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  814. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  815. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  816. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  817. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  818. };
  819. ECX_KEY *key = ossl_ecx_key_new(ctx->libctx, ECX_KEY_TYPE_X448, 1,
  820. ctx->propquery);
  821. unsigned char *privkey = NULL, *pubkey;
  822. if (key == NULL) {
  823. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  824. goto err;
  825. }
  826. pubkey = key->pubkey;
  827. privkey = ossl_ecx_key_allocate_privkey(key);
  828. if (privkey == NULL) {
  829. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  830. goto err;
  831. }
  832. if (RAND_priv_bytes_ex(ctx->libctx, privkey, X448_KEYLEN, 0) <= 0)
  833. goto err;
  834. privkey[0] &= 252;
  835. privkey[55] |= 128;
  836. if (s390x_x448_mul(pubkey, generator, privkey) != 1)
  837. goto err;
  838. EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, key);
  839. return 1;
  840. err:
  841. ossl_ecx_key_free(key);
  842. return 0;
  843. }
  844. static int s390x_pkey_ecd_keygen25519(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  845. {
  846. static const unsigned char generator_x[] = {
  847. 0x1a, 0xd5, 0x25, 0x8f, 0x60, 0x2d, 0x56, 0xc9, 0xb2, 0xa7, 0x25, 0x95,
  848. 0x60, 0xc7, 0x2c, 0x69, 0x5c, 0xdc, 0xd6, 0xfd, 0x31, 0xe2, 0xa4, 0xc0,
  849. 0xfe, 0x53, 0x6e, 0xcd, 0xd3, 0x36, 0x69, 0x21
  850. };
  851. static const unsigned char generator_y[] = {
  852. 0x58, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  853. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  854. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  855. };
  856. unsigned char x_dst[32], buff[SHA512_DIGEST_LENGTH];
  857. ECX_KEY *key = ossl_ecx_key_new(ctx->libctx, ECX_KEY_TYPE_ED25519, 1,
  858. ctx->propquery);
  859. unsigned char *privkey = NULL, *pubkey;
  860. unsigned int sz;
  861. EVP_MD *md = NULL;
  862. int rv;
  863. if (key == NULL) {
  864. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  865. goto err;
  866. }
  867. pubkey = key->pubkey;
  868. privkey = ossl_ecx_key_allocate_privkey(key);
  869. if (privkey == NULL) {
  870. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  871. goto err;
  872. }
  873. if (RAND_priv_bytes_ex(ctx->libctx, privkey, ED25519_KEYLEN, 0) <= 0)
  874. goto err;
  875. md = EVP_MD_fetch(ctx->libctx, "SHA512", ctx->propquery);
  876. if (md == NULL)
  877. goto err;
  878. rv = EVP_Digest(privkey, 32, buff, &sz, md, NULL);
  879. EVP_MD_free(md);
  880. if (!rv)
  881. goto err;
  882. buff[0] &= 248;
  883. buff[31] &= 63;
  884. buff[31] |= 64;
  885. if (s390x_ed25519_mul(x_dst, pubkey,
  886. generator_x, generator_y, buff) != 1)
  887. goto err;
  888. pubkey[31] |= ((x_dst[0] & 0x01) << 7);
  889. EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, key);
  890. return 1;
  891. err:
  892. ossl_ecx_key_free(key);
  893. return 0;
  894. }
  895. static int s390x_pkey_ecd_keygen448(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  896. {
  897. static const unsigned char generator_x[] = {
  898. 0x5e, 0xc0, 0x0c, 0xc7, 0x2b, 0xa8, 0x26, 0x26, 0x8e, 0x93, 0x00, 0x8b,
  899. 0xe1, 0x80, 0x3b, 0x43, 0x11, 0x65, 0xb6, 0x2a, 0xf7, 0x1a, 0xae, 0x12,
  900. 0x64, 0xa4, 0xd3, 0xa3, 0x24, 0xe3, 0x6d, 0xea, 0x67, 0x17, 0x0f, 0x47,
  901. 0x70, 0x65, 0x14, 0x9e, 0xda, 0x36, 0xbf, 0x22, 0xa6, 0x15, 0x1d, 0x22,
  902. 0xed, 0x0d, 0xed, 0x6b, 0xc6, 0x70, 0x19, 0x4f, 0x00
  903. };
  904. static const unsigned char generator_y[] = {
  905. 0x14, 0xfa, 0x30, 0xf2, 0x5b, 0x79, 0x08, 0x98, 0xad, 0xc8, 0xd7, 0x4e,
  906. 0x2c, 0x13, 0xbd, 0xfd, 0xc4, 0x39, 0x7c, 0xe6, 0x1c, 0xff, 0xd3, 0x3a,
  907. 0xd7, 0xc2, 0xa0, 0x05, 0x1e, 0x9c, 0x78, 0x87, 0x40, 0x98, 0xa3, 0x6c,
  908. 0x73, 0x73, 0xea, 0x4b, 0x62, 0xc7, 0xc9, 0x56, 0x37, 0x20, 0x76, 0x88,
  909. 0x24, 0xbc, 0xb6, 0x6e, 0x71, 0x46, 0x3f, 0x69, 0x00
  910. };
  911. unsigned char x_dst[57], buff[114];
  912. ECX_KEY *key = ossl_ecx_key_new(ctx->libctx, ECX_KEY_TYPE_ED448, 1,
  913. ctx->propquery);
  914. unsigned char *privkey = NULL, *pubkey;
  915. EVP_MD_CTX *hashctx = NULL;
  916. EVP_MD *md = NULL;
  917. int rv;
  918. if (key == NULL) {
  919. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  920. goto err;
  921. }
  922. pubkey = key->pubkey;
  923. privkey = ossl_ecx_key_allocate_privkey(key);
  924. if (privkey == NULL) {
  925. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  926. goto err;
  927. }
  928. if (RAND_priv_bytes_ex(ctx->libctx, privkey, ED448_KEYLEN, 0) <= 0)
  929. goto err;
  930. hashctx = EVP_MD_CTX_new();
  931. if (hashctx == NULL)
  932. goto err;
  933. md = EVP_MD_fetch(ctx->libctx, "SHAKE256", ctx->propquery);
  934. if (md == NULL)
  935. goto err;
  936. rv = EVP_DigestInit_ex(hashctx, md, NULL);
  937. EVP_MD_free(md);
  938. if (rv != 1)
  939. goto err;
  940. if (EVP_DigestUpdate(hashctx, privkey, 57) != 1)
  941. goto err;
  942. if (EVP_DigestFinalXOF(hashctx, buff, sizeof(buff)) != 1)
  943. goto err;
  944. buff[0] &= -4;
  945. buff[55] |= 0x80;
  946. buff[56] = 0;
  947. if (s390x_ed448_mul(x_dst, pubkey,
  948. generator_x, generator_y, buff) != 1)
  949. goto err;
  950. pubkey[56] |= ((x_dst[0] & 0x01) << 7);
  951. EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, key);
  952. EVP_MD_CTX_free(hashctx);
  953. return 1;
  954. err:
  955. ossl_ecx_key_free(key);
  956. EVP_MD_CTX_free(hashctx);
  957. return 0;
  958. }
  959. static int s390x_pkey_ecx_derive25519(EVP_PKEY_CTX *ctx, unsigned char *key,
  960. size_t *keylen)
  961. {
  962. const unsigned char *privkey, *pubkey;
  963. if (!validate_ecx_derive(ctx, key, keylen, &privkey, &pubkey)
  964. || (key != NULL
  965. && s390x_x25519_mul(key, privkey, pubkey) == 0))
  966. return 0;
  967. *keylen = X25519_KEYLEN;
  968. return 1;
  969. }
  970. static int s390x_pkey_ecx_derive448(EVP_PKEY_CTX *ctx, unsigned char *key,
  971. size_t *keylen)
  972. {
  973. const unsigned char *privkey, *pubkey;
  974. if (!validate_ecx_derive(ctx, key, keylen, &privkey, &pubkey)
  975. || (key != NULL
  976. && s390x_x448_mul(key, pubkey, privkey) == 0))
  977. return 0;
  978. *keylen = X448_KEYLEN;
  979. return 1;
  980. }
  981. static int s390x_pkey_ecd_digestsign25519(EVP_MD_CTX *ctx,
  982. unsigned char *sig, size_t *siglen,
  983. const unsigned char *tbs,
  984. size_t tbslen)
  985. {
  986. union {
  987. struct {
  988. unsigned char sig[64];
  989. unsigned char priv[32];
  990. } ed25519;
  991. unsigned long long buff[512];
  992. } param;
  993. const ECX_KEY *edkey = evp_pkey_get_legacy(EVP_MD_CTX_get_pkey_ctx(ctx)->pkey);
  994. int rc;
  995. if (edkey == NULL) {
  996. ERR_raise(ERR_LIB_EC, EC_R_INVALID_KEY);
  997. return 0;
  998. }
  999. if (sig == NULL) {
  1000. *siglen = ED25519_SIGSIZE;
  1001. return 1;
  1002. }
  1003. if (*siglen < ED25519_SIGSIZE) {
  1004. ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
  1005. return 0;
  1006. }
  1007. memset(&param, 0, sizeof(param));
  1008. memcpy(param.ed25519.priv, edkey->privkey, sizeof(param.ed25519.priv));
  1009. rc = s390x_kdsa(S390X_EDDSA_SIGN_ED25519, &param.ed25519, tbs, tbslen);
  1010. OPENSSL_cleanse(param.ed25519.priv, sizeof(param.ed25519.priv));
  1011. if (rc != 0)
  1012. return 0;
  1013. s390x_flip_endian32(sig, param.ed25519.sig);
  1014. s390x_flip_endian32(sig + 32, param.ed25519.sig + 32);
  1015. *siglen = ED25519_SIGSIZE;
  1016. return 1;
  1017. }
  1018. static int s390x_pkey_ecd_digestsign448(EVP_MD_CTX *ctx,
  1019. unsigned char *sig, size_t *siglen,
  1020. const unsigned char *tbs,
  1021. size_t tbslen)
  1022. {
  1023. union {
  1024. struct {
  1025. unsigned char sig[128];
  1026. unsigned char priv[64];
  1027. } ed448;
  1028. unsigned long long buff[512];
  1029. } param;
  1030. const ECX_KEY *edkey = evp_pkey_get_legacy(EVP_MD_CTX_get_pkey_ctx(ctx)->pkey);
  1031. int rc;
  1032. if (edkey == NULL) {
  1033. ERR_raise(ERR_LIB_EC, EC_R_INVALID_KEY);
  1034. return 0;
  1035. }
  1036. if (sig == NULL) {
  1037. *siglen = ED448_SIGSIZE;
  1038. return 1;
  1039. }
  1040. if (*siglen < ED448_SIGSIZE) {
  1041. ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
  1042. return 0;
  1043. }
  1044. memset(&param, 0, sizeof(param));
  1045. memcpy(param.ed448.priv + 64 - 57, edkey->privkey, 57);
  1046. rc = s390x_kdsa(S390X_EDDSA_SIGN_ED448, &param.ed448, tbs, tbslen);
  1047. OPENSSL_cleanse(param.ed448.priv, sizeof(param.ed448.priv));
  1048. if (rc != 0)
  1049. return 0;
  1050. s390x_flip_endian64(param.ed448.sig, param.ed448.sig);
  1051. s390x_flip_endian64(param.ed448.sig + 64, param.ed448.sig + 64);
  1052. memcpy(sig, param.ed448.sig, 57);
  1053. memcpy(sig + 57, param.ed448.sig + 64, 57);
  1054. *siglen = ED448_SIGSIZE;
  1055. return 1;
  1056. }
  1057. static int s390x_pkey_ecd_digestverify25519(EVP_MD_CTX *ctx,
  1058. const unsigned char *sig,
  1059. size_t siglen,
  1060. const unsigned char *tbs,
  1061. size_t tbslen)
  1062. {
  1063. union {
  1064. struct {
  1065. unsigned char sig[64];
  1066. unsigned char pub[32];
  1067. } ed25519;
  1068. unsigned long long buff[512];
  1069. } param;
  1070. const ECX_KEY *edkey = evp_pkey_get_legacy(EVP_MD_CTX_get_pkey_ctx(ctx)->pkey);
  1071. if (edkey == NULL) {
  1072. ERR_raise(ERR_LIB_EC, EC_R_INVALID_KEY);
  1073. return 0;
  1074. }
  1075. if (siglen != ED25519_SIGSIZE)
  1076. return 0;
  1077. memset(&param, 0, sizeof(param));
  1078. s390x_flip_endian32(param.ed25519.sig, sig);
  1079. s390x_flip_endian32(param.ed25519.sig + 32, sig + 32);
  1080. s390x_flip_endian32(param.ed25519.pub, edkey->pubkey);
  1081. return s390x_kdsa(S390X_EDDSA_VERIFY_ED25519,
  1082. &param.ed25519, tbs, tbslen) == 0 ? 1 : 0;
  1083. }
  1084. static int s390x_pkey_ecd_digestverify448(EVP_MD_CTX *ctx,
  1085. const unsigned char *sig,
  1086. size_t siglen,
  1087. const unsigned char *tbs,
  1088. size_t tbslen)
  1089. {
  1090. union {
  1091. struct {
  1092. unsigned char sig[128];
  1093. unsigned char pub[64];
  1094. } ed448;
  1095. unsigned long long buff[512];
  1096. } param;
  1097. const ECX_KEY *edkey = evp_pkey_get_legacy(EVP_MD_CTX_get_pkey_ctx(ctx)->pkey);
  1098. if (edkey == NULL) {
  1099. ERR_raise(ERR_LIB_EC, EC_R_INVALID_KEY);
  1100. return 0;
  1101. }
  1102. if (siglen != ED448_SIGSIZE)
  1103. return 0;
  1104. memset(&param, 0, sizeof(param));
  1105. memcpy(param.ed448.sig, sig, 57);
  1106. s390x_flip_endian64(param.ed448.sig, param.ed448.sig);
  1107. memcpy(param.ed448.sig + 64, sig + 57, 57);
  1108. s390x_flip_endian64(param.ed448.sig + 64, param.ed448.sig + 64);
  1109. memcpy(param.ed448.pub, edkey->pubkey, 57);
  1110. s390x_flip_endian64(param.ed448.pub, param.ed448.pub);
  1111. return s390x_kdsa(S390X_EDDSA_VERIFY_ED448,
  1112. &param.ed448, tbs, tbslen) == 0 ? 1 : 0;
  1113. }
  1114. static const EVP_PKEY_METHOD ecx25519_s390x_pkey_meth = {
  1115. EVP_PKEY_X25519,
  1116. 0, 0, 0, 0, 0, 0, 0,
  1117. s390x_pkey_ecx_keygen25519,
  1118. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1119. s390x_pkey_ecx_derive25519,
  1120. pkey_ecx_ctrl,
  1121. 0
  1122. };
  1123. static const EVP_PKEY_METHOD ecx448_s390x_pkey_meth = {
  1124. EVP_PKEY_X448,
  1125. 0, 0, 0, 0, 0, 0, 0,
  1126. s390x_pkey_ecx_keygen448,
  1127. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1128. s390x_pkey_ecx_derive448,
  1129. pkey_ecx_ctrl,
  1130. 0
  1131. };
  1132. static const EVP_PKEY_METHOD ed25519_s390x_pkey_meth = {
  1133. EVP_PKEY_ED25519, EVP_PKEY_FLAG_SIGCTX_CUSTOM,
  1134. 0, 0, 0, 0, 0, 0,
  1135. s390x_pkey_ecd_keygen25519,
  1136. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1137. pkey_ecd_ctrl,
  1138. 0,
  1139. s390x_pkey_ecd_digestsign25519,
  1140. s390x_pkey_ecd_digestverify25519
  1141. };
  1142. static const EVP_PKEY_METHOD ed448_s390x_pkey_meth = {
  1143. EVP_PKEY_ED448, EVP_PKEY_FLAG_SIGCTX_CUSTOM,
  1144. 0, 0, 0, 0, 0, 0,
  1145. s390x_pkey_ecd_keygen448,
  1146. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1147. pkey_ecd_ctrl,
  1148. 0,
  1149. s390x_pkey_ecd_digestsign448,
  1150. s390x_pkey_ecd_digestverify448
  1151. };
  1152. #endif
  1153. const EVP_PKEY_METHOD *ossl_ecx25519_pkey_method(void)
  1154. {
  1155. #ifdef S390X_EC_ASM
  1156. if (OPENSSL_s390xcap_P.pcc[1] & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_X25519))
  1157. return &ecx25519_s390x_pkey_meth;
  1158. #endif
  1159. return &ecx25519_pkey_meth;
  1160. }
  1161. const EVP_PKEY_METHOD *ossl_ecx448_pkey_method(void)
  1162. {
  1163. #ifdef S390X_EC_ASM
  1164. if (OPENSSL_s390xcap_P.pcc[1] & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_X448))
  1165. return &ecx448_s390x_pkey_meth;
  1166. #endif
  1167. return &ecx448_pkey_meth;
  1168. }
  1169. const EVP_PKEY_METHOD *ossl_ed25519_pkey_method(void)
  1170. {
  1171. #ifdef S390X_EC_ASM
  1172. if (OPENSSL_s390xcap_P.pcc[1] & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_ED25519)
  1173. && OPENSSL_s390xcap_P.kdsa[0] & S390X_CAPBIT(S390X_EDDSA_SIGN_ED25519)
  1174. && OPENSSL_s390xcap_P.kdsa[0]
  1175. & S390X_CAPBIT(S390X_EDDSA_VERIFY_ED25519))
  1176. return &ed25519_s390x_pkey_meth;
  1177. #endif
  1178. return &ed25519_pkey_meth;
  1179. }
  1180. const EVP_PKEY_METHOD *ossl_ed448_pkey_method(void)
  1181. {
  1182. #ifdef S390X_EC_ASM
  1183. if (OPENSSL_s390xcap_P.pcc[1] & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_ED448)
  1184. && OPENSSL_s390xcap_P.kdsa[0] & S390X_CAPBIT(S390X_EDDSA_SIGN_ED448)
  1185. && OPENSSL_s390xcap_P.kdsa[0] & S390X_CAPBIT(S390X_EDDSA_VERIFY_ED448))
  1186. return &ed448_s390x_pkey_meth;
  1187. #endif
  1188. return &ed448_pkey_meth;
  1189. }