xref: /netbsd-src/crypto/external/bsd/openssh/dist/kexgex.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: kexgex.c,v 1.6 2017/04/18 18:41:46 christos Exp $	*/
2 /* $OpenBSD: kexgex.c,v 1.29 2015/01/19 20:16:15 markus Exp $ */
3 /*
4  * Copyright (c) 2000 Niels Provos.  All rights reserved.
5  * Copyright (c) 2001 Markus Friedl.  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: kexgex.c,v 1.6 2017/04/18 18:41:46 christos Exp $");
30 #include <sys/types.h>
31 
32 #include <openssl/evp.h>
33 #include <signal.h>
34 
35 #include "sshkey.h"
36 #include "cipher.h"
37 #include "kex.h"
38 #include "ssh2.h"
39 #include "ssherr.h"
40 #include "sshbuf.h"
41 #include "digest.h"
42 
43 int
44 kexgex_hash(
45     int hash_alg,
46     const char *client_version_string,
47     const char *server_version_string,
48     const u_char *ckexinit, size_t ckexinitlen,
49     const u_char *skexinit, size_t skexinitlen,
50     const u_char *serverhostkeyblob, size_t sbloblen,
51     int min, int wantbits, int max,
52     const BIGNUM *prime,
53     const BIGNUM *gen,
54     const BIGNUM *client_dh_pub,
55     const BIGNUM *server_dh_pub,
56     const BIGNUM *shared_secret,
57     u_char *hash, size_t *hashlen)
58 {
59 	struct sshbuf *b;
60 	int r;
61 
62 	if (*hashlen < ssh_digest_bytes(SSH_DIGEST_SHA1))
63 		return SSH_ERR_INVALID_ARGUMENT;
64 	if ((b = sshbuf_new()) == NULL)
65 		return SSH_ERR_ALLOC_FAIL;
66 	if ((r = sshbuf_put_cstring(b, client_version_string)) != 0 ||
67 	    (r = sshbuf_put_cstring(b, server_version_string)) != 0 ||
68 	    /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
69 	    (r = sshbuf_put_u32(b, ckexinitlen+1)) != 0 ||
70 	    (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
71 	    (r = sshbuf_put(b, ckexinit, ckexinitlen)) != 0 ||
72 	    (r = sshbuf_put_u32(b, skexinitlen+1)) != 0 ||
73 	    (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
74 	    (r = sshbuf_put(b, skexinit, skexinitlen)) != 0 ||
75 	    (r = sshbuf_put_string(b, serverhostkeyblob, sbloblen)) != 0 ||
76 	    (min != -1 && (r = sshbuf_put_u32(b, min)) != 0) ||
77 	    (r = sshbuf_put_u32(b, wantbits)) != 0 ||
78 	    (max != -1 && (r = sshbuf_put_u32(b, max)) != 0) ||
79 	    (r = sshbuf_put_bignum2(b, prime)) != 0 ||
80 	    (r = sshbuf_put_bignum2(b, gen)) != 0 ||
81 	    (r = sshbuf_put_bignum2(b, client_dh_pub)) != 0 ||
82 	    (r = sshbuf_put_bignum2(b, server_dh_pub)) != 0 ||
83 	    (r = sshbuf_put_bignum2(b, shared_secret)) != 0) {
84 		sshbuf_free(b);
85 		return r;
86 	}
87 #ifdef DEBUG_KEXDH
88 	sshbuf_dump(b, stderr);
89 #endif
90 	if (ssh_digest_buffer(hash_alg, b, hash, *hashlen) != 0) {
91 		sshbuf_free(b);
92 		return SSH_ERR_LIBCRYPTO_ERROR;
93 	}
94 	sshbuf_free(b);
95 	*hashlen = ssh_digest_bytes(hash_alg);
96 #ifdef DEBUG_KEXDH
97 	dump_digest("hash", hash, *hashlen);
98 #endif
99 	return 0;
100 }
101