blob: 52f9e74c0844f628a7d1552293738eb89cded729 [file] [log] [blame]
Damien Miller3e192952013-12-29 17:47:50 +11001/* $OpenBSD: ssh-ecdsa.c,v 1.7 2013/12/27 22:30:17 djm 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 Miller6af914a2010-09-10 11:39:26 +100029#ifdef OPENSSL_HAS_ECC
30
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
40#include "xmalloc.h"
41#include "buffer.h"
42#include "compat.h"
43#include "log.h"
44#include "key.h"
45
46int
47ssh_ecdsa_sign(const Key *key, u_char **sigp, u_int *lenp,
48 const u_char *data, u_int datalen)
49{
50 ECDSA_SIG *sig;
Damien Miller041ab7c2010-09-10 11:23:34 +100051 const EVP_MD *evp_md;
Damien Millereb8b60e2010-08-31 22:41:14 +100052 EVP_MD_CTX md;
53 u_char digest[EVP_MAX_MD_SIZE];
54 u_int len, dlen;
55 Buffer b, bb;
56
Damien Miller3e192952013-12-29 17:47:50 +110057 if (key == NULL || key_type_plain(key->type) != KEY_ECDSA ||
58 key->ecdsa == NULL) {
Damien Millereb8b60e2010-08-31 22:41:14 +100059 error("%s: no ECDSA key", __func__);
60 return -1;
61 }
Damien Miller3e192952013-12-29 17:47:50 +110062
Damien Miller041ab7c2010-09-10 11:23:34 +100063 evp_md = key_ec_nid_to_evpmd(key->ecdsa_nid);
Damien Millereb8b60e2010-08-31 22:41:14 +100064 EVP_DigestInit(&md, evp_md);
65 EVP_DigestUpdate(&md, data, datalen);
66 EVP_DigestFinal(&md, digest, &dlen);
67
68 sig = ECDSA_do_sign(digest, dlen, key->ecdsa);
69 memset(digest, 'd', sizeof(digest));
70
71 if (sig == NULL) {
72 error("%s: sign failed", __func__);
73 return -1;
74 }
75
76 buffer_init(&bb);
77 buffer_put_bignum2(&bb, sig->r);
78 buffer_put_bignum2(&bb, sig->s);
79 ECDSA_SIG_free(sig);
80
81 buffer_init(&b);
82 buffer_put_cstring(&b, key_ssh_name_plain(key));
83 buffer_put_string(&b, buffer_ptr(&bb), buffer_len(&bb));
84 buffer_free(&bb);
85 len = buffer_len(&b);
86 if (lenp != NULL)
87 *lenp = len;
88 if (sigp != NULL) {
89 *sigp = xmalloc(len);
90 memcpy(*sigp, buffer_ptr(&b), len);
91 }
92 buffer_free(&b);
93
94 return 0;
95}
96int
97ssh_ecdsa_verify(const Key *key, const u_char *signature, u_int signaturelen,
98 const u_char *data, u_int datalen)
99{
100 ECDSA_SIG *sig;
Damien Miller041ab7c2010-09-10 11:23:34 +1000101 const EVP_MD *evp_md;
Damien Millereb8b60e2010-08-31 22:41:14 +1000102 EVP_MD_CTX md;
103 u_char digest[EVP_MAX_MD_SIZE], *sigblob;
104 u_int len, dlen;
105 int rlen, ret;
106 Buffer b, bb;
Damien Miller041ab7c2010-09-10 11:23:34 +1000107 char *ktype;
Damien Millereb8b60e2010-08-31 22:41:14 +1000108
Damien Miller3e192952013-12-29 17:47:50 +1100109 if (key == NULL || key_type_plain(key->type) != KEY_ECDSA ||
110 key->ecdsa == NULL) {
Damien Millereb8b60e2010-08-31 22:41:14 +1000111 error("%s: no ECDSA key", __func__);
112 return -1;
113 }
Damien Miller3e192952013-12-29 17:47:50 +1100114
Damien Miller041ab7c2010-09-10 11:23:34 +1000115 evp_md = key_ec_nid_to_evpmd(key->ecdsa_nid);
Damien Millereb8b60e2010-08-31 22:41:14 +1000116
117 /* fetch signature */
Damien Millereb8b60e2010-08-31 22:41:14 +1000118 buffer_init(&b);
119 buffer_append(&b, signature, signaturelen);
120 ktype = buffer_get_string(&b, NULL);
121 if (strcmp(key_ssh_name_plain(key), ktype) != 0) {
122 error("%s: cannot handle type %s", __func__, ktype);
123 buffer_free(&b);
Darren Tuckera627d422013-06-02 07:31:17 +1000124 free(ktype);
Damien Millereb8b60e2010-08-31 22:41:14 +1000125 return -1;
126 }
Darren Tuckera627d422013-06-02 07:31:17 +1000127 free(ktype);
Damien Millereb8b60e2010-08-31 22:41:14 +1000128 sigblob = buffer_get_string(&b, &len);
129 rlen = buffer_len(&b);
130 buffer_free(&b);
131 if (rlen != 0) {
132 error("%s: remaining bytes in signature %d", __func__, rlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000133 free(sigblob);
Damien Millereb8b60e2010-08-31 22:41:14 +1000134 return -1;
135 }
136
137 /* parse signature */
138 if ((sig = ECDSA_SIG_new()) == NULL)
139 fatal("%s: ECDSA_SIG_new failed", __func__);
140 if ((sig->r = BN_new()) == NULL ||
141 (sig->s = BN_new()) == NULL)
142 fatal("%s: BN_new failed", __func__);
143
144 buffer_init(&bb);
145 buffer_append(&bb, sigblob, len);
146 buffer_get_bignum2(&bb, sig->r);
147 buffer_get_bignum2(&bb, sig->s);
148 if (buffer_len(&bb) != 0)
149 fatal("%s: remaining bytes in inner sigblob", __func__);
Damien Miller83ba8e62012-02-11 08:17:27 +1100150 buffer_free(&bb);
Damien Millereb8b60e2010-08-31 22:41:14 +1000151
152 /* clean up */
153 memset(sigblob, 0, len);
Darren Tuckera627d422013-06-02 07:31:17 +1000154 free(sigblob);
Damien Millereb8b60e2010-08-31 22:41:14 +1000155
156 /* hash the data */
157 EVP_DigestInit(&md, evp_md);
158 EVP_DigestUpdate(&md, data, datalen);
159 EVP_DigestFinal(&md, digest, &dlen);
160
161 ret = ECDSA_do_verify(digest, dlen, sig, key->ecdsa);
162 memset(digest, 'd', sizeof(digest));
163
164 ECDSA_SIG_free(sig);
165
166 debug("%s: signature %s", __func__,
167 ret == 1 ? "correct" : ret == 0 ? "incorrect" : "error");
168 return ret;
169}
Damien Miller6af914a2010-09-10 11:39:26 +1000170
171#endif /* OPENSSL_HAS_ECC */