1 /* $OpenBSD: ssh-ecdsa.c,v 1.16 2019/01/21 09:54:11 djm Exp $ */
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Copyright (c) 2010 Damien Miller. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
29 #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
31 #include <sys/types.h>
33 #include <openssl/bn.h>
34 #include <openssl/ec.h>
35 #include <openssl/ecdsa.h>
36 #include <openssl/evp.h>
43 #define SSHKEY_INTERNAL
46 #include "openbsd-compat/openssl-compat.h"
50 ssh_ecdsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
51 const u_char *data, size_t datalen, u_int compat)
53 ECDSA_SIG *sig = NULL;
54 const BIGNUM *sig_r, *sig_s;
56 u_char digest[SSH_DIGEST_MAX_LENGTH];
58 struct sshbuf *b = NULL, *bb = NULL;
59 int ret = SSH_ERR_INTERNAL_ERROR;
66 if (key == NULL || key->ecdsa == NULL ||
67 sshkey_type_plain(key->type) != KEY_ECDSA)
68 return SSH_ERR_INVALID_ARGUMENT;
70 if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1 ||
71 (dlen = ssh_digest_bytes(hash_alg)) == 0)
72 return SSH_ERR_INTERNAL_ERROR;
73 if ((ret = ssh_digest_memory(hash_alg, data, datalen,
74 digest, sizeof(digest))) != 0)
77 if ((sig = ECDSA_do_sign(digest, dlen, key->ecdsa)) == NULL) {
78 ret = SSH_ERR_LIBCRYPTO_ERROR;
82 if ((bb = sshbuf_new()) == NULL || (b = sshbuf_new()) == NULL) {
83 ret = SSH_ERR_ALLOC_FAIL;
86 ECDSA_SIG_get0(sig, &sig_r, &sig_s);
87 if ((ret = sshbuf_put_bignum2(bb, sig_r)) != 0 ||
88 (ret = sshbuf_put_bignum2(bb, sig_s)) != 0)
90 if ((ret = sshbuf_put_cstring(b, sshkey_ssh_name_plain(key))) != 0 ||
91 (ret = sshbuf_put_stringb(b, bb)) != 0)
95 if ((*sigp = malloc(len)) == NULL) {
96 ret = SSH_ERR_ALLOC_FAIL;
99 memcpy(*sigp, sshbuf_ptr(b), len);
105 explicit_bzero(digest, sizeof(digest));
114 ssh_ecdsa_verify(const struct sshkey *key,
115 const u_char *signature, size_t signaturelen,
116 const u_char *data, size_t datalen, u_int compat)
118 ECDSA_SIG *sig = NULL;
119 BIGNUM *sig_r = NULL, *sig_s = NULL;
121 u_char digest[SSH_DIGEST_MAX_LENGTH];
123 int ret = SSH_ERR_INTERNAL_ERROR;
124 struct sshbuf *b = NULL, *sigbuf = NULL;
127 if (key == NULL || key->ecdsa == NULL ||
128 sshkey_type_plain(key->type) != KEY_ECDSA ||
129 signature == NULL || signaturelen == 0)
130 return SSH_ERR_INVALID_ARGUMENT;
132 if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1 ||
133 (dlen = ssh_digest_bytes(hash_alg)) == 0)
134 return SSH_ERR_INTERNAL_ERROR;
136 /* fetch signature */
137 if ((b = sshbuf_from(signature, signaturelen)) == NULL)
138 return SSH_ERR_ALLOC_FAIL;
139 if (sshbuf_get_cstring(b, &ktype, NULL) != 0 ||
140 sshbuf_froms(b, &sigbuf) != 0) {
141 ret = SSH_ERR_INVALID_FORMAT;
144 if (strcmp(sshkey_ssh_name_plain(key), ktype) != 0) {
145 ret = SSH_ERR_KEY_TYPE_MISMATCH;
148 if (sshbuf_len(b) != 0) {
149 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
153 /* parse signature */
154 if (sshbuf_get_bignum2(sigbuf, &sig_r) != 0 ||
155 sshbuf_get_bignum2(sigbuf, &sig_s) != 0) {
156 ret = SSH_ERR_INVALID_FORMAT;
159 if ((sig = ECDSA_SIG_new()) == NULL) {
160 ret = SSH_ERR_ALLOC_FAIL;
163 if (!ECDSA_SIG_set0(sig, sig_r, sig_s)) {
164 ret = SSH_ERR_LIBCRYPTO_ERROR;
167 sig_r = sig_s = NULL; /* transferred */
169 if (sshbuf_len(sigbuf) != 0) {
170 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
173 if ((ret = ssh_digest_memory(hash_alg, data, datalen,
174 digest, sizeof(digest))) != 0)
177 switch (ECDSA_do_verify(digest, dlen, sig, key->ecdsa)) {
182 ret = SSH_ERR_SIGNATURE_INVALID;
185 ret = SSH_ERR_LIBCRYPTO_ERROR;
190 explicit_bzero(digest, sizeof(digest));
194 BN_clear_free(sig_r);
195 BN_clear_free(sig_s);
200 #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */