]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/implementations/signature/eddsa.c
Rename <openssl/core_numbers.h> -> <openssl/core_dispatch.h>
[thirdparty/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_dispatch.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, const char *propq_unused)
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 void *vedkey)
55 {
56 PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
57 ECX_KEY *edkey = (ECX_KEY *)vedkey;
58
59 if (mdname != NULL && mdname[0] != '\0') {
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,
91 peddsactx->libctx, NULL) == 0) {
92 PROVerr(0, PROV_R_FAILED_TO_SIGN);
93 return 0;
94 }
95 *siglen = ED25519_SIGSIZE;
96 return 1;
97 }
98
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)
102 {
103 PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
104 const ECX_KEY *edkey = peddsactx->key;
105
106 if (sigret == NULL) {
107 *siglen = ED448_SIGSIZE;
108 return 1;
109 }
110 if (sigsize < ED448_SIGSIZE) {
111 PROVerr(0, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
112 return 0;
113 }
114
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);
118 return 0;
119 }
120 *siglen = ED448_SIGSIZE;
121 return 1;
122 }
123
124 int ed25519_digest_verify(void *vpeddsactx, const unsigned char *sig,
125 size_t siglen, const unsigned char *tbs,
126 size_t tbslen)
127 {
128 PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
129 const ECX_KEY *edkey = peddsactx->key;
130
131 if (siglen != ED25519_SIGSIZE)
132 return 0;
133
134 return ED25519_verify(tbs, tbslen, sig, edkey->pubkey, peddsactx->libctx,
135 NULL);
136 }
137
138 int ed448_digest_verify(void *vpeddsactx, const unsigned char *sig,
139 size_t siglen, const unsigned char *tbs,
140 size_t tbslen)
141 {
142 PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
143 const ECX_KEY *edkey = peddsactx->key;
144
145 if (siglen != ED448_SIGSIZE)
146 return 0;
147
148 return ED448_verify(peddsactx->libctx, tbs, tbslen, sig, edkey->pubkey,
149 NULL, 0);
150 }
151
152 static void eddsa_freectx(void *vpeddsactx)
153 {
154 PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
155
156 ecx_key_free(peddsactx->key);
157
158 OPENSSL_free(peddsactx);
159 }
160
161 static void *eddsa_dupctx(void *vpeddsactx)
162 {
163 PROV_EDDSA_CTX *srcctx = (PROV_EDDSA_CTX *)vpeddsactx;
164 PROV_EDDSA_CTX *dstctx;
165
166 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
167 if (dstctx == NULL)
168 return NULL;
169
170 *dstctx = *srcctx;
171 dstctx->key = NULL;
172
173 if (srcctx->key != NULL && !ecx_key_up_ref(srcctx->key)) {
174 PROVerr(0, ERR_R_INTERNAL_ERROR);
175 goto err;
176 }
177 dstctx->key = srcctx->key;
178
179 return dstctx;
180 err:
181 eddsa_freectx(dstctx);
182 return NULL;
183 }
184
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 },
197 { 0, NULL }
198 };
199
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 },
212 { 0, NULL }
213 };