1323124Sdes/* $OpenBSD: kexdh.c,v 1.26 2016/05/02 10:26:04 djm Exp $ */
276259Sgreen/*
376259Sgreen * Copyright (c) 2001 Markus Friedl.  All rights reserved.
476259Sgreen *
576259Sgreen * Redistribution and use in source and binary forms, with or without
676259Sgreen * modification, are permitted provided that the following conditions
776259Sgreen * are met:
876259Sgreen * 1. Redistributions of source code must retain the above copyright
976259Sgreen *    notice, this list of conditions and the following disclaimer.
1076259Sgreen * 2. Redistributions in binary form must reproduce the above copyright
1176259Sgreen *    notice, this list of conditions and the following disclaimer in the
1276259Sgreen *    documentation and/or other materials provided with the distribution.
1376259Sgreen *
1476259Sgreen * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1576259Sgreen * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1676259Sgreen * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1776259Sgreen * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1876259Sgreen * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1976259Sgreen * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2076259Sgreen * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2176259Sgreen * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2276259Sgreen * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2376259Sgreen * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2476259Sgreen */
2576259Sgreen
2676259Sgreen#include "includes.h"
2776259Sgreen
28295367Sdes#ifdef WITH_OPENSSL
29295367Sdes
30162852Sdes#include <sys/types.h>
31162852Sdes
32162852Sdes#include <signal.h>
33162852Sdes
34113908Sdes#include <openssl/evp.h>
3576259Sgreen
36113908Sdes#include "ssh2.h"
37295367Sdes#include "sshkey.h"
38162852Sdes#include "cipher.h"
3976259Sgreen#include "kex.h"
40295367Sdes#include "ssherr.h"
41295367Sdes#include "sshbuf.h"
42262566Sdes#include "digest.h"
4376259Sgreen
44295367Sdesint
4576259Sgreenkex_dh_hash(
46323124Sdes    int hash_alg,
47295367Sdes    const char *client_version_string,
48295367Sdes    const char *server_version_string,
49295367Sdes    const u_char *ckexinit, size_t ckexinitlen,
50295367Sdes    const u_char *skexinit, size_t skexinitlen,
51295367Sdes    const u_char *serverhostkeyblob, size_t sbloblen,
52295367Sdes    const BIGNUM *client_dh_pub,
53295367Sdes    const BIGNUM *server_dh_pub,
54295367Sdes    const BIGNUM *shared_secret,
55295367Sdes    u_char *hash, size_t *hashlen)
5676259Sgreen{
57295367Sdes	struct sshbuf *b;
58295367Sdes	int r;
5976259Sgreen
60323124Sdes	if (*hashlen < ssh_digest_bytes(hash_alg))
61295367Sdes		return SSH_ERR_INVALID_ARGUMENT;
62295367Sdes	if ((b = sshbuf_new()) == NULL)
63295367Sdes		return SSH_ERR_ALLOC_FAIL;
64295367Sdes	if ((r = sshbuf_put_cstring(b, client_version_string)) != 0 ||
65295367Sdes	    (r = sshbuf_put_cstring(b, server_version_string)) != 0 ||
66295367Sdes	    /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
67295367Sdes	    (r = sshbuf_put_u32(b, ckexinitlen+1)) != 0 ||
68295367Sdes	    (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
69295367Sdes	    (r = sshbuf_put(b, ckexinit, ckexinitlen)) != 0 ||
70295367Sdes	    (r = sshbuf_put_u32(b, skexinitlen+1)) != 0 ||
71295367Sdes	    (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
72295367Sdes	    (r = sshbuf_put(b, skexinit, skexinitlen)) != 0 ||
73295367Sdes	    (r = sshbuf_put_string(b, serverhostkeyblob, sbloblen)) != 0 ||
74295367Sdes	    (r = sshbuf_put_bignum2(b, client_dh_pub)) != 0 ||
75295367Sdes	    (r = sshbuf_put_bignum2(b, server_dh_pub)) != 0 ||
76295367Sdes	    (r = sshbuf_put_bignum2(b, shared_secret)) != 0) {
77295367Sdes		sshbuf_free(b);
78295367Sdes		return r;
79295367Sdes	}
8076259Sgreen#ifdef DEBUG_KEX
81295367Sdes	sshbuf_dump(b, stderr);
8276259Sgreen#endif
83323124Sdes	if (ssh_digest_buffer(hash_alg, b, hash, *hashlen) != 0) {
84295367Sdes		sshbuf_free(b);
85295367Sdes		return SSH_ERR_LIBCRYPTO_ERROR;
86295367Sdes	}
87295367Sdes	sshbuf_free(b);
88323124Sdes	*hashlen = ssh_digest_bytes(hash_alg);
8976259Sgreen#ifdef DEBUG_KEX
90295367Sdes	dump_digest("hash", hash, *hashlen);
9176259Sgreen#endif
92295367Sdes	return 0;
9376259Sgreen}
94295367Sdes#endif /* WITH_OPENSSL */
95