1*ba1276acSMatthew Dillon /* $OpenBSD: ssh-ed25519.c,v 1.19 2022/10/28 00:44:44 djm Exp $ */
236e94dc5SPeter Avalos /*
336e94dc5SPeter Avalos * Copyright (c) 2013 Markus Friedl <markus@openbsd.org>
436e94dc5SPeter Avalos *
536e94dc5SPeter Avalos * Permission to use, copy, modify, and distribute this software for any
636e94dc5SPeter Avalos * purpose with or without fee is hereby granted, provided that the above
736e94dc5SPeter Avalos * copyright notice and this permission notice appear in all copies.
836e94dc5SPeter Avalos *
936e94dc5SPeter Avalos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1036e94dc5SPeter Avalos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1136e94dc5SPeter Avalos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1236e94dc5SPeter Avalos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1336e94dc5SPeter Avalos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1436e94dc5SPeter Avalos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1536e94dc5SPeter Avalos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1636e94dc5SPeter Avalos */
1736e94dc5SPeter Avalos
1836e94dc5SPeter Avalos #include "includes.h"
1936e94dc5SPeter Avalos
2036e94dc5SPeter Avalos #include <sys/types.h>
2136e94dc5SPeter Avalos #include <limits.h>
2236e94dc5SPeter Avalos
2336e94dc5SPeter Avalos #include "crypto_api.h"
2436e94dc5SPeter Avalos
2536e94dc5SPeter Avalos #include <string.h>
2636e94dc5SPeter Avalos #include <stdarg.h>
2736e94dc5SPeter Avalos
2836e94dc5SPeter Avalos #include "log.h"
29e9778795SPeter Avalos #include "sshbuf.h"
3036e94dc5SPeter Avalos #define SSHKEY_INTERNAL
3136e94dc5SPeter Avalos #include "sshkey.h"
3236e94dc5SPeter Avalos #include "ssherr.h"
3336e94dc5SPeter Avalos #include "ssh.h"
3436e94dc5SPeter Avalos
35*ba1276acSMatthew Dillon static void
ssh_ed25519_cleanup(struct sshkey * k)36*ba1276acSMatthew Dillon ssh_ed25519_cleanup(struct sshkey *k)
37*ba1276acSMatthew Dillon {
38*ba1276acSMatthew Dillon freezero(k->ed25519_pk, ED25519_PK_SZ);
39*ba1276acSMatthew Dillon freezero(k->ed25519_sk, ED25519_SK_SZ);
40*ba1276acSMatthew Dillon k->ed25519_pk = NULL;
41*ba1276acSMatthew Dillon k->ed25519_sk = NULL;
42*ba1276acSMatthew Dillon }
43*ba1276acSMatthew Dillon
44*ba1276acSMatthew Dillon static int
ssh_ed25519_equal(const struct sshkey * a,const struct sshkey * b)45*ba1276acSMatthew Dillon ssh_ed25519_equal(const struct sshkey *a, const struct sshkey *b)
46*ba1276acSMatthew Dillon {
47*ba1276acSMatthew Dillon if (a->ed25519_pk == NULL || b->ed25519_pk == NULL)
48*ba1276acSMatthew Dillon return 0;
49*ba1276acSMatthew Dillon if (memcmp(a->ed25519_pk, b->ed25519_pk, ED25519_PK_SZ) != 0)
50*ba1276acSMatthew Dillon return 0;
51*ba1276acSMatthew Dillon return 1;
52*ba1276acSMatthew Dillon }
53*ba1276acSMatthew Dillon
54*ba1276acSMatthew Dillon static int
ssh_ed25519_serialize_public(const struct sshkey * key,struct sshbuf * b,enum sshkey_serialize_rep opts)55*ba1276acSMatthew Dillon ssh_ed25519_serialize_public(const struct sshkey *key, struct sshbuf *b,
56*ba1276acSMatthew Dillon enum sshkey_serialize_rep opts)
57*ba1276acSMatthew Dillon {
58*ba1276acSMatthew Dillon int r;
59*ba1276acSMatthew Dillon
60*ba1276acSMatthew Dillon if (key->ed25519_pk == NULL)
61*ba1276acSMatthew Dillon return SSH_ERR_INVALID_ARGUMENT;
62*ba1276acSMatthew Dillon if ((r = sshbuf_put_string(b, key->ed25519_pk, ED25519_PK_SZ)) != 0)
63*ba1276acSMatthew Dillon return r;
64*ba1276acSMatthew Dillon
65*ba1276acSMatthew Dillon return 0;
66*ba1276acSMatthew Dillon }
67*ba1276acSMatthew Dillon
68*ba1276acSMatthew Dillon static int
ssh_ed25519_serialize_private(const struct sshkey * key,struct sshbuf * b,enum sshkey_serialize_rep opts)69*ba1276acSMatthew Dillon ssh_ed25519_serialize_private(const struct sshkey *key, struct sshbuf *b,
70*ba1276acSMatthew Dillon enum sshkey_serialize_rep opts)
71*ba1276acSMatthew Dillon {
72*ba1276acSMatthew Dillon int r;
73*ba1276acSMatthew Dillon
74*ba1276acSMatthew Dillon if ((r = sshbuf_put_string(b, key->ed25519_pk, ED25519_PK_SZ)) != 0 ||
75*ba1276acSMatthew Dillon (r = sshbuf_put_string(b, key->ed25519_sk, ED25519_SK_SZ)) != 0)
76*ba1276acSMatthew Dillon return r;
77*ba1276acSMatthew Dillon
78*ba1276acSMatthew Dillon return 0;
79*ba1276acSMatthew Dillon }
80*ba1276acSMatthew Dillon
81*ba1276acSMatthew Dillon static int
ssh_ed25519_generate(struct sshkey * k,int bits)82*ba1276acSMatthew Dillon ssh_ed25519_generate(struct sshkey *k, int bits)
83*ba1276acSMatthew Dillon {
84*ba1276acSMatthew Dillon if ((k->ed25519_pk = malloc(ED25519_PK_SZ)) == NULL ||
85*ba1276acSMatthew Dillon (k->ed25519_sk = malloc(ED25519_SK_SZ)) == NULL)
86*ba1276acSMatthew Dillon return SSH_ERR_ALLOC_FAIL;
87*ba1276acSMatthew Dillon crypto_sign_ed25519_keypair(k->ed25519_pk, k->ed25519_sk);
88*ba1276acSMatthew Dillon return 0;
89*ba1276acSMatthew Dillon }
90*ba1276acSMatthew Dillon
91*ba1276acSMatthew Dillon static int
ssh_ed25519_copy_public(const struct sshkey * from,struct sshkey * to)92*ba1276acSMatthew Dillon ssh_ed25519_copy_public(const struct sshkey *from, struct sshkey *to)
93*ba1276acSMatthew Dillon {
94*ba1276acSMatthew Dillon if (from->ed25519_pk == NULL)
95*ba1276acSMatthew Dillon return 0; /* XXX SSH_ERR_INTERNAL_ERROR ? */
96*ba1276acSMatthew Dillon if ((to->ed25519_pk = malloc(ED25519_PK_SZ)) == NULL)
97*ba1276acSMatthew Dillon return SSH_ERR_ALLOC_FAIL;
98*ba1276acSMatthew Dillon memcpy(to->ed25519_pk, from->ed25519_pk, ED25519_PK_SZ);
99*ba1276acSMatthew Dillon return 0;
100*ba1276acSMatthew Dillon }
101*ba1276acSMatthew Dillon
102*ba1276acSMatthew Dillon static int
ssh_ed25519_deserialize_public(const char * ktype,struct sshbuf * b,struct sshkey * key)103*ba1276acSMatthew Dillon ssh_ed25519_deserialize_public(const char *ktype, struct sshbuf *b,
104*ba1276acSMatthew Dillon struct sshkey *key)
105*ba1276acSMatthew Dillon {
106*ba1276acSMatthew Dillon u_char *pk = NULL;
107*ba1276acSMatthew Dillon size_t len = 0;
108*ba1276acSMatthew Dillon int r;
109*ba1276acSMatthew Dillon
110*ba1276acSMatthew Dillon if ((r = sshbuf_get_string(b, &pk, &len)) != 0)
111*ba1276acSMatthew Dillon return r;
112*ba1276acSMatthew Dillon if (len != ED25519_PK_SZ) {
113*ba1276acSMatthew Dillon freezero(pk, len);
114*ba1276acSMatthew Dillon return SSH_ERR_INVALID_FORMAT;
115*ba1276acSMatthew Dillon }
116*ba1276acSMatthew Dillon key->ed25519_pk = pk;
117*ba1276acSMatthew Dillon return 0;
118*ba1276acSMatthew Dillon }
119*ba1276acSMatthew Dillon
120*ba1276acSMatthew Dillon static int
ssh_ed25519_deserialize_private(const char * ktype,struct sshbuf * b,struct sshkey * key)121*ba1276acSMatthew Dillon ssh_ed25519_deserialize_private(const char *ktype, struct sshbuf *b,
122*ba1276acSMatthew Dillon struct sshkey *key)
123*ba1276acSMatthew Dillon {
124*ba1276acSMatthew Dillon int r;
125*ba1276acSMatthew Dillon size_t sklen = 0;
126*ba1276acSMatthew Dillon u_char *ed25519_sk = NULL;
127*ba1276acSMatthew Dillon
128*ba1276acSMatthew Dillon if ((r = ssh_ed25519_deserialize_public(NULL, b, key)) != 0)
129*ba1276acSMatthew Dillon goto out;
130*ba1276acSMatthew Dillon if ((r = sshbuf_get_string(b, &ed25519_sk, &sklen)) != 0)
131*ba1276acSMatthew Dillon goto out;
132*ba1276acSMatthew Dillon if (sklen != ED25519_SK_SZ) {
133*ba1276acSMatthew Dillon r = SSH_ERR_INVALID_FORMAT;
134*ba1276acSMatthew Dillon goto out;
135*ba1276acSMatthew Dillon }
136*ba1276acSMatthew Dillon key->ed25519_sk = ed25519_sk;
137*ba1276acSMatthew Dillon ed25519_sk = NULL; /* transferred */
138*ba1276acSMatthew Dillon /* success */
139*ba1276acSMatthew Dillon r = 0;
140*ba1276acSMatthew Dillon out:
141*ba1276acSMatthew Dillon freezero(ed25519_sk, sklen);
142*ba1276acSMatthew Dillon return r;
143*ba1276acSMatthew Dillon }
144*ba1276acSMatthew Dillon
145*ba1276acSMatthew Dillon 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*ba1276acSMatthew Dillon ssh_ed25519_sign(struct sshkey *key,
147*ba1276acSMatthew Dillon u_char **sigp, size_t *lenp,
148*ba1276acSMatthew Dillon const u_char *data, size_t datalen,
149*ba1276acSMatthew Dillon const char *alg, const char *sk_provider, const char *sk_pin, u_int compat)
15036e94dc5SPeter Avalos {
15136e94dc5SPeter Avalos u_char *sig = NULL;
15236e94dc5SPeter Avalos size_t slen = 0, len;
15336e94dc5SPeter Avalos unsigned long long smlen;
15436e94dc5SPeter Avalos int r, ret;
15536e94dc5SPeter Avalos struct sshbuf *b = NULL;
15636e94dc5SPeter Avalos
15736e94dc5SPeter Avalos if (lenp != NULL)
15836e94dc5SPeter Avalos *lenp = 0;
15936e94dc5SPeter Avalos if (sigp != NULL)
16036e94dc5SPeter Avalos *sigp = NULL;
16136e94dc5SPeter Avalos
16236e94dc5SPeter Avalos if (key == NULL ||
16336e94dc5SPeter Avalos sshkey_type_plain(key->type) != KEY_ED25519 ||
16436e94dc5SPeter Avalos key->ed25519_sk == NULL ||
16536e94dc5SPeter Avalos datalen >= INT_MAX - crypto_sign_ed25519_BYTES)
16636e94dc5SPeter Avalos return SSH_ERR_INVALID_ARGUMENT;
16736e94dc5SPeter Avalos smlen = slen = datalen + crypto_sign_ed25519_BYTES;
16836e94dc5SPeter Avalos if ((sig = malloc(slen)) == NULL)
16936e94dc5SPeter Avalos return SSH_ERR_ALLOC_FAIL;
17036e94dc5SPeter Avalos
17136e94dc5SPeter Avalos if ((ret = crypto_sign_ed25519(sig, &smlen, data, datalen,
17236e94dc5SPeter Avalos key->ed25519_sk)) != 0 || smlen <= datalen) {
17336e94dc5SPeter Avalos r = SSH_ERR_INVALID_ARGUMENT; /* XXX better error? */
17436e94dc5SPeter Avalos goto out;
17536e94dc5SPeter Avalos }
17636e94dc5SPeter Avalos /* encode signature */
17736e94dc5SPeter Avalos if ((b = sshbuf_new()) == NULL) {
17836e94dc5SPeter Avalos r = SSH_ERR_ALLOC_FAIL;
17936e94dc5SPeter Avalos goto out;
18036e94dc5SPeter Avalos }
18136e94dc5SPeter Avalos if ((r = sshbuf_put_cstring(b, "ssh-ed25519")) != 0 ||
18236e94dc5SPeter Avalos (r = sshbuf_put_string(b, sig, smlen - datalen)) != 0)
18336e94dc5SPeter Avalos goto out;
18436e94dc5SPeter Avalos len = sshbuf_len(b);
18536e94dc5SPeter Avalos if (sigp != NULL) {
18636e94dc5SPeter Avalos if ((*sigp = malloc(len)) == NULL) {
18736e94dc5SPeter Avalos r = SSH_ERR_ALLOC_FAIL;
18836e94dc5SPeter Avalos goto out;
18936e94dc5SPeter Avalos }
19036e94dc5SPeter Avalos memcpy(*sigp, sshbuf_ptr(b), len);
19136e94dc5SPeter Avalos }
19236e94dc5SPeter Avalos if (lenp != NULL)
19336e94dc5SPeter Avalos *lenp = len;
19436e94dc5SPeter Avalos /* success */
19536e94dc5SPeter Avalos r = 0;
19636e94dc5SPeter Avalos out:
19736e94dc5SPeter Avalos sshbuf_free(b);
1980cbfa66cSDaniel Fojt if (sig != NULL)
1990cbfa66cSDaniel Fojt freezero(sig, slen);
20036e94dc5SPeter Avalos
20136e94dc5SPeter Avalos return r;
20236e94dc5SPeter Avalos }
20336e94dc5SPeter Avalos
204*ba1276acSMatthew Dillon 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)20536e94dc5SPeter Avalos ssh_ed25519_verify(const struct sshkey *key,
206*ba1276acSMatthew Dillon const u_char *sig, size_t siglen,
207*ba1276acSMatthew Dillon const u_char *data, size_t dlen, const char *alg, u_int compat,
208*ba1276acSMatthew Dillon struct sshkey_sig_details **detailsp)
20936e94dc5SPeter Avalos {
21036e94dc5SPeter Avalos struct sshbuf *b = NULL;
21136e94dc5SPeter Avalos char *ktype = NULL;
21236e94dc5SPeter Avalos const u_char *sigblob;
21336e94dc5SPeter Avalos u_char *sm = NULL, *m = NULL;
21436e94dc5SPeter Avalos size_t len;
21536e94dc5SPeter Avalos unsigned long long smlen = 0, mlen = 0;
21636e94dc5SPeter Avalos int r, ret;
21736e94dc5SPeter Avalos
21836e94dc5SPeter Avalos if (key == NULL ||
21936e94dc5SPeter Avalos sshkey_type_plain(key->type) != KEY_ED25519 ||
22036e94dc5SPeter Avalos key->ed25519_pk == NULL ||
221*ba1276acSMatthew Dillon dlen >= INT_MAX - crypto_sign_ed25519_BYTES ||
222*ba1276acSMatthew Dillon sig == NULL || siglen == 0)
22336e94dc5SPeter Avalos return SSH_ERR_INVALID_ARGUMENT;
22436e94dc5SPeter Avalos
225*ba1276acSMatthew Dillon if ((b = sshbuf_from(sig, siglen)) == NULL)
22636e94dc5SPeter Avalos return SSH_ERR_ALLOC_FAIL;
22736e94dc5SPeter Avalos if ((r = sshbuf_get_cstring(b, &ktype, NULL)) != 0 ||
22836e94dc5SPeter Avalos (r = sshbuf_get_string_direct(b, &sigblob, &len)) != 0)
22936e94dc5SPeter Avalos goto out;
23036e94dc5SPeter Avalos if (strcmp("ssh-ed25519", ktype) != 0) {
23136e94dc5SPeter Avalos r = SSH_ERR_KEY_TYPE_MISMATCH;
23236e94dc5SPeter Avalos goto out;
23336e94dc5SPeter Avalos }
23436e94dc5SPeter Avalos if (sshbuf_len(b) != 0) {
23536e94dc5SPeter Avalos r = SSH_ERR_UNEXPECTED_TRAILING_DATA;
23636e94dc5SPeter Avalos goto out;
23736e94dc5SPeter Avalos }
23836e94dc5SPeter Avalos if (len > crypto_sign_ed25519_BYTES) {
23936e94dc5SPeter Avalos r = SSH_ERR_INVALID_FORMAT;
24036e94dc5SPeter Avalos goto out;
24136e94dc5SPeter Avalos }
242*ba1276acSMatthew Dillon if (dlen >= SIZE_MAX - len) {
243e9778795SPeter Avalos r = SSH_ERR_INVALID_ARGUMENT;
244e9778795SPeter Avalos goto out;
245e9778795SPeter Avalos }
246*ba1276acSMatthew Dillon smlen = len + dlen;
24736e94dc5SPeter Avalos mlen = smlen;
248e9778795SPeter Avalos if ((sm = malloc(smlen)) == NULL || (m = malloc(mlen)) == NULL) {
24936e94dc5SPeter Avalos r = SSH_ERR_ALLOC_FAIL;
25036e94dc5SPeter Avalos goto out;
25136e94dc5SPeter Avalos }
25236e94dc5SPeter Avalos memcpy(sm, sigblob, len);
253*ba1276acSMatthew Dillon memcpy(sm+len, data, dlen);
25436e94dc5SPeter Avalos if ((ret = crypto_sign_ed25519_open(m, &mlen, sm, smlen,
25536e94dc5SPeter Avalos key->ed25519_pk)) != 0) {
25650a69bb5SSascha Wildner debug2_f("crypto_sign_ed25519_open failed: %d", ret);
25736e94dc5SPeter Avalos }
258*ba1276acSMatthew Dillon if (ret != 0 || mlen != dlen) {
25936e94dc5SPeter Avalos r = SSH_ERR_SIGNATURE_INVALID;
26036e94dc5SPeter Avalos goto out;
26136e94dc5SPeter Avalos }
26236e94dc5SPeter Avalos /* XXX compare 'm' and 'data' ? */
26336e94dc5SPeter Avalos /* success */
26436e94dc5SPeter Avalos r = 0;
26536e94dc5SPeter Avalos out:
2660cbfa66cSDaniel Fojt if (sm != NULL)
2670cbfa66cSDaniel Fojt freezero(sm, smlen);
2680cbfa66cSDaniel Fojt if (m != NULL)
2690cbfa66cSDaniel Fojt freezero(m, smlen); /* NB mlen may be invalid if r != 0 */
27036e94dc5SPeter Avalos sshbuf_free(b);
27136e94dc5SPeter Avalos free(ktype);
27236e94dc5SPeter Avalos return r;
27336e94dc5SPeter Avalos }
274*ba1276acSMatthew Dillon
275*ba1276acSMatthew Dillon /* NB. not static; used by ED25519-SK */
276*ba1276acSMatthew Dillon const struct sshkey_impl_funcs sshkey_ed25519_funcs = {
277*ba1276acSMatthew Dillon /* .size = */ NULL,
278*ba1276acSMatthew Dillon /* .alloc = */ NULL,
279*ba1276acSMatthew Dillon /* .cleanup = */ ssh_ed25519_cleanup,
280*ba1276acSMatthew Dillon /* .equal = */ ssh_ed25519_equal,
281*ba1276acSMatthew Dillon /* .ssh_serialize_public = */ ssh_ed25519_serialize_public,
282*ba1276acSMatthew Dillon /* .ssh_deserialize_public = */ ssh_ed25519_deserialize_public,
283*ba1276acSMatthew Dillon /* .ssh_serialize_private = */ ssh_ed25519_serialize_private,
284*ba1276acSMatthew Dillon /* .ssh_deserialize_private = */ ssh_ed25519_deserialize_private,
285*ba1276acSMatthew Dillon /* .generate = */ ssh_ed25519_generate,
286*ba1276acSMatthew Dillon /* .copy_public = */ ssh_ed25519_copy_public,
287*ba1276acSMatthew Dillon /* .sign = */ ssh_ed25519_sign,
288*ba1276acSMatthew Dillon /* .verify = */ ssh_ed25519_verify,
289*ba1276acSMatthew Dillon };
290*ba1276acSMatthew Dillon
291*ba1276acSMatthew Dillon const struct sshkey_impl sshkey_ed25519_impl = {
292*ba1276acSMatthew Dillon /* .name = */ "ssh-ed25519",
293*ba1276acSMatthew Dillon /* .shortname = */ "ED25519",
294*ba1276acSMatthew Dillon /* .sigalg = */ NULL,
295*ba1276acSMatthew Dillon /* .type = */ KEY_ED25519,
296*ba1276acSMatthew Dillon /* .nid = */ 0,
297*ba1276acSMatthew Dillon /* .cert = */ 0,
298*ba1276acSMatthew Dillon /* .sigonly = */ 0,
299*ba1276acSMatthew Dillon /* .keybits = */ 256,
300*ba1276acSMatthew Dillon /* .funcs = */ &sshkey_ed25519_funcs,
301*ba1276acSMatthew Dillon };
302*ba1276acSMatthew Dillon
303*ba1276acSMatthew Dillon const struct sshkey_impl sshkey_ed25519_cert_impl = {
304*ba1276acSMatthew Dillon /* .name = */ "ssh-ed25519-cert-v01@openssh.com",
305*ba1276acSMatthew Dillon /* .shortname = */ "ED25519-CERT",
306*ba1276acSMatthew Dillon /* .sigalg = */ NULL,
307*ba1276acSMatthew Dillon /* .type = */ KEY_ED25519_CERT,
308*ba1276acSMatthew Dillon /* .nid = */ 0,
309*ba1276acSMatthew Dillon /* .cert = */ 1,
310*ba1276acSMatthew Dillon /* .sigonly = */ 0,
311*ba1276acSMatthew Dillon /* .keybits = */ 256,
312*ba1276acSMatthew Dillon /* .funcs = */ &sshkey_ed25519_funcs,
313*ba1276acSMatthew Dillon };
314