1*f374ba41SEd Maste /* $OpenBSD: ssh-ed25519-sk.c,v 1.15 2022/10/28 00:44:44 djm Exp $ */
219261079SEd Maste /*
319261079SEd Maste * Copyright (c) 2019 Markus Friedl. All rights reserved.
419261079SEd Maste *
519261079SEd Maste * Permission to use, copy, modify, and distribute this software for any
619261079SEd Maste * purpose with or without fee is hereby granted, provided that the above
719261079SEd Maste * copyright notice and this permission notice appear in all copies.
819261079SEd Maste *
919261079SEd Maste * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1019261079SEd Maste * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1119261079SEd Maste * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1219261079SEd Maste * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1319261079SEd Maste * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1419261079SEd Maste * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1519261079SEd Maste * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1619261079SEd Maste */
1719261079SEd Maste
1819261079SEd Maste /* #define DEBUG_SK 1 */
1919261079SEd Maste
2019261079SEd Maste #include "includes.h"
2119261079SEd Maste
2219261079SEd Maste #define SSHKEY_INTERNAL
2319261079SEd Maste #include <sys/types.h>
2419261079SEd Maste #include <limits.h>
2519261079SEd Maste
2619261079SEd Maste #include "crypto_api.h"
2719261079SEd Maste
2819261079SEd Maste #include <string.h>
2919261079SEd Maste #include <stdarg.h>
3019261079SEd Maste
3119261079SEd Maste #include "log.h"
3219261079SEd Maste #include "sshbuf.h"
3319261079SEd Maste #include "sshkey.h"
3419261079SEd Maste #include "ssherr.h"
3519261079SEd Maste #include "ssh.h"
3619261079SEd Maste #include "digest.h"
3719261079SEd Maste
38*f374ba41SEd Maste /* Reuse some ED25519 internals */
39*f374ba41SEd Maste extern struct sshkey_impl_funcs sshkey_ed25519_funcs;
40*f374ba41SEd Maste
41*f374ba41SEd Maste static void
ssh_ed25519_sk_cleanup(struct sshkey * k)42*f374ba41SEd Maste ssh_ed25519_sk_cleanup(struct sshkey *k)
43*f374ba41SEd Maste {
44*f374ba41SEd Maste sshkey_sk_cleanup(k);
45*f374ba41SEd Maste sshkey_ed25519_funcs.cleanup(k);
46*f374ba41SEd Maste }
47*f374ba41SEd Maste
48*f374ba41SEd Maste static int
ssh_ed25519_sk_equal(const struct sshkey * a,const struct sshkey * b)49*f374ba41SEd Maste ssh_ed25519_sk_equal(const struct sshkey *a, const struct sshkey *b)
50*f374ba41SEd Maste {
51*f374ba41SEd Maste if (!sshkey_sk_fields_equal(a, b))
52*f374ba41SEd Maste return 0;
53*f374ba41SEd Maste if (!sshkey_ed25519_funcs.equal(a, b))
54*f374ba41SEd Maste return 0;
55*f374ba41SEd Maste return 1;
56*f374ba41SEd Maste }
57*f374ba41SEd Maste
58*f374ba41SEd Maste static int
ssh_ed25519_sk_serialize_public(const struct sshkey * key,struct sshbuf * b,enum sshkey_serialize_rep opts)59*f374ba41SEd Maste ssh_ed25519_sk_serialize_public(const struct sshkey *key, struct sshbuf *b,
60*f374ba41SEd Maste enum sshkey_serialize_rep opts)
61*f374ba41SEd Maste {
62*f374ba41SEd Maste int r;
63*f374ba41SEd Maste
64*f374ba41SEd Maste if ((r = sshkey_ed25519_funcs.serialize_public(key, b, opts)) != 0)
65*f374ba41SEd Maste return r;
66*f374ba41SEd Maste if ((r = sshkey_serialize_sk(key, b)) != 0)
67*f374ba41SEd Maste return r;
68*f374ba41SEd Maste
69*f374ba41SEd Maste return 0;
70*f374ba41SEd Maste }
71*f374ba41SEd Maste
72*f374ba41SEd Maste static int
ssh_ed25519_sk_serialize_private(const struct sshkey * key,struct sshbuf * b,enum sshkey_serialize_rep opts)73*f374ba41SEd Maste ssh_ed25519_sk_serialize_private(const struct sshkey *key, struct sshbuf *b,
74*f374ba41SEd Maste enum sshkey_serialize_rep opts)
75*f374ba41SEd Maste {
76*f374ba41SEd Maste int r;
77*f374ba41SEd Maste
78*f374ba41SEd Maste if ((r = sshkey_ed25519_funcs.serialize_public(key, b, opts)) != 0)
79*f374ba41SEd Maste return r;
80*f374ba41SEd Maste if ((r = sshkey_serialize_private_sk(key, b)) != 0)
81*f374ba41SEd Maste return r;
82*f374ba41SEd Maste
83*f374ba41SEd Maste return 0;
84*f374ba41SEd Maste }
85*f374ba41SEd Maste
86*f374ba41SEd Maste static int
ssh_ed25519_sk_copy_public(const struct sshkey * from,struct sshkey * to)87*f374ba41SEd Maste ssh_ed25519_sk_copy_public(const struct sshkey *from, struct sshkey *to)
88*f374ba41SEd Maste {
89*f374ba41SEd Maste int r;
90*f374ba41SEd Maste
91*f374ba41SEd Maste if ((r = sshkey_ed25519_funcs.copy_public(from, to)) != 0)
92*f374ba41SEd Maste return r;
93*f374ba41SEd Maste if ((r = sshkey_copy_public_sk(from, to)) != 0)
94*f374ba41SEd Maste return r;
95*f374ba41SEd Maste return 0;
96*f374ba41SEd Maste }
97*f374ba41SEd Maste
98*f374ba41SEd Maste static int
ssh_ed25519_sk_deserialize_public(const char * ktype,struct sshbuf * b,struct sshkey * key)99*f374ba41SEd Maste ssh_ed25519_sk_deserialize_public(const char *ktype, struct sshbuf *b,
100*f374ba41SEd Maste struct sshkey *key)
101*f374ba41SEd Maste {
102*f374ba41SEd Maste int r;
103*f374ba41SEd Maste
104*f374ba41SEd Maste if ((r = sshkey_ed25519_funcs.deserialize_public(ktype, b, key)) != 0)
105*f374ba41SEd Maste return r;
106*f374ba41SEd Maste if ((r = sshkey_deserialize_sk(b, key)) != 0)
107*f374ba41SEd Maste return r;
108*f374ba41SEd Maste return 0;
109*f374ba41SEd Maste }
110*f374ba41SEd Maste
111*f374ba41SEd Maste static int
ssh_ed25519_sk_deserialize_private(const char * ktype,struct sshbuf * b,struct sshkey * key)112*f374ba41SEd Maste ssh_ed25519_sk_deserialize_private(const char *ktype, struct sshbuf *b,
113*f374ba41SEd Maste struct sshkey *key)
114*f374ba41SEd Maste {
115*f374ba41SEd Maste int r;
116*f374ba41SEd Maste
117*f374ba41SEd Maste if ((r = sshkey_ed25519_funcs.deserialize_public(ktype, b, key)) != 0)
118*f374ba41SEd Maste return r;
119*f374ba41SEd Maste if ((r = sshkey_private_deserialize_sk(b, key)) != 0)
120*f374ba41SEd Maste return r;
121*f374ba41SEd Maste return 0;
122*f374ba41SEd Maste }
123*f374ba41SEd Maste
124*f374ba41SEd Maste 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)12519261079SEd Maste ssh_ed25519_sk_verify(const struct sshkey *key,
126*f374ba41SEd Maste const u_char *sig, size_t siglen,
127*f374ba41SEd Maste const u_char *data, size_t dlen, const char *alg, u_int compat,
12819261079SEd Maste struct sshkey_sig_details **detailsp)
12919261079SEd Maste {
13019261079SEd Maste struct sshbuf *b = NULL;
13119261079SEd Maste struct sshbuf *encoded = NULL;
13219261079SEd Maste char *ktype = NULL;
13319261079SEd Maste const u_char *sigblob;
13419261079SEd Maste const u_char *sm;
13519261079SEd Maste u_char *m = NULL;
13619261079SEd Maste u_char apphash[32];
13719261079SEd Maste u_char msghash[32];
13819261079SEd Maste u_char sig_flags;
13919261079SEd Maste u_int sig_counter;
14019261079SEd Maste size_t len;
14119261079SEd Maste unsigned long long smlen = 0, mlen = 0;
14219261079SEd Maste int r = SSH_ERR_INTERNAL_ERROR;
14319261079SEd Maste int ret;
14419261079SEd Maste struct sshkey_sig_details *details = NULL;
14519261079SEd Maste
14619261079SEd Maste if (detailsp != NULL)
14719261079SEd Maste *detailsp = NULL;
14819261079SEd Maste
14919261079SEd Maste if (key == NULL ||
15019261079SEd Maste sshkey_type_plain(key->type) != KEY_ED25519_SK ||
15119261079SEd Maste key->ed25519_pk == NULL ||
152*f374ba41SEd Maste sig == NULL || siglen == 0)
15319261079SEd Maste return SSH_ERR_INVALID_ARGUMENT;
15419261079SEd Maste
155*f374ba41SEd Maste if ((b = sshbuf_from(sig, siglen)) == NULL)
15619261079SEd Maste return SSH_ERR_ALLOC_FAIL;
15719261079SEd Maste if (sshbuf_get_cstring(b, &ktype, NULL) != 0 ||
15819261079SEd Maste sshbuf_get_string_direct(b, &sigblob, &len) != 0 ||
15919261079SEd Maste sshbuf_get_u8(b, &sig_flags) != 0 ||
16019261079SEd Maste sshbuf_get_u32(b, &sig_counter) != 0) {
16119261079SEd Maste r = SSH_ERR_INVALID_FORMAT;
16219261079SEd Maste goto out;
16319261079SEd Maste }
16419261079SEd Maste #ifdef DEBUG_SK
16519261079SEd Maste fprintf(stderr, "%s: data:\n", __func__);
16619261079SEd Maste /* sshbuf_dump_data(data, datalen, stderr); */
16719261079SEd Maste fprintf(stderr, "%s: sigblob:\n", __func__);
16819261079SEd Maste sshbuf_dump_data(sigblob, len, stderr);
16919261079SEd Maste fprintf(stderr, "%s: sig_flags = 0x%02x, sig_counter = %u\n",
17019261079SEd Maste __func__, sig_flags, sig_counter);
17119261079SEd Maste #endif
17219261079SEd Maste if (strcmp(sshkey_ssh_name_plain(key), ktype) != 0) {
17319261079SEd Maste r = SSH_ERR_KEY_TYPE_MISMATCH;
17419261079SEd Maste goto out;
17519261079SEd Maste }
17619261079SEd Maste if (sshbuf_len(b) != 0) {
17719261079SEd Maste r = SSH_ERR_UNEXPECTED_TRAILING_DATA;
17819261079SEd Maste goto out;
17919261079SEd Maste }
18019261079SEd Maste if (len > crypto_sign_ed25519_BYTES) {
18119261079SEd Maste r = SSH_ERR_INVALID_FORMAT;
18219261079SEd Maste goto out;
18319261079SEd Maste }
18419261079SEd Maste if (ssh_digest_memory(SSH_DIGEST_SHA256, key->sk_application,
18519261079SEd Maste strlen(key->sk_application), apphash, sizeof(apphash)) != 0 ||
186*f374ba41SEd Maste ssh_digest_memory(SSH_DIGEST_SHA256, data, dlen,
18719261079SEd Maste msghash, sizeof(msghash)) != 0) {
18819261079SEd Maste r = SSH_ERR_INVALID_ARGUMENT;
18919261079SEd Maste goto out;
19019261079SEd Maste }
19119261079SEd Maste #ifdef DEBUG_SK
19219261079SEd Maste fprintf(stderr, "%s: hashed application:\n", __func__);
19319261079SEd Maste sshbuf_dump_data(apphash, sizeof(apphash), stderr);
19419261079SEd Maste fprintf(stderr, "%s: hashed message:\n", __func__);
19519261079SEd Maste sshbuf_dump_data(msghash, sizeof(msghash), stderr);
19619261079SEd Maste #endif
19719261079SEd Maste if ((details = calloc(1, sizeof(*details))) == NULL) {
19819261079SEd Maste r = SSH_ERR_ALLOC_FAIL;
19919261079SEd Maste goto out;
20019261079SEd Maste }
20119261079SEd Maste details->sk_counter = sig_counter;
20219261079SEd Maste details->sk_flags = sig_flags;
20319261079SEd Maste if ((encoded = sshbuf_new()) == NULL) {
20419261079SEd Maste r = SSH_ERR_ALLOC_FAIL;
20519261079SEd Maste goto out;
20619261079SEd Maste }
20719261079SEd Maste if (sshbuf_put(encoded, sigblob, len) != 0 ||
20819261079SEd Maste sshbuf_put(encoded, apphash, sizeof(apphash)) != 0 ||
20919261079SEd Maste sshbuf_put_u8(encoded, sig_flags) != 0 ||
21019261079SEd Maste sshbuf_put_u32(encoded, sig_counter) != 0 ||
21119261079SEd Maste sshbuf_put(encoded, msghash, sizeof(msghash)) != 0) {
21219261079SEd Maste r = SSH_ERR_ALLOC_FAIL;
21319261079SEd Maste goto out;
21419261079SEd Maste }
21519261079SEd Maste #ifdef DEBUG_SK
21619261079SEd Maste fprintf(stderr, "%s: signed buf:\n", __func__);
21719261079SEd Maste sshbuf_dump(encoded, stderr);
21819261079SEd Maste #endif
21919261079SEd Maste sm = sshbuf_ptr(encoded);
22019261079SEd Maste smlen = sshbuf_len(encoded);
22119261079SEd Maste mlen = smlen;
22219261079SEd Maste if ((m = malloc(smlen)) == NULL) {
22319261079SEd Maste r = SSH_ERR_ALLOC_FAIL;
22419261079SEd Maste goto out;
22519261079SEd Maste }
22619261079SEd Maste if ((ret = crypto_sign_ed25519_open(m, &mlen, sm, smlen,
22719261079SEd Maste key->ed25519_pk)) != 0) {
22819261079SEd Maste debug2_f("crypto_sign_ed25519_open failed: %d", ret);
22919261079SEd Maste }
23019261079SEd Maste if (ret != 0 || mlen != smlen - len) {
23119261079SEd Maste r = SSH_ERR_SIGNATURE_INVALID;
23219261079SEd Maste goto out;
23319261079SEd Maste }
23419261079SEd Maste /* XXX compare 'm' and 'sm + len' ? */
23519261079SEd Maste /* success */
23619261079SEd Maste r = 0;
23719261079SEd Maste if (detailsp != NULL) {
23819261079SEd Maste *detailsp = details;
23919261079SEd Maste details = NULL;
24019261079SEd Maste }
24119261079SEd Maste out:
24219261079SEd Maste if (m != NULL)
24319261079SEd Maste freezero(m, smlen); /* NB mlen may be invalid if r != 0 */
24419261079SEd Maste sshkey_sig_details_free(details);
24519261079SEd Maste sshbuf_free(b);
24619261079SEd Maste sshbuf_free(encoded);
24719261079SEd Maste free(ktype);
24819261079SEd Maste return r;
24919261079SEd Maste }
250*f374ba41SEd Maste
251*f374ba41SEd Maste static const struct sshkey_impl_funcs sshkey_ed25519_sk_funcs = {
252*f374ba41SEd Maste /* .size = */ NULL,
253*f374ba41SEd Maste /* .alloc = */ NULL,
254*f374ba41SEd Maste /* .cleanup = */ ssh_ed25519_sk_cleanup,
255*f374ba41SEd Maste /* .equal = */ ssh_ed25519_sk_equal,
256*f374ba41SEd Maste /* .ssh_serialize_public = */ ssh_ed25519_sk_serialize_public,
257*f374ba41SEd Maste /* .ssh_deserialize_public = */ ssh_ed25519_sk_deserialize_public,
258*f374ba41SEd Maste /* .ssh_serialize_private = */ ssh_ed25519_sk_serialize_private,
259*f374ba41SEd Maste /* .ssh_deserialize_private = */ ssh_ed25519_sk_deserialize_private,
260*f374ba41SEd Maste /* .generate = */ NULL,
261*f374ba41SEd Maste /* .copy_public = */ ssh_ed25519_sk_copy_public,
262*f374ba41SEd Maste /* .sign = */ NULL,
263*f374ba41SEd Maste /* .verify = */ ssh_ed25519_sk_verify,
264*f374ba41SEd Maste };
265*f374ba41SEd Maste
266*f374ba41SEd Maste const struct sshkey_impl sshkey_ed25519_sk_impl = {
267*f374ba41SEd Maste /* .name = */ "sk-ssh-ed25519@openssh.com",
268*f374ba41SEd Maste /* .shortname = */ "ED25519-SK",
269*f374ba41SEd Maste /* .sigalg = */ NULL,
270*f374ba41SEd Maste /* .type = */ KEY_ED25519_SK,
271*f374ba41SEd Maste /* .nid = */ 0,
272*f374ba41SEd Maste /* .cert = */ 0,
273*f374ba41SEd Maste /* .sigonly = */ 0,
274*f374ba41SEd Maste /* .keybits = */ 256,
275*f374ba41SEd Maste /* .funcs = */ &sshkey_ed25519_sk_funcs,
276*f374ba41SEd Maste };
277*f374ba41SEd Maste
278*f374ba41SEd Maste const struct sshkey_impl sshkey_ed25519_sk_cert_impl = {
279*f374ba41SEd Maste /* .name = */ "sk-ssh-ed25519-cert-v01@openssh.com",
280*f374ba41SEd Maste /* .shortname = */ "ED25519-SK-CERT",
281*f374ba41SEd Maste /* .sigalg = */ NULL,
282*f374ba41SEd Maste /* .type = */ KEY_ED25519_SK_CERT,
283*f374ba41SEd Maste /* .nid = */ 0,
284*f374ba41SEd Maste /* .cert = */ 1,
285*f374ba41SEd Maste /* .sigonly = */ 0,
286*f374ba41SEd Maste /* .keybits = */ 256,
287*f374ba41SEd Maste /* .funcs = */ &sshkey_ed25519_sk_funcs,
288*f374ba41SEd Maste };
289