xref: /netbsd-src/crypto/external/bsd/openssh/dist/ssh-ed25519-sk.c (revision b1066cf3cd3cc4bc809a7791a10cc5857ad5d501)
1*b1066cf3Schristos /*	$NetBSD: ssh-ed25519-sk.c,v 1.5 2023/07/26 17:58:16 christos Exp $	*/
2*b1066cf3Schristos /* $OpenBSD: ssh-ed25519-sk.c,v 1.15 2022/10/28 00:44:44 djm Exp $ */
318504831Schristos /*
418504831Schristos  * Copyright (c) 2019 Markus Friedl.  All rights reserved.
518504831Schristos  *
618504831Schristos  * Permission to use, copy, modify, and distribute this software for any
718504831Schristos  * purpose with or without fee is hereby granted, provided that the above
818504831Schristos  * copyright notice and this permission notice appear in all copies.
918504831Schristos  *
1018504831Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1118504831Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1218504831Schristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1318504831Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1418504831Schristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1518504831Schristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1618504831Schristos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1718504831Schristos  */
18ed75d7a8Schristos #include "includes.h"
19*b1066cf3Schristos __RCSID("$NetBSD: ssh-ed25519-sk.c,v 1.5 2023/07/26 17:58:16 christos Exp $");
2018504831Schristos 
2118504831Schristos /* #define DEBUG_SK 1 */
2218504831Schristos 
2318504831Schristos #define SSHKEY_INTERNAL
2418504831Schristos #include <sys/types.h>
2518504831Schristos #include <limits.h>
2618504831Schristos 
2718504831Schristos #include "crypto_api.h"
2818504831Schristos 
2918504831Schristos #include <string.h>
3018504831Schristos #include <stdarg.h>
3118504831Schristos 
3218504831Schristos #include "log.h"
3318504831Schristos #include "sshbuf.h"
3418504831Schristos #include "sshkey.h"
3518504831Schristos #include "ssherr.h"
3618504831Schristos #include "ssh.h"
3718504831Schristos #include "digest.h"
3818504831Schristos 
39*b1066cf3Schristos /* Reuse some ED25519 internals */
40*b1066cf3Schristos extern struct sshkey_impl_funcs sshkey_ed25519_funcs;
41*b1066cf3Schristos 
42*b1066cf3Schristos static void
ssh_ed25519_sk_cleanup(struct sshkey * k)43*b1066cf3Schristos ssh_ed25519_sk_cleanup(struct sshkey *k)
44*b1066cf3Schristos {
45*b1066cf3Schristos 	sshkey_sk_cleanup(k);
46*b1066cf3Schristos 	sshkey_ed25519_funcs.cleanup(k);
47*b1066cf3Schristos }
48*b1066cf3Schristos 
49*b1066cf3Schristos static int
ssh_ed25519_sk_equal(const struct sshkey * a,const struct sshkey * b)50*b1066cf3Schristos ssh_ed25519_sk_equal(const struct sshkey *a, const struct sshkey *b)
51*b1066cf3Schristos {
52*b1066cf3Schristos 	if (!sshkey_sk_fields_equal(a, b))
53*b1066cf3Schristos 		return 0;
54*b1066cf3Schristos 	if (!sshkey_ed25519_funcs.equal(a, b))
55*b1066cf3Schristos 		return 0;
56*b1066cf3Schristos 	return 1;
57*b1066cf3Schristos }
58*b1066cf3Schristos 
59*b1066cf3Schristos static int
ssh_ed25519_sk_serialize_public(const struct sshkey * key,struct sshbuf * b,enum sshkey_serialize_rep opts)60*b1066cf3Schristos ssh_ed25519_sk_serialize_public(const struct sshkey *key, struct sshbuf *b,
61*b1066cf3Schristos     enum sshkey_serialize_rep opts)
62*b1066cf3Schristos {
63*b1066cf3Schristos 	int r;
64*b1066cf3Schristos 
65*b1066cf3Schristos 	if ((r = sshkey_ed25519_funcs.serialize_public(key, b, opts)) != 0)
66*b1066cf3Schristos 		return r;
67*b1066cf3Schristos 	if ((r = sshkey_serialize_sk(key, b)) != 0)
68*b1066cf3Schristos 		return r;
69*b1066cf3Schristos 
70*b1066cf3Schristos 	return 0;
71*b1066cf3Schristos }
72*b1066cf3Schristos 
73*b1066cf3Schristos static int
ssh_ed25519_sk_serialize_private(const struct sshkey * key,struct sshbuf * b,enum sshkey_serialize_rep opts)74*b1066cf3Schristos ssh_ed25519_sk_serialize_private(const struct sshkey *key, struct sshbuf *b,
75*b1066cf3Schristos     enum sshkey_serialize_rep opts)
76*b1066cf3Schristos {
77*b1066cf3Schristos 	int r;
78*b1066cf3Schristos 
79*b1066cf3Schristos 	if ((r = sshkey_ed25519_funcs.serialize_public(key, b, opts)) != 0)
80*b1066cf3Schristos 		return r;
81*b1066cf3Schristos 	if ((r = sshkey_serialize_private_sk(key, b)) != 0)
82*b1066cf3Schristos 		return r;
83*b1066cf3Schristos 
84*b1066cf3Schristos 	return 0;
85*b1066cf3Schristos }
86*b1066cf3Schristos 
87*b1066cf3Schristos static int
ssh_ed25519_sk_copy_public(const struct sshkey * from,struct sshkey * to)88*b1066cf3Schristos ssh_ed25519_sk_copy_public(const struct sshkey *from, struct sshkey *to)
89*b1066cf3Schristos {
90*b1066cf3Schristos 	int r;
91*b1066cf3Schristos 
92*b1066cf3Schristos 	if ((r = sshkey_ed25519_funcs.copy_public(from, to)) != 0)
93*b1066cf3Schristos 		return r;
94*b1066cf3Schristos 	if ((r = sshkey_copy_public_sk(from, to)) != 0)
95*b1066cf3Schristos 		return r;
96*b1066cf3Schristos 	return 0;
97*b1066cf3Schristos }
98*b1066cf3Schristos 
99*b1066cf3Schristos static int
ssh_ed25519_sk_deserialize_public(const char * ktype,struct sshbuf * b,struct sshkey * key)100*b1066cf3Schristos ssh_ed25519_sk_deserialize_public(const char *ktype, struct sshbuf *b,
101*b1066cf3Schristos     struct sshkey *key)
102*b1066cf3Schristos {
103*b1066cf3Schristos 	int r;
104*b1066cf3Schristos 
105*b1066cf3Schristos 	if ((r = sshkey_ed25519_funcs.deserialize_public(ktype, b, key)) != 0)
106*b1066cf3Schristos 		return r;
107*b1066cf3Schristos 	if ((r = sshkey_deserialize_sk(b, key)) != 0)
108*b1066cf3Schristos 		return r;
109*b1066cf3Schristos 	return 0;
110*b1066cf3Schristos }
111*b1066cf3Schristos 
112*b1066cf3Schristos static int
ssh_ed25519_sk_deserialize_private(const char * ktype,struct sshbuf * b,struct sshkey * key)113*b1066cf3Schristos ssh_ed25519_sk_deserialize_private(const char *ktype, struct sshbuf *b,
114*b1066cf3Schristos     struct sshkey *key)
115*b1066cf3Schristos {
116*b1066cf3Schristos 	int r;
117*b1066cf3Schristos 
118*b1066cf3Schristos 	if ((r = sshkey_ed25519_funcs.deserialize_public(ktype, b, key)) != 0)
119*b1066cf3Schristos 		return r;
120*b1066cf3Schristos 	if ((r = sshkey_private_deserialize_sk(b, key)) != 0)
121*b1066cf3Schristos 		return r;
122*b1066cf3Schristos 	return 0;
123*b1066cf3Schristos }
124*b1066cf3Schristos 
125*b1066cf3Schristos static int
ssh_ed25519_sk_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)12618504831Schristos ssh_ed25519_sk_verify(const struct sshkey *key,
127*b1066cf3Schristos     const u_char *sig, size_t siglen,
128*b1066cf3Schristos     const u_char *data, size_t dlen, const char *alg, u_int compat,
12918504831Schristos     struct sshkey_sig_details **detailsp)
13018504831Schristos {
13118504831Schristos 	struct sshbuf *b = NULL;
13218504831Schristos 	struct sshbuf *encoded = NULL;
13318504831Schristos 	char *ktype = NULL;
13418504831Schristos 	const u_char *sigblob;
13518504831Schristos 	const u_char *sm;
13618504831Schristos 	u_char *m = NULL;
13718504831Schristos 	u_char apphash[32];
13818504831Schristos 	u_char msghash[32];
13918504831Schristos 	u_char sig_flags;
14018504831Schristos 	u_int sig_counter;
14118504831Schristos 	size_t len;
14218504831Schristos 	unsigned long long smlen = 0, mlen = 0;
14318504831Schristos 	int r = SSH_ERR_INTERNAL_ERROR;
14418504831Schristos 	int ret;
14518504831Schristos 	struct sshkey_sig_details *details = NULL;
14618504831Schristos 
14718504831Schristos 	if (detailsp != NULL)
14818504831Schristos 		*detailsp = NULL;
14918504831Schristos 
15018504831Schristos 	if (key == NULL ||
15118504831Schristos 	    sshkey_type_plain(key->type) != KEY_ED25519_SK ||
15218504831Schristos 	    key->ed25519_pk == NULL ||
153*b1066cf3Schristos 	    sig == NULL || siglen == 0)
15418504831Schristos 		return SSH_ERR_INVALID_ARGUMENT;
15518504831Schristos 
156*b1066cf3Schristos 	if ((b = sshbuf_from(sig, siglen)) == NULL)
15718504831Schristos 		return SSH_ERR_ALLOC_FAIL;
15818504831Schristos 	if (sshbuf_get_cstring(b, &ktype, NULL) != 0 ||
15918504831Schristos 	    sshbuf_get_string_direct(b, &sigblob, &len) != 0 ||
16018504831Schristos 	    sshbuf_get_u8(b, &sig_flags) != 0 ||
16118504831Schristos 	    sshbuf_get_u32(b, &sig_counter) != 0) {
16218504831Schristos 		r = SSH_ERR_INVALID_FORMAT;
16318504831Schristos 		goto out;
16418504831Schristos 	}
16518504831Schristos #ifdef DEBUG_SK
16618504831Schristos 	fprintf(stderr, "%s: data:\n", __func__);
16718504831Schristos 	/* sshbuf_dump_data(data, datalen, stderr); */
16818504831Schristos 	fprintf(stderr, "%s: sigblob:\n", __func__);
16918504831Schristos 	sshbuf_dump_data(sigblob, len, stderr);
17018504831Schristos 	fprintf(stderr, "%s: sig_flags = 0x%02x, sig_counter = %u\n",
17118504831Schristos 	    __func__, sig_flags, sig_counter);
17218504831Schristos #endif
17318504831Schristos 	if (strcmp(sshkey_ssh_name_plain(key), ktype) != 0) {
17418504831Schristos 		r = SSH_ERR_KEY_TYPE_MISMATCH;
17518504831Schristos 		goto out;
17618504831Schristos 	}
17718504831Schristos 	if (sshbuf_len(b) != 0) {
17818504831Schristos 		r = SSH_ERR_UNEXPECTED_TRAILING_DATA;
17918504831Schristos 		goto out;
18018504831Schristos 	}
18118504831Schristos 	if (len > crypto_sign_ed25519_BYTES) {
18218504831Schristos 		r = SSH_ERR_INVALID_FORMAT;
18318504831Schristos 		goto out;
18418504831Schristos 	}
18518504831Schristos 	if (ssh_digest_memory(SSH_DIGEST_SHA256, key->sk_application,
18618504831Schristos 	    strlen(key->sk_application), apphash, sizeof(apphash)) != 0 ||
187*b1066cf3Schristos 	    ssh_digest_memory(SSH_DIGEST_SHA256, data, dlen,
18818504831Schristos 	    msghash, sizeof(msghash)) != 0) {
18918504831Schristos 		r = SSH_ERR_INVALID_ARGUMENT;
19018504831Schristos 		goto out;
19118504831Schristos 	}
19218504831Schristos #ifdef DEBUG_SK
19318504831Schristos 	fprintf(stderr, "%s: hashed application:\n", __func__);
19418504831Schristos 	sshbuf_dump_data(apphash, sizeof(apphash), stderr);
19518504831Schristos 	fprintf(stderr, "%s: hashed message:\n", __func__);
19618504831Schristos 	sshbuf_dump_data(msghash, sizeof(msghash), stderr);
19718504831Schristos #endif
19818504831Schristos 	if ((details = calloc(1, sizeof(*details))) == NULL) {
19918504831Schristos 		r = SSH_ERR_ALLOC_FAIL;
20018504831Schristos 		goto out;
20118504831Schristos 	}
20218504831Schristos 	details->sk_counter = sig_counter;
20318504831Schristos 	details->sk_flags = sig_flags;
20418504831Schristos 	if ((encoded = sshbuf_new()) == NULL) {
20518504831Schristos 		r = SSH_ERR_ALLOC_FAIL;
20618504831Schristos 		goto out;
20718504831Schristos 	}
20818504831Schristos 	if (sshbuf_put(encoded, sigblob, len) != 0 ||
20918504831Schristos 	    sshbuf_put(encoded, apphash, sizeof(apphash)) != 0 ||
21018504831Schristos 	    sshbuf_put_u8(encoded, sig_flags) != 0 ||
21118504831Schristos 	    sshbuf_put_u32(encoded, sig_counter) != 0 ||
21218504831Schristos 	    sshbuf_put(encoded, msghash, sizeof(msghash)) != 0) {
21318504831Schristos 		r = SSH_ERR_ALLOC_FAIL;
21418504831Schristos 		goto out;
21518504831Schristos 	}
21618504831Schristos #ifdef DEBUG_SK
21718504831Schristos 	fprintf(stderr, "%s: signed buf:\n", __func__);
21818504831Schristos 	sshbuf_dump(encoded, stderr);
21918504831Schristos #endif
22018504831Schristos 	sm = sshbuf_ptr(encoded);
22118504831Schristos 	smlen = sshbuf_len(encoded);
22218504831Schristos 	mlen = smlen;
22318504831Schristos 	if ((m = malloc(smlen)) == NULL) {
22418504831Schristos 		r = SSH_ERR_ALLOC_FAIL;
22518504831Schristos 		goto out;
22618504831Schristos 	}
22718504831Schristos 	if ((ret = crypto_sign_ed25519_open(m, &mlen, sm, smlen,
22818504831Schristos 	    key->ed25519_pk)) != 0) {
22917418e98Schristos 		debug2_f("crypto_sign_ed25519_open failed: %d", ret);
23018504831Schristos 	}
23118504831Schristos 	if (ret != 0 || mlen != smlen - len) {
23218504831Schristos 		r = SSH_ERR_SIGNATURE_INVALID;
23318504831Schristos 		goto out;
23418504831Schristos 	}
23518504831Schristos 	/* XXX compare 'm' and 'sm + len' ? */
23618504831Schristos 	/* success */
23718504831Schristos 	r = 0;
23818504831Schristos 	if (detailsp != NULL) {
23918504831Schristos 		*detailsp = details;
24018504831Schristos 		details = NULL;
24118504831Schristos 	}
24218504831Schristos  out:
2438db691beSchristos 	if (m != NULL)
2448db691beSchristos 		freezero(m, smlen); /* NB mlen may be invalid if r != 0 */
24518504831Schristos 	sshkey_sig_details_free(details);
24618504831Schristos 	sshbuf_free(b);
24718504831Schristos 	sshbuf_free(encoded);
24818504831Schristos 	free(ktype);
24918504831Schristos 	return r;
25018504831Schristos }
251*b1066cf3Schristos 
252*b1066cf3Schristos static const struct sshkey_impl_funcs sshkey_ed25519_sk_funcs = {
253*b1066cf3Schristos 	/* .size = */		NULL,
254*b1066cf3Schristos 	/* .alloc = */		NULL,
255*b1066cf3Schristos 	/* .cleanup = */	ssh_ed25519_sk_cleanup,
256*b1066cf3Schristos 	/* .equal = */		ssh_ed25519_sk_equal,
257*b1066cf3Schristos 	/* .ssh_serialize_public = */ ssh_ed25519_sk_serialize_public,
258*b1066cf3Schristos 	/* .ssh_deserialize_public = */ ssh_ed25519_sk_deserialize_public,
259*b1066cf3Schristos 	/* .ssh_serialize_private = */ ssh_ed25519_sk_serialize_private,
260*b1066cf3Schristos 	/* .ssh_deserialize_private = */ ssh_ed25519_sk_deserialize_private,
261*b1066cf3Schristos 	/* .generate = */	NULL,
262*b1066cf3Schristos 	/* .copy_public = */	ssh_ed25519_sk_copy_public,
263*b1066cf3Schristos 	/* .sign = */		NULL,
264*b1066cf3Schristos 	/* .verify = */		ssh_ed25519_sk_verify,
265*b1066cf3Schristos };
266*b1066cf3Schristos 
267*b1066cf3Schristos const struct sshkey_impl sshkey_ed25519_sk_impl = {
268*b1066cf3Schristos 	/* .name = */		"sk-ssh-ed25519@openssh.com",
269*b1066cf3Schristos 	/* .shortname = */	"ED25519-SK",
270*b1066cf3Schristos 	/* .sigalg = */		NULL,
271*b1066cf3Schristos 	/* .type = */		KEY_ED25519_SK,
272*b1066cf3Schristos 	/* .nid = */		0,
273*b1066cf3Schristos 	/* .cert = */		0,
274*b1066cf3Schristos 	/* .sigonly = */	0,
275*b1066cf3Schristos 	/* .keybits = */	256,
276*b1066cf3Schristos 	/* .funcs = */		&sshkey_ed25519_sk_funcs,
277*b1066cf3Schristos };
278*b1066cf3Schristos 
279*b1066cf3Schristos const struct sshkey_impl sshkey_ed25519_sk_cert_impl = {
280*b1066cf3Schristos 	/* .name = */		"sk-ssh-ed25519-cert-v01@openssh.com",
281*b1066cf3Schristos 	/* .shortname = */	"ED25519-SK-CERT",
282*b1066cf3Schristos 	/* .sigalg = */		NULL,
283*b1066cf3Schristos 	/* .type = */		KEY_ED25519_SK_CERT,
284*b1066cf3Schristos 	/* .nid = */		0,
285*b1066cf3Schristos 	/* .cert = */		1,
286*b1066cf3Schristos 	/* .sigonly = */	0,
287*b1066cf3Schristos 	/* .keybits = */	256,
288*b1066cf3Schristos 	/* .funcs = */		&sshkey_ed25519_sk_funcs,
289*b1066cf3Schristos };
290