xref: /netbsd-src/crypto/external/bsd/openssh/dist/ssh-ed25519.c (revision b1066cf3cd3cc4bc809a7791a10cc5857ad5d501)
1*b1066cf3Schristos /* $OpenBSD: ssh-ed25519.c,v 1.19 2022/10/28 00:44:44 djm Exp $ */
25484a5efSchristos /*
35484a5efSchristos  * Copyright (c) 2013 Markus Friedl <markus@openbsd.org>
45484a5efSchristos  *
55484a5efSchristos  * Permission to use, copy, modify, and distribute this software for any
65484a5efSchristos  * purpose with or without fee is hereby granted, provided that the above
75484a5efSchristos  * copyright notice and this permission notice appear in all copies.
85484a5efSchristos  *
95484a5efSchristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
105484a5efSchristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
115484a5efSchristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
125484a5efSchristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
135484a5efSchristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
145484a5efSchristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
155484a5efSchristos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
165484a5efSchristos  */
175484a5efSchristos #define SSHKEY_INTERNAL
188a4530f9Schristos #include "includes.h"
19*b1066cf3Schristos __RCSID("$NetBSD: ssh-ed25519.c,v 1.10 2023/07/26 17:58:16 christos Exp $");
208a4530f9Schristos 
215484a5efSchristos #include <sys/types.h>
225484a5efSchristos #include <limits.h>
235484a5efSchristos 
245484a5efSchristos #include "crypto_api.h"
255484a5efSchristos 
265484a5efSchristos #include <string.h>
275484a5efSchristos #include <stdarg.h>
285484a5efSchristos 
295484a5efSchristos #include "log.h"
30e4d43b82Schristos #include "sshbuf.h"
315484a5efSchristos #include "sshkey.h"
325484a5efSchristos #include "ssherr.h"
335484a5efSchristos #include "ssh.h"
345484a5efSchristos 
35*b1066cf3Schristos static void
ssh_ed25519_cleanup(struct sshkey * k)36*b1066cf3Schristos ssh_ed25519_cleanup(struct sshkey *k)
37*b1066cf3Schristos {
38*b1066cf3Schristos 	freezero(k->ed25519_pk, ED25519_PK_SZ);
39*b1066cf3Schristos 	freezero(k->ed25519_sk, ED25519_SK_SZ);
40*b1066cf3Schristos 	k->ed25519_pk = NULL;
41*b1066cf3Schristos 	k->ed25519_sk = NULL;
42*b1066cf3Schristos }
43*b1066cf3Schristos 
44*b1066cf3Schristos static int
ssh_ed25519_equal(const struct sshkey * a,const struct sshkey * b)45*b1066cf3Schristos ssh_ed25519_equal(const struct sshkey *a, const struct sshkey *b)
46*b1066cf3Schristos {
47*b1066cf3Schristos 	if (a->ed25519_pk == NULL || b->ed25519_pk == NULL)
48*b1066cf3Schristos 		return 0;
49*b1066cf3Schristos 	if (memcmp(a->ed25519_pk, b->ed25519_pk, ED25519_PK_SZ) != 0)
50*b1066cf3Schristos 		return 0;
51*b1066cf3Schristos 	return 1;
52*b1066cf3Schristos }
53*b1066cf3Schristos 
54*b1066cf3Schristos static int
ssh_ed25519_serialize_public(const struct sshkey * key,struct sshbuf * b,enum sshkey_serialize_rep opts)55*b1066cf3Schristos ssh_ed25519_serialize_public(const struct sshkey *key, struct sshbuf *b,
56*b1066cf3Schristos     enum sshkey_serialize_rep opts)
57*b1066cf3Schristos {
58*b1066cf3Schristos 	int r;
59*b1066cf3Schristos 
60*b1066cf3Schristos 	if (key->ed25519_pk == NULL)
61*b1066cf3Schristos 		return SSH_ERR_INVALID_ARGUMENT;
62*b1066cf3Schristos 	if ((r = sshbuf_put_string(b, key->ed25519_pk, ED25519_PK_SZ)) != 0)
63*b1066cf3Schristos 		return r;
64*b1066cf3Schristos 
65*b1066cf3Schristos 	return 0;
66*b1066cf3Schristos }
67*b1066cf3Schristos 
68*b1066cf3Schristos static int
ssh_ed25519_serialize_private(const struct sshkey * key,struct sshbuf * b,enum sshkey_serialize_rep opts)69*b1066cf3Schristos ssh_ed25519_serialize_private(const struct sshkey *key, struct sshbuf *b,
70*b1066cf3Schristos     enum sshkey_serialize_rep opts)
71*b1066cf3Schristos {
72*b1066cf3Schristos 	int r;
73*b1066cf3Schristos 
74*b1066cf3Schristos 	if ((r = sshbuf_put_string(b, key->ed25519_pk, ED25519_PK_SZ)) != 0 ||
75*b1066cf3Schristos 	    (r = sshbuf_put_string(b, key->ed25519_sk, ED25519_SK_SZ)) != 0)
76*b1066cf3Schristos 		return r;
77*b1066cf3Schristos 
78*b1066cf3Schristos 	return 0;
79*b1066cf3Schristos }
80*b1066cf3Schristos 
81*b1066cf3Schristos static int
ssh_ed25519_generate(struct sshkey * k,int bits)82*b1066cf3Schristos ssh_ed25519_generate(struct sshkey *k, int bits)
83*b1066cf3Schristos {
84*b1066cf3Schristos 	if ((k->ed25519_pk = malloc(ED25519_PK_SZ)) == NULL ||
85*b1066cf3Schristos 	    (k->ed25519_sk = malloc(ED25519_SK_SZ)) == NULL)
86*b1066cf3Schristos 		return SSH_ERR_ALLOC_FAIL;
87*b1066cf3Schristos 	crypto_sign_ed25519_keypair(k->ed25519_pk, k->ed25519_sk);
88*b1066cf3Schristos 	return 0;
89*b1066cf3Schristos }
90*b1066cf3Schristos 
91*b1066cf3Schristos static int
ssh_ed25519_copy_public(const struct sshkey * from,struct sshkey * to)92*b1066cf3Schristos ssh_ed25519_copy_public(const struct sshkey *from, struct sshkey *to)
93*b1066cf3Schristos {
94*b1066cf3Schristos 	if (from->ed25519_pk == NULL)
95*b1066cf3Schristos 		return 0; /* XXX SSH_ERR_INTERNAL_ERROR ? */
96*b1066cf3Schristos 	if ((to->ed25519_pk = malloc(ED25519_PK_SZ)) == NULL)
97*b1066cf3Schristos 		return SSH_ERR_ALLOC_FAIL;
98*b1066cf3Schristos 	memcpy(to->ed25519_pk, from->ed25519_pk, ED25519_PK_SZ);
99*b1066cf3Schristos 	return 0;
100*b1066cf3Schristos }
101*b1066cf3Schristos 
102*b1066cf3Schristos static int
ssh_ed25519_deserialize_public(const char * ktype,struct sshbuf * b,struct sshkey * key)103*b1066cf3Schristos ssh_ed25519_deserialize_public(const char *ktype, struct sshbuf *b,
104*b1066cf3Schristos     struct sshkey *key)
105*b1066cf3Schristos {
106*b1066cf3Schristos 	u_char *pk = NULL;
107*b1066cf3Schristos 	size_t len = 0;
108*b1066cf3Schristos 	int r;
109*b1066cf3Schristos 
110*b1066cf3Schristos 	if ((r = sshbuf_get_string(b, &pk, &len)) != 0)
111*b1066cf3Schristos 		return r;
112*b1066cf3Schristos 	if (len != ED25519_PK_SZ) {
113*b1066cf3Schristos 		freezero(pk, len);
114*b1066cf3Schristos 		return SSH_ERR_INVALID_FORMAT;
115*b1066cf3Schristos 	}
116*b1066cf3Schristos 	key->ed25519_pk = pk;
117*b1066cf3Schristos 	return 0;
118*b1066cf3Schristos }
119*b1066cf3Schristos 
120*b1066cf3Schristos static int
ssh_ed25519_deserialize_private(const char * ktype,struct sshbuf * b,struct sshkey * key)121*b1066cf3Schristos ssh_ed25519_deserialize_private(const char *ktype, struct sshbuf *b,
122*b1066cf3Schristos     struct sshkey *key)
123*b1066cf3Schristos {
124*b1066cf3Schristos 	int r;
125*b1066cf3Schristos 	size_t sklen = 0;
126*b1066cf3Schristos 	u_char *ed25519_sk = NULL;
127*b1066cf3Schristos 
128*b1066cf3Schristos 	if ((r = ssh_ed25519_deserialize_public(NULL, b, key)) != 0)
129*b1066cf3Schristos 		goto out;
130*b1066cf3Schristos 	if ((r = sshbuf_get_string(b, &ed25519_sk, &sklen)) != 0)
131*b1066cf3Schristos 		goto out;
132*b1066cf3Schristos 	if (sklen != ED25519_SK_SZ) {
133*b1066cf3Schristos 		r = SSH_ERR_INVALID_FORMAT;
134*b1066cf3Schristos 		goto out;
135*b1066cf3Schristos 	}
136*b1066cf3Schristos 	key->ed25519_sk = ed25519_sk;
137*b1066cf3Schristos 	ed25519_sk = NULL; /* transferred */
138*b1066cf3Schristos 	/* success */
139*b1066cf3Schristos 	r = 0;
140*b1066cf3Schristos  out:
141*b1066cf3Schristos 	freezero(ed25519_sk, sklen);
142*b1066cf3Schristos 	return r;
143*b1066cf3Schristos }
144*b1066cf3Schristos 
145*b1066cf3Schristos 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*b1066cf3Schristos ssh_ed25519_sign(struct sshkey *key,
147*b1066cf3Schristos     u_char **sigp, size_t *lenp,
148*b1066cf3Schristos     const u_char *data, size_t datalen,
149*b1066cf3Schristos     const char *alg, const char *sk_provider, const char *sk_pin, u_int compat)
1505484a5efSchristos {
1515484a5efSchristos 	u_char *sig = NULL;
1525484a5efSchristos 	size_t slen = 0, len;
1535484a5efSchristos 	unsigned long long smlen;
1545484a5efSchristos 	int r, ret;
1555484a5efSchristos 	struct sshbuf *b = NULL;
1565484a5efSchristos 
1575484a5efSchristos 	if (lenp != NULL)
1585484a5efSchristos 		*lenp = 0;
1595484a5efSchristos 	if (sigp != NULL)
1605484a5efSchristos 		*sigp = NULL;
1615484a5efSchristos 
1625484a5efSchristos 	if (key == NULL ||
1635484a5efSchristos 	    sshkey_type_plain(key->type) != KEY_ED25519 ||
1645484a5efSchristos 	    key->ed25519_sk == NULL ||
1655484a5efSchristos 	    datalen >= INT_MAX - crypto_sign_ed25519_BYTES)
1665484a5efSchristos 		return SSH_ERR_INVALID_ARGUMENT;
1675484a5efSchristos 	smlen = slen = datalen + crypto_sign_ed25519_BYTES;
1685484a5efSchristos 	if ((sig = malloc(slen)) == NULL)
1695484a5efSchristos 		return SSH_ERR_ALLOC_FAIL;
1705484a5efSchristos 
1715484a5efSchristos 	if ((ret = crypto_sign_ed25519(sig, &smlen, data, datalen,
1725484a5efSchristos 	    key->ed25519_sk)) != 0 || smlen <= datalen) {
1735484a5efSchristos 		r = SSH_ERR_INVALID_ARGUMENT; /* XXX better error? */
1745484a5efSchristos 		goto out;
1755484a5efSchristos 	}
1765484a5efSchristos 	/* encode signature */
1775484a5efSchristos 	if ((b = sshbuf_new()) == NULL) {
1785484a5efSchristos 		r = SSH_ERR_ALLOC_FAIL;
1795484a5efSchristos 		goto out;
1805484a5efSchristos 	}
1815484a5efSchristos 	if ((r = sshbuf_put_cstring(b, "ssh-ed25519")) != 0 ||
1825484a5efSchristos 	    (r = sshbuf_put_string(b, sig, smlen - datalen)) != 0)
1835484a5efSchristos 		goto out;
1845484a5efSchristos 	len = sshbuf_len(b);
1855484a5efSchristos 	if (sigp != NULL) {
1865484a5efSchristos 		if ((*sigp = malloc(len)) == NULL) {
1875484a5efSchristos 			r = SSH_ERR_ALLOC_FAIL;
1885484a5efSchristos 			goto out;
1895484a5efSchristos 		}
1905484a5efSchristos 		memcpy(*sigp, sshbuf_ptr(b), len);
1915484a5efSchristos 	}
1925484a5efSchristos 	if (lenp != NULL)
1935484a5efSchristos 		*lenp = len;
1945484a5efSchristos 	/* success */
1955484a5efSchristos 	r = 0;
1965484a5efSchristos  out:
1975484a5efSchristos 	sshbuf_free(b);
1988db691beSchristos 	if (sig != NULL)
1998db691beSchristos 		freezero(sig, slen);
2005484a5efSchristos 
2015484a5efSchristos 	return r;
2025484a5efSchristos }
2035484a5efSchristos 
204*b1066cf3Schristos 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)2055484a5efSchristos ssh_ed25519_verify(const struct sshkey *key,
206*b1066cf3Schristos     const u_char *sig, size_t siglen,
207*b1066cf3Schristos     const u_char *data, size_t dlen, const char *alg, u_int compat,
208*b1066cf3Schristos     struct sshkey_sig_details **detailsp)
2095484a5efSchristos {
2105484a5efSchristos 	struct sshbuf *b = NULL;
2115484a5efSchristos 	char *ktype = NULL;
2125484a5efSchristos 	const u_char *sigblob;
2135484a5efSchristos 	u_char *sm = NULL, *m = NULL;
2145484a5efSchristos 	size_t len;
2155484a5efSchristos 	unsigned long long smlen = 0, mlen = 0;
2165484a5efSchristos 	int r, ret;
2175484a5efSchristos 
2185484a5efSchristos 	if (key == NULL ||
2195484a5efSchristos 	    sshkey_type_plain(key->type) != KEY_ED25519 ||
2205484a5efSchristos 	    key->ed25519_pk == NULL ||
221*b1066cf3Schristos 	    dlen >= INT_MAX - crypto_sign_ed25519_BYTES ||
222*b1066cf3Schristos 	    sig == NULL || siglen == 0)
2235484a5efSchristos 		return SSH_ERR_INVALID_ARGUMENT;
2245484a5efSchristos 
225*b1066cf3Schristos 	if ((b = sshbuf_from(sig, siglen)) == NULL)
2265484a5efSchristos 		return SSH_ERR_ALLOC_FAIL;
2275484a5efSchristos 	if ((r = sshbuf_get_cstring(b, &ktype, NULL)) != 0 ||
2285484a5efSchristos 	    (r = sshbuf_get_string_direct(b, &sigblob, &len)) != 0)
2295484a5efSchristos 		goto out;
2305484a5efSchristos 	if (strcmp("ssh-ed25519", ktype) != 0) {
2315484a5efSchristos 		r = SSH_ERR_KEY_TYPE_MISMATCH;
2325484a5efSchristos 		goto out;
2335484a5efSchristos 	}
2345484a5efSchristos 	if (sshbuf_len(b) != 0) {
2355484a5efSchristos 		r = SSH_ERR_UNEXPECTED_TRAILING_DATA;
2365484a5efSchristos 		goto out;
2375484a5efSchristos 	}
2385484a5efSchristos 	if (len > crypto_sign_ed25519_BYTES) {
2395484a5efSchristos 		r = SSH_ERR_INVALID_FORMAT;
2405484a5efSchristos 		goto out;
2415484a5efSchristos 	}
242*b1066cf3Schristos 	if (dlen >= SIZE_MAX - len) {
243e4d43b82Schristos 		r = SSH_ERR_INVALID_ARGUMENT;
244e4d43b82Schristos 		goto out;
245e4d43b82Schristos 	}
246*b1066cf3Schristos 	smlen = len + dlen;
2475484a5efSchristos 	mlen = smlen;
248e4d43b82Schristos 	if ((sm = malloc(smlen)) == NULL || (m = malloc(mlen)) == NULL) {
2495484a5efSchristos 		r = SSH_ERR_ALLOC_FAIL;
2505484a5efSchristos 		goto out;
2515484a5efSchristos 	}
2525484a5efSchristos 	memcpy(sm, sigblob, len);
253*b1066cf3Schristos 	memcpy(sm+len, data, dlen);
2545484a5efSchristos 	if ((ret = crypto_sign_ed25519_open(m, &mlen, sm, smlen,
2555484a5efSchristos 	    key->ed25519_pk)) != 0) {
25617418e98Schristos 		debug2_f("crypto_sign_ed25519_open failed: %d", ret);
2575484a5efSchristos 	}
258*b1066cf3Schristos 	if (ret != 0 || mlen != dlen) {
2595484a5efSchristos 		r = SSH_ERR_SIGNATURE_INVALID;
2605484a5efSchristos 		goto out;
2615484a5efSchristos 	}
2625484a5efSchristos 	/* XXX compare 'm' and 'data' ? */
2635484a5efSchristos 	/* success */
2645484a5efSchristos 	r = 0;
2655484a5efSchristos  out:
2668db691beSchristos 	if (sm != NULL)
2678db691beSchristos 		freezero(sm, smlen);
2688db691beSchristos 	if (m != NULL)
2698db691beSchristos 		freezero(m, smlen); /* NB mlen may be invalid if r != 0 */
2705484a5efSchristos 	sshbuf_free(b);
2715484a5efSchristos 	free(ktype);
2725484a5efSchristos 	return r;
2735484a5efSchristos }
274*b1066cf3Schristos 
275*b1066cf3Schristos /* NB. not static; used by ED25519-SK */
276*b1066cf3Schristos const struct sshkey_impl_funcs sshkey_ed25519_funcs = {
277*b1066cf3Schristos 	/* .size = */		NULL,
278*b1066cf3Schristos 	/* .alloc = */		NULL,
279*b1066cf3Schristos 	/* .cleanup = */	ssh_ed25519_cleanup,
280*b1066cf3Schristos 	/* .equal = */		ssh_ed25519_equal,
281*b1066cf3Schristos 	/* .ssh_serialize_public = */ ssh_ed25519_serialize_public,
282*b1066cf3Schristos 	/* .ssh_deserialize_public = */ ssh_ed25519_deserialize_public,
283*b1066cf3Schristos 	/* .ssh_serialize_private = */ ssh_ed25519_serialize_private,
284*b1066cf3Schristos 	/* .ssh_deserialize_private = */ ssh_ed25519_deserialize_private,
285*b1066cf3Schristos 	/* .generate = */	ssh_ed25519_generate,
286*b1066cf3Schristos 	/* .copy_public = */	ssh_ed25519_copy_public,
287*b1066cf3Schristos 	/* .sign = */		ssh_ed25519_sign,
288*b1066cf3Schristos 	/* .verify = */		ssh_ed25519_verify,
289*b1066cf3Schristos };
290*b1066cf3Schristos 
291*b1066cf3Schristos const struct sshkey_impl sshkey_ed25519_impl = {
292*b1066cf3Schristos 	/* .name = */		"ssh-ed25519",
293*b1066cf3Schristos 	/* .shortname = */	"ED25519",
294*b1066cf3Schristos 	/* .sigalg = */		NULL,
295*b1066cf3Schristos 	/* .type = */		KEY_ED25519,
296*b1066cf3Schristos 	/* .nid = */		0,
297*b1066cf3Schristos 	/* .cert = */		0,
298*b1066cf3Schristos 	/* .sigonly = */	0,
299*b1066cf3Schristos 	/* .keybits = */	256,
300*b1066cf3Schristos 	/* .funcs = */		&sshkey_ed25519_funcs,
301*b1066cf3Schristos };
302*b1066cf3Schristos 
303*b1066cf3Schristos const struct sshkey_impl sshkey_ed25519_cert_impl = {
304*b1066cf3Schristos 	/* .name = */		"ssh-ed25519-cert-v01@openssh.com",
305*b1066cf3Schristos 	/* .shortname = */	"ED25519-CERT",
306*b1066cf3Schristos 	/* .sigalg = */		NULL,
307*b1066cf3Schristos 	/* .type = */		KEY_ED25519_CERT,
308*b1066cf3Schristos 	/* .nid = */		0,
309*b1066cf3Schristos 	/* .cert = */		1,
310*b1066cf3Schristos 	/* .sigonly = */	0,
311*b1066cf3Schristos 	/* .keybits = */	256,
312*b1066cf3Schristos 	/* .funcs = */		&sshkey_ed25519_funcs,
313*b1066cf3Schristos };
314