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)
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,
54 const char *props, void *vedkey)
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) == 0) {
91 PROVerr(0, PROV_R_FAILED_TO_SIGN);
94 *siglen = ED25519_SIGSIZE;
98 int ed448_digest_sign(void *vpeddsactx, unsigned char *sigret,
99 size_t *siglen, size_t sigsize,
100 const unsigned char *tbs, size_t tbslen)
102 PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
103 const ECX_KEY *edkey = peddsactx->key;
105 if (sigret == NULL) {
106 *siglen = ED448_SIGSIZE;
109 if (sigsize < ED448_SIGSIZE) {
110 PROVerr(0, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
114 if (ED448_sign(peddsactx->libctx, sigret, tbs, tbslen, edkey->pubkey,
115 edkey->privkey, NULL, 0) == 0) {
116 PROVerr(0, PROV_R_FAILED_TO_SIGN);
119 *siglen = ED448_SIGSIZE;
123 int ed25519_digest_verify(void *vpeddsactx, const unsigned char *sig,
124 size_t siglen, const unsigned char *tbs,
127 PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
128 const ECX_KEY *edkey = peddsactx->key;
130 if (siglen != ED25519_SIGSIZE)
133 return ED25519_verify(tbs, tbslen, sig, edkey->pubkey);
136 int ed448_digest_verify(void *vpeddsactx, const unsigned char *sig,
137 size_t siglen, const unsigned char *tbs,
140 PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
141 const ECX_KEY *edkey = peddsactx->key;
143 if (siglen != ED448_SIGSIZE)
146 return ED448_verify(peddsactx->libctx, tbs, tbslen, sig, edkey->pubkey,
150 static void eddsa_freectx(void *vpeddsactx)
152 PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
154 ecx_key_free(peddsactx->key);
156 OPENSSL_free(peddsactx);
159 static void *eddsa_dupctx(void *vpeddsactx)
161 PROV_EDDSA_CTX *srcctx = (PROV_EDDSA_CTX *)vpeddsactx;
162 PROV_EDDSA_CTX *dstctx;
164 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
171 if (srcctx->key != NULL && !ecx_key_up_ref(srcctx->key)) {
172 PROVerr(0, ERR_R_INTERNAL_ERROR);
175 dstctx->key = srcctx->key;
179 eddsa_freectx(dstctx);
183 const OSSL_DISPATCH ed25519_signature_functions[] = {
184 { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))eddsa_newctx },
185 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
186 (void (*)(void))eddsa_digest_signverify_init },
187 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN,
188 (void (*)(void))ed25519_digest_sign },
189 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
190 (void (*)(void))eddsa_digest_signverify_init },
191 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY,
192 (void (*)(void))ed25519_digest_verify },
193 { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))eddsa_freectx },
194 { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))eddsa_dupctx },
198 const OSSL_DISPATCH ed448_signature_functions[] = {
199 { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))eddsa_newctx },
200 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
201 (void (*)(void))eddsa_digest_signverify_init },
202 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN,
203 (void (*)(void))ed448_digest_sign },
204 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
205 (void (*)(void))eddsa_digest_signverify_init },
206 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY,
207 (void (*)(void))ed448_digest_verify },
208 { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))eddsa_freectx },
209 { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))eddsa_dupctx },