1 /* $OpenBSD: ssh-dss.c,v 1.37 2018/02/07 02:06:51 jsing Exp $ */
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/types.h>
32 #include <openssl/bn.h>
33 #include <openssl/dsa.h>
34 #include <openssl/evp.h>
43 #define SSHKEY_INTERNAL
46 #define INTBLOB_LEN 20
47 #define SIGBLOB_LEN (2*INTBLOB_LEN)
50 ssh_dss_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
51 const u_char *data, size_t datalen, u_int compat)
54 u_char digest[SSH_DIGEST_MAX_LENGTH], sigblob[SIGBLOB_LEN];
55 size_t rlen, slen, len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
57 struct sshbuf *b = NULL;
58 int ret = SSH_ERR_INVALID_ARGUMENT;
65 if (key == NULL || key->dsa == NULL ||
66 sshkey_type_plain(key->type) != KEY_DSA)
67 return SSH_ERR_INVALID_ARGUMENT;
69 return SSH_ERR_INTERNAL_ERROR;
71 if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
72 digest, sizeof(digest))) != 0)
75 if ((sig = DSA_do_sign(digest, dlen, key->dsa)) == NULL) {
76 ret = SSH_ERR_LIBCRYPTO_ERROR;
80 DSA_SIG_get0(sig, &r, &s);
81 rlen = BN_num_bytes(r);
82 slen = BN_num_bytes(s);
83 if (rlen > INTBLOB_LEN || slen > INTBLOB_LEN) {
84 ret = SSH_ERR_INTERNAL_ERROR;
87 explicit_bzero(sigblob, SIGBLOB_LEN);
88 BN_bn2bin(r, sigblob + SIGBLOB_LEN - INTBLOB_LEN - rlen);
89 BN_bn2bin(s, sigblob + SIGBLOB_LEN - slen);
91 if ((b = sshbuf_new()) == NULL) {
92 ret = SSH_ERR_ALLOC_FAIL;
95 if ((ret = sshbuf_put_cstring(b, "ssh-dss")) != 0 ||
96 (ret = sshbuf_put_string(b, sigblob, SIGBLOB_LEN)) != 0)
101 if ((*sigp = malloc(len)) == NULL) {
102 ret = SSH_ERR_ALLOC_FAIL;
105 memcpy(*sigp, sshbuf_ptr(b), len);
111 explicit_bzero(digest, sizeof(digest));
118 ssh_dss_verify(const struct sshkey *key,
119 const u_char *signature, size_t signaturelen,
120 const u_char *data, size_t datalen, u_int compat)
123 u_char digest[SSH_DIGEST_MAX_LENGTH], *sigblob = NULL;
124 size_t len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
125 int ret = SSH_ERR_INTERNAL_ERROR;
126 struct sshbuf *b = NULL;
129 if (key == NULL || key->dsa == NULL ||
130 sshkey_type_plain(key->type) != KEY_DSA ||
131 signature == NULL || signaturelen == 0)
132 return SSH_ERR_INVALID_ARGUMENT;
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_get_string(b, &sigblob, &len) != 0) {
141 ret = SSH_ERR_INVALID_FORMAT;
144 if (strcmp("ssh-dss", ktype) != 0) {
145 ret = SSH_ERR_KEY_TYPE_MISMATCH;
148 if (sshbuf_len(b) != 0) {
149 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
153 if (len != SIGBLOB_LEN) {
154 ret = SSH_ERR_INVALID_FORMAT;
158 /* parse signature */
160 BIGNUM *r=NULL, *s=NULL;
161 if ((sig = DSA_SIG_new()) == NULL ||
162 (r = BN_new()) == NULL ||
163 (s = BN_new()) == NULL) {
164 ret = SSH_ERR_ALLOC_FAIL;
169 if ((BN_bin2bn(sigblob, INTBLOB_LEN, r) == NULL) ||
170 (BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, s) == NULL)) {
171 ret = SSH_ERR_LIBCRYPTO_ERROR;
176 DSA_SIG_set0(sig, r, s);
181 if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
182 digest, sizeof(digest))) != 0)
185 switch (DSA_do_verify(digest, dlen, sig, key->dsa)) {
190 ret = SSH_ERR_SIGNATURE_INVALID;
193 ret = SSH_ERR_LIBCRYPTO_ERROR;
198 explicit_bzero(digest, sizeof(digest));
202 if (sigblob != NULL) {
203 explicit_bzero(sigblob, len);
208 #endif /* WITH_OPENSSL */