xref: /openbsd-src/usr.bin/ssh/ssh-ecdsa.c (revision e5157e49389faebcb42b7237d55fbf096d9c2523)
1 /* $OpenBSD: ssh-ecdsa.c,v 1.11 2014/06/24 01:13:21 djm Exp $ */
2 /*
3  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
4  * Copyright (c) 2010 Damien Miller.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/types.h>
28 
29 #include <openssl/bn.h>
30 #include <openssl/ec.h>
31 #include <openssl/ecdsa.h>
32 #include <openssl/evp.h>
33 
34 #include <string.h>
35 
36 #include "sshbuf.h"
37 #include "ssherr.h"
38 #include "digest.h"
39 #define SSHKEY_INTERNAL
40 #include "sshkey.h"
41 
42 /* ARGSUSED */
43 int
44 ssh_ecdsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
45     const u_char *data, size_t datalen, u_int compat)
46 {
47 	ECDSA_SIG *sig = NULL;
48 	int hash_alg;
49 	u_char digest[SSH_DIGEST_MAX_LENGTH];
50 	size_t len, dlen;
51 	struct sshbuf *b = NULL, *bb = NULL;
52 	int ret = SSH_ERR_INTERNAL_ERROR;
53 
54 	if (lenp != NULL)
55 		*lenp = 0;
56 	if (sigp != NULL)
57 		*sigp = NULL;
58 
59 	if (key == NULL || key->ecdsa == NULL ||
60 	    sshkey_type_plain(key->type) != KEY_ECDSA)
61 		return SSH_ERR_INVALID_ARGUMENT;
62 
63 	if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1 ||
64 	    (dlen = ssh_digest_bytes(hash_alg)) == 0)
65 		return SSH_ERR_INTERNAL_ERROR;
66 	if ((ret = ssh_digest_memory(hash_alg, data, datalen,
67 	    digest, sizeof(digest))) != 0)
68 		goto out;
69 
70 	if ((sig = ECDSA_do_sign(digest, dlen, key->ecdsa)) == NULL) {
71 		ret = SSH_ERR_LIBCRYPTO_ERROR;
72 		goto out;
73 	}
74 
75 	if ((bb = sshbuf_new()) == NULL || (b = sshbuf_new()) == NULL) {
76 		ret = SSH_ERR_ALLOC_FAIL;
77 		goto out;
78 	}
79 	if ((ret = sshbuf_put_bignum2(bb, sig->r)) != 0 ||
80 	    (ret = sshbuf_put_bignum2(bb, sig->s)) != 0)
81 		goto out;
82 	if ((ret = sshbuf_put_cstring(b, sshkey_ssh_name_plain(key))) != 0 ||
83 	    (ret = sshbuf_put_stringb(b, bb)) != 0)
84 		goto out;
85 	len = sshbuf_len(b);
86 	if (sigp != NULL) {
87 		if ((*sigp = malloc(len)) == NULL) {
88 			ret = SSH_ERR_ALLOC_FAIL;
89 			goto out;
90 		}
91 		memcpy(*sigp, sshbuf_ptr(b), len);
92 	}
93 	if (lenp != NULL)
94 		*lenp = len;
95 	ret = 0;
96  out:
97 	explicit_bzero(digest, sizeof(digest));
98 	if (b != NULL)
99 		sshbuf_free(b);
100 	if (bb != NULL)
101 		sshbuf_free(bb);
102 	if (sig != NULL)
103 		ECDSA_SIG_free(sig);
104 	return ret;
105 }
106 
107 /* ARGSUSED */
108 int
109 ssh_ecdsa_verify(const struct sshkey *key,
110     const u_char *signature, size_t signaturelen,
111     const u_char *data, size_t datalen, u_int compat)
112 {
113 	ECDSA_SIG *sig = NULL;
114 	int hash_alg;
115 	u_char digest[SSH_DIGEST_MAX_LENGTH];
116 	size_t dlen;
117 	int ret = SSH_ERR_INTERNAL_ERROR;
118 	struct sshbuf *b = NULL, *sigbuf = NULL;
119 	char *ktype = NULL;
120 
121 	if (key == NULL || key->ecdsa == NULL ||
122 	    sshkey_type_plain(key->type) != KEY_ECDSA)
123 		return SSH_ERR_INVALID_ARGUMENT;
124 
125 	if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1 ||
126 	    (dlen = ssh_digest_bytes(hash_alg)) == 0)
127 		return SSH_ERR_INTERNAL_ERROR;
128 
129 	/* fetch signature */
130 	if ((b = sshbuf_from(signature, signaturelen)) == NULL)
131 		return SSH_ERR_ALLOC_FAIL;
132 	if (sshbuf_get_cstring(b, &ktype, NULL) != 0 ||
133 	    sshbuf_froms(b, &sigbuf) != 0) {
134 		ret = SSH_ERR_INVALID_FORMAT;
135 		goto out;
136 	}
137 	if (strcmp(sshkey_ssh_name_plain(key), ktype) != 0) {
138 		ret = SSH_ERR_KEY_TYPE_MISMATCH;
139 		goto out;
140 	}
141 	if (sshbuf_len(b) != 0) {
142 		ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
143 		goto out;
144 	}
145 
146 	/* parse signature */
147 	if ((sig = ECDSA_SIG_new()) == NULL) {
148 		ret = SSH_ERR_ALLOC_FAIL;
149 		goto out;
150 	}
151 	if (sshbuf_get_bignum2(sigbuf, sig->r) != 0 ||
152 	    sshbuf_get_bignum2(sigbuf, sig->s) != 0) {
153 		ret = SSH_ERR_INVALID_FORMAT;
154 		goto out;
155 	}
156 	if (sshbuf_len(sigbuf) != 0) {
157 		ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
158 		goto out;
159 	}
160 	if ((ret = ssh_digest_memory(hash_alg, data, datalen,
161 	    digest, sizeof(digest))) != 0)
162 		goto out;
163 
164 	switch (ECDSA_do_verify(digest, dlen, sig, key->ecdsa)) {
165 	case 1:
166 		ret = 0;
167 		break;
168 	case 0:
169 		ret = SSH_ERR_SIGNATURE_INVALID;
170 		goto out;
171 	default:
172 		ret = SSH_ERR_LIBCRYPTO_ERROR;
173 		goto out;
174 	}
175 
176  out:
177 	explicit_bzero(digest, sizeof(digest));
178 	if (sigbuf != NULL)
179 		sshbuf_free(sigbuf);
180 	if (b != NULL)
181 		sshbuf_free(b);
182 	if (sig != NULL)
183 		ECDSA_SIG_free(sig);
184 	free(ktype);
185 	return ret;
186 }
187