eddsa_sig.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  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 <openssl/crypto.h>
  10. #include <openssl/core_dispatch.h>
  11. #include <openssl/core_names.h>
  12. #include <openssl/err.h>
  13. #include <openssl/params.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/proverr.h>
  16. #include "internal/nelem.h"
  17. #include "internal/sizes.h"
  18. #include "prov/providercommon.h"
  19. #include "prov/implementations.h"
  20. #include "prov/securitycheck.h"
  21. #include "prov/provider_ctx.h"
  22. #include "prov/der_ecx.h"
  23. #include "crypto/ecx.h"
  24. #ifdef S390X_EC_ASM
  25. # include "s390x_arch.h"
  26. # define S390X_CAN_SIGN(edtype) \
  27. ((OPENSSL_s390xcap_P.pcc[1] & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_##edtype)) \
  28. && (OPENSSL_s390xcap_P.kdsa[0] & S390X_CAPBIT(S390X_EDDSA_SIGN_##edtype)) \
  29. && (OPENSSL_s390xcap_P.kdsa[0] & S390X_CAPBIT(S390X_EDDSA_VERIFY_##edtype)))
  30. static int s390x_ed25519_digestsign(const ECX_KEY *edkey, unsigned char *sig,
  31. const unsigned char *tbs, size_t tbslen);
  32. static int s390x_ed448_digestsign(const ECX_KEY *edkey, unsigned char *sig,
  33. const unsigned char *tbs, size_t tbslen);
  34. static int s390x_ed25519_digestverify(const ECX_KEY *edkey,
  35. const unsigned char *sig,
  36. const unsigned char *tbs, size_t tbslen);
  37. static int s390x_ed448_digestverify(const ECX_KEY *edkey,
  38. const unsigned char *sig,
  39. const unsigned char *tbs, size_t tbslen);
  40. #endif /* S390X_EC_ASM */
  41. enum ID_EdDSA_INSTANCE {
  42. ID_NOT_SET = 0,
  43. ID_Ed25519,
  44. ID_Ed25519ctx,
  45. ID_Ed25519ph,
  46. ID_Ed448,
  47. ID_Ed448ph
  48. };
  49. #define SN_Ed25519 "Ed25519"
  50. #define SN_Ed25519ph "Ed25519ph"
  51. #define SN_Ed25519ctx "Ed25519ctx"
  52. #define SN_Ed448 "Ed448"
  53. #define SN_Ed448ph "Ed448ph"
  54. #define EDDSA_MAX_CONTEXT_STRING_LEN 255
  55. #define EDDSA_PREHASH_OUTPUT_LEN 64
  56. static OSSL_FUNC_signature_newctx_fn eddsa_newctx;
  57. static OSSL_FUNC_signature_sign_message_init_fn ed25519_signverify_message_init;
  58. static OSSL_FUNC_signature_sign_message_init_fn ed25519ph_signverify_message_init;
  59. static OSSL_FUNC_signature_sign_message_init_fn ed25519ctx_signverify_message_init;
  60. static OSSL_FUNC_signature_sign_message_init_fn ed448_signverify_message_init;
  61. static OSSL_FUNC_signature_sign_message_init_fn ed448ph_signverify_message_init;
  62. static OSSL_FUNC_signature_sign_fn ed25519_sign;
  63. static OSSL_FUNC_signature_sign_fn ed448_sign;
  64. static OSSL_FUNC_signature_verify_fn ed25519_verify;
  65. static OSSL_FUNC_signature_verify_fn ed448_verify;
  66. static OSSL_FUNC_signature_digest_sign_init_fn ed25519_digest_signverify_init;
  67. static OSSL_FUNC_signature_digest_sign_init_fn ed448_digest_signverify_init;
  68. static OSSL_FUNC_signature_digest_sign_fn ed25519_digest_sign;
  69. static OSSL_FUNC_signature_digest_sign_fn ed448_digest_sign;
  70. static OSSL_FUNC_signature_digest_verify_fn ed25519_digest_verify;
  71. static OSSL_FUNC_signature_digest_verify_fn ed448_digest_verify;
  72. static OSSL_FUNC_signature_freectx_fn eddsa_freectx;
  73. static OSSL_FUNC_signature_dupctx_fn eddsa_dupctx;
  74. static OSSL_FUNC_signature_query_key_types_fn ed25519_sigalg_query_key_types;
  75. static OSSL_FUNC_signature_query_key_types_fn ed448_sigalg_query_key_types;
  76. static OSSL_FUNC_signature_get_ctx_params_fn eddsa_get_ctx_params;
  77. static OSSL_FUNC_signature_gettable_ctx_params_fn eddsa_gettable_ctx_params;
  78. static OSSL_FUNC_signature_set_ctx_params_fn eddsa_set_ctx_params;
  79. static OSSL_FUNC_signature_settable_ctx_params_fn eddsa_settable_ctx_params;
  80. static OSSL_FUNC_signature_settable_ctx_params_fn eddsa_settable_variant_ctx_params;
  81. /* there are five EdDSA instances:
  82. Ed25519
  83. Ed25519ph
  84. Ed25519ctx
  85. Ed448
  86. Ed448ph
  87. Quoting from RFC 8032, Section 5.1:
  88. For Ed25519, dom2(f,c) is the empty string. The phflag value is
  89. irrelevant. The context (if present at all) MUST be empty. This
  90. causes the scheme to be one and the same with the Ed25519 scheme
  91. published earlier.
  92. For Ed25519ctx, phflag=0. The context input SHOULD NOT be empty.
  93. For Ed25519ph, phflag=1 and PH is SHA512 instead. That is, the input
  94. is hashed using SHA-512 before signing with Ed25519.
  95. Quoting from RFC 8032, Section 5.2:
  96. Ed448ph is the same but with PH being SHAKE256(x, 64) and phflag
  97. being 1, i.e., the input is hashed before signing with Ed448 with a
  98. hash constant modified.
  99. Value of context is set by signer and verifier (maximum of 255
  100. octets; the default is empty string) and has to match octet by octet
  101. for verification to be successful.
  102. Quoting from RFC 8032, Section 2:
  103. dom2(x, y) The blank octet string when signing or verifying
  104. Ed25519. Otherwise, the octet string: "SigEd25519 no
  105. Ed25519 collisions" || octet(x) || octet(OLEN(y)) ||
  106. y, where x is in range 0-255 and y is an octet string
  107. of at most 255 octets. "SigEd25519 no Ed25519
  108. collisions" is in ASCII (32 octets).
  109. dom4(x, y) The octet string "SigEd448" || octet(x) ||
  110. octet(OLEN(y)) || y, where x is in range 0-255 and y
  111. is an octet string of at most 255 octets. "SigEd448"
  112. is in ASCII (8 octets).
  113. Note above that x is the pre-hash flag, and y is the context string.
  114. */
  115. typedef struct {
  116. OSSL_LIB_CTX *libctx;
  117. ECX_KEY *key;
  118. /* The Algorithm Identifier of the signature algorithm */
  119. unsigned char aid_buf[OSSL_MAX_ALGORITHM_ID_SIZE];
  120. size_t aid_len;
  121. /* id indicating the EdDSA instance */
  122. int instance_id;
  123. /* indicates that instance_id and associated flags are preset / hardcoded */
  124. unsigned int instance_id_preset_flag : 1;
  125. /* for ph instances, this indicates whether the caller is expected to prehash */
  126. unsigned int prehash_by_caller_flag : 1;
  127. unsigned int dom2_flag : 1;
  128. unsigned int prehash_flag : 1;
  129. /* indicates that a non-empty context string is required, as in Ed25519ctx */
  130. unsigned int context_string_flag : 1;
  131. unsigned char context_string[EDDSA_MAX_CONTEXT_STRING_LEN];
  132. size_t context_string_len;
  133. } PROV_EDDSA_CTX;
  134. static void *eddsa_newctx(void *provctx, const char *propq_unused)
  135. {
  136. PROV_EDDSA_CTX *peddsactx;
  137. if (!ossl_prov_is_running())
  138. return NULL;
  139. peddsactx = OPENSSL_zalloc(sizeof(PROV_EDDSA_CTX));
  140. if (peddsactx == NULL)
  141. return NULL;
  142. peddsactx->libctx = PROV_LIBCTX_OF(provctx);
  143. return peddsactx;
  144. }
  145. static int eddsa_setup_instance(void *vpeddsactx, int instance_id,
  146. unsigned int instance_id_preset,
  147. unsigned int prehash_by_caller)
  148. {
  149. PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
  150. switch (instance_id) {
  151. case ID_Ed25519:
  152. if (peddsactx->key->type != ECX_KEY_TYPE_ED25519)
  153. return 0;
  154. peddsactx->dom2_flag = 0;
  155. peddsactx->prehash_flag = 0;
  156. peddsactx->context_string_flag = 0;
  157. break;
  158. case ID_Ed25519ctx:
  159. if (peddsactx->key->type != ECX_KEY_TYPE_ED25519)
  160. return 0;
  161. peddsactx->dom2_flag = 1;
  162. peddsactx->prehash_flag = 0;
  163. peddsactx->context_string_flag = 1;
  164. break;
  165. case ID_Ed25519ph:
  166. if (peddsactx->key->type != ECX_KEY_TYPE_ED25519)
  167. return 0;
  168. peddsactx->dom2_flag = 1;
  169. peddsactx->prehash_flag = 1;
  170. peddsactx->context_string_flag = 0;
  171. break;
  172. case ID_Ed448:
  173. if (peddsactx->key->type != ECX_KEY_TYPE_ED448)
  174. return 0;
  175. peddsactx->prehash_flag = 0;
  176. peddsactx->context_string_flag = 0;
  177. break;
  178. case ID_Ed448ph:
  179. if (peddsactx->key->type != ECX_KEY_TYPE_ED448)
  180. return 0;
  181. peddsactx->prehash_flag = 1;
  182. peddsactx->context_string_flag = 0;
  183. break;
  184. default:
  185. /* we did not recognize the instance */
  186. return 0;
  187. }
  188. peddsactx->instance_id = instance_id;
  189. peddsactx->instance_id_preset_flag = instance_id_preset;
  190. peddsactx->prehash_by_caller_flag = prehash_by_caller;
  191. return 1;
  192. }
  193. static int eddsa_signverify_init(void *vpeddsactx, void *vedkey)
  194. {
  195. PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
  196. ECX_KEY *edkey = (ECX_KEY *)vedkey;
  197. WPACKET pkt;
  198. int ret;
  199. unsigned char *aid = NULL;
  200. if (!ossl_prov_is_running())
  201. return 0;
  202. if (edkey == NULL) {
  203. ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
  204. return 0;
  205. }
  206. if (!ossl_ecx_key_up_ref(edkey)) {
  207. ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
  208. return 0;
  209. }
  210. peddsactx->instance_id_preset_flag = 0;
  211. peddsactx->dom2_flag = 0;
  212. peddsactx->prehash_flag = 0;
  213. peddsactx->context_string_flag = 0;
  214. peddsactx->context_string_len = 0;
  215. peddsactx->key = edkey;
  216. /*
  217. * We do not care about DER writing errors.
  218. * All it really means is that for some reason, there's no
  219. * AlgorithmIdentifier to be had, but the operation itself is
  220. * still valid, just as long as it's not used to construct
  221. * anything that needs an AlgorithmIdentifier.
  222. */
  223. peddsactx->aid_len = 0;
  224. ret = WPACKET_init_der(&pkt, peddsactx->aid_buf, sizeof(peddsactx->aid_buf));
  225. switch (edkey->type) {
  226. case ECX_KEY_TYPE_ED25519:
  227. ret = ret && ossl_DER_w_algorithmIdentifier_ED25519(&pkt, -1, edkey);
  228. break;
  229. case ECX_KEY_TYPE_ED448:
  230. ret = ret && ossl_DER_w_algorithmIdentifier_ED448(&pkt, -1, edkey);
  231. break;
  232. default:
  233. /* Should never happen */
  234. ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
  235. ossl_ecx_key_free(edkey);
  236. peddsactx->key = NULL;
  237. WPACKET_cleanup(&pkt);
  238. return 0;
  239. }
  240. if (ret && WPACKET_finish(&pkt)) {
  241. WPACKET_get_total_written(&pkt, &peddsactx->aid_len);
  242. aid = WPACKET_get_curr(&pkt);
  243. }
  244. WPACKET_cleanup(&pkt);
  245. if (aid != NULL && peddsactx->aid_len != 0)
  246. memmove(peddsactx->aid_buf, aid, peddsactx->aid_len);
  247. return 1;
  248. }
  249. static int ed25519_signverify_message_init(void *vpeddsactx, void *vedkey,
  250. const OSSL_PARAM params[])
  251. {
  252. return eddsa_signverify_init(vpeddsactx, vedkey)
  253. && eddsa_setup_instance(vpeddsactx, ID_Ed25519, 1, 0)
  254. && eddsa_set_ctx_params(vpeddsactx, params);
  255. }
  256. static int ed25519ph_signverify_message_init(void *vpeddsactx, void *vedkey,
  257. const OSSL_PARAM params[])
  258. {
  259. return eddsa_signverify_init(vpeddsactx, vedkey)
  260. && eddsa_setup_instance(vpeddsactx, ID_Ed25519ph, 1, 0)
  261. && eddsa_set_ctx_params(vpeddsactx, params);
  262. }
  263. static int ed25519ph_signverify_init(void *vpeddsactx, void *vedkey,
  264. const OSSL_PARAM params[])
  265. {
  266. return eddsa_signverify_init(vpeddsactx, vedkey)
  267. && eddsa_setup_instance(vpeddsactx, ID_Ed25519ph, 1, 1)
  268. && eddsa_set_ctx_params(vpeddsactx, params);
  269. }
  270. /*
  271. * This supports using ED25519 with EVP_PKEY_{sign,verify}_init_ex() and
  272. * EVP_PKEY_{sign,verify}_init_ex2(), under the condition that the caller
  273. * explicitly sets the Ed25519ph instance (this is verified by ed25519_sign()
  274. * and ed25519_verify())
  275. */
  276. static int ed25519_signverify_init(void *vpeddsactx, void *vedkey,
  277. const OSSL_PARAM params[])
  278. {
  279. return eddsa_signverify_init(vpeddsactx, vedkey)
  280. && eddsa_setup_instance(vpeddsactx, ID_Ed25519, 0, 1)
  281. && eddsa_set_ctx_params(vpeddsactx, params);
  282. }
  283. static int ed25519ctx_signverify_message_init(void *vpeddsactx, void *vedkey,
  284. const OSSL_PARAM params[])
  285. {
  286. return eddsa_signverify_init(vpeddsactx, vedkey)
  287. && eddsa_setup_instance(vpeddsactx, ID_Ed25519ctx, 1, 0)
  288. && eddsa_set_ctx_params(vpeddsactx, params);
  289. }
  290. static int ed448_signverify_message_init(void *vpeddsactx, void *vedkey,
  291. const OSSL_PARAM params[])
  292. {
  293. return eddsa_signverify_init(vpeddsactx, vedkey)
  294. && eddsa_setup_instance(vpeddsactx, ID_Ed448, 1, 0)
  295. && eddsa_set_ctx_params(vpeddsactx, params);
  296. }
  297. static int ed448ph_signverify_message_init(void *vpeddsactx, void *vedkey,
  298. const OSSL_PARAM params[])
  299. {
  300. return eddsa_signverify_init(vpeddsactx, vedkey)
  301. && eddsa_setup_instance(vpeddsactx, ID_Ed448ph, 1, 0)
  302. && eddsa_set_ctx_params(vpeddsactx, params);
  303. }
  304. static int ed448ph_signverify_init(void *vpeddsactx, void *vedkey,
  305. const OSSL_PARAM params[])
  306. {
  307. return eddsa_signverify_init(vpeddsactx, vedkey)
  308. && eddsa_setup_instance(vpeddsactx, ID_Ed448ph, 1, 1)
  309. && eddsa_set_ctx_params(vpeddsactx, params);
  310. }
  311. /*
  312. * This supports using ED448 with EVP_PKEY_{sign,verify}_init_ex() and
  313. * EVP_PKEY_{sign,verify}_init_ex2(), under the condition that the caller
  314. * explicitly sets the Ed448ph instance (this is verified by ed448_sign()
  315. * and ed448_verify())
  316. */
  317. static int ed448_signverify_init(void *vpeddsactx, void *vedkey,
  318. const OSSL_PARAM params[])
  319. {
  320. return eddsa_signverify_init(vpeddsactx, vedkey)
  321. && eddsa_setup_instance(vpeddsactx, ID_Ed448, 0, 1)
  322. && eddsa_set_ctx_params(vpeddsactx, params);
  323. }
  324. /*
  325. * This is used directly for OSSL_FUNC_SIGNATURE_SIGN and indirectly
  326. * for OSSL_FUNC_SIGNATURE_DIGEST_SIGN
  327. */
  328. static int ed25519_sign(void *vpeddsactx,
  329. unsigned char *sigret, size_t *siglen, size_t sigsize,
  330. const unsigned char *tbs, size_t tbslen)
  331. {
  332. PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
  333. const ECX_KEY *edkey = peddsactx->key;
  334. uint8_t md[EVP_MAX_MD_SIZE];
  335. size_t mdlen;
  336. if (!ossl_prov_is_running())
  337. return 0;
  338. if (sigret == NULL) {
  339. *siglen = ED25519_SIGSIZE;
  340. return 1;
  341. }
  342. if (sigsize < ED25519_SIGSIZE) {
  343. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  344. return 0;
  345. }
  346. if (edkey->privkey == NULL) {
  347. ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
  348. return 0;
  349. }
  350. #ifdef S390X_EC_ASM
  351. /*
  352. * s390x_ed25519_digestsign() does not yet support dom2 or context-strings.
  353. * fall back to non-accelerated sign if those options are set, or pre-hasing
  354. * is provided.
  355. */
  356. if (S390X_CAN_SIGN(ED25519)
  357. && !peddsactx->dom2_flag
  358. && !peddsactx->context_string_flag
  359. && peddsactx->context_string_len == 0
  360. && !peddsactx->prehash_flag
  361. && !peddsactx->prehash_by_caller_flag) {
  362. if (s390x_ed25519_digestsign(edkey, sigret, tbs, tbslen) == 0) {
  363. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SIGN);
  364. return 0;
  365. }
  366. *siglen = ED25519_SIGSIZE;
  367. return 1;
  368. }
  369. #endif /* S390X_EC_ASM */
  370. if (peddsactx->prehash_flag) {
  371. if (!peddsactx->prehash_by_caller_flag) {
  372. if (!EVP_Q_digest(peddsactx->libctx, SN_sha512, NULL,
  373. tbs, tbslen, md, &mdlen)
  374. || mdlen != EDDSA_PREHASH_OUTPUT_LEN) {
  375. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_PREHASHED_DIGEST_LENGTH);
  376. return 0;
  377. }
  378. tbs = md;
  379. tbslen = mdlen;
  380. } else if (tbslen != EDDSA_PREHASH_OUTPUT_LEN) {
  381. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH);
  382. return 0;
  383. }
  384. } else if (peddsactx->prehash_by_caller_flag) {
  385. /* The caller is supposed to set up a ph instance! */
  386. ERR_raise(ERR_LIB_PROV,
  387. PROV_R_INVALID_EDDSA_INSTANCE_FOR_ATTEMPTED_OPERATION);
  388. return 0;
  389. }
  390. if (ossl_ed25519_sign(sigret, tbs, tbslen, edkey->pubkey, edkey->privkey,
  391. peddsactx->dom2_flag, peddsactx->prehash_flag, peddsactx->context_string_flag,
  392. peddsactx->context_string, peddsactx->context_string_len,
  393. peddsactx->libctx, NULL) == 0) {
  394. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SIGN);
  395. return 0;
  396. }
  397. *siglen = ED25519_SIGSIZE;
  398. return 1;
  399. }
  400. /* EVP_Q_digest() does not allow variable output length for XOFs,
  401. so we use this function */
  402. static int ed448_shake256(OSSL_LIB_CTX *libctx,
  403. const char *propq,
  404. const uint8_t *in, size_t inlen,
  405. uint8_t *out, size_t outlen)
  406. {
  407. int ret = 0;
  408. EVP_MD_CTX *hash_ctx = EVP_MD_CTX_new();
  409. EVP_MD *shake256 = EVP_MD_fetch(libctx, SN_shake256, propq);
  410. if (hash_ctx == NULL || shake256 == NULL)
  411. goto err;
  412. if (!EVP_DigestInit_ex(hash_ctx, shake256, NULL)
  413. || !EVP_DigestUpdate(hash_ctx, in, inlen)
  414. || !EVP_DigestFinalXOF(hash_ctx, out, outlen))
  415. goto err;
  416. ret = 1;
  417. err:
  418. EVP_MD_CTX_free(hash_ctx);
  419. EVP_MD_free(shake256);
  420. return ret;
  421. }
  422. /*
  423. * This is used directly for OSSL_FUNC_SIGNATURE_SIGN and indirectly
  424. * for OSSL_FUNC_SIGNATURE_DIGEST_SIGN
  425. */
  426. static int ed448_sign(void *vpeddsactx,
  427. unsigned char *sigret, size_t *siglen, size_t sigsize,
  428. const unsigned char *tbs, size_t tbslen)
  429. {
  430. PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
  431. const ECX_KEY *edkey = peddsactx->key;
  432. uint8_t md[EDDSA_PREHASH_OUTPUT_LEN];
  433. size_t mdlen = sizeof(md);
  434. if (!ossl_prov_is_running())
  435. return 0;
  436. if (sigret == NULL) {
  437. *siglen = ED448_SIGSIZE;
  438. return 1;
  439. }
  440. if (sigsize < ED448_SIGSIZE) {
  441. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  442. return 0;
  443. }
  444. if (edkey->privkey == NULL) {
  445. ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
  446. return 0;
  447. }
  448. #ifdef S390X_EC_ASM
  449. /*
  450. * s390x_ed448_digestsign() does not yet support context-strings or
  451. * pre-hashing. Fall back to non-accelerated sign if a context-string or
  452. * pre-hasing is provided.
  453. */
  454. if (S390X_CAN_SIGN(ED448)
  455. && peddsactx->context_string_len == 0
  456. && !peddsactx->prehash_flag
  457. && !peddsactx->prehash_by_caller_flag) {
  458. if (s390x_ed448_digestsign(edkey, sigret, tbs, tbslen) == 0) {
  459. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SIGN);
  460. return 0;
  461. }
  462. *siglen = ED448_SIGSIZE;
  463. return 1;
  464. }
  465. #endif /* S390X_EC_ASM */
  466. if (peddsactx->prehash_flag) {
  467. if (!peddsactx->prehash_by_caller_flag) {
  468. if (!ed448_shake256(peddsactx->libctx, NULL, tbs, tbslen, md, mdlen))
  469. return 0;
  470. tbs = md;
  471. tbslen = mdlen;
  472. } else if (tbslen != EDDSA_PREHASH_OUTPUT_LEN) {
  473. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH);
  474. return 0;
  475. }
  476. } else if (peddsactx->prehash_by_caller_flag) {
  477. /* The caller is supposed to set up a ph instance! */
  478. ERR_raise(ERR_LIB_PROV,
  479. PROV_R_INVALID_EDDSA_INSTANCE_FOR_ATTEMPTED_OPERATION);
  480. return 0;
  481. }
  482. if (ossl_ed448_sign(peddsactx->libctx, sigret, tbs, tbslen,
  483. edkey->pubkey, edkey->privkey,
  484. peddsactx->context_string, peddsactx->context_string_len,
  485. peddsactx->prehash_flag, edkey->propq) == 0) {
  486. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SIGN);
  487. return 0;
  488. }
  489. *siglen = ED448_SIGSIZE;
  490. return 1;
  491. }
  492. /*
  493. * This is used directly for OSSL_FUNC_SIGNATURE_VERIFY and indirectly
  494. * for OSSL_FUNC_SIGNATURE_DIGEST_VERIFY
  495. */
  496. static int ed25519_verify(void *vpeddsactx,
  497. const unsigned char *sig, size_t siglen,
  498. const unsigned char *tbs, size_t tbslen)
  499. {
  500. PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
  501. const ECX_KEY *edkey = peddsactx->key;
  502. uint8_t md[EVP_MAX_MD_SIZE];
  503. size_t mdlen;
  504. if (!ossl_prov_is_running() || siglen != ED25519_SIGSIZE)
  505. return 0;
  506. #ifdef S390X_EC_ASM
  507. /*
  508. * s390x_ed25519_digestverify() does not yet support dom2 or context-strings.
  509. * fall back to non-accelerated verify if those options are set, or
  510. * pre-hasing is provided.
  511. */
  512. if (S390X_CAN_SIGN(ED25519)
  513. && !peddsactx->dom2_flag
  514. && !peddsactx->context_string_flag
  515. && peddsactx->context_string_len == 0
  516. && !peddsactx->prehash_flag
  517. && !peddsactx->prehash_by_caller_flag)
  518. return s390x_ed25519_digestverify(edkey, sig, tbs, tbslen);
  519. #endif /* S390X_EC_ASM */
  520. if (peddsactx->prehash_flag) {
  521. if (!peddsactx->prehash_by_caller_flag) {
  522. if (!EVP_Q_digest(peddsactx->libctx, SN_sha512, NULL,
  523. tbs, tbslen, md, &mdlen)
  524. || mdlen != EDDSA_PREHASH_OUTPUT_LEN) {
  525. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_PREHASHED_DIGEST_LENGTH);
  526. return 0;
  527. }
  528. tbs = md;
  529. tbslen = mdlen;
  530. } else if (tbslen != EDDSA_PREHASH_OUTPUT_LEN) {
  531. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH);
  532. return 0;
  533. }
  534. } else if (peddsactx->prehash_by_caller_flag) {
  535. /* The caller is supposed to set up a ph instance! */
  536. ERR_raise(ERR_LIB_PROV,
  537. PROV_R_INVALID_EDDSA_INSTANCE_FOR_ATTEMPTED_OPERATION);
  538. return 0;
  539. }
  540. return ossl_ed25519_verify(tbs, tbslen, sig, edkey->pubkey,
  541. peddsactx->dom2_flag, peddsactx->prehash_flag, peddsactx->context_string_flag,
  542. peddsactx->context_string, peddsactx->context_string_len,
  543. peddsactx->libctx, edkey->propq);
  544. }
  545. /*
  546. * This is used directly for OSSL_FUNC_SIGNATURE_VERIFY and indirectly
  547. * for OSSL_FUNC_SIGNATURE_DIGEST_VERIFY
  548. */
  549. static int ed448_verify(void *vpeddsactx,
  550. const unsigned char *sig, size_t siglen,
  551. const unsigned char *tbs, size_t tbslen)
  552. {
  553. PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
  554. const ECX_KEY *edkey = peddsactx->key;
  555. uint8_t md[EDDSA_PREHASH_OUTPUT_LEN];
  556. size_t mdlen = sizeof(md);
  557. if (!ossl_prov_is_running() || siglen != ED448_SIGSIZE)
  558. return 0;
  559. #ifdef S390X_EC_ASM
  560. /*
  561. * s390x_ed448_digestverify() does not yet support context-strings or
  562. * pre-hashing. Fall back to non-accelerated verify if a context-string or
  563. * pre-hasing is provided.
  564. */
  565. if (S390X_CAN_SIGN(ED448)
  566. && peddsactx->context_string_len == 0
  567. && !peddsactx->prehash_flag
  568. && !peddsactx->prehash_by_caller_flag)
  569. return s390x_ed448_digestverify(edkey, sig, tbs, tbslen);
  570. #endif /* S390X_EC_ASM */
  571. if (peddsactx->prehash_flag) {
  572. if (!peddsactx->prehash_by_caller_flag) {
  573. if (!ed448_shake256(peddsactx->libctx, NULL, tbs, tbslen, md, mdlen))
  574. return 0;
  575. tbs = md;
  576. tbslen = mdlen;
  577. } else if (tbslen != EDDSA_PREHASH_OUTPUT_LEN) {
  578. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH);
  579. return 0;
  580. }
  581. } else if (peddsactx->prehash_by_caller_flag) {
  582. /* The caller is supposed to set up a ph instance! */
  583. ERR_raise(ERR_LIB_PROV,
  584. PROV_R_INVALID_EDDSA_INSTANCE_FOR_ATTEMPTED_OPERATION);
  585. return 0;
  586. }
  587. return ossl_ed448_verify(peddsactx->libctx, tbs, tbslen, sig, edkey->pubkey,
  588. peddsactx->context_string, peddsactx->context_string_len,
  589. peddsactx->prehash_flag, edkey->propq);
  590. }
  591. /* All digest_{sign,verify} are simple wrappers around the functions above */
  592. static int ed25519_digest_signverify_init(void *vpeddsactx, const char *mdname,
  593. void *vedkey,
  594. const OSSL_PARAM params[])
  595. {
  596. PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
  597. if (mdname != NULL && mdname[0] != '\0') {
  598. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
  599. "Explicit digest not allowed with EdDSA operations");
  600. return 0;
  601. }
  602. if (vedkey == NULL && peddsactx->key != NULL)
  603. return eddsa_set_ctx_params(peddsactx, params);
  604. return eddsa_signverify_init(vpeddsactx, vedkey)
  605. && eddsa_setup_instance(vpeddsactx, ID_Ed25519, 0, 0)
  606. && eddsa_set_ctx_params(vpeddsactx, params);
  607. }
  608. static int ed25519_digest_sign(void *vpeddsactx,
  609. unsigned char *sigret, size_t *siglen, size_t sigsize,
  610. const unsigned char *tbs, size_t tbslen)
  611. {
  612. return ed25519_sign(vpeddsactx, sigret, siglen, sigsize, tbs, tbslen);
  613. }
  614. static int ed25519_digest_verify(void *vpeddsactx,
  615. const unsigned char *sigret, size_t siglen,
  616. const unsigned char *tbs, size_t tbslen)
  617. {
  618. return ed25519_verify(vpeddsactx, sigret, siglen, tbs, tbslen);
  619. }
  620. static int ed448_digest_signverify_init(void *vpeddsactx, const char *mdname,
  621. void *vedkey,
  622. const OSSL_PARAM params[])
  623. {
  624. PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
  625. if (mdname != NULL && mdname[0] != '\0') {
  626. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
  627. "Explicit digest not allowed with EdDSA operations");
  628. return 0;
  629. }
  630. if (vedkey == NULL && peddsactx->key != NULL)
  631. return eddsa_set_ctx_params(peddsactx, params);
  632. return eddsa_signverify_init(vpeddsactx, vedkey)
  633. && eddsa_setup_instance(vpeddsactx, ID_Ed448, 0, 0)
  634. && eddsa_set_ctx_params(vpeddsactx, params);
  635. }
  636. static int ed448_digest_sign(void *vpeddsactx,
  637. unsigned char *sigret, size_t *siglen, size_t sigsize,
  638. const unsigned char *tbs, size_t tbslen)
  639. {
  640. return ed448_sign(vpeddsactx, sigret, siglen, sigsize, tbs, tbslen);
  641. }
  642. static int ed448_digest_verify(void *vpeddsactx,
  643. const unsigned char *sigret, size_t siglen,
  644. const unsigned char *tbs, size_t tbslen)
  645. {
  646. return ed448_verify(vpeddsactx, sigret, siglen, tbs, tbslen);
  647. }
  648. static void eddsa_freectx(void *vpeddsactx)
  649. {
  650. PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
  651. ossl_ecx_key_free(peddsactx->key);
  652. OPENSSL_free(peddsactx);
  653. }
  654. static void *eddsa_dupctx(void *vpeddsactx)
  655. {
  656. PROV_EDDSA_CTX *srcctx = (PROV_EDDSA_CTX *)vpeddsactx;
  657. PROV_EDDSA_CTX *dstctx;
  658. if (!ossl_prov_is_running())
  659. return NULL;
  660. dstctx = OPENSSL_zalloc(sizeof(*srcctx));
  661. if (dstctx == NULL)
  662. return NULL;
  663. *dstctx = *srcctx;
  664. dstctx->key = NULL;
  665. if (srcctx->key != NULL && !ossl_ecx_key_up_ref(srcctx->key)) {
  666. ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
  667. goto err;
  668. }
  669. dstctx->key = srcctx->key;
  670. return dstctx;
  671. err:
  672. eddsa_freectx(dstctx);
  673. return NULL;
  674. }
  675. static const char **ed25519_sigalg_query_key_types(void)
  676. {
  677. static const char *keytypes[] = { "ED25519", NULL };
  678. return keytypes;
  679. }
  680. static const char **ed448_sigalg_query_key_types(void)
  681. {
  682. static const char *keytypes[] = { "ED448", NULL };
  683. return keytypes;
  684. }
  685. static int eddsa_get_ctx_params(void *vpeddsactx, OSSL_PARAM *params)
  686. {
  687. PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
  688. OSSL_PARAM *p;
  689. if (peddsactx == NULL)
  690. return 0;
  691. p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID);
  692. if (p != NULL
  693. && !OSSL_PARAM_set_octet_string(p,
  694. peddsactx->aid_len == 0 ? NULL : peddsactx->aid_buf,
  695. peddsactx->aid_len))
  696. return 0;
  697. return 1;
  698. }
  699. static const OSSL_PARAM known_gettable_ctx_params[] = {
  700. OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0),
  701. OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_INSTANCE, NULL, 0),
  702. OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_CONTEXT_STRING, NULL, 0),
  703. OSSL_PARAM_END
  704. };
  705. static const OSSL_PARAM *eddsa_gettable_ctx_params(ossl_unused void *vpeddsactx,
  706. ossl_unused void *provctx)
  707. {
  708. return known_gettable_ctx_params;
  709. }
  710. static int eddsa_set_ctx_params(void *vpeddsactx, const OSSL_PARAM params[])
  711. {
  712. PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
  713. const OSSL_PARAM *p;
  714. if (peddsactx == NULL)
  715. return 0;
  716. if (ossl_param_is_empty(params))
  717. return 1;
  718. p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_INSTANCE);
  719. if (p != NULL) {
  720. char instance_name[OSSL_MAX_NAME_SIZE] = "";
  721. char *pinstance_name = instance_name;
  722. if (peddsactx->instance_id_preset_flag) {
  723. /* When the instance is preset, the caller must no try to set it */
  724. ERR_raise_data(ERR_LIB_PROV, PROV_R_NO_INSTANCE_ALLOWED,
  725. "the EdDSA instance is preset, you may not try to specify it",
  726. NULL);
  727. return 0;
  728. }
  729. if (!OSSL_PARAM_get_utf8_string(p, &pinstance_name, sizeof(instance_name)))
  730. return 0;
  731. /*
  732. * When setting the new instance, we're careful not to change the
  733. * prehash_by_caller flag, as that's always preset by the init
  734. * functions. The sign functions will determine if the instance
  735. * matches this flag.
  736. */
  737. if (OPENSSL_strcasecmp(pinstance_name, SN_Ed25519) == 0) {
  738. eddsa_setup_instance(peddsactx, ID_Ed25519, 0,
  739. peddsactx->prehash_by_caller_flag);
  740. } else if (OPENSSL_strcasecmp(pinstance_name, SN_Ed25519ctx) == 0) {
  741. eddsa_setup_instance(peddsactx, ID_Ed25519ctx, 0,
  742. peddsactx->prehash_by_caller_flag);
  743. } else if (OPENSSL_strcasecmp(pinstance_name, SN_Ed25519ph) == 0) {
  744. eddsa_setup_instance(peddsactx, ID_Ed25519ph, 0,
  745. peddsactx->prehash_by_caller_flag);
  746. } else if (OPENSSL_strcasecmp(pinstance_name, SN_Ed448) == 0) {
  747. eddsa_setup_instance(peddsactx, ID_Ed448, 0,
  748. peddsactx->prehash_by_caller_flag);
  749. } else if (OPENSSL_strcasecmp(pinstance_name, SN_Ed448ph) == 0) {
  750. eddsa_setup_instance(peddsactx, ID_Ed448ph, 0,
  751. peddsactx->prehash_by_caller_flag);
  752. } else {
  753. /* we did not recognize the instance */
  754. return 0;
  755. }
  756. }
  757. p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_CONTEXT_STRING);
  758. if (p != NULL) {
  759. void *vp_context_string = peddsactx->context_string;
  760. if (!OSSL_PARAM_get_octet_string(p, &vp_context_string, sizeof(peddsactx->context_string), &(peddsactx->context_string_len))) {
  761. peddsactx->context_string_len = 0;
  762. return 0;
  763. }
  764. }
  765. return 1;
  766. }
  767. static const OSSL_PARAM settable_ctx_params[] = {
  768. OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_INSTANCE, NULL, 0),
  769. OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_CONTEXT_STRING, NULL, 0),
  770. OSSL_PARAM_END
  771. };
  772. static const OSSL_PARAM *eddsa_settable_ctx_params(ossl_unused void *vpeddsactx,
  773. ossl_unused void *provctx)
  774. {
  775. return settable_ctx_params;
  776. }
  777. static const OSSL_PARAM settable_variant_ctx_params[] = {
  778. OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_CONTEXT_STRING, NULL, 0),
  779. OSSL_PARAM_END
  780. };
  781. static const OSSL_PARAM *
  782. eddsa_settable_variant_ctx_params(ossl_unused void *vpeddsactx,
  783. ossl_unused void *provctx)
  784. {
  785. return settable_variant_ctx_params;
  786. }
  787. /*
  788. * Ed25519 can be used with:
  789. * - EVP_PKEY_sign_init_ex2() [ instance and prehash assumed done by caller ]
  790. * - EVP_PKEY_verify_init_ex2() [ instance and prehash assumed done by caller ]
  791. * - EVP_PKEY_sign_message_init()
  792. * - EVP_PKEY_verify_message_init()
  793. * - EVP_DigestSignInit_ex()
  794. * - EVP_DigestVerifyInit_ex()
  795. * Ed25519ph can be used with:
  796. * - EVP_PKEY_sign_init_ex2() [ prehash assumed done by caller ]
  797. * - EVP_PKEY_verify_init_ex2() [ prehash assumed done by caller ]
  798. * - EVP_PKEY_sign_message_init()
  799. * - EVP_PKEY_verify_message_init()
  800. * Ed25519ctx can be used with:
  801. * - EVP_PKEY_sign_message_init()
  802. * - EVP_PKEY_verify_message_init()
  803. * Ed448 can be used with:
  804. * - EVP_PKEY_sign_init_ex2() [ instance and prehash assumed done by caller ]
  805. * - EVP_PKEY_verify_init_ex2() [ instance and prehash assumed done by caller ]
  806. * - EVP_PKEY_sign_message_init()
  807. * - EVP_PKEY_verify_message_init()
  808. * - EVP_DigestSignInit_ex()
  809. * - EVP_DigestVerifyInit_ex()
  810. * Ed448ph can be used with:
  811. * - EVP_PKEY_sign_init_ex2() [ prehash assumed done by caller ]
  812. * - EVP_PKEY_verify_init_ex2() [ prehash assumed done by caller ]
  813. * - EVP_PKEY_sign_message_init()
  814. * - EVP_PKEY_verify_message_init()
  815. */
  816. #define ed25519_DISPATCH_END \
  817. { OSSL_FUNC_SIGNATURE_SIGN_INIT, \
  818. (void (*)(void))ed25519_signverify_init }, \
  819. { OSSL_FUNC_SIGNATURE_VERIFY_INIT, \
  820. (void (*)(void))ed25519_signverify_init }, \
  821. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, \
  822. (void (*)(void))ed25519_digest_signverify_init }, \
  823. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN, \
  824. (void (*)(void))ed25519_digest_sign }, \
  825. { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT, \
  826. (void (*)(void))ed25519_digest_signverify_init }, \
  827. { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY, \
  828. (void (*)(void))ed25519_digest_verify }, \
  829. { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, \
  830. (void (*)(void))eddsa_get_ctx_params }, \
  831. { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS, \
  832. (void (*)(void))eddsa_gettable_ctx_params }, \
  833. { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, \
  834. (void (*)(void))eddsa_set_ctx_params }, \
  835. { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS, \
  836. (void (*)(void))eddsa_settable_ctx_params }, \
  837. OSSL_DISPATCH_END
  838. #define eddsa_variant_DISPATCH_END(v) \
  839. { OSSL_FUNC_SIGNATURE_SIGN_INIT, \
  840. (void (*)(void))v##_signverify_message_init }, \
  841. { OSSL_FUNC_SIGNATURE_VERIFY_INIT, \
  842. (void (*)(void))v##_signverify_message_init }, \
  843. { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, \
  844. (void (*)(void))eddsa_get_ctx_params }, \
  845. { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS, \
  846. (void (*)(void))eddsa_gettable_ctx_params }, \
  847. { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, \
  848. (void (*)(void))eddsa_set_ctx_params }, \
  849. { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS, \
  850. (void (*)(void))eddsa_settable_variant_ctx_params }, \
  851. OSSL_DISPATCH_END
  852. #define ed25519ph_DISPATCH_END \
  853. { OSSL_FUNC_SIGNATURE_SIGN_INIT, \
  854. (void (*)(void))ed25519ph_signverify_init }, \
  855. { OSSL_FUNC_SIGNATURE_VERIFY_INIT, \
  856. (void (*)(void))ed25519ph_signverify_init }, \
  857. eddsa_variant_DISPATCH_END(ed25519ph)
  858. #define ed25519ctx_DISPATCH_END eddsa_variant_DISPATCH_END(ed25519ctx)
  859. #define ed448_DISPATCH_END \
  860. { OSSL_FUNC_SIGNATURE_SIGN_INIT, \
  861. (void (*)(void))ed448_signverify_init }, \
  862. { OSSL_FUNC_SIGNATURE_VERIFY_INIT, \
  863. (void (*)(void))ed448_signverify_init }, \
  864. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, \
  865. (void (*)(void))ed448_digest_signverify_init }, \
  866. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN, \
  867. (void (*)(void))ed448_digest_sign }, \
  868. { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT, \
  869. (void (*)(void))ed448_digest_signverify_init }, \
  870. { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY, \
  871. (void (*)(void))ed448_digest_verify }, \
  872. { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, \
  873. (void (*)(void))eddsa_get_ctx_params }, \
  874. { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS, \
  875. (void (*)(void))eddsa_gettable_ctx_params }, \
  876. { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, \
  877. (void (*)(void))eddsa_set_ctx_params }, \
  878. { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS, \
  879. (void (*)(void))eddsa_settable_ctx_params }, \
  880. OSSL_DISPATCH_END
  881. #define ed448ph_DISPATCH_END \
  882. { OSSL_FUNC_SIGNATURE_SIGN_INIT, \
  883. (void (*)(void))ed448ph_signverify_init }, \
  884. { OSSL_FUNC_SIGNATURE_VERIFY_INIT, \
  885. (void (*)(void))ed448ph_signverify_init }, \
  886. eddsa_variant_DISPATCH_END(ed448ph)
  887. /* vn = variant name, bn = base name */
  888. #define IMPL_EDDSA_DISPATCH(vn,bn) \
  889. const OSSL_DISPATCH ossl_##vn##_signature_functions[] = { \
  890. { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))eddsa_newctx }, \
  891. { OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_INIT, \
  892. (void (*)(void))vn##_signverify_message_init }, \
  893. { OSSL_FUNC_SIGNATURE_SIGN, \
  894. (void (*)(void))bn##_sign }, \
  895. { OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_INIT, \
  896. (void (*)(void))vn##_signverify_message_init }, \
  897. { OSSL_FUNC_SIGNATURE_VERIFY, \
  898. (void (*)(void))bn##_verify }, \
  899. { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))eddsa_freectx }, \
  900. { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))eddsa_dupctx }, \
  901. { OSSL_FUNC_SIGNATURE_QUERY_KEY_TYPES, \
  902. (void (*)(void))bn##_sigalg_query_key_types }, \
  903. vn##_DISPATCH_END \
  904. }
  905. IMPL_EDDSA_DISPATCH(ed25519,ed25519);
  906. IMPL_EDDSA_DISPATCH(ed25519ph,ed25519);
  907. IMPL_EDDSA_DISPATCH(ed25519ctx,ed25519);
  908. IMPL_EDDSA_DISPATCH(ed448,ed448);
  909. IMPL_EDDSA_DISPATCH(ed448ph,ed448);
  910. #ifdef S390X_EC_ASM
  911. static int s390x_ed25519_digestsign(const ECX_KEY *edkey, unsigned char *sig,
  912. const unsigned char *tbs, size_t tbslen)
  913. {
  914. int rc;
  915. union {
  916. struct {
  917. unsigned char sig[64];
  918. unsigned char priv[32];
  919. } ed25519;
  920. unsigned long long buff[512];
  921. } param;
  922. memset(&param, 0, sizeof(param));
  923. memcpy(param.ed25519.priv, edkey->privkey, sizeof(param.ed25519.priv));
  924. rc = s390x_kdsa(S390X_EDDSA_SIGN_ED25519, &param.ed25519, tbs, tbslen);
  925. OPENSSL_cleanse(param.ed25519.priv, sizeof(param.ed25519.priv));
  926. if (rc != 0)
  927. return 0;
  928. s390x_flip_endian32(sig, param.ed25519.sig);
  929. s390x_flip_endian32(sig + 32, param.ed25519.sig + 32);
  930. return 1;
  931. }
  932. static int s390x_ed448_digestsign(const ECX_KEY *edkey, unsigned char *sig,
  933. const unsigned char *tbs, size_t tbslen)
  934. {
  935. int rc;
  936. union {
  937. struct {
  938. unsigned char sig[128];
  939. unsigned char priv[64];
  940. } ed448;
  941. unsigned long long buff[512];
  942. } param;
  943. memset(&param, 0, sizeof(param));
  944. memcpy(param.ed448.priv + 64 - 57, edkey->privkey, 57);
  945. rc = s390x_kdsa(S390X_EDDSA_SIGN_ED448, &param.ed448, tbs, tbslen);
  946. OPENSSL_cleanse(param.ed448.priv, sizeof(param.ed448.priv));
  947. if (rc != 0)
  948. return 0;
  949. s390x_flip_endian64(param.ed448.sig, param.ed448.sig);
  950. s390x_flip_endian64(param.ed448.sig + 64, param.ed448.sig + 64);
  951. memcpy(sig, param.ed448.sig, 57);
  952. memcpy(sig + 57, param.ed448.sig + 64, 57);
  953. return 1;
  954. }
  955. static int s390x_ed25519_digestverify(const ECX_KEY *edkey,
  956. const unsigned char *sig,
  957. const unsigned char *tbs, size_t tbslen)
  958. {
  959. union {
  960. struct {
  961. unsigned char sig[64];
  962. unsigned char pub[32];
  963. } ed25519;
  964. unsigned long long buff[512];
  965. } param;
  966. memset(&param, 0, sizeof(param));
  967. s390x_flip_endian32(param.ed25519.sig, sig);
  968. s390x_flip_endian32(param.ed25519.sig + 32, sig + 32);
  969. s390x_flip_endian32(param.ed25519.pub, edkey->pubkey);
  970. return s390x_kdsa(S390X_EDDSA_VERIFY_ED25519,
  971. &param.ed25519, tbs, tbslen) == 0 ? 1 : 0;
  972. }
  973. static int s390x_ed448_digestverify(const ECX_KEY *edkey,
  974. const unsigned char *sig,
  975. const unsigned char *tbs,
  976. size_t tbslen)
  977. {
  978. union {
  979. struct {
  980. unsigned char sig[128];
  981. unsigned char pub[64];
  982. } ed448;
  983. unsigned long long buff[512];
  984. } param;
  985. memset(&param, 0, sizeof(param));
  986. memcpy(param.ed448.sig, sig, 57);
  987. s390x_flip_endian64(param.ed448.sig, param.ed448.sig);
  988. memcpy(param.ed448.sig + 64, sig + 57, 57);
  989. s390x_flip_endian64(param.ed448.sig + 64, param.ed448.sig + 64);
  990. memcpy(param.ed448.pub, edkey->pubkey, 57);
  991. s390x_flip_endian64(param.ed448.pub, param.ed448.pub);
  992. return s390x_kdsa(S390X_EDDSA_VERIFY_ED448,
  993. &param.ed448, tbs, tbslen) == 0 ? 1 : 0;
  994. }
  995. #endif /* S390X_EC_ASM */