eddsa_sig.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. /*
  2. * Copyright 2020-2021 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/provider_ctx.h"
  21. #include "prov/der_ecx.h"
  22. #include "crypto/ecx.h"
  23. #ifdef S390X_EC_ASM
  24. # include "s390x_arch.h"
  25. # define S390X_CAN_SIGN(edtype) \
  26. ((OPENSSL_s390xcap_P.pcc[1] & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_##edtype)) \
  27. && (OPENSSL_s390xcap_P.kdsa[0] & S390X_CAPBIT(S390X_EDDSA_SIGN_##edtype)) \
  28. && (OPENSSL_s390xcap_P.kdsa[0] & S390X_CAPBIT(S390X_EDDSA_VERIFY_##edtype)))
  29. static int s390x_ed25519_digestsign(const ECX_KEY *edkey, unsigned char *sig,
  30. const unsigned char *tbs, size_t tbslen);
  31. static int s390x_ed448_digestsign(const ECX_KEY *edkey, unsigned char *sig,
  32. const unsigned char *tbs, size_t tbslen);
  33. static int s390x_ed25519_digestverify(const ECX_KEY *edkey,
  34. const unsigned char *sig,
  35. const unsigned char *tbs, size_t tbslen);
  36. static int s390x_ed448_digestverify(const ECX_KEY *edkey,
  37. const unsigned char *sig,
  38. const unsigned char *tbs, size_t tbslen);
  39. #endif /* S390X_EC_ASM */
  40. enum ID_EdDSA_INSTANCE {
  41. ID_NOT_SET = 0,
  42. ID_Ed25519,
  43. ID_Ed25519ctx,
  44. ID_Ed25519ph,
  45. ID_Ed448,
  46. ID_Ed448ph
  47. };
  48. #define SN_Ed25519 "Ed25519"
  49. #define SN_Ed25519ph "Ed25519ph"
  50. #define SN_Ed25519ctx "Ed25519ctx"
  51. #define SN_Ed448 "Ed448"
  52. #define SN_Ed448ph "Ed448ph"
  53. #define EDDSA_MAX_CONTEXT_STRING_LEN 255
  54. #define EDDSA_PREHASH_OUTPUT_LEN 64
  55. static OSSL_FUNC_signature_newctx_fn eddsa_newctx;
  56. static OSSL_FUNC_signature_digest_sign_init_fn eddsa_digest_signverify_init;
  57. static OSSL_FUNC_signature_digest_sign_fn ed25519_digest_sign;
  58. static OSSL_FUNC_signature_digest_sign_fn ed448_digest_sign;
  59. static OSSL_FUNC_signature_digest_verify_fn ed25519_digest_verify;
  60. static OSSL_FUNC_signature_digest_verify_fn ed448_digest_verify;
  61. static OSSL_FUNC_signature_freectx_fn eddsa_freectx;
  62. static OSSL_FUNC_signature_dupctx_fn eddsa_dupctx;
  63. static OSSL_FUNC_signature_get_ctx_params_fn eddsa_get_ctx_params;
  64. static OSSL_FUNC_signature_gettable_ctx_params_fn eddsa_gettable_ctx_params;
  65. static OSSL_FUNC_signature_set_ctx_params_fn eddsa_set_ctx_params;
  66. static OSSL_FUNC_signature_settable_ctx_params_fn eddsa_settable_ctx_params;
  67. /* there are five EdDSA instances:
  68. Ed25519
  69. Ed25519ph
  70. Ed25519ctx
  71. Ed448
  72. Ed448ph
  73. Quoting from RFC 8032, Section 5.1:
  74. For Ed25519, dom2(f,c) is the empty string. The phflag value is
  75. irrelevant. The context (if present at all) MUST be empty. This
  76. causes the scheme to be one and the same with the Ed25519 scheme
  77. published earlier.
  78. For Ed25519ctx, phflag=0. The context input SHOULD NOT be empty.
  79. For Ed25519ph, phflag=1 and PH is SHA512 instead. That is, the input
  80. is hashed using SHA-512 before signing with Ed25519.
  81. Quoting from RFC 8032, Section 5.2:
  82. Ed448ph is the same but with PH being SHAKE256(x, 64) and phflag
  83. being 1, i.e., the input is hashed before signing with Ed448 with a
  84. hash constant modified.
  85. Value of context is set by signer and verifier (maximum of 255
  86. octets; the default is empty string) and has to match octet by octet
  87. for verification to be successful.
  88. Quoting from RFC 8032, Section 2:
  89. dom2(x, y) The blank octet string when signing or verifying
  90. Ed25519. Otherwise, the octet string: "SigEd25519 no
  91. Ed25519 collisions" || octet(x) || octet(OLEN(y)) ||
  92. y, where x is in range 0-255 and y is an octet string
  93. of at most 255 octets. "SigEd25519 no Ed25519
  94. collisions" is in ASCII (32 octets).
  95. dom4(x, y) The octet string "SigEd448" || octet(x) ||
  96. octet(OLEN(y)) || y, where x is in range 0-255 and y
  97. is an octet string of at most 255 octets. "SigEd448"
  98. is in ASCII (8 octets).
  99. Note above that x is the pre-hash flag, and y is the context string.
  100. */
  101. typedef struct {
  102. OSSL_LIB_CTX *libctx;
  103. ECX_KEY *key;
  104. /* The Algorithm Identifier of the signature algorithm */
  105. unsigned char aid_buf[OSSL_MAX_ALGORITHM_ID_SIZE];
  106. unsigned char *aid;
  107. size_t aid_len;
  108. /* id indicating the EdDSA instance */
  109. int instance_id;
  110. unsigned int dom2_flag : 1;
  111. unsigned int prehash_flag : 1;
  112. /* indicates that a non-empty context string is required, as in Ed25519ctx */
  113. unsigned int context_string_flag : 1;
  114. unsigned char context_string[EDDSA_MAX_CONTEXT_STRING_LEN];
  115. size_t context_string_len;
  116. } PROV_EDDSA_CTX;
  117. static void *eddsa_newctx(void *provctx, const char *propq_unused)
  118. {
  119. PROV_EDDSA_CTX *peddsactx;
  120. if (!ossl_prov_is_running())
  121. return NULL;
  122. peddsactx = OPENSSL_zalloc(sizeof(PROV_EDDSA_CTX));
  123. if (peddsactx == NULL)
  124. return NULL;
  125. peddsactx->libctx = PROV_LIBCTX_OF(provctx);
  126. return peddsactx;
  127. }
  128. static int eddsa_digest_signverify_init(void *vpeddsactx, const char *mdname,
  129. void *vedkey,
  130. const OSSL_PARAM params[])
  131. {
  132. PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
  133. ECX_KEY *edkey = (ECX_KEY *)vedkey;
  134. WPACKET pkt;
  135. int ret;
  136. if (!ossl_prov_is_running())
  137. return 0;
  138. if (mdname != NULL && mdname[0] != '\0') {
  139. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
  140. return 0;
  141. }
  142. if (edkey == NULL) {
  143. if (peddsactx->key != NULL)
  144. return eddsa_set_ctx_params(peddsactx, params);
  145. ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
  146. return 0;
  147. }
  148. if (!ossl_ecx_key_up_ref(edkey)) {
  149. ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
  150. return 0;
  151. }
  152. peddsactx->dom2_flag = 0;
  153. peddsactx->prehash_flag = 0;
  154. peddsactx->context_string_flag = 0;
  155. peddsactx->context_string_len = 0;
  156. /*
  157. * We do not care about DER writing errors.
  158. * All it really means is that for some reason, there's no
  159. * AlgorithmIdentifier to be had, but the operation itself is
  160. * still valid, just as long as it's not used to construct
  161. * anything that needs an AlgorithmIdentifier.
  162. */
  163. peddsactx->aid_len = 0;
  164. ret = WPACKET_init_der(&pkt, peddsactx->aid_buf, sizeof(peddsactx->aid_buf));
  165. switch (edkey->type) {
  166. case ECX_KEY_TYPE_ED25519:
  167. ret = ret && ossl_DER_w_algorithmIdentifier_ED25519(&pkt, -1, edkey);
  168. peddsactx->instance_id = ID_Ed25519;
  169. break;
  170. case ECX_KEY_TYPE_ED448:
  171. ret = ret && ossl_DER_w_algorithmIdentifier_ED448(&pkt, -1, edkey);
  172. peddsactx->instance_id = ID_Ed448;
  173. break;
  174. default:
  175. /* Should never happen */
  176. ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
  177. ossl_ecx_key_free(edkey);
  178. return 0;
  179. }
  180. if (ret && WPACKET_finish(&pkt)) {
  181. WPACKET_get_total_written(&pkt, &peddsactx->aid_len);
  182. peddsactx->aid = WPACKET_get_curr(&pkt);
  183. }
  184. WPACKET_cleanup(&pkt);
  185. peddsactx->key = edkey;
  186. if (!eddsa_set_ctx_params(peddsactx, params))
  187. return 0;
  188. return 1;
  189. }
  190. int ed25519_digest_sign(void *vpeddsactx, unsigned char *sigret,
  191. size_t *siglen, size_t sigsize,
  192. const unsigned char *tbs, size_t tbslen)
  193. {
  194. PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
  195. const ECX_KEY *edkey = peddsactx->key;
  196. uint8_t md[EVP_MAX_MD_SIZE];
  197. size_t mdlen;
  198. if (!ossl_prov_is_running())
  199. return 0;
  200. if (sigret == NULL) {
  201. *siglen = ED25519_SIGSIZE;
  202. return 1;
  203. }
  204. if (sigsize < ED25519_SIGSIZE) {
  205. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  206. return 0;
  207. }
  208. if (edkey->privkey == NULL) {
  209. ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
  210. return 0;
  211. }
  212. #ifdef S390X_EC_ASM
  213. /* s390x_ed25519_digestsign() does not yet support dom2 or context-strings.
  214. fall back to non-accelerated sign if those options are set. */
  215. if (S390X_CAN_SIGN(ED25519)
  216. && !peddsactx->dom2_flag
  217. && !peddsactx->context_string_flag
  218. && peddsactx->context_string_len == 0) {
  219. if (s390x_ed25519_digestsign(edkey, sigret, tbs, tbslen) == 0) {
  220. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SIGN);
  221. return 0;
  222. }
  223. *siglen = ED25519_SIGSIZE;
  224. return 1;
  225. }
  226. #endif /* S390X_EC_ASM */
  227. if (peddsactx->prehash_flag) {
  228. if (!EVP_Q_digest(peddsactx->libctx, SN_sha512, NULL, tbs, tbslen, md, &mdlen)
  229. || mdlen != EDDSA_PREHASH_OUTPUT_LEN)
  230. return 0;
  231. tbs = md;
  232. tbslen = mdlen;
  233. }
  234. if (ossl_ed25519_sign(sigret, tbs, tbslen, edkey->pubkey, edkey->privkey,
  235. peddsactx->dom2_flag, peddsactx->prehash_flag, peddsactx->context_string_flag,
  236. peddsactx->context_string, peddsactx->context_string_len,
  237. peddsactx->libctx, NULL) == 0) {
  238. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SIGN);
  239. return 0;
  240. }
  241. *siglen = ED25519_SIGSIZE;
  242. return 1;
  243. }
  244. /* EVP_Q_digest() does not allow variable output length for XOFs,
  245. so we use this function */
  246. static int ed448_shake256(OSSL_LIB_CTX *libctx,
  247. const char *propq,
  248. const uint8_t *in, size_t inlen,
  249. uint8_t *out, size_t outlen)
  250. {
  251. int ret = 0;
  252. EVP_MD_CTX *hash_ctx = EVP_MD_CTX_new();
  253. EVP_MD *shake256 = EVP_MD_fetch(libctx, SN_shake256, propq);
  254. if (hash_ctx == NULL || shake256 == NULL)
  255. goto err;
  256. if (!EVP_DigestInit_ex(hash_ctx, shake256, NULL)
  257. || !EVP_DigestUpdate(hash_ctx, in, inlen)
  258. || !EVP_DigestFinalXOF(hash_ctx, out, outlen))
  259. goto err;
  260. ret = 1;
  261. err:
  262. EVP_MD_CTX_free(hash_ctx);
  263. EVP_MD_free(shake256);
  264. return ret;
  265. }
  266. int ed448_digest_sign(void *vpeddsactx, unsigned char *sigret,
  267. size_t *siglen, size_t sigsize,
  268. const unsigned char *tbs, size_t tbslen)
  269. {
  270. PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
  271. const ECX_KEY *edkey = peddsactx->key;
  272. uint8_t md[EDDSA_PREHASH_OUTPUT_LEN];
  273. size_t mdlen = sizeof(md);
  274. if (!ossl_prov_is_running())
  275. return 0;
  276. if (sigret == NULL) {
  277. *siglen = ED448_SIGSIZE;
  278. return 1;
  279. }
  280. if (sigsize < ED448_SIGSIZE) {
  281. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  282. return 0;
  283. }
  284. if (edkey->privkey == NULL) {
  285. ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
  286. return 0;
  287. }
  288. #ifdef S390X_EC_ASM
  289. /* s390x_ed448_digestsign() does not yet support context-strings or pre-hashing.
  290. fall back to non-accelerated sign if a context-string or pre-hasing is provided. */
  291. if (S390X_CAN_SIGN(ED448)
  292. && peddsactx->context_string_len == 0
  293. && peddsactx->prehash_flag == 0) {
  294. if (s390x_ed448_digestsign(edkey, sigret, tbs, tbslen) == 0) {
  295. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SIGN);
  296. return 0;
  297. }
  298. *siglen = ED448_SIGSIZE;
  299. return 1;
  300. }
  301. #endif /* S390X_EC_ASM */
  302. if (peddsactx->prehash_flag) {
  303. if (!ed448_shake256(peddsactx->libctx, NULL, tbs, tbslen, md, mdlen))
  304. return 0;
  305. tbs = md;
  306. tbslen = mdlen;
  307. }
  308. if (ossl_ed448_sign(peddsactx->libctx, sigret, tbs, tbslen,
  309. edkey->pubkey, edkey->privkey,
  310. peddsactx->context_string, peddsactx->context_string_len,
  311. peddsactx->prehash_flag, edkey->propq) == 0) {
  312. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SIGN);
  313. return 0;
  314. }
  315. *siglen = ED448_SIGSIZE;
  316. return 1;
  317. }
  318. int ed25519_digest_verify(void *vpeddsactx, const unsigned char *sig,
  319. size_t siglen, const unsigned char *tbs,
  320. size_t tbslen)
  321. {
  322. PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
  323. const ECX_KEY *edkey = peddsactx->key;
  324. uint8_t md[EVP_MAX_MD_SIZE];
  325. size_t mdlen;
  326. if (!ossl_prov_is_running() || siglen != ED25519_SIGSIZE)
  327. return 0;
  328. #ifdef S390X_EC_ASM
  329. /* s390x_ed25519_digestverify() does not yet support dom2 or context-strings.
  330. fall back to non-accelerated verify if those options are set. */
  331. if (S390X_CAN_SIGN(ED25519)
  332. && !peddsactx->dom2_flag
  333. && !peddsactx->context_string_flag
  334. && peddsactx->context_string_len == 0) {
  335. return s390x_ed25519_digestverify(edkey, sig, tbs, tbslen);
  336. }
  337. #endif /* S390X_EC_ASM */
  338. if (peddsactx->prehash_flag) {
  339. if (!EVP_Q_digest(peddsactx->libctx, SN_sha512, NULL, tbs, tbslen, md, &mdlen)
  340. || mdlen != EDDSA_PREHASH_OUTPUT_LEN)
  341. return 0;
  342. tbs = md;
  343. tbslen = mdlen;
  344. }
  345. return ossl_ed25519_verify(tbs, tbslen, sig, edkey->pubkey,
  346. peddsactx->dom2_flag, peddsactx->prehash_flag, peddsactx->context_string_flag,
  347. peddsactx->context_string, peddsactx->context_string_len,
  348. peddsactx->libctx, edkey->propq);
  349. }
  350. int ed448_digest_verify(void *vpeddsactx, const unsigned char *sig,
  351. size_t siglen, const unsigned char *tbs,
  352. size_t tbslen)
  353. {
  354. PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
  355. const ECX_KEY *edkey = peddsactx->key;
  356. uint8_t md[EDDSA_PREHASH_OUTPUT_LEN];
  357. size_t mdlen = sizeof(md);
  358. if (!ossl_prov_is_running() || siglen != ED448_SIGSIZE)
  359. return 0;
  360. #ifdef S390X_EC_ASM
  361. /* s390x_ed448_digestverify() does not yet support context-strings or pre-hashing.
  362. fall back to non-accelerated verify if a context-string or pre-hasing is provided. */
  363. if (S390X_CAN_SIGN(ED448)
  364. && peddsactx->context_string_len == 0
  365. && peddsactx->prehash_flag == 0) {
  366. return s390x_ed448_digestverify(edkey, sig, tbs, tbslen);
  367. }
  368. #endif /* S390X_EC_ASM */
  369. if (peddsactx->prehash_flag) {
  370. if (!ed448_shake256(peddsactx->libctx, NULL, tbs, tbslen, md, mdlen))
  371. return 0;
  372. tbs = md;
  373. tbslen = mdlen;
  374. }
  375. return ossl_ed448_verify(peddsactx->libctx, tbs, tbslen, sig, edkey->pubkey,
  376. peddsactx->context_string, peddsactx->context_string_len,
  377. peddsactx->prehash_flag, edkey->propq);
  378. }
  379. static void eddsa_freectx(void *vpeddsactx)
  380. {
  381. PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
  382. ossl_ecx_key_free(peddsactx->key);
  383. OPENSSL_free(peddsactx);
  384. }
  385. static void *eddsa_dupctx(void *vpeddsactx)
  386. {
  387. PROV_EDDSA_CTX *srcctx = (PROV_EDDSA_CTX *)vpeddsactx;
  388. PROV_EDDSA_CTX *dstctx;
  389. if (!ossl_prov_is_running())
  390. return NULL;
  391. dstctx = OPENSSL_zalloc(sizeof(*srcctx));
  392. if (dstctx == NULL)
  393. return NULL;
  394. *dstctx = *srcctx;
  395. dstctx->key = NULL;
  396. if (srcctx->key != NULL && !ossl_ecx_key_up_ref(srcctx->key)) {
  397. ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
  398. goto err;
  399. }
  400. dstctx->key = srcctx->key;
  401. return dstctx;
  402. err:
  403. eddsa_freectx(dstctx);
  404. return NULL;
  405. }
  406. static int eddsa_get_ctx_params(void *vpeddsactx, OSSL_PARAM *params)
  407. {
  408. PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
  409. OSSL_PARAM *p;
  410. if (peddsactx == NULL)
  411. return 0;
  412. p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID);
  413. if (p != NULL && !OSSL_PARAM_set_octet_string(p, peddsactx->aid,
  414. peddsactx->aid_len))
  415. return 0;
  416. return 1;
  417. }
  418. static const OSSL_PARAM known_gettable_ctx_params[] = {
  419. OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0),
  420. OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_INSTANCE, NULL, 0),
  421. OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_CONTEXT_STRING, NULL, 0),
  422. OSSL_PARAM_END
  423. };
  424. static const OSSL_PARAM *eddsa_gettable_ctx_params(ossl_unused void *vpeddsactx,
  425. ossl_unused void *provctx)
  426. {
  427. return known_gettable_ctx_params;
  428. }
  429. static int eddsa_set_ctx_params(void *vpeddsactx, const OSSL_PARAM params[])
  430. {
  431. PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
  432. const OSSL_PARAM *p;
  433. if (peddsactx == NULL)
  434. return 0;
  435. if (params == NULL)
  436. return 1;
  437. p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_INSTANCE);
  438. if (p != NULL) {
  439. char instance_name[OSSL_MAX_NAME_SIZE] = "";
  440. char *pinstance_name = instance_name;
  441. if (!OSSL_PARAM_get_utf8_string(p, &pinstance_name, sizeof(instance_name)))
  442. return 0;
  443. if (OPENSSL_strcasecmp(pinstance_name, SN_Ed25519) == 0) {
  444. peddsactx->instance_id = ID_Ed25519;
  445. if (peddsactx->key->type != ECX_KEY_TYPE_ED25519) return 0;
  446. peddsactx->dom2_flag = 0;
  447. peddsactx->prehash_flag = 0;
  448. peddsactx->context_string_flag = 0;
  449. } else if (OPENSSL_strcasecmp(pinstance_name, SN_Ed25519ctx) == 0) {
  450. peddsactx->instance_id = ID_Ed25519ctx;
  451. if (peddsactx->key->type != ECX_KEY_TYPE_ED25519) return 0;
  452. peddsactx->dom2_flag = 1;
  453. peddsactx->prehash_flag = 0;
  454. peddsactx->context_string_flag = 1;
  455. } else if (OPENSSL_strcasecmp(pinstance_name, SN_Ed25519ph) == 0) {
  456. peddsactx->instance_id = ID_Ed25519ph;
  457. if (peddsactx->key->type != ECX_KEY_TYPE_ED25519) return 0;
  458. peddsactx->dom2_flag = 1;
  459. peddsactx->prehash_flag = 1;
  460. peddsactx->context_string_flag = 0;
  461. } else if (OPENSSL_strcasecmp(pinstance_name, SN_Ed448) == 0) {
  462. peddsactx->instance_id = ID_Ed448;
  463. if (peddsactx->key->type != ECX_KEY_TYPE_ED448) return 0;
  464. peddsactx->prehash_flag = 0;
  465. peddsactx->context_string_flag = 0;
  466. } else if (OPENSSL_strcasecmp(pinstance_name, SN_Ed448ph) == 0) {
  467. peddsactx->instance_id = ID_Ed448ph;
  468. if (peddsactx->key->type != ECX_KEY_TYPE_ED448) return 0;
  469. peddsactx->prehash_flag = 1;
  470. peddsactx->context_string_flag = 0;
  471. } else {
  472. /* we did not recognize the instance */
  473. return 0;
  474. }
  475. }
  476. p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_CONTEXT_STRING);
  477. if (p != NULL) {
  478. void *vp_context_string = peddsactx->context_string;
  479. if (!OSSL_PARAM_get_octet_string(p, &vp_context_string, sizeof(peddsactx->context_string), &(peddsactx->context_string_len))) {
  480. peddsactx->context_string_len = 0;
  481. return 0;
  482. }
  483. }
  484. return 1;
  485. }
  486. static const OSSL_PARAM settable_ctx_params[] = {
  487. OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_INSTANCE, NULL, 0),
  488. OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_CONTEXT_STRING, NULL, 0),
  489. OSSL_PARAM_END
  490. };
  491. static const OSSL_PARAM *eddsa_settable_ctx_params(ossl_unused void *vpeddsactx,
  492. ossl_unused void *provctx)
  493. {
  494. return settable_ctx_params;
  495. }
  496. const OSSL_DISPATCH ossl_ed25519_signature_functions[] = {
  497. { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))eddsa_newctx },
  498. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
  499. (void (*)(void))eddsa_digest_signverify_init },
  500. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN,
  501. (void (*)(void))ed25519_digest_sign },
  502. { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
  503. (void (*)(void))eddsa_digest_signverify_init },
  504. { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY,
  505. (void (*)(void))ed25519_digest_verify },
  506. { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))eddsa_freectx },
  507. { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))eddsa_dupctx },
  508. { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))eddsa_get_ctx_params },
  509. { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
  510. (void (*)(void))eddsa_gettable_ctx_params },
  511. { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))eddsa_set_ctx_params },
  512. { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
  513. (void (*)(void))eddsa_settable_ctx_params },
  514. OSSL_DISPATCH_END
  515. };
  516. const OSSL_DISPATCH ossl_ed448_signature_functions[] = {
  517. { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))eddsa_newctx },
  518. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
  519. (void (*)(void))eddsa_digest_signverify_init },
  520. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN,
  521. (void (*)(void))ed448_digest_sign },
  522. { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
  523. (void (*)(void))eddsa_digest_signverify_init },
  524. { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY,
  525. (void (*)(void))ed448_digest_verify },
  526. { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))eddsa_freectx },
  527. { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))eddsa_dupctx },
  528. { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))eddsa_get_ctx_params },
  529. { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
  530. (void (*)(void))eddsa_gettable_ctx_params },
  531. { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))eddsa_set_ctx_params },
  532. { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
  533. (void (*)(void))eddsa_settable_ctx_params },
  534. OSSL_DISPATCH_END
  535. };
  536. #ifdef S390X_EC_ASM
  537. static int s390x_ed25519_digestsign(const ECX_KEY *edkey, unsigned char *sig,
  538. const unsigned char *tbs, size_t tbslen)
  539. {
  540. int rc;
  541. union {
  542. struct {
  543. unsigned char sig[64];
  544. unsigned char priv[32];
  545. } ed25519;
  546. unsigned long long buff[512];
  547. } param;
  548. memset(&param, 0, sizeof(param));
  549. memcpy(param.ed25519.priv, edkey->privkey, sizeof(param.ed25519.priv));
  550. rc = s390x_kdsa(S390X_EDDSA_SIGN_ED25519, &param.ed25519, tbs, tbslen);
  551. OPENSSL_cleanse(param.ed25519.priv, sizeof(param.ed25519.priv));
  552. if (rc != 0)
  553. return 0;
  554. s390x_flip_endian32(sig, param.ed25519.sig);
  555. s390x_flip_endian32(sig + 32, param.ed25519.sig + 32);
  556. return 1;
  557. }
  558. static int s390x_ed448_digestsign(const ECX_KEY *edkey, unsigned char *sig,
  559. const unsigned char *tbs, size_t tbslen)
  560. {
  561. int rc;
  562. union {
  563. struct {
  564. unsigned char sig[128];
  565. unsigned char priv[64];
  566. } ed448;
  567. unsigned long long buff[512];
  568. } param;
  569. memset(&param, 0, sizeof(param));
  570. memcpy(param.ed448.priv + 64 - 57, edkey->privkey, 57);
  571. rc = s390x_kdsa(S390X_EDDSA_SIGN_ED448, &param.ed448, tbs, tbslen);
  572. OPENSSL_cleanse(param.ed448.priv, sizeof(param.ed448.priv));
  573. if (rc != 0)
  574. return 0;
  575. s390x_flip_endian64(param.ed448.sig, param.ed448.sig);
  576. s390x_flip_endian64(param.ed448.sig + 64, param.ed448.sig + 64);
  577. memcpy(sig, param.ed448.sig, 57);
  578. memcpy(sig + 57, param.ed448.sig + 64, 57);
  579. return 1;
  580. }
  581. static int s390x_ed25519_digestverify(const ECX_KEY *edkey,
  582. const unsigned char *sig,
  583. const unsigned char *tbs, size_t tbslen)
  584. {
  585. union {
  586. struct {
  587. unsigned char sig[64];
  588. unsigned char pub[32];
  589. } ed25519;
  590. unsigned long long buff[512];
  591. } param;
  592. memset(&param, 0, sizeof(param));
  593. s390x_flip_endian32(param.ed25519.sig, sig);
  594. s390x_flip_endian32(param.ed25519.sig + 32, sig + 32);
  595. s390x_flip_endian32(param.ed25519.pub, edkey->pubkey);
  596. return s390x_kdsa(S390X_EDDSA_VERIFY_ED25519,
  597. &param.ed25519, tbs, tbslen) == 0 ? 1 : 0;
  598. }
  599. static int s390x_ed448_digestverify(const ECX_KEY *edkey,
  600. const unsigned char *sig,
  601. const unsigned char *tbs,
  602. size_t tbslen)
  603. {
  604. union {
  605. struct {
  606. unsigned char sig[128];
  607. unsigned char pub[64];
  608. } ed448;
  609. unsigned long long buff[512];
  610. } param;
  611. memset(&param, 0, sizeof(param));
  612. memcpy(param.ed448.sig, sig, 57);
  613. s390x_flip_endian64(param.ed448.sig, param.ed448.sig);
  614. memcpy(param.ed448.sig + 64, sig + 57, 57);
  615. s390x_flip_endian64(param.ed448.sig + 64, param.ed448.sig + 64);
  616. memcpy(param.ed448.pub, edkey->pubkey, 57);
  617. s390x_flip_endian64(param.ed448.pub, param.ed448.pub);
  618. return s390x_kdsa(S390X_EDDSA_VERIFY_ED448,
  619. &param.ed448, tbs, tbslen) == 0 ? 1 : 0;
  620. }
  621. #endif /* S390X_EC_ASM */