1/*	$NetBSD: kexgex.c,v 1.7 2019/04/20 17:16:40 christos Exp $	*/
2/* $OpenBSD: kexgex.c,v 1.32 2019/01/23 00:30:41 djm 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.7 2019/04/20 17:16:40 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
43int
44kexgex_hash(
45    int hash_alg,
46    const struct sshbuf *client_version,
47    const struct sshbuf *server_version,
48    const struct sshbuf *client_kexinit,
49    const struct sshbuf *server_kexinit,
50    const struct sshbuf *server_host_key_blob,
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 u_char *shared_secret, size_t secretlen,
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_stringb(b, client_version)) < 0 ||
67	    (r = sshbuf_put_stringb(b, server_version)) < 0 ||
68	    /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
69	    (r = sshbuf_put_u32(b, sshbuf_len(client_kexinit) + 1)) != 0 ||
70	    (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
71	    (r = sshbuf_putb(b, client_kexinit)) != 0 ||
72	    (r = sshbuf_put_u32(b, sshbuf_len(server_kexinit) + 1)) != 0 ||
73	    (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
74	    (r = sshbuf_putb(b, server_kexinit)) != 0 ||
75	    (r = sshbuf_put_stringb(b, server_host_key_blob)) != 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(b, shared_secret, secretlen)) != 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