xref: /freebsd/crypto/openssh/kexdh.c (revision f7167e0ea0bf5aaabff9490453b3b71b3f1b4d51)
1*f7167e0eSDag-Erling Smørgrav /* $OpenBSD: kexdh.c,v 1.24 2014/01/09 23:20:00 djm Exp $ */
21e8db6e2SBrian Feldman /*
31e8db6e2SBrian Feldman  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
41e8db6e2SBrian Feldman  *
51e8db6e2SBrian Feldman  * Redistribution and use in source and binary forms, with or without
61e8db6e2SBrian Feldman  * modification, are permitted provided that the following conditions
71e8db6e2SBrian Feldman  * are met:
81e8db6e2SBrian Feldman  * 1. Redistributions of source code must retain the above copyright
91e8db6e2SBrian Feldman  *    notice, this list of conditions and the following disclaimer.
101e8db6e2SBrian Feldman  * 2. Redistributions in binary form must reproduce the above copyright
111e8db6e2SBrian Feldman  *    notice, this list of conditions and the following disclaimer in the
121e8db6e2SBrian Feldman  *    documentation and/or other materials provided with the distribution.
131e8db6e2SBrian Feldman  *
141e8db6e2SBrian Feldman  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
151e8db6e2SBrian Feldman  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
161e8db6e2SBrian Feldman  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
171e8db6e2SBrian Feldman  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
181e8db6e2SBrian Feldman  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
191e8db6e2SBrian Feldman  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
201e8db6e2SBrian Feldman  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
211e8db6e2SBrian Feldman  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
221e8db6e2SBrian Feldman  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
231e8db6e2SBrian Feldman  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
241e8db6e2SBrian Feldman  */
251e8db6e2SBrian Feldman 
261e8db6e2SBrian Feldman #include "includes.h"
27761efaa7SDag-Erling Smørgrav 
28761efaa7SDag-Erling Smørgrav #include <sys/types.h>
29761efaa7SDag-Erling Smørgrav 
30761efaa7SDag-Erling Smørgrav #include <signal.h>
311e8db6e2SBrian Feldman 
32d0c8c0bcSDag-Erling Smørgrav #include <openssl/evp.h>
331e8db6e2SBrian Feldman 
341e8db6e2SBrian Feldman #include "buffer.h"
351e8db6e2SBrian Feldman #include "ssh2.h"
36761efaa7SDag-Erling Smørgrav #include "key.h"
37761efaa7SDag-Erling Smørgrav #include "cipher.h"
38d0c8c0bcSDag-Erling Smørgrav #include "kex.h"
39*f7167e0eSDag-Erling Smørgrav #include "digest.h"
40*f7167e0eSDag-Erling Smørgrav #include "log.h"
411e8db6e2SBrian Feldman 
42021d409fSDag-Erling Smørgrav void
431e8db6e2SBrian Feldman kex_dh_hash(
441e8db6e2SBrian Feldman     char *client_version_string,
451e8db6e2SBrian Feldman     char *server_version_string,
461e8db6e2SBrian Feldman     char *ckexinit, int ckexinitlen,
471e8db6e2SBrian Feldman     char *skexinit, int skexinitlen,
48ae1f160dSDag-Erling Smørgrav     u_char *serverhostkeyblob, int sbloblen,
491e8db6e2SBrian Feldman     BIGNUM *client_dh_pub,
501e8db6e2SBrian Feldman     BIGNUM *server_dh_pub,
51021d409fSDag-Erling Smørgrav     BIGNUM *shared_secret,
52021d409fSDag-Erling Smørgrav     u_char **hash, u_int *hashlen)
531e8db6e2SBrian Feldman {
541e8db6e2SBrian Feldman 	Buffer b;
55*f7167e0eSDag-Erling Smørgrav 	static u_char digest[SSH_DIGEST_MAX_LENGTH];
561e8db6e2SBrian Feldman 
571e8db6e2SBrian Feldman 	buffer_init(&b);
58ae1f160dSDag-Erling Smørgrav 	buffer_put_cstring(&b, client_version_string);
59ae1f160dSDag-Erling Smørgrav 	buffer_put_cstring(&b, server_version_string);
601e8db6e2SBrian Feldman 
611e8db6e2SBrian Feldman 	/* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
621e8db6e2SBrian Feldman 	buffer_put_int(&b, ckexinitlen+1);
631e8db6e2SBrian Feldman 	buffer_put_char(&b, SSH2_MSG_KEXINIT);
641e8db6e2SBrian Feldman 	buffer_append(&b, ckexinit, ckexinitlen);
651e8db6e2SBrian Feldman 	buffer_put_int(&b, skexinitlen+1);
661e8db6e2SBrian Feldman 	buffer_put_char(&b, SSH2_MSG_KEXINIT);
671e8db6e2SBrian Feldman 	buffer_append(&b, skexinit, skexinitlen);
681e8db6e2SBrian Feldman 
691e8db6e2SBrian Feldman 	buffer_put_string(&b, serverhostkeyblob, sbloblen);
701e8db6e2SBrian Feldman 	buffer_put_bignum2(&b, client_dh_pub);
711e8db6e2SBrian Feldman 	buffer_put_bignum2(&b, server_dh_pub);
721e8db6e2SBrian Feldman 	buffer_put_bignum2(&b, shared_secret);
731e8db6e2SBrian Feldman 
741e8db6e2SBrian Feldman #ifdef DEBUG_KEX
751e8db6e2SBrian Feldman 	buffer_dump(&b);
761e8db6e2SBrian Feldman #endif
77*f7167e0eSDag-Erling Smørgrav 	if (ssh_digest_buffer(SSH_DIGEST_SHA1, &b, digest, sizeof(digest)) != 0)
78*f7167e0eSDag-Erling Smørgrav 		fatal("%s: ssh_digest_buffer failed", __func__);
791e8db6e2SBrian Feldman 
801e8db6e2SBrian Feldman 	buffer_free(&b);
811e8db6e2SBrian Feldman 
821e8db6e2SBrian Feldman #ifdef DEBUG_KEX
83*f7167e0eSDag-Erling Smørgrav 	dump_digest("hash", digest, ssh_digest_bytes(SSH_DIGEST_SHA1));
841e8db6e2SBrian Feldman #endif
85021d409fSDag-Erling Smørgrav 	*hash = digest;
86*f7167e0eSDag-Erling Smørgrav 	*hashlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
871e8db6e2SBrian Feldman }
88