2 * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/crypto.h>
11 #include <openssl/core_numbers.h>
12 #include <openssl/core_names.h>
13 #include <openssl/err.h>
14 #include <openssl/params.h>
15 #include <openssl/evp.h>
16 #include <openssl/err.h>
17 #include "internal/nelem.h"
18 #include "internal/sizes.h"
19 #include "prov/providercommonerr.h"
20 #include "prov/implementations.h"
21 #include "prov/providercommonerr.h"
22 #include "prov/provider_ctx.h"
23 #include "crypto/ecx.h"
25 static OSSL_OP_signature_newctx_fn eddsa_newctx
;
26 static OSSL_OP_signature_digest_sign_init_fn eddsa_digest_signverify_init
;
27 static OSSL_OP_signature_digest_sign_fn ed25519_digest_sign
;
28 static OSSL_OP_signature_digest_sign_fn ed448_digest_sign
;
29 static OSSL_OP_signature_digest_verify_fn ed25519_digest_verify
;
30 static OSSL_OP_signature_digest_verify_fn ed448_digest_verify
;
31 static OSSL_OP_signature_freectx_fn eddsa_freectx
;
32 static OSSL_OP_signature_dupctx_fn eddsa_dupctx
;
39 static void *eddsa_newctx(void *provctx
, const char *propq_unused
)
41 PROV_EDDSA_CTX
*peddsactx
= OPENSSL_zalloc(sizeof(PROV_EDDSA_CTX
));
43 if (peddsactx
== NULL
) {
44 PROVerr(0, ERR_R_MALLOC_FAILURE
);
48 peddsactx
->libctx
= PROV_LIBRARY_CONTEXT_OF(provctx
);
53 static int eddsa_digest_signverify_init(void *vpeddsactx
, const char *mdname
,
56 PROV_EDDSA_CTX
*peddsactx
= (PROV_EDDSA_CTX
*)vpeddsactx
;
57 ECX_KEY
*edkey
= (ECX_KEY
*)vedkey
;
60 PROVerr(0, PROV_R_INVALID_DIGEST
);
64 if (!ecx_key_up_ref(edkey
)) {
65 PROVerr(0, ERR_R_INTERNAL_ERROR
);
69 peddsactx
->key
= edkey
;
74 int ed25519_digest_sign(void *vpeddsactx
, unsigned char *sigret
,
75 size_t *siglen
, size_t sigsize
,
76 const unsigned char *tbs
, size_t tbslen
)
78 PROV_EDDSA_CTX
*peddsactx
= (PROV_EDDSA_CTX
*)vpeddsactx
;
79 const ECX_KEY
*edkey
= peddsactx
->key
;
82 *siglen
= ED25519_SIGSIZE
;
85 if (sigsize
< ED25519_SIGSIZE
) {
86 PROVerr(0, PROV_R_OUTPUT_BUFFER_TOO_SMALL
);
90 if (ED25519_sign(sigret
, tbs
, tbslen
, edkey
->pubkey
, edkey
->privkey
,
91 peddsactx
->libctx
, NULL
) == 0) {
92 PROVerr(0, PROV_R_FAILED_TO_SIGN
);
95 *siglen
= ED25519_SIGSIZE
;
99 int ed448_digest_sign(void *vpeddsactx
, unsigned char *sigret
,
100 size_t *siglen
, size_t sigsize
,
101 const unsigned char *tbs
, size_t tbslen
)
103 PROV_EDDSA_CTX
*peddsactx
= (PROV_EDDSA_CTX
*)vpeddsactx
;
104 const ECX_KEY
*edkey
= peddsactx
->key
;
106 if (sigret
== NULL
) {
107 *siglen
= ED448_SIGSIZE
;
110 if (sigsize
< ED448_SIGSIZE
) {
111 PROVerr(0, PROV_R_OUTPUT_BUFFER_TOO_SMALL
);
115 if (ED448_sign(peddsactx
->libctx
, sigret
, tbs
, tbslen
, edkey
->pubkey
,
116 edkey
->privkey
, NULL
, 0) == 0) {
117 PROVerr(0, PROV_R_FAILED_TO_SIGN
);
120 *siglen
= ED448_SIGSIZE
;
124 int ed25519_digest_verify(void *vpeddsactx
, const unsigned char *sig
,
125 size_t siglen
, const unsigned char *tbs
,
128 PROV_EDDSA_CTX
*peddsactx
= (PROV_EDDSA_CTX
*)vpeddsactx
;
129 const ECX_KEY
*edkey
= peddsactx
->key
;
131 if (siglen
!= ED25519_SIGSIZE
)
134 return ED25519_verify(tbs
, tbslen
, sig
, edkey
->pubkey
, peddsactx
->libctx
,
138 int ed448_digest_verify(void *vpeddsactx
, const unsigned char *sig
,
139 size_t siglen
, const unsigned char *tbs
,
142 PROV_EDDSA_CTX
*peddsactx
= (PROV_EDDSA_CTX
*)vpeddsactx
;
143 const ECX_KEY
*edkey
= peddsactx
->key
;
145 if (siglen
!= ED448_SIGSIZE
)
148 return ED448_verify(peddsactx
->libctx
, tbs
, tbslen
, sig
, edkey
->pubkey
,
152 static void eddsa_freectx(void *vpeddsactx
)
154 PROV_EDDSA_CTX
*peddsactx
= (PROV_EDDSA_CTX
*)vpeddsactx
;
156 ecx_key_free(peddsactx
->key
);
158 OPENSSL_free(peddsactx
);
161 static void *eddsa_dupctx(void *vpeddsactx
)
163 PROV_EDDSA_CTX
*srcctx
= (PROV_EDDSA_CTX
*)vpeddsactx
;
164 PROV_EDDSA_CTX
*dstctx
;
166 dstctx
= OPENSSL_zalloc(sizeof(*srcctx
));
173 if (srcctx
->key
!= NULL
&& !ecx_key_up_ref(srcctx
->key
)) {
174 PROVerr(0, ERR_R_INTERNAL_ERROR
);
177 dstctx
->key
= srcctx
->key
;
181 eddsa_freectx(dstctx
);
185 const OSSL_DISPATCH ed25519_signature_functions
[] = {
186 { OSSL_FUNC_SIGNATURE_NEWCTX
, (void (*)(void))eddsa_newctx
},
187 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT
,
188 (void (*)(void))eddsa_digest_signverify_init
},
189 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN
,
190 (void (*)(void))ed25519_digest_sign
},
191 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT
,
192 (void (*)(void))eddsa_digest_signverify_init
},
193 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY
,
194 (void (*)(void))ed25519_digest_verify
},
195 { OSSL_FUNC_SIGNATURE_FREECTX
, (void (*)(void))eddsa_freectx
},
196 { OSSL_FUNC_SIGNATURE_DUPCTX
, (void (*)(void))eddsa_dupctx
},
200 const OSSL_DISPATCH ed448_signature_functions
[] = {
201 { OSSL_FUNC_SIGNATURE_NEWCTX
, (void (*)(void))eddsa_newctx
},
202 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT
,
203 (void (*)(void))eddsa_digest_signverify_init
},
204 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN
,
205 (void (*)(void))ed448_digest_sign
},
206 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT
,
207 (void (*)(void))eddsa_digest_signverify_init
},
208 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY
,
209 (void (*)(void))ed448_digest_verify
},
210 { OSSL_FUNC_SIGNATURE_FREECTX
, (void (*)(void))eddsa_freectx
},
211 { OSSL_FUNC_SIGNATURE_DUPCTX
, (void (*)(void))eddsa_dupctx
},