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