Coverage Report

Created: 2025-08-25 06:30

/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 int eddsa_get_ctx_params_decoder
809
    (const OSSL_PARAM *p, struct eddsa_get_ctx_params_st *r)
810
0
{
811
0
    const char *s;
812
813
0
    memset(r, 0, sizeof(*r));
814
0
    if (p != NULL)
815
0
        for (; (s = p->key) != NULL; p++)
816
0
            if (ossl_likely(strcmp("algorithm-id", s + 0) == 0)) {
817
                /* SIGNATURE_PARAM_ALGORITHM_ID */
818
0
                if (ossl_unlikely(r->id != NULL)) {
819
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
820
0
                                   "param %s is repeated", s);
821
0
                    return 0;
822
0
                }
823
0
                r->id = (OSSL_PARAM *)p;
824
0
            }
825
0
    return 1;
826
0
}
827
#endif
828
/* End of machine generated */
829
830
static int eddsa_get_ctx_params(void *vpeddsactx, OSSL_PARAM *params)
831
0
{
832
0
    PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
833
0
    struct eddsa_get_ctx_params_st p;
834
835
0
    if (peddsactx == NULL || !eddsa_get_ctx_params_decoder(params, &p))
836
0
        return 0;
837
838
0
    if (p.id != NULL
839
0
        && !OSSL_PARAM_set_octet_string(p.id,
840
0
                                        peddsactx->aid_len == 0 ? NULL : peddsactx->aid_buf,
841
0
                                        peddsactx->aid_len))
842
0
        return 0;
843
844
0
    return 1;
845
0
}
846
847
static const OSSL_PARAM *eddsa_gettable_ctx_params(ossl_unused void *vpeddsactx,
848
                                                   ossl_unused void *provctx)
849
0
{
850
0
    return eddsa_get_ctx_params_list;
851
0
}
852
853
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
854
#ifndef eddsa_set_ctx_params_list
855
static const OSSL_PARAM eddsa_set_ctx_params_list[] = {
856
    OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_INSTANCE, NULL, 0),
857
    OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_CONTEXT_STRING, NULL, 0),
858
    OSSL_PARAM_END
859
};
860
#endif
861
862
#ifndef eddsa_set_ctx_params_st
863
struct eddsa_set_ctx_params_st {
864
    OSSL_PARAM *ctx;
865
    OSSL_PARAM *inst;
866
};
867
#endif
868
869
#ifndef eddsa_set_ctx_params_decoder
870
static int eddsa_set_ctx_params_decoder
871
    (const OSSL_PARAM *p, struct eddsa_set_ctx_params_st *r)
872
0
{
873
0
    const char *s;
874
875
0
    memset(r, 0, sizeof(*r));
876
0
    if (p != NULL)
877
0
        for (; (s = p->key) != NULL; p++)
878
0
            switch(s[0]) {
879
0
            default:
880
0
                break;
881
0
            case 'c':
882
0
                if (ossl_likely(strcmp("ontext-string", s + 1) == 0)) {
883
                    /* SIGNATURE_PARAM_CONTEXT_STRING */
884
0
                    if (ossl_unlikely(r->ctx != NULL)) {
885
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
886
0
                                       "param %s is repeated", s);
887
0
                        return 0;
888
0
                    }
889
0
                    r->ctx = (OSSL_PARAM *)p;
890
0
                }
891
0
                break;
892
0
            case 'i':
893
0
                if (ossl_likely(strcmp("nstance", s + 1) == 0)) {
894
                    /* SIGNATURE_PARAM_INSTANCE */
895
0
                    if (ossl_unlikely(r->inst != NULL)) {
896
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
897
0
                                       "param %s is repeated", s);
898
0
                        return 0;
899
0
                    }
900
0
                    r->inst = (OSSL_PARAM *)p;
901
0
                }
902
0
            }
903
0
    return 1;
904
0
}
905
#endif
906
/* End of machine generated */
907
908
static int eddsa_set_ctx_params_internal
909
        (PROV_EDDSA_CTX *peddsactx, const struct eddsa_set_ctx_params_st *p)
910
0
{
911
0
    if (p->inst != NULL) {
912
0
        char instance_name[OSSL_MAX_NAME_SIZE] = "";
913
0
        char *pinstance_name = instance_name;
914
915
0
        if (peddsactx->instance_id_preset_flag) {
916
            /* When the instance is preset, the caller must not try to set it */
917
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_NO_INSTANCE_ALLOWED,
918
0
                           "the EdDSA instance is preset, you may not try to specify it",
919
0
                           NULL);
920
0
            return 0;
921
0
        }
922
923
0
        if (!OSSL_PARAM_get_utf8_string(p->inst, &pinstance_name,
924
0
                                        sizeof(instance_name)))
925
0
            return 0;
926
927
        /*
928
         * When setting the new instance, we're careful not to change the
929
         * prehash_by_caller flag, as that's always preset by the init
930
         * functions.  The sign functions will determine if the instance
931
         * matches this flag.
932
         */
933
0
        if (OPENSSL_strcasecmp(pinstance_name, SN_Ed25519) == 0) {
934
0
            eddsa_setup_instance(peddsactx, ID_Ed25519, 0,
935
0
                                 peddsactx->prehash_by_caller_flag);
936
0
        } else if (OPENSSL_strcasecmp(pinstance_name, SN_Ed25519ctx) == 0) {
937
0
            eddsa_setup_instance(peddsactx, ID_Ed25519ctx, 0,
938
0
                                 peddsactx->prehash_by_caller_flag);
939
0
        } else if (OPENSSL_strcasecmp(pinstance_name, SN_Ed25519ph) == 0) {
940
0
            eddsa_setup_instance(peddsactx, ID_Ed25519ph, 0,
941
0
                                 peddsactx->prehash_by_caller_flag);
942
0
        } else if (OPENSSL_strcasecmp(pinstance_name, SN_Ed448) == 0) {
943
0
            eddsa_setup_instance(peddsactx, ID_Ed448, 0,
944
0
                                 peddsactx->prehash_by_caller_flag);
945
0
        } else if (OPENSSL_strcasecmp(pinstance_name, SN_Ed448ph) == 0) {
946
0
            eddsa_setup_instance(peddsactx, ID_Ed448ph, 0,
947
0
                                 peddsactx->prehash_by_caller_flag);
948
0
        } else {
949
            /* we did not recognize the instance */
950
0
            return 0;
951
0
        }
952
953
0
    }
954
955
0
    if (p->ctx != NULL) {
956
0
        void *vp_context_string = peddsactx->context_string;
957
958
0
        if (!OSSL_PARAM_get_octet_string(p->ctx, &vp_context_string,
959
0
                                         sizeof(peddsactx->context_string),
960
0
                                         &(peddsactx->context_string_len))) {
961
0
            peddsactx->context_string_len = 0;
962
0
            return 0;
963
0
        }
964
0
    }
965
966
0
    return 1;
967
0
}
968
969
static const OSSL_PARAM *eddsa_settable_ctx_params(ossl_unused void *vpeddsactx,
970
                                                   ossl_unused void *provctx)
971
0
{
972
0
    return eddsa_set_ctx_params_list;
973
0
}
974
975
976
static int eddsa_set_ctx_params(void *vpeddsactx, const OSSL_PARAM params[])
977
0
{
978
0
    PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
979
0
    struct eddsa_set_ctx_params_st p;
980
981
0
    if (peddsactx == NULL || !eddsa_set_ctx_params_decoder(params, &p))
982
0
        return 0;
983
0
    return eddsa_set_ctx_params_internal(peddsactx, &p);
984
0
}
985
986
#define eddsa_set_variant_ctx_params_st         eddsa_set_ctx_params_st
987
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
988
#ifndef eddsa_set_variant_ctx_params_list
989
static const OSSL_PARAM eddsa_set_variant_ctx_params_list[] = {
990
    OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_CONTEXT_STRING, NULL, 0),
991
    OSSL_PARAM_END
992
};
993
#endif
994
995
#ifndef eddsa_set_variant_ctx_params_st
996
struct eddsa_set_variant_ctx_params_st {
997
    OSSL_PARAM *ctx;
998
};
999
#endif
1000
1001
#ifndef eddsa_set_variant_ctx_params_decoder
1002
static int eddsa_set_variant_ctx_params_decoder
1003
    (const OSSL_PARAM *p, struct eddsa_set_variant_ctx_params_st *r)
1004
0
{
1005
0
    const char *s;
1006
1007
0
    memset(r, 0, sizeof(*r));
1008
0
    if (p != NULL)
1009
0
        for (; (s = p->key) != NULL; p++)
1010
0
            if (ossl_likely(strcmp("context-string", s + 0) == 0)) {
1011
                /* SIGNATURE_PARAM_CONTEXT_STRING */
1012
0
                if (ossl_unlikely(r->ctx != NULL)) {
1013
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
1014
0
                                   "param %s is repeated", s);
1015
0
                    return 0;
1016
0
                }
1017
0
                r->ctx = (OSSL_PARAM *)p;
1018
0
            }
1019
0
    return 1;
1020
0
}
1021
#endif
1022
/* End of machine generated */
1023
1024
static const OSSL_PARAM *
1025
eddsa_settable_variant_ctx_params(ossl_unused void *vpeddsactx,
1026
                                  ossl_unused void *provctx)
1027
0
{
1028
0
    return eddsa_set_variant_ctx_params_list;
1029
0
}
1030
1031
static int eddsa_set_variant_ctx_params(void *vpeddsactx,
1032
                                        const OSSL_PARAM params[])
1033
0
{
1034
0
    PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
1035
0
    struct eddsa_set_ctx_params_st p;
1036
1037
0
    if (peddsactx == NULL || !eddsa_set_variant_ctx_params_decoder(params, &p))
1038
0
        return 0;
1039
0
    return eddsa_set_ctx_params_internal(peddsactx, &p);
1040
0
}
1041
1042
/*
1043
 * Ed25519 can be used with:
1044
 * - EVP_PKEY_sign_init_ex2()   [ instance and prehash assumed done by caller ]
1045
 * - EVP_PKEY_verify_init_ex2() [ instance and prehash assumed done by caller ]
1046
 * - EVP_PKEY_sign_message_init()
1047
 * - EVP_PKEY_verify_message_init()
1048
 * - EVP_DigestSignInit_ex()
1049
 * - EVP_DigestVerifyInit_ex()
1050
 * Ed25519ph can be used with:
1051
 * - EVP_PKEY_sign_init_ex2()   [ prehash assumed done by caller ]
1052
 * - EVP_PKEY_verify_init_ex2() [ prehash assumed done by caller ]
1053
 * - EVP_PKEY_sign_message_init()
1054
 * - EVP_PKEY_verify_message_init()
1055
 * Ed25519ctx can be used with:
1056
 * - EVP_PKEY_sign_message_init()
1057
 * - EVP_PKEY_verify_message_init()
1058
 * Ed448 can be used with:
1059
 * - EVP_PKEY_sign_init_ex2()   [ instance and prehash assumed done by caller ]
1060
 * - EVP_PKEY_verify_init_ex2() [ instance and prehash assumed done by caller ]
1061
 * - EVP_PKEY_sign_message_init()
1062
 * - EVP_PKEY_verify_message_init()
1063
 * - EVP_DigestSignInit_ex()
1064
 * - EVP_DigestVerifyInit_ex()
1065
 * Ed448ph can be used with:
1066
 * - EVP_PKEY_sign_init_ex2()   [ prehash assumed done by caller ]
1067
 * - EVP_PKEY_verify_init_ex2() [ prehash assumed done by caller ]
1068
 * - EVP_PKEY_sign_message_init()
1069
 * - EVP_PKEY_verify_message_init()
1070
 */
1071
1072
#define ed25519_DISPATCH_END                                            \
1073
    { OSSL_FUNC_SIGNATURE_SIGN_INIT,                                    \
1074
        (void (*)(void))ed25519_signverify_init },                      \
1075
    { OSSL_FUNC_SIGNATURE_VERIFY_INIT,                                  \
1076
        (void (*)(void))ed25519_signverify_init },                      \
1077
    { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,                             \
1078
        (void (*)(void))ed25519_digest_signverify_init },               \
1079
    { OSSL_FUNC_SIGNATURE_DIGEST_SIGN,                                  \
1080
        (void (*)(void))ed25519_digest_sign },                          \
1081
    { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,                           \
1082
        (void (*)(void))ed25519_digest_signverify_init },               \
1083
    { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY,                                \
1084
        (void (*)(void))ed25519_digest_verify },                        \
1085
    { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS,                               \
1086
        (void (*)(void))eddsa_get_ctx_params },                         \
1087
    { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,                          \
1088
        (void (*)(void))eddsa_gettable_ctx_params },                    \
1089
    { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS,                               \
1090
        (void (*)(void))eddsa_set_ctx_params },                         \
1091
    { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,                          \
1092
        (void (*)(void))eddsa_settable_ctx_params },                    \
1093
    OSSL_DISPATCH_END
1094
1095
#define eddsa_variant_DISPATCH_END(v)                                   \
1096
    { OSSL_FUNC_SIGNATURE_SIGN_INIT,                                    \
1097
        (void (*)(void))v##_signverify_message_init },                  \
1098
    { OSSL_FUNC_SIGNATURE_VERIFY_INIT,                                  \
1099
        (void (*)(void))v##_signverify_message_init },                  \
1100
    { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS,                               \
1101
        (void (*)(void))eddsa_get_ctx_params },                         \
1102
    { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,                          \
1103
        (void (*)(void))eddsa_gettable_ctx_params },                    \
1104
    { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS,                               \
1105
        (void (*)(void))eddsa_set_variant_ctx_params },                 \
1106
    { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,                          \
1107
        (void (*)(void))eddsa_settable_variant_ctx_params },            \
1108
    OSSL_DISPATCH_END
1109
1110
#define ed25519ph_DISPATCH_END                                          \
1111
    { OSSL_FUNC_SIGNATURE_SIGN_INIT,                                    \
1112
        (void (*)(void))ed25519ph_signverify_init },                    \
1113
    { OSSL_FUNC_SIGNATURE_VERIFY_INIT,                                  \
1114
        (void (*)(void))ed25519ph_signverify_init },                    \
1115
    eddsa_variant_DISPATCH_END(ed25519ph)
1116
1117
#define ed25519ctx_DISPATCH_END eddsa_variant_DISPATCH_END(ed25519ctx)
1118
1119
#define ed448_DISPATCH_END                                              \
1120
    { OSSL_FUNC_SIGNATURE_SIGN_INIT,                                    \
1121
        (void (*)(void))ed448_signverify_init },                        \
1122
    { OSSL_FUNC_SIGNATURE_VERIFY_INIT,                                  \
1123
        (void (*)(void))ed448_signverify_init },                        \
1124
    { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,                             \
1125
        (void (*)(void))ed448_digest_signverify_init },                 \
1126
    { OSSL_FUNC_SIGNATURE_DIGEST_SIGN,                                  \
1127
        (void (*)(void))ed448_digest_sign },                            \
1128
    { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,                           \
1129
        (void (*)(void))ed448_digest_signverify_init },                 \
1130
    { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY,                                \
1131
        (void (*)(void))ed448_digest_verify },                          \
1132
    { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS,                               \
1133
        (void (*)(void))eddsa_get_ctx_params },                         \
1134
    { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,                          \
1135
        (void (*)(void))eddsa_gettable_ctx_params },                    \
1136
    { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS,                               \
1137
        (void (*)(void))eddsa_set_ctx_params },                         \
1138
    { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,                          \
1139
        (void (*)(void))eddsa_settable_ctx_params },                    \
1140
    OSSL_DISPATCH_END
1141
1142
#define ed448ph_DISPATCH_END                                            \
1143
    { OSSL_FUNC_SIGNATURE_SIGN_INIT,                                    \
1144
        (void (*)(void))ed448ph_signverify_init },                      \
1145
    { OSSL_FUNC_SIGNATURE_VERIFY_INIT,                                  \
1146
        (void (*)(void))ed448ph_signverify_init },                      \
1147
    eddsa_variant_DISPATCH_END(ed448ph)
1148
1149
/* vn = variant name, bn = base name */
1150
#define IMPL_EDDSA_DISPATCH(vn,bn)                                      \
1151
    const OSSL_DISPATCH ossl_##vn##_signature_functions[] = {           \
1152
        { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))eddsa_newctx },   \
1153
        { OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_INIT,                        \
1154
          (void (*)(void))vn##_signverify_message_init },               \
1155
        { OSSL_FUNC_SIGNATURE_SIGN,                                     \
1156
          (void (*)(void))bn##_sign },                                  \
1157
        { OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_INIT,                      \
1158
          (void (*)(void))vn##_signverify_message_init },               \
1159
        { OSSL_FUNC_SIGNATURE_VERIFY,                                   \
1160
          (void (*)(void))bn##_verify },                                \
1161
        { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))eddsa_freectx }, \
1162
        { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))eddsa_dupctx },   \
1163
        { OSSL_FUNC_SIGNATURE_QUERY_KEY_TYPES,                          \
1164
          (void (*)(void))bn##_sigalg_query_key_types },                \
1165
        vn##_DISPATCH_END                                               \
1166
    }
1167
1168
IMPL_EDDSA_DISPATCH(ed25519,ed25519);
1169
IMPL_EDDSA_DISPATCH(ed25519ph,ed25519);
1170
IMPL_EDDSA_DISPATCH(ed25519ctx,ed25519);
1171
IMPL_EDDSA_DISPATCH(ed448,ed448);
1172
IMPL_EDDSA_DISPATCH(ed448ph,ed448);
1173
1174
#ifdef S390X_EC_ASM
1175
1176
static int s390x_ed25519_digestsign(const ECX_KEY *edkey, unsigned char *sig,
1177
                                    const unsigned char *tbs, size_t tbslen)
1178
{
1179
    int rc;
1180
    union {
1181
        struct {
1182
            unsigned char sig[64];
1183
            unsigned char priv[32];
1184
        } ed25519;
1185
        unsigned long long buff[512];
1186
    } param;
1187
1188
    memset(&param, 0, sizeof(param));
1189
    memcpy(param.ed25519.priv, edkey->privkey, sizeof(param.ed25519.priv));
1190
1191
    rc = s390x_kdsa(S390X_EDDSA_SIGN_ED25519, &param.ed25519, tbs, tbslen);
1192
    OPENSSL_cleanse(param.ed25519.priv, sizeof(param.ed25519.priv));
1193
    if (rc != 0)
1194
        return 0;
1195
1196
    s390x_flip_endian32(sig, param.ed25519.sig);
1197
    s390x_flip_endian32(sig + 32, param.ed25519.sig + 32);
1198
    return 1;
1199
}
1200
1201
static int s390x_ed448_digestsign(const ECX_KEY *edkey, unsigned char *sig,
1202
                                  const unsigned char *tbs, size_t tbslen)
1203
{
1204
    int rc;
1205
    union {
1206
        struct {
1207
            unsigned char sig[128];
1208
            unsigned char priv[64];
1209
        } ed448;
1210
        unsigned long long buff[512];
1211
    } param;
1212
1213
    memset(&param, 0, sizeof(param));
1214
    memcpy(param.ed448.priv + 64 - 57, edkey->privkey, 57);
1215
1216
    rc = s390x_kdsa(S390X_EDDSA_SIGN_ED448, &param.ed448, tbs, tbslen);
1217
    OPENSSL_cleanse(param.ed448.priv, sizeof(param.ed448.priv));
1218
    if (rc != 0)
1219
        return 0;
1220
1221
    s390x_flip_endian64(param.ed448.sig, param.ed448.sig);
1222
    s390x_flip_endian64(param.ed448.sig + 64, param.ed448.sig + 64);
1223
    memcpy(sig, param.ed448.sig, 57);
1224
    memcpy(sig + 57, param.ed448.sig + 64, 57);
1225
    return 1;
1226
}
1227
1228
static int s390x_ed25519_digestverify(const ECX_KEY *edkey,
1229
                                      const unsigned char *sig,
1230
                                      const unsigned char *tbs, size_t tbslen)
1231
{
1232
    union {
1233
        struct {
1234
            unsigned char sig[64];
1235
            unsigned char pub[32];
1236
        } ed25519;
1237
        unsigned long long buff[512];
1238
    } param;
1239
1240
    memset(&param, 0, sizeof(param));
1241
    s390x_flip_endian32(param.ed25519.sig, sig);
1242
    s390x_flip_endian32(param.ed25519.sig + 32, sig + 32);
1243
    s390x_flip_endian32(param.ed25519.pub, edkey->pubkey);
1244
1245
    return s390x_kdsa(S390X_EDDSA_VERIFY_ED25519,
1246
                      &param.ed25519, tbs, tbslen) == 0 ? 1 : 0;
1247
}
1248
1249
static int s390x_ed448_digestverify(const ECX_KEY *edkey,
1250
                                    const unsigned char *sig,
1251
                                    const unsigned char *tbs,
1252
                                    size_t tbslen)
1253
{
1254
    union {
1255
        struct {
1256
            unsigned char sig[128];
1257
            unsigned char pub[64];
1258
        } ed448;
1259
        unsigned long long buff[512];
1260
    } param;
1261
1262
    memset(&param, 0, sizeof(param));
1263
    memcpy(param.ed448.sig, sig, 57);
1264
    s390x_flip_endian64(param.ed448.sig, param.ed448.sig);
1265
    memcpy(param.ed448.sig + 64, sig + 57, 57);
1266
    s390x_flip_endian64(param.ed448.sig + 64, param.ed448.sig + 64);
1267
    memcpy(param.ed448.pub, edkey->pubkey, 57);
1268
    s390x_flip_endian64(param.ed448.pub, param.ed448.pub);
1269
1270
    return s390x_kdsa(S390X_EDDSA_VERIFY_ED448,
1271
                      &param.ed448, tbs, tbslen) == 0 ? 1 : 0;
1272
}
1273
1274
#endif /* S390X_EC_ASM */