1/*	$NetBSD: ssh-ecdsa.c,v 1.2 2011/07/25 03:03:11 christos Exp $	*/
2/* $OpenBSD: ssh-ecdsa.c,v 1.4 2010/09/10 01:04:10 djm Exp $ */
3/*
4 * Copyright (c) 2000 Markus Friedl.  All rights reserved.
5 * Copyright (c) 2010 Damien Miller.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "includes.h"
29__RCSID("$NetBSD: ssh-ecdsa.c,v 1.2 2011/07/25 03:03:11 christos Exp $");
30#include <sys/types.h>
31
32#include <openssl/bn.h>
33#include <openssl/ec.h>
34#include <openssl/ecdsa.h>
35#include <openssl/evp.h>
36
37#include <string.h>
38
39#include "xmalloc.h"
40#include "buffer.h"
41#include "compat.h"
42#include "log.h"
43#include "key.h"
44
45int
46ssh_ecdsa_sign(const Key *key, u_char **sigp, u_int *lenp,
47    const u_char *data, u_int datalen)
48{
49	ECDSA_SIG *sig;
50	const EVP_MD *evp_md;
51	EVP_MD_CTX md;
52	u_char digest[EVP_MAX_MD_SIZE];
53	u_int len, dlen;
54	Buffer b, bb;
55
56	if (key == NULL || key->ecdsa == NULL ||
57	    (key->type != KEY_ECDSA && key->type != KEY_ECDSA_CERT)) {
58		error("%s: no ECDSA key", __func__);
59		return -1;
60	}
61	evp_md = key_ec_nid_to_evpmd(key->ecdsa_nid);
62	EVP_DigestInit(&md, evp_md);
63	EVP_DigestUpdate(&md, data, datalen);
64	EVP_DigestFinal(&md, digest, &dlen);
65
66	sig = ECDSA_do_sign(digest, dlen, key->ecdsa);
67	memset(digest, 'd', sizeof(digest));
68
69	if (sig == NULL) {
70		error("%s: sign failed", __func__);
71		return -1;
72	}
73
74	buffer_init(&bb);
75	buffer_put_bignum2(&bb, sig->r);
76	buffer_put_bignum2(&bb, sig->s);
77	ECDSA_SIG_free(sig);
78
79	buffer_init(&b);
80	buffer_put_cstring(&b, key_ssh_name_plain(key));
81	buffer_put_string(&b, buffer_ptr(&bb), buffer_len(&bb));
82	buffer_free(&bb);
83	len = buffer_len(&b);
84	if (lenp != NULL)
85		*lenp = len;
86	if (sigp != NULL) {
87		*sigp = xmalloc(len);
88		memcpy(*sigp, buffer_ptr(&b), len);
89	}
90	buffer_free(&b);
91
92	return 0;
93}
94int
95ssh_ecdsa_verify(const Key *key, const u_char *signature, u_int signaturelen,
96    const u_char *data, u_int datalen)
97{
98	ECDSA_SIG *sig;
99	const EVP_MD *evp_md;
100	EVP_MD_CTX md;
101	u_char digest[EVP_MAX_MD_SIZE], *sigblob;
102	u_int len, dlen;
103	int rlen, ret;
104	Buffer b, bb;
105	char *ktype;
106
107	if (key == NULL || key->ecdsa == NULL ||
108	    (key->type != KEY_ECDSA && key->type != KEY_ECDSA_CERT)) {
109		error("%s: no ECDSA key", __func__);
110		return -1;
111	}
112	evp_md = key_ec_nid_to_evpmd(key->ecdsa_nid);
113
114	/* fetch signature */
115	buffer_init(&b);
116	buffer_append(&b, signature, signaturelen);
117	ktype = buffer_get_string(&b, NULL);
118	if (strcmp(key_ssh_name_plain(key), ktype) != 0) {
119		error("%s: cannot handle type %s", __func__, ktype);
120		buffer_free(&b);
121		xfree(ktype);
122		return -1;
123	}
124	xfree(ktype);
125	sigblob = buffer_get_string(&b, &len);
126	rlen = buffer_len(&b);
127	buffer_free(&b);
128	if (rlen != 0) {
129		error("%s: remaining bytes in signature %d", __func__, rlen);
130		xfree(sigblob);
131		return -1;
132	}
133
134	/* parse signature */
135	if ((sig = ECDSA_SIG_new()) == NULL)
136		fatal("%s: ECDSA_SIG_new failed", __func__);
137	if ((sig->r = BN_new()) == NULL ||
138	    (sig->s = BN_new()) == NULL)
139		fatal("%s: BN_new failed", __func__);
140
141	buffer_init(&bb);
142	buffer_append(&bb, sigblob, len);
143	buffer_get_bignum2(&bb, sig->r);
144	buffer_get_bignum2(&bb, sig->s);
145	if (buffer_len(&bb) != 0)
146		fatal("%s: remaining bytes in inner sigblob", __func__);
147
148	/* clean up */
149	memset(sigblob, 0, len);
150	xfree(sigblob);
151
152	/* hash the data */
153	EVP_DigestInit(&md, evp_md);
154	EVP_DigestUpdate(&md, data, datalen);
155	EVP_DigestFinal(&md, digest, &dlen);
156
157	ret = ECDSA_do_verify(digest, dlen, sig, key->ecdsa);
158	memset(digest, 'd', sizeof(digest));
159
160	ECDSA_SIG_free(sig);
161
162	debug("%s: signature %s", __func__,
163	    ret == 1 ? "correct" : ret == 0 ? "incorrect" : "error");
164	return ret;
165}
166