Move legacy ciphers into the legacy provider
[oweals/openssl.git] / providers / implementations / signature / eddsa.c
1 /*
2  * Copyright 2020 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 #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"
24
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;
33
34 typedef struct {
35     OPENSSL_CTX *libctx;
36     ECX_KEY *key;
37 } PROV_EDDSA_CTX;
38
39 static void *eddsa_newctx(void *provctx)
40 {
41     PROV_EDDSA_CTX *peddsactx = OPENSSL_zalloc(sizeof(PROV_EDDSA_CTX));
42
43     if (peddsactx == NULL) {
44         PROVerr(0, ERR_R_MALLOC_FAILURE);
45         return NULL;
46     }
47
48     peddsactx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
49
50     return peddsactx;
51 }
52
53 static int eddsa_digest_signverify_init(void *vpeddsactx, const char *mdname,
54                                         const char *props, void *vedkey)
55 {
56     PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
57     ECX_KEY *edkey = (ECX_KEY *)vedkey;
58
59     if (mdname != NULL) {
60         PROVerr(0, PROV_R_INVALID_DIGEST);
61         return 0;
62     }
63
64     if (!ecx_key_up_ref(edkey)) {
65         PROVerr(0, ERR_R_INTERNAL_ERROR);
66         return 0;
67     }
68
69     peddsactx->key = edkey;
70
71     return 1;
72 }
73
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)
77 {
78     PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
79     const ECX_KEY *edkey = peddsactx->key;
80
81     if (sigret == NULL) {
82         *siglen = ED25519_SIGSIZE;
83         return 1;
84     }
85     if (sigsize < ED25519_SIGSIZE) {
86         PROVerr(0, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
87         return 0;
88     }
89
90     if (ED25519_sign(sigret, tbs, tbslen, edkey->pubkey, edkey->privkey) == 0) {
91         PROVerr(0, PROV_R_FAILED_TO_SIGN);
92         return 0;
93     }
94     *siglen = ED25519_SIGSIZE;
95     return 1;
96 }
97
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)
101 {
102     PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
103     const ECX_KEY *edkey = peddsactx->key;
104
105     if (sigret == NULL) {
106         *siglen = ED448_SIGSIZE;
107         return 1;
108     }
109     if (sigsize < ED448_SIGSIZE) {
110         PROVerr(0, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
111         return 0;
112     }
113
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);
117         return 0;
118     }
119     *siglen = ED448_SIGSIZE;
120     return 1;
121 }
122
123 int ed25519_digest_verify(void *vpeddsactx, const unsigned char *sig,
124                           size_t siglen, const unsigned char *tbs,
125                           size_t tbslen)
126 {
127     PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
128     const ECX_KEY *edkey = peddsactx->key;
129
130     if (siglen != ED25519_SIGSIZE)
131         return 0;
132
133     return ED25519_verify(tbs, tbslen, sig, edkey->pubkey);
134 }
135
136 int ed448_digest_verify(void *vpeddsactx, const unsigned char *sig,
137                         size_t siglen, const unsigned char *tbs,
138                         size_t tbslen)
139 {
140     PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
141     const ECX_KEY *edkey = peddsactx->key;
142
143     if (siglen != ED448_SIGSIZE)
144         return 0;
145
146     return ED448_verify(peddsactx->libctx, tbs, tbslen, sig, edkey->pubkey,
147                         NULL, 0);
148 }
149
150 static void eddsa_freectx(void *vpeddsactx)
151 {
152     PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
153
154     ecx_key_free(peddsactx->key);
155
156     OPENSSL_free(peddsactx);
157 }
158
159 static void *eddsa_dupctx(void *vpeddsactx)
160 {
161     PROV_EDDSA_CTX *srcctx = (PROV_EDDSA_CTX *)vpeddsactx;
162     PROV_EDDSA_CTX *dstctx;
163
164     dstctx = OPENSSL_zalloc(sizeof(*srcctx));
165     if (dstctx == NULL)
166         return NULL;
167
168     *dstctx = *srcctx;
169     dstctx->key = NULL;
170
171     if (srcctx->key != NULL && !ecx_key_up_ref(srcctx->key)) {
172         PROVerr(0, ERR_R_INTERNAL_ERROR);
173         goto err;
174     }
175     dstctx->key = srcctx->key;
176
177     return dstctx;
178  err:
179     eddsa_freectx(dstctx);
180     return NULL;
181 }
182
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 },
195     { 0, NULL }
196 };
197
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 },
210     { 0, NULL }
211 };