blob: 3d3b78d7b42e3a6579d84a6bae5b18d39afd5770 [file] [log] [blame]
jsing@openbsd.org7cd31632018-02-07 02:06:50 +00001/* $OpenBSD: ssh-ecdsa.c,v 1.14 2018/02/07 02:06:51 jsing Exp $ */
Damien Millereb8b60e2010-08-31 22:41:14 +10002/*
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
Damien Millerc79ff072010-08-31 22:50:48 +100027#include "includes.h"
28
Damien Miller72ef7c12015-01-15 02:21:31 +110029#if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
Damien Miller6af914a2010-09-10 11:39:26 +100030
Damien Millereb8b60e2010-08-31 22:41:14 +100031#include <sys/types.h>
32
33#include <openssl/bn.h>
34#include <openssl/ec.h>
35#include <openssl/ecdsa.h>
36#include <openssl/evp.h>
37
38#include <string.h>
39
Damien Miller86687062014-07-02 15:28:02 +100040#include "sshbuf.h"
41#include "ssherr.h"
Damien Millerb3051d02014-01-10 10:58:53 +110042#include "digest.h"
Damien Miller86687062014-07-02 15:28:02 +100043#define SSHKEY_INTERNAL
44#include "sshkey.h"
Damien Millereb8b60e2010-08-31 22:41:14 +100045
Damien Miller86687062014-07-02 15:28:02 +100046/* ARGSUSED */
Damien Millereb8b60e2010-08-31 22:41:14 +100047int
Damien Miller86687062014-07-02 15:28:02 +100048ssh_ecdsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
49 const u_char *data, size_t datalen, u_int compat)
Damien Millereb8b60e2010-08-31 22:41:14 +100050{
Damien Miller86687062014-07-02 15:28:02 +100051 ECDSA_SIG *sig = NULL;
Damien Millerb3051d02014-01-10 10:58:53 +110052 int hash_alg;
53 u_char digest[SSH_DIGEST_MAX_LENGTH];
Damien Miller86687062014-07-02 15:28:02 +100054 size_t len, dlen;
55 struct sshbuf *b = NULL, *bb = NULL;
56 int ret = SSH_ERR_INTERNAL_ERROR;
Damien Millereb8b60e2010-08-31 22:41:14 +100057
Damien Miller86687062014-07-02 15:28:02 +100058 if (lenp != NULL)
59 *lenp = 0;
60 if (sigp != NULL)
61 *sigp = NULL;
62
63 if (key == NULL || key->ecdsa == NULL ||
64 sshkey_type_plain(key->type) != KEY_ECDSA)
65 return SSH_ERR_INVALID_ARGUMENT;
66
67 if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1 ||
68 (dlen = ssh_digest_bytes(hash_alg)) == 0)
69 return SSH_ERR_INTERNAL_ERROR;
70 if ((ret = ssh_digest_memory(hash_alg, data, datalen,
71 digest, sizeof(digest))) != 0)
72 goto out;
73
74 if ((sig = ECDSA_do_sign(digest, dlen, key->ecdsa)) == NULL) {
75 ret = SSH_ERR_LIBCRYPTO_ERROR;
76 goto out;
Damien Millereb8b60e2010-08-31 22:41:14 +100077 }
Damien Miller3e192952013-12-29 17:47:50 +110078
Damien Miller86687062014-07-02 15:28:02 +100079 if ((bb = sshbuf_new()) == NULL || (b = sshbuf_new()) == NULL) {
80 ret = SSH_ERR_ALLOC_FAIL;
81 goto out;
Damien Millerb3051d02014-01-10 10:58:53 +110082 }
Damien Miller86687062014-07-02 15:28:02 +100083 if ((ret = sshbuf_put_bignum2(bb, sig->r)) != 0 ||
84 (ret = sshbuf_put_bignum2(bb, sig->s)) != 0)
85 goto out;
86 if ((ret = sshbuf_put_cstring(b, sshkey_ssh_name_plain(key))) != 0 ||
87 (ret = sshbuf_put_stringb(b, bb)) != 0)
88 goto out;
89 len = sshbuf_len(b);
90 if (sigp != NULL) {
91 if ((*sigp = malloc(len)) == NULL) {
92 ret = SSH_ERR_ALLOC_FAIL;
93 goto out;
94 }
95 memcpy(*sigp, sshbuf_ptr(b), len);
Damien Millerb3051d02014-01-10 10:58:53 +110096 }
Damien Millereb8b60e2010-08-31 22:41:14 +100097 if (lenp != NULL)
98 *lenp = len;
Damien Miller86687062014-07-02 15:28:02 +100099 ret = 0;
100 out:
101 explicit_bzero(digest, sizeof(digest));
mmcc@openbsd.org52d70782015-12-11 04:21:11 +0000102 sshbuf_free(b);
103 sshbuf_free(bb);
jsing@openbsd.org7cd31632018-02-07 02:06:50 +0000104 ECDSA_SIG_free(sig);
Damien Miller86687062014-07-02 15:28:02 +1000105 return ret;
Damien Millereb8b60e2010-08-31 22:41:14 +1000106}
Damien Millereb8b60e2010-08-31 22:41:14 +1000107
Damien Miller86687062014-07-02 15:28:02 +1000108/* ARGSUSED */
109int
110ssh_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 ||
djm@openbsd.orgb6e01402016-04-21 06:08:02 +0000123 sshkey_type_plain(key->type) != KEY_ECDSA ||
124 signature == NULL || signaturelen == 0)
Damien Miller86687062014-07-02 15:28:02 +1000125 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;
Damien Miller3e192952013-12-29 17:47:50 +1100130
Damien Millereb8b60e2010-08-31 22:41:14 +1000131 /* fetch signature */
Damien Miller86687062014-07-02 15:28:02 +1000132 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;
Damien Millereb8b60e2010-08-31 22:41:14 +1000138 }
Damien Miller86687062014-07-02 15:28:02 +1000139 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;
Damien Millereb8b60e2010-08-31 22:41:14 +1000146 }
147
148 /* parse signature */
Damien Miller86687062014-07-02 15:28:02 +1000149 if ((sig = ECDSA_SIG_new()) == NULL) {
150 ret = SSH_ERR_ALLOC_FAIL;
151 goto out;
Damien Millerb3051d02014-01-10 10:58:53 +1100152 }
Damien Miller86687062014-07-02 15:28:02 +1000153 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;
Damien Millerb3051d02014-01-10 10:58:53 +1100176 }
Damien Millereb8b60e2010-08-31 22:41:14 +1000177
Damien Miller86687062014-07-02 15:28:02 +1000178 out:
Damien Millera5103f42014-02-04 11:20:14 +1100179 explicit_bzero(digest, sizeof(digest));
mmcc@openbsd.org52d70782015-12-11 04:21:11 +0000180 sshbuf_free(sigbuf);
181 sshbuf_free(b);
jsing@openbsd.org7cd31632018-02-07 02:06:50 +0000182 ECDSA_SIG_free(sig);
Damien Miller86687062014-07-02 15:28:02 +1000183 free(ktype);
Damien Millereb8b60e2010-08-31 22:41:14 +1000184 return ret;
185}
Damien Miller6af914a2010-09-10 11:39:26 +1000186
Damien Miller72ef7c12015-01-15 02:21:31 +1100187#endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */