xref: /netbsd-src/crypto/external/bsd/openssh/dist/ssh-ecdsa.c (revision f89f6560d453f5e37386cc7938c072d2f528b9fa)
1 /*	$NetBSD: ssh-ecdsa.c,v 1.6 2015/04/03 23:58:19 christos Exp $	*/
2 /* $OpenBSD: ssh-ecdsa.c,v 1.11 2014/06/24 01:13:21 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.6 2015/04/03 23:58:19 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 "sshbuf.h"
40 #include "ssherr.h"
41 #include "digest.h"
42 #define SSHKEY_INTERNAL
43 #include "sshkey.h"
44 
45 /* ARGSUSED */
46 int
47 ssh_ecdsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
48     const u_char *data, size_t datalen, u_int compat)
49 {
50 	ECDSA_SIG *sig = NULL;
51 	int hash_alg;
52 	u_char digest[SSH_DIGEST_MAX_LENGTH];
53 	size_t len, dlen;
54 	struct sshbuf *b = NULL, *bb = NULL;
55 	int ret = SSH_ERR_INTERNAL_ERROR;
56 
57 	if (lenp != NULL)
58 		*lenp = 0;
59 	if (sigp != NULL)
60 		*sigp = NULL;
61 
62 	if (key == NULL || key->ecdsa == NULL ||
63 	    sshkey_type_plain(key->type) != KEY_ECDSA)
64 		return SSH_ERR_INVALID_ARGUMENT;
65 
66 	if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1 ||
67 	    (dlen = ssh_digest_bytes(hash_alg)) == 0)
68 		return SSH_ERR_INTERNAL_ERROR;
69 	if ((ret = ssh_digest_memory(hash_alg, data, datalen,
70 	    digest, sizeof(digest))) != 0)
71 		goto out;
72 
73 	if ((sig = ECDSA_do_sign(digest, dlen, key->ecdsa)) == NULL) {
74 		ret = SSH_ERR_LIBCRYPTO_ERROR;
75 		goto out;
76 	}
77 
78 	if ((bb = sshbuf_new()) == NULL || (b = sshbuf_new()) == NULL) {
79 		ret = SSH_ERR_ALLOC_FAIL;
80 		goto out;
81 	}
82 	if ((ret = sshbuf_put_bignum2(bb, sig->r)) != 0 ||
83 	    (ret = sshbuf_put_bignum2(bb, sig->s)) != 0)
84 		goto out;
85 	if ((ret = sshbuf_put_cstring(b, sshkey_ssh_name_plain(key))) != 0 ||
86 	    (ret = sshbuf_put_stringb(b, bb)) != 0)
87 		goto out;
88 	len = sshbuf_len(b);
89 	if (sigp != NULL) {
90 		if ((*sigp = malloc(len)) == NULL) {
91 			ret = SSH_ERR_ALLOC_FAIL;
92 			goto out;
93 		}
94 		memcpy(*sigp, sshbuf_ptr(b), len);
95 	}
96 	if (lenp != NULL)
97 		*lenp = len;
98 	ret = 0;
99  out:
100 	explicit_bzero(digest, sizeof(digest));
101 	if (b != NULL)
102 		sshbuf_free(b);
103 	if (bb != NULL)
104 		sshbuf_free(bb);
105 	if (sig != NULL)
106 		ECDSA_SIG_free(sig);
107 	return ret;
108 }
109 
110 /* ARGSUSED */
111 int
112 ssh_ecdsa_verify(const struct sshkey *key,
113     const u_char *signature, size_t signaturelen,
114     const u_char *data, size_t datalen, u_int compat)
115 {
116 	ECDSA_SIG *sig = NULL;
117 	int hash_alg;
118 	u_char digest[SSH_DIGEST_MAX_LENGTH];
119 	size_t dlen;
120 	int ret = SSH_ERR_INTERNAL_ERROR;
121 	struct sshbuf *b = NULL, *sigbuf = NULL;
122 	char *ktype = NULL;
123 
124 	if (key == NULL || key->ecdsa == NULL ||
125 	    sshkey_type_plain(key->type) != KEY_ECDSA)
126 		return SSH_ERR_INVALID_ARGUMENT;
127 
128 	if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1 ||
129 	    (dlen = ssh_digest_bytes(hash_alg)) == 0)
130 		return SSH_ERR_INTERNAL_ERROR;
131 
132 	/* fetch signature */
133 	if ((b = sshbuf_from(signature, signaturelen)) == NULL)
134 		return SSH_ERR_ALLOC_FAIL;
135 	if (sshbuf_get_cstring(b, &ktype, NULL) != 0 ||
136 	    sshbuf_froms(b, &sigbuf) != 0) {
137 		ret = SSH_ERR_INVALID_FORMAT;
138 		goto out;
139 	}
140 	if (strcmp(sshkey_ssh_name_plain(key), ktype) != 0) {
141 		ret = SSH_ERR_KEY_TYPE_MISMATCH;
142 		goto out;
143 	}
144 	if (sshbuf_len(b) != 0) {
145 		ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
146 		goto out;
147 	}
148 
149 	/* parse signature */
150 	if ((sig = ECDSA_SIG_new()) == NULL) {
151 		ret = SSH_ERR_ALLOC_FAIL;
152 		goto out;
153 	}
154 	if (sshbuf_get_bignum2(sigbuf, sig->r) != 0 ||
155 	    sshbuf_get_bignum2(sigbuf, sig->s) != 0) {
156 		ret = SSH_ERR_INVALID_FORMAT;
157 		goto out;
158 	}
159 	if (sshbuf_len(sigbuf) != 0) {
160 		ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
161 		goto out;
162 	}
163 	if ((ret = ssh_digest_memory(hash_alg, data, datalen,
164 	    digest, sizeof(digest))) != 0)
165 		goto out;
166 
167 	switch (ECDSA_do_verify(digest, dlen, sig, key->ecdsa)) {
168 	case 1:
169 		ret = 0;
170 		break;
171 	case 0:
172 		ret = SSH_ERR_SIGNATURE_INVALID;
173 		goto out;
174 	default:
175 		ret = SSH_ERR_LIBCRYPTO_ERROR;
176 		goto out;
177 	}
178 
179  out:
180 	explicit_bzero(digest, sizeof(digest));
181 	if (sigbuf != NULL)
182 		sshbuf_free(sigbuf);
183 	if (b != NULL)
184 		sshbuf_free(b);
185 	if (sig != NULL)
186 		ECDSA_SIG_free(sig);
187 	free(ktype);
188 	return ret;
189 }
190