1188943Sthompsa/*	$NetBSD$	*/
2188943Sthompsa/* $OpenBSD: ssh-ecdsa.c,v 1.4 2010/09/10 01:04:10 djm Exp $ */
3188943Sthompsa/*
4188943Sthompsa * Copyright (c) 2000 Markus Friedl.  All rights reserved.
5188943Sthompsa * Copyright (c) 2010 Damien Miller.  All rights reserved.
6188943Sthompsa *
7188943Sthompsa * Redistribution and use in source and binary forms, with or without
8188943Sthompsa * modification, are permitted provided that the following conditions
9188943Sthompsa * are met:
10188943Sthompsa * 1. Redistributions of source code must retain the above copyright
11188943Sthompsa *    notice, this list of conditions and the following disclaimer.
12188943Sthompsa * 2. Redistributions in binary form must reproduce the above copyright
13188943Sthompsa *    notice, this list of conditions and the following disclaimer in the
14188943Sthompsa *    documentation and/or other materials provided with the distribution.
15188943Sthompsa *
16188943Sthompsa * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17188943Sthompsa * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18188943Sthompsa * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19188943Sthompsa * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20188943Sthompsa * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21188943Sthompsa * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22188943Sthompsa * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23188943Sthompsa * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24188943Sthompsa * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25188943Sthompsa * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26188943Sthompsa */
27188943Sthompsa
28319182Sngie#include "includes.h"
29188943Sthompsa__RCSID("$NetBSD: progressmeter.c,v 1.3 2011/06/20 09:11:16 mrg Exp $");
30188943Sthompsa#include <sys/types.h>
31188943Sthompsa
32188943Sthompsa#include <openssl/bn.h>
33188943Sthompsa#include <openssl/ec.h>
34188943Sthompsa#include <openssl/ecdsa.h>
35188943Sthompsa#include <openssl/evp.h>
36188943Sthompsa
37188943Sthompsa#include <string.h>
38188943Sthompsa
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