kexdh.c revision 181110
1170809Sdelphij/* $OpenBSD: kexdh.c,v 1.23 2006/08/03 03:34:42 deraadt Exp $ */
2170809Sdelphij/*
3171801Sdelphij * Copyright (c) 2001 Markus Friedl.  All rights reserved.
4170809Sdelphij *
5171801Sdelphij * Redistribution and use in source and binary forms, with or without
6170809Sdelphij * modification, are permitted provided that the following conditions
7170809Sdelphij * are met:
8170809Sdelphij * 1. Redistributions of source code must retain the above copyright
9170809Sdelphij *    notice, this list of conditions and the following disclaimer.
10170809Sdelphij * 2. Redistributions in binary form must reproduce the above copyright
11170809Sdelphij *    notice, this list of conditions and the following disclaimer in the
12170809Sdelphij *    documentation and/or other materials provided with the distribution.
13170809Sdelphij *
14170809Sdelphij * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15170809Sdelphij * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16170809Sdelphij * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17170809Sdelphij * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18170809Sdelphij * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19170809Sdelphij * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20170809Sdelphij * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21170809Sdelphij * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22170809Sdelphij * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23170809Sdelphij * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24170809Sdelphij */
25170809Sdelphij
26170809Sdelphij#include "includes.h"
27170809Sdelphij
28170809Sdelphij#include <sys/types.h>
29170809Sdelphij
30170809Sdelphij#include <signal.h>
31170809Sdelphij
32170809Sdelphij#include <openssl/evp.h>
33170809Sdelphij
34170809Sdelphij#include "buffer.h"
35170809Sdelphij#include "ssh2.h"
36170809Sdelphij#include "key.h"
37170809Sdelphij#include "cipher.h"
38170809Sdelphij#include "kex.h"
39170809Sdelphij
40170809Sdelphijvoid
41170809Sdelphijkex_dh_hash(
42170809Sdelphij    char *client_version_string,
43170809Sdelphij    char *server_version_string,
44170809Sdelphij    char *ckexinit, int ckexinitlen,
45170809Sdelphij    char *skexinit, int skexinitlen,
46170809Sdelphij    u_char *serverhostkeyblob, int sbloblen,
47170809Sdelphij    BIGNUM *client_dh_pub,
48170809Sdelphij    BIGNUM *server_dh_pub,
49170809Sdelphij    BIGNUM *shared_secret,
50170809Sdelphij    u_char **hash, u_int *hashlen)
51170809Sdelphij{
52170809Sdelphij	Buffer b;
53170809Sdelphij	static u_char digest[EVP_MAX_MD_SIZE];
54170809Sdelphij	const EVP_MD *evp_md = EVP_sha1();
55170809Sdelphij	EVP_MD_CTX md;
56170809Sdelphij
57170809Sdelphij	buffer_init(&b);
58170809Sdelphij	buffer_put_cstring(&b, client_version_string);
59170809Sdelphij	buffer_put_cstring(&b, server_version_string);
60170809Sdelphij
61170809Sdelphij	/* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
62170809Sdelphij	buffer_put_int(&b, ckexinitlen+1);
63170809Sdelphij	buffer_put_char(&b, SSH2_MSG_KEXINIT);
64170809Sdelphij	buffer_append(&b, ckexinit, ckexinitlen);
65170809Sdelphij	buffer_put_int(&b, skexinitlen+1);
66170809Sdelphij	buffer_put_char(&b, SSH2_MSG_KEXINIT);
67170809Sdelphij	buffer_append(&b, skexinit, skexinitlen);
68170809Sdelphij
69170809Sdelphij	buffer_put_string(&b, serverhostkeyblob, sbloblen);
70170809Sdelphij	buffer_put_bignum2(&b, client_dh_pub);
71170809Sdelphij	buffer_put_bignum2(&b, server_dh_pub);
72170809Sdelphij	buffer_put_bignum2(&b, shared_secret);
73170809Sdelphij
74170809Sdelphij#ifdef DEBUG_KEX
75170809Sdelphij	buffer_dump(&b);
76170809Sdelphij#endif
77170809Sdelphij	EVP_DigestInit(&md, evp_md);
78170809Sdelphij	EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
79	EVP_DigestFinal(&md, digest, NULL);
80
81	buffer_free(&b);
82
83#ifdef DEBUG_KEX
84	dump_digest("hash", digest, EVP_MD_size(evp_md));
85#endif
86	*hash = digest;
87	*hashlen = EVP_MD_size(evp_md);
88}
89