1*f374ba41SEd Maste /* $OpenBSD: ssh-ed25519.c,v 1.19 2022/10/28 00:44:44 djm Exp $ */
2f7167e0eSDag-Erling Smørgrav /*
3f7167e0eSDag-Erling Smørgrav * Copyright (c) 2013 Markus Friedl <markus@openbsd.org>
4f7167e0eSDag-Erling Smørgrav *
5f7167e0eSDag-Erling Smørgrav * Permission to use, copy, modify, and distribute this software for any
6f7167e0eSDag-Erling Smørgrav * purpose with or without fee is hereby granted, provided that the above
7f7167e0eSDag-Erling Smørgrav * copyright notice and this permission notice appear in all copies.
8f7167e0eSDag-Erling Smørgrav *
9f7167e0eSDag-Erling Smørgrav * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10f7167e0eSDag-Erling Smørgrav * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11f7167e0eSDag-Erling Smørgrav * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12f7167e0eSDag-Erling Smørgrav * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13f7167e0eSDag-Erling Smørgrav * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14f7167e0eSDag-Erling Smørgrav * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15f7167e0eSDag-Erling Smørgrav * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16f7167e0eSDag-Erling Smørgrav */
17f7167e0eSDag-Erling Smørgrav
18f7167e0eSDag-Erling Smørgrav #include "includes.h"
19f7167e0eSDag-Erling Smørgrav
20f7167e0eSDag-Erling Smørgrav #include <sys/types.h>
21a0ee8cc6SDag-Erling Smørgrav #include <limits.h>
22f7167e0eSDag-Erling Smørgrav
23f7167e0eSDag-Erling Smørgrav #include "crypto_api.h"
24f7167e0eSDag-Erling Smørgrav
25f7167e0eSDag-Erling Smørgrav #include <string.h>
26f7167e0eSDag-Erling Smørgrav #include <stdarg.h>
27f7167e0eSDag-Erling Smørgrav
28f7167e0eSDag-Erling Smørgrav #include "log.h"
29bc5531deSDag-Erling Smørgrav #include "sshbuf.h"
30a0ee8cc6SDag-Erling Smørgrav #define SSHKEY_INTERNAL
31a0ee8cc6SDag-Erling Smørgrav #include "sshkey.h"
32a0ee8cc6SDag-Erling Smørgrav #include "ssherr.h"
33f7167e0eSDag-Erling Smørgrav #include "ssh.h"
34f7167e0eSDag-Erling Smørgrav
35*f374ba41SEd Maste static void
ssh_ed25519_cleanup(struct sshkey * k)36*f374ba41SEd Maste ssh_ed25519_cleanup(struct sshkey *k)
37*f374ba41SEd Maste {
38*f374ba41SEd Maste freezero(k->ed25519_pk, ED25519_PK_SZ);
39*f374ba41SEd Maste freezero(k->ed25519_sk, ED25519_SK_SZ);
40*f374ba41SEd Maste k->ed25519_pk = NULL;
41*f374ba41SEd Maste k->ed25519_sk = NULL;
42*f374ba41SEd Maste }
43*f374ba41SEd Maste
44*f374ba41SEd Maste static int
ssh_ed25519_equal(const struct sshkey * a,const struct sshkey * b)45*f374ba41SEd Maste ssh_ed25519_equal(const struct sshkey *a, const struct sshkey *b)
46*f374ba41SEd Maste {
47*f374ba41SEd Maste if (a->ed25519_pk == NULL || b->ed25519_pk == NULL)
48*f374ba41SEd Maste return 0;
49*f374ba41SEd Maste if (memcmp(a->ed25519_pk, b->ed25519_pk, ED25519_PK_SZ) != 0)
50*f374ba41SEd Maste return 0;
51*f374ba41SEd Maste return 1;
52*f374ba41SEd Maste }
53*f374ba41SEd Maste
54*f374ba41SEd Maste static int
ssh_ed25519_serialize_public(const struct sshkey * key,struct sshbuf * b,enum sshkey_serialize_rep opts)55*f374ba41SEd Maste ssh_ed25519_serialize_public(const struct sshkey *key, struct sshbuf *b,
56*f374ba41SEd Maste enum sshkey_serialize_rep opts)
57*f374ba41SEd Maste {
58*f374ba41SEd Maste int r;
59*f374ba41SEd Maste
60*f374ba41SEd Maste if (key->ed25519_pk == NULL)
61*f374ba41SEd Maste return SSH_ERR_INVALID_ARGUMENT;
62*f374ba41SEd Maste if ((r = sshbuf_put_string(b, key->ed25519_pk, ED25519_PK_SZ)) != 0)
63*f374ba41SEd Maste return r;
64*f374ba41SEd Maste
65*f374ba41SEd Maste return 0;
66*f374ba41SEd Maste }
67*f374ba41SEd Maste
68*f374ba41SEd Maste static int
ssh_ed25519_serialize_private(const struct sshkey * key,struct sshbuf * b,enum sshkey_serialize_rep opts)69*f374ba41SEd Maste ssh_ed25519_serialize_private(const struct sshkey *key, struct sshbuf *b,
70*f374ba41SEd Maste enum sshkey_serialize_rep opts)
71*f374ba41SEd Maste {
72*f374ba41SEd Maste int r;
73*f374ba41SEd Maste
74*f374ba41SEd Maste if ((r = sshbuf_put_string(b, key->ed25519_pk, ED25519_PK_SZ)) != 0 ||
75*f374ba41SEd Maste (r = sshbuf_put_string(b, key->ed25519_sk, ED25519_SK_SZ)) != 0)
76*f374ba41SEd Maste return r;
77*f374ba41SEd Maste
78*f374ba41SEd Maste return 0;
79*f374ba41SEd Maste }
80*f374ba41SEd Maste
81*f374ba41SEd Maste static int
ssh_ed25519_generate(struct sshkey * k,int bits)82*f374ba41SEd Maste ssh_ed25519_generate(struct sshkey *k, int bits)
83*f374ba41SEd Maste {
84*f374ba41SEd Maste if ((k->ed25519_pk = malloc(ED25519_PK_SZ)) == NULL ||
85*f374ba41SEd Maste (k->ed25519_sk = malloc(ED25519_SK_SZ)) == NULL)
86*f374ba41SEd Maste return SSH_ERR_ALLOC_FAIL;
87*f374ba41SEd Maste crypto_sign_ed25519_keypair(k->ed25519_pk, k->ed25519_sk);
88*f374ba41SEd Maste return 0;
89*f374ba41SEd Maste }
90*f374ba41SEd Maste
91*f374ba41SEd Maste static int
ssh_ed25519_copy_public(const struct sshkey * from,struct sshkey * to)92*f374ba41SEd Maste ssh_ed25519_copy_public(const struct sshkey *from, struct sshkey *to)
93*f374ba41SEd Maste {
94*f374ba41SEd Maste if (from->ed25519_pk == NULL)
95*f374ba41SEd Maste return 0; /* XXX SSH_ERR_INTERNAL_ERROR ? */
96*f374ba41SEd Maste if ((to->ed25519_pk = malloc(ED25519_PK_SZ)) == NULL)
97*f374ba41SEd Maste return SSH_ERR_ALLOC_FAIL;
98*f374ba41SEd Maste memcpy(to->ed25519_pk, from->ed25519_pk, ED25519_PK_SZ);
99*f374ba41SEd Maste return 0;
100*f374ba41SEd Maste }
101*f374ba41SEd Maste
102*f374ba41SEd Maste static int
ssh_ed25519_deserialize_public(const char * ktype,struct sshbuf * b,struct sshkey * key)103*f374ba41SEd Maste ssh_ed25519_deserialize_public(const char *ktype, struct sshbuf *b,
104*f374ba41SEd Maste struct sshkey *key)
105*f374ba41SEd Maste {
106*f374ba41SEd Maste u_char *pk = NULL;
107*f374ba41SEd Maste size_t len = 0;
108*f374ba41SEd Maste int r;
109*f374ba41SEd Maste
110*f374ba41SEd Maste if ((r = sshbuf_get_string(b, &pk, &len)) != 0)
111*f374ba41SEd Maste return r;
112*f374ba41SEd Maste if (len != ED25519_PK_SZ) {
113*f374ba41SEd Maste freezero(pk, len);
114*f374ba41SEd Maste return SSH_ERR_INVALID_FORMAT;
115*f374ba41SEd Maste }
116*f374ba41SEd Maste key->ed25519_pk = pk;
117*f374ba41SEd Maste return 0;
118*f374ba41SEd Maste }
119*f374ba41SEd Maste
120*f374ba41SEd Maste static int
ssh_ed25519_deserialize_private(const char * ktype,struct sshbuf * b,struct sshkey * key)121*f374ba41SEd Maste ssh_ed25519_deserialize_private(const char *ktype, struct sshbuf *b,
122*f374ba41SEd Maste struct sshkey *key)
123*f374ba41SEd Maste {
124*f374ba41SEd Maste int r;
125*f374ba41SEd Maste size_t sklen = 0;
126*f374ba41SEd Maste u_char *ed25519_sk = NULL;
127*f374ba41SEd Maste
128*f374ba41SEd Maste if ((r = ssh_ed25519_deserialize_public(NULL, b, key)) != 0)
129*f374ba41SEd Maste goto out;
130*f374ba41SEd Maste if ((r = sshbuf_get_string(b, &ed25519_sk, &sklen)) != 0)
131*f374ba41SEd Maste goto out;
132*f374ba41SEd Maste if (sklen != ED25519_SK_SZ) {
133*f374ba41SEd Maste r = SSH_ERR_INVALID_FORMAT;
134*f374ba41SEd Maste goto out;
135*f374ba41SEd Maste }
136*f374ba41SEd Maste key->ed25519_sk = ed25519_sk;
137*f374ba41SEd Maste ed25519_sk = NULL; /* transferred */
138*f374ba41SEd Maste /* success */
139*f374ba41SEd Maste r = 0;
140*f374ba41SEd Maste out:
141*f374ba41SEd Maste freezero(ed25519_sk, sklen);
142*f374ba41SEd Maste return r;
143*f374ba41SEd Maste }
144*f374ba41SEd Maste
145*f374ba41SEd Maste static int
ssh_ed25519_sign(struct sshkey * key,u_char ** sigp,size_t * lenp,const u_char * data,size_t datalen,const char * alg,const char * sk_provider,const char * sk_pin,u_int compat)146*f374ba41SEd Maste ssh_ed25519_sign(struct sshkey *key,
147*f374ba41SEd Maste u_char **sigp, size_t *lenp,
148*f374ba41SEd Maste const u_char *data, size_t datalen,
149*f374ba41SEd Maste const char *alg, const char *sk_provider, const char *sk_pin, u_int compat)
150f7167e0eSDag-Erling Smørgrav {
151a0ee8cc6SDag-Erling Smørgrav u_char *sig = NULL;
152a0ee8cc6SDag-Erling Smørgrav size_t slen = 0, len;
153f7167e0eSDag-Erling Smørgrav unsigned long long smlen;
154a0ee8cc6SDag-Erling Smørgrav int r, ret;
155a0ee8cc6SDag-Erling Smørgrav struct sshbuf *b = NULL;
156f7167e0eSDag-Erling Smørgrav
157a0ee8cc6SDag-Erling Smørgrav if (lenp != NULL)
158a0ee8cc6SDag-Erling Smørgrav *lenp = 0;
159a0ee8cc6SDag-Erling Smørgrav if (sigp != NULL)
160a0ee8cc6SDag-Erling Smørgrav *sigp = NULL;
161b83788ffSDag-Erling Smørgrav
162a0ee8cc6SDag-Erling Smørgrav if (key == NULL ||
163a0ee8cc6SDag-Erling Smørgrav sshkey_type_plain(key->type) != KEY_ED25519 ||
164a0ee8cc6SDag-Erling Smørgrav key->ed25519_sk == NULL ||
165a0ee8cc6SDag-Erling Smørgrav datalen >= INT_MAX - crypto_sign_ed25519_BYTES)
166a0ee8cc6SDag-Erling Smørgrav return SSH_ERR_INVALID_ARGUMENT;
167f7167e0eSDag-Erling Smørgrav smlen = slen = datalen + crypto_sign_ed25519_BYTES;
168a0ee8cc6SDag-Erling Smørgrav if ((sig = malloc(slen)) == NULL)
169a0ee8cc6SDag-Erling Smørgrav return SSH_ERR_ALLOC_FAIL;
170f7167e0eSDag-Erling Smørgrav
171f7167e0eSDag-Erling Smørgrav if ((ret = crypto_sign_ed25519(sig, &smlen, data, datalen,
172f7167e0eSDag-Erling Smørgrav key->ed25519_sk)) != 0 || smlen <= datalen) {
173a0ee8cc6SDag-Erling Smørgrav r = SSH_ERR_INVALID_ARGUMENT; /* XXX better error? */
174a0ee8cc6SDag-Erling Smørgrav goto out;
175f7167e0eSDag-Erling Smørgrav }
176f7167e0eSDag-Erling Smørgrav /* encode signature */
177a0ee8cc6SDag-Erling Smørgrav if ((b = sshbuf_new()) == NULL) {
178a0ee8cc6SDag-Erling Smørgrav r = SSH_ERR_ALLOC_FAIL;
179a0ee8cc6SDag-Erling Smørgrav goto out;
180a0ee8cc6SDag-Erling Smørgrav }
181a0ee8cc6SDag-Erling Smørgrav if ((r = sshbuf_put_cstring(b, "ssh-ed25519")) != 0 ||
182a0ee8cc6SDag-Erling Smørgrav (r = sshbuf_put_string(b, sig, smlen - datalen)) != 0)
183a0ee8cc6SDag-Erling Smørgrav goto out;
184a0ee8cc6SDag-Erling Smørgrav len = sshbuf_len(b);
185a0ee8cc6SDag-Erling Smørgrav if (sigp != NULL) {
186a0ee8cc6SDag-Erling Smørgrav if ((*sigp = malloc(len)) == NULL) {
187a0ee8cc6SDag-Erling Smørgrav r = SSH_ERR_ALLOC_FAIL;
188a0ee8cc6SDag-Erling Smørgrav goto out;
189a0ee8cc6SDag-Erling Smørgrav }
190a0ee8cc6SDag-Erling Smørgrav memcpy(*sigp, sshbuf_ptr(b), len);
191a0ee8cc6SDag-Erling Smørgrav }
192f7167e0eSDag-Erling Smørgrav if (lenp != NULL)
193f7167e0eSDag-Erling Smørgrav *lenp = len;
194a0ee8cc6SDag-Erling Smørgrav /* success */
195a0ee8cc6SDag-Erling Smørgrav r = 0;
196a0ee8cc6SDag-Erling Smørgrav out:
197a0ee8cc6SDag-Erling Smørgrav sshbuf_free(b);
19819261079SEd Maste if (sig != NULL)
19919261079SEd Maste freezero(sig, slen);
200f7167e0eSDag-Erling Smørgrav
201a0ee8cc6SDag-Erling Smørgrav return r;
202f7167e0eSDag-Erling Smørgrav }
203f7167e0eSDag-Erling Smørgrav
204*f374ba41SEd Maste static int
ssh_ed25519_verify(const struct sshkey * key,const u_char * sig,size_t siglen,const u_char * data,size_t dlen,const char * alg,u_int compat,struct sshkey_sig_details ** detailsp)205a0ee8cc6SDag-Erling Smørgrav ssh_ed25519_verify(const struct sshkey *key,
206*f374ba41SEd Maste const u_char *sig, size_t siglen,
207*f374ba41SEd Maste const u_char *data, size_t dlen, const char *alg, u_int compat,
208*f374ba41SEd Maste struct sshkey_sig_details **detailsp)
209f7167e0eSDag-Erling Smørgrav {
210a0ee8cc6SDag-Erling Smørgrav struct sshbuf *b = NULL;
211a0ee8cc6SDag-Erling Smørgrav char *ktype = NULL;
212a0ee8cc6SDag-Erling Smørgrav const u_char *sigblob;
213a0ee8cc6SDag-Erling Smørgrav u_char *sm = NULL, *m = NULL;
214a0ee8cc6SDag-Erling Smørgrav size_t len;
215a0ee8cc6SDag-Erling Smørgrav unsigned long long smlen = 0, mlen = 0;
216a0ee8cc6SDag-Erling Smørgrav int r, ret;
217f7167e0eSDag-Erling Smørgrav
218a0ee8cc6SDag-Erling Smørgrav if (key == NULL ||
219a0ee8cc6SDag-Erling Smørgrav sshkey_type_plain(key->type) != KEY_ED25519 ||
220a0ee8cc6SDag-Erling Smørgrav key->ed25519_pk == NULL ||
221*f374ba41SEd Maste dlen >= INT_MAX - crypto_sign_ed25519_BYTES ||
222*f374ba41SEd Maste sig == NULL || siglen == 0)
223a0ee8cc6SDag-Erling Smørgrav return SSH_ERR_INVALID_ARGUMENT;
224a0ee8cc6SDag-Erling Smørgrav
225*f374ba41SEd Maste if ((b = sshbuf_from(sig, siglen)) == NULL)
226a0ee8cc6SDag-Erling Smørgrav return SSH_ERR_ALLOC_FAIL;
227a0ee8cc6SDag-Erling Smørgrav if ((r = sshbuf_get_cstring(b, &ktype, NULL)) != 0 ||
228a0ee8cc6SDag-Erling Smørgrav (r = sshbuf_get_string_direct(b, &sigblob, &len)) != 0)
229a0ee8cc6SDag-Erling Smørgrav goto out;
230f7167e0eSDag-Erling Smørgrav if (strcmp("ssh-ed25519", ktype) != 0) {
231a0ee8cc6SDag-Erling Smørgrav r = SSH_ERR_KEY_TYPE_MISMATCH;
232a0ee8cc6SDag-Erling Smørgrav goto out;
233f7167e0eSDag-Erling Smørgrav }
234a0ee8cc6SDag-Erling Smørgrav if (sshbuf_len(b) != 0) {
235a0ee8cc6SDag-Erling Smørgrav r = SSH_ERR_UNEXPECTED_TRAILING_DATA;
236a0ee8cc6SDag-Erling Smørgrav goto out;
237f7167e0eSDag-Erling Smørgrav }
238f7167e0eSDag-Erling Smørgrav if (len > crypto_sign_ed25519_BYTES) {
239a0ee8cc6SDag-Erling Smørgrav r = SSH_ERR_INVALID_FORMAT;
240a0ee8cc6SDag-Erling Smørgrav goto out;
241f7167e0eSDag-Erling Smørgrav }
242*f374ba41SEd Maste if (dlen >= SIZE_MAX - len) {
243bc5531deSDag-Erling Smørgrav r = SSH_ERR_INVALID_ARGUMENT;
244bc5531deSDag-Erling Smørgrav goto out;
245bc5531deSDag-Erling Smørgrav }
246*f374ba41SEd Maste smlen = len + dlen;
247a0ee8cc6SDag-Erling Smørgrav mlen = smlen;
248bc5531deSDag-Erling Smørgrav if ((sm = malloc(smlen)) == NULL || (m = malloc(mlen)) == NULL) {
249a0ee8cc6SDag-Erling Smørgrav r = SSH_ERR_ALLOC_FAIL;
250a0ee8cc6SDag-Erling Smørgrav goto out;
251a0ee8cc6SDag-Erling Smørgrav }
252f7167e0eSDag-Erling Smørgrav memcpy(sm, sigblob, len);
253*f374ba41SEd Maste memcpy(sm+len, data, dlen);
254f7167e0eSDag-Erling Smørgrav if ((ret = crypto_sign_ed25519_open(m, &mlen, sm, smlen,
255f7167e0eSDag-Erling Smørgrav key->ed25519_pk)) != 0) {
25619261079SEd Maste debug2_f("crypto_sign_ed25519_open failed: %d", ret);
257f7167e0eSDag-Erling Smørgrav }
258*f374ba41SEd Maste if (ret != 0 || mlen != dlen) {
259a0ee8cc6SDag-Erling Smørgrav r = SSH_ERR_SIGNATURE_INVALID;
260a0ee8cc6SDag-Erling Smørgrav goto out;
261f7167e0eSDag-Erling Smørgrav }
262f7167e0eSDag-Erling Smørgrav /* XXX compare 'm' and 'data' ? */
263a0ee8cc6SDag-Erling Smørgrav /* success */
264a0ee8cc6SDag-Erling Smørgrav r = 0;
265a0ee8cc6SDag-Erling Smørgrav out:
26619261079SEd Maste if (sm != NULL)
26719261079SEd Maste freezero(sm, smlen);
26819261079SEd Maste if (m != NULL)
26919261079SEd Maste freezero(m, smlen); /* NB mlen may be invalid if r != 0 */
270a0ee8cc6SDag-Erling Smørgrav sshbuf_free(b);
271a0ee8cc6SDag-Erling Smørgrav free(ktype);
272a0ee8cc6SDag-Erling Smørgrav return r;
273a0ee8cc6SDag-Erling Smørgrav }
274*f374ba41SEd Maste
275*f374ba41SEd Maste /* NB. not static; used by ED25519-SK */
276*f374ba41SEd Maste const struct sshkey_impl_funcs sshkey_ed25519_funcs = {
277*f374ba41SEd Maste /* .size = */ NULL,
278*f374ba41SEd Maste /* .alloc = */ NULL,
279*f374ba41SEd Maste /* .cleanup = */ ssh_ed25519_cleanup,
280*f374ba41SEd Maste /* .equal = */ ssh_ed25519_equal,
281*f374ba41SEd Maste /* .ssh_serialize_public = */ ssh_ed25519_serialize_public,
282*f374ba41SEd Maste /* .ssh_deserialize_public = */ ssh_ed25519_deserialize_public,
283*f374ba41SEd Maste /* .ssh_serialize_private = */ ssh_ed25519_serialize_private,
284*f374ba41SEd Maste /* .ssh_deserialize_private = */ ssh_ed25519_deserialize_private,
285*f374ba41SEd Maste /* .generate = */ ssh_ed25519_generate,
286*f374ba41SEd Maste /* .copy_public = */ ssh_ed25519_copy_public,
287*f374ba41SEd Maste /* .sign = */ ssh_ed25519_sign,
288*f374ba41SEd Maste /* .verify = */ ssh_ed25519_verify,
289*f374ba41SEd Maste };
290*f374ba41SEd Maste
291*f374ba41SEd Maste const struct sshkey_impl sshkey_ed25519_impl = {
292*f374ba41SEd Maste /* .name = */ "ssh-ed25519",
293*f374ba41SEd Maste /* .shortname = */ "ED25519",
294*f374ba41SEd Maste /* .sigalg = */ NULL,
295*f374ba41SEd Maste /* .type = */ KEY_ED25519,
296*f374ba41SEd Maste /* .nid = */ 0,
297*f374ba41SEd Maste /* .cert = */ 0,
298*f374ba41SEd Maste /* .sigonly = */ 0,
299*f374ba41SEd Maste /* .keybits = */ 256,
300*f374ba41SEd Maste /* .funcs = */ &sshkey_ed25519_funcs,
301*f374ba41SEd Maste };
302*f374ba41SEd Maste
303*f374ba41SEd Maste const struct sshkey_impl sshkey_ed25519_cert_impl = {
304*f374ba41SEd Maste /* .name = */ "ssh-ed25519-cert-v01@openssh.com",
305*f374ba41SEd Maste /* .shortname = */ "ED25519-CERT",
306*f374ba41SEd Maste /* .sigalg = */ NULL,
307*f374ba41SEd Maste /* .type = */ KEY_ED25519_CERT,
308*f374ba41SEd Maste /* .nid = */ 0,
309*f374ba41SEd Maste /* .cert = */ 1,
310*f374ba41SEd Maste /* .sigonly = */ 0,
311*f374ba41SEd Maste /* .keybits = */ 256,
312*f374ba41SEd Maste /* .funcs = */ &sshkey_ed25519_funcs,
313*f374ba41SEd Maste };
314