xref: /netbsd-src/crypto/external/bsd/openssh/dist/ssh-ecdsa.c (revision 796c32c94f6e154afc9de0f63da35c91bb739b45)
1 /*	$NetBSD: ssh-ecdsa.c,v 1.10 2017/04/18 18:41:46 christos Exp $	*/
2 /* $OpenBSD: ssh-ecdsa.c,v 1.13 2016/04/21 06:08:02 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.10 2017/04/18 18:41:46 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 	sshbuf_free(b);
102 	sshbuf_free(bb);
103 	if (sig != NULL)
104 		ECDSA_SIG_free(sig);
105 	return ret;
106 }
107 
108 /* ARGSUSED */
109 int
110 ssh_ecdsa_verify(const struct sshkey *key,
111     const u_char *signature, size_t signaturelen,
112     const u_char *data, size_t datalen, u_int compat)
113 {
114 	ECDSA_SIG *sig = NULL;
115 	int hash_alg;
116 	u_char digest[SSH_DIGEST_MAX_LENGTH];
117 	size_t dlen;
118 	int ret = SSH_ERR_INTERNAL_ERROR;
119 	struct sshbuf *b = NULL, *sigbuf = NULL;
120 	char *ktype = NULL;
121 
122 	if (key == NULL || key->ecdsa == NULL ||
123 	    sshkey_type_plain(key->type) != KEY_ECDSA ||
124 	    signature == NULL || signaturelen == 0)
125 		return SSH_ERR_INVALID_ARGUMENT;
126 
127 	if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1 ||
128 	    (dlen = ssh_digest_bytes(hash_alg)) == 0)
129 		return SSH_ERR_INTERNAL_ERROR;
130 
131 	/* fetch signature */
132 	if ((b = sshbuf_from(signature, signaturelen)) == NULL)
133 		return SSH_ERR_ALLOC_FAIL;
134 	if (sshbuf_get_cstring(b, &ktype, NULL) != 0 ||
135 	    sshbuf_froms(b, &sigbuf) != 0) {
136 		ret = SSH_ERR_INVALID_FORMAT;
137 		goto out;
138 	}
139 	if (strcmp(sshkey_ssh_name_plain(key), ktype) != 0) {
140 		ret = SSH_ERR_KEY_TYPE_MISMATCH;
141 		goto out;
142 	}
143 	if (sshbuf_len(b) != 0) {
144 		ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
145 		goto out;
146 	}
147 
148 	/* parse signature */
149 	if ((sig = ECDSA_SIG_new()) == NULL) {
150 		ret = SSH_ERR_ALLOC_FAIL;
151 		goto out;
152 	}
153 	if (sshbuf_get_bignum2(sigbuf, sig->r) != 0 ||
154 	    sshbuf_get_bignum2(sigbuf, sig->s) != 0) {
155 		ret = SSH_ERR_INVALID_FORMAT;
156 		goto out;
157 	}
158 	if (sshbuf_len(sigbuf) != 0) {
159 		ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
160 		goto out;
161 	}
162 	if ((ret = ssh_digest_memory(hash_alg, data, datalen,
163 	    digest, sizeof(digest))) != 0)
164 		goto out;
165 
166 	switch (ECDSA_do_verify(digest, dlen, sig, key->ecdsa)) {
167 	case 1:
168 		ret = 0;
169 		break;
170 	case 0:
171 		ret = SSH_ERR_SIGNATURE_INVALID;
172 		goto out;
173 	default:
174 		ret = SSH_ERR_LIBCRYPTO_ERROR;
175 		goto out;
176 	}
177 
178  out:
179 	explicit_bzero(digest, sizeof(digest));
180 	sshbuf_free(sigbuf);
181 	sshbuf_free(b);
182 	if (sig != NULL)
183 		ECDSA_SIG_free(sig);
184 	free(ktype);
185 	return ret;
186 }
187