xref: /netbsd-src/crypto/external/bsd/openssh/dist/kexgex.c (revision aa36fcac57926c2ee7244eeb1b53bf604da30be9)
1*aa36fcacSchristos /*	$NetBSD: kexgex.c,v 1.7 2019/04/20 17:16:40 christos Exp $	*/
2*aa36fcacSchristos /* $OpenBSD: kexgex.c,v 1.32 2019/01/23 00:30:41 djm Exp $ */
3ca32bd8dSchristos /*
4ca32bd8dSchristos  * Copyright (c) 2000 Niels Provos.  All rights reserved.
5ca32bd8dSchristos  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
6ca32bd8dSchristos  *
7ca32bd8dSchristos  * Redistribution and use in source and binary forms, with or without
8ca32bd8dSchristos  * modification, are permitted provided that the following conditions
9ca32bd8dSchristos  * are met:
10ca32bd8dSchristos  * 1. Redistributions of source code must retain the above copyright
11ca32bd8dSchristos  *    notice, this list of conditions and the following disclaimer.
12ca32bd8dSchristos  * 2. Redistributions in binary form must reproduce the above copyright
13ca32bd8dSchristos  *    notice, this list of conditions and the following disclaimer in the
14ca32bd8dSchristos  *    documentation and/or other materials provided with the distribution.
15ca32bd8dSchristos  *
16ca32bd8dSchristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17ca32bd8dSchristos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18ca32bd8dSchristos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19ca32bd8dSchristos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20ca32bd8dSchristos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21ca32bd8dSchristos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22ca32bd8dSchristos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23ca32bd8dSchristos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24ca32bd8dSchristos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25ca32bd8dSchristos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26ca32bd8dSchristos  */
27ca32bd8dSchristos 
28313c6c94Schristos #include "includes.h"
29*aa36fcacSchristos __RCSID("$NetBSD: kexgex.c,v 1.7 2019/04/20 17:16:40 christos Exp $");
30ca32bd8dSchristos #include <sys/types.h>
31ca32bd8dSchristos 
32ca32bd8dSchristos #include <openssl/evp.h>
33ca32bd8dSchristos #include <signal.h>
34ca32bd8dSchristos 
35e4d43b82Schristos #include "sshkey.h"
36ca32bd8dSchristos #include "cipher.h"
37ca32bd8dSchristos #include "kex.h"
38ca32bd8dSchristos #include "ssh2.h"
39e4d43b82Schristos #include "ssherr.h"
40e4d43b82Schristos #include "sshbuf.h"
418a4530f9Schristos #include "digest.h"
42ca32bd8dSchristos 
43e4d43b82Schristos int
kexgex_hash(int hash_alg,const struct sshbuf * client_version,const struct sshbuf * server_version,const struct sshbuf * client_kexinit,const struct sshbuf * server_kexinit,const struct sshbuf * server_host_key_blob,int min,int wantbits,int max,const BIGNUM * prime,const BIGNUM * gen,const BIGNUM * client_dh_pub,const BIGNUM * server_dh_pub,const u_char * shared_secret,size_t secretlen,u_char * hash,size_t * hashlen)44ca32bd8dSchristos kexgex_hash(
458a4530f9Schristos     int hash_alg,
46*aa36fcacSchristos     const struct sshbuf *client_version,
47*aa36fcacSchristos     const struct sshbuf *server_version,
48*aa36fcacSchristos     const struct sshbuf *client_kexinit,
49*aa36fcacSchristos     const struct sshbuf *server_kexinit,
50*aa36fcacSchristos     const struct sshbuf *server_host_key_blob,
51e4d43b82Schristos     int min, int wantbits, int max,
52e4d43b82Schristos     const BIGNUM *prime,
53e4d43b82Schristos     const BIGNUM *gen,
54e4d43b82Schristos     const BIGNUM *client_dh_pub,
55e4d43b82Schristos     const BIGNUM *server_dh_pub,
56*aa36fcacSchristos     const u_char *shared_secret, size_t secretlen,
57e4d43b82Schristos     u_char *hash, size_t *hashlen)
58ca32bd8dSchristos {
59e4d43b82Schristos 	struct sshbuf *b;
60e4d43b82Schristos 	int r;
61ca32bd8dSchristos 
62e4d43b82Schristos 	if (*hashlen < ssh_digest_bytes(SSH_DIGEST_SHA1))
63e4d43b82Schristos 		return SSH_ERR_INVALID_ARGUMENT;
64e4d43b82Schristos 	if ((b = sshbuf_new()) == NULL)
65e4d43b82Schristos 		return SSH_ERR_ALLOC_FAIL;
66*aa36fcacSchristos 	if ((r = sshbuf_put_stringb(b, client_version)) < 0 ||
67*aa36fcacSchristos 	    (r = sshbuf_put_stringb(b, server_version)) < 0 ||
68ca32bd8dSchristos 	    /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
69*aa36fcacSchristos 	    (r = sshbuf_put_u32(b, sshbuf_len(client_kexinit) + 1)) != 0 ||
70e4d43b82Schristos 	    (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
71*aa36fcacSchristos 	    (r = sshbuf_putb(b, client_kexinit)) != 0 ||
72*aa36fcacSchristos 	    (r = sshbuf_put_u32(b, sshbuf_len(server_kexinit) + 1)) != 0 ||
73e4d43b82Schristos 	    (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
74*aa36fcacSchristos 	    (r = sshbuf_putb(b, server_kexinit)) != 0 ||
75*aa36fcacSchristos 	    (r = sshbuf_put_stringb(b, server_host_key_blob)) != 0 ||
76e4d43b82Schristos 	    (min != -1 && (r = sshbuf_put_u32(b, min)) != 0) ||
77e4d43b82Schristos 	    (r = sshbuf_put_u32(b, wantbits)) != 0 ||
78e4d43b82Schristos 	    (max != -1 && (r = sshbuf_put_u32(b, max)) != 0) ||
79e4d43b82Schristos 	    (r = sshbuf_put_bignum2(b, prime)) != 0 ||
80e4d43b82Schristos 	    (r = sshbuf_put_bignum2(b, gen)) != 0 ||
81e4d43b82Schristos 	    (r = sshbuf_put_bignum2(b, client_dh_pub)) != 0 ||
82e4d43b82Schristos 	    (r = sshbuf_put_bignum2(b, server_dh_pub)) != 0 ||
83*aa36fcacSchristos 	    (r = sshbuf_put(b, shared_secret, secretlen)) != 0) {
84e4d43b82Schristos 		sshbuf_free(b);
85e4d43b82Schristos 		return r;
86ca32bd8dSchristos 	}
87ca32bd8dSchristos #ifdef DEBUG_KEXDH
88e4d43b82Schristos 	sshbuf_dump(b, stderr);
89ca32bd8dSchristos #endif
90e4d43b82Schristos 	if (ssh_digest_buffer(hash_alg, b, hash, *hashlen) != 0) {
91e4d43b82Schristos 		sshbuf_free(b);
92e4d43b82Schristos 		return SSH_ERR_LIBCRYPTO_ERROR;
93e4d43b82Schristos 	}
94e4d43b82Schristos 	sshbuf_free(b);
958a4530f9Schristos 	*hashlen = ssh_digest_bytes(hash_alg);
96e4d43b82Schristos #ifdef DEBUG_KEXDH
97e4d43b82Schristos 	dump_digest("hash", hash, *hashlen);
98e4d43b82Schristos #endif
99e4d43b82Schristos 	return 0;
100ca32bd8dSchristos }
101