1255767Sdes/* $OpenBSD: kexecdh.c,v 1.4 2013/04/19 01:06:50 djm Exp $ */
2218767Sdes/*
3218767Sdes * Copyright (c) 2001 Markus Friedl.  All rights reserved.
4218767Sdes * Copyright (c) 2010 Damien Miller.  All rights reserved.
5218767Sdes *
6218767Sdes * Redistribution and use in source and binary forms, with or without
7218767Sdes * modification, are permitted provided that the following conditions
8218767Sdes * are met:
9218767Sdes * 1. Redistributions of source code must retain the above copyright
10218767Sdes *    notice, this list of conditions and the following disclaimer.
11218767Sdes * 2. Redistributions in binary form must reproduce the above copyright
12218767Sdes *    notice, this list of conditions and the following disclaimer in the
13218767Sdes *    documentation and/or other materials provided with the distribution.
14218767Sdes *
15218767Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16218767Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17218767Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18218767Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19218767Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20218767Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21218767Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22218767Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23218767Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24218767Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25218767Sdes */
26218767Sdes
27218767Sdes#include "includes.h"
28218767Sdes
29218767Sdes#ifdef OPENSSL_HAS_ECC
30218767Sdes
31218767Sdes#include <sys/types.h>
32218767Sdes
33218767Sdes#include <signal.h>
34218767Sdes#include <string.h>
35218767Sdes
36218767Sdes#include <openssl/bn.h>
37218767Sdes#include <openssl/evp.h>
38218767Sdes#include <openssl/ec.h>
39218767Sdes#include <openssl/ecdh.h>
40218767Sdes
41218767Sdes#include "buffer.h"
42218767Sdes#include "ssh2.h"
43218767Sdes#include "key.h"
44218767Sdes#include "cipher.h"
45218767Sdes#include "kex.h"
46218767Sdes#include "log.h"
47218767Sdes
48218767Sdesvoid
49218767Sdeskex_ecdh_hash(
50218767Sdes    const EVP_MD *evp_md,
51218767Sdes    const EC_GROUP *ec_group,
52218767Sdes    char *client_version_string,
53218767Sdes    char *server_version_string,
54218767Sdes    char *ckexinit, int ckexinitlen,
55218767Sdes    char *skexinit, int skexinitlen,
56218767Sdes    u_char *serverhostkeyblob, int sbloblen,
57218767Sdes    const EC_POINT *client_dh_pub,
58218767Sdes    const EC_POINT *server_dh_pub,
59218767Sdes    const BIGNUM *shared_secret,
60218767Sdes    u_char **hash, u_int *hashlen)
61218767Sdes{
62218767Sdes	Buffer b;
63218767Sdes	EVP_MD_CTX md;
64218767Sdes	static u_char digest[EVP_MAX_MD_SIZE];
65218767Sdes
66218767Sdes	buffer_init(&b);
67218767Sdes	buffer_put_cstring(&b, client_version_string);
68218767Sdes	buffer_put_cstring(&b, server_version_string);
69218767Sdes
70218767Sdes	/* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
71218767Sdes	buffer_put_int(&b, ckexinitlen+1);
72218767Sdes	buffer_put_char(&b, SSH2_MSG_KEXINIT);
73218767Sdes	buffer_append(&b, ckexinit, ckexinitlen);
74218767Sdes	buffer_put_int(&b, skexinitlen+1);
75218767Sdes	buffer_put_char(&b, SSH2_MSG_KEXINIT);
76218767Sdes	buffer_append(&b, skexinit, skexinitlen);
77218767Sdes
78218767Sdes	buffer_put_string(&b, serverhostkeyblob, sbloblen);
79218767Sdes	buffer_put_ecpoint(&b, ec_group, client_dh_pub);
80218767Sdes	buffer_put_ecpoint(&b, ec_group, server_dh_pub);
81218767Sdes	buffer_put_bignum2(&b, shared_secret);
82218767Sdes
83218767Sdes#ifdef DEBUG_KEX
84218767Sdes	buffer_dump(&b);
85218767Sdes#endif
86218767Sdes	EVP_DigestInit(&md, evp_md);
87218767Sdes	EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
88218767Sdes	EVP_DigestFinal(&md, digest, NULL);
89218767Sdes
90218767Sdes	buffer_free(&b);
91218767Sdes
92218767Sdes#ifdef DEBUG_KEX
93218767Sdes	dump_digest("hash", digest, EVP_MD_size(evp_md));
94218767Sdes#endif
95218767Sdes	*hash = digest;
96218767Sdes	*hashlen = EVP_MD_size(evp_md);
97218767Sdes}
98218767Sdes
99218767Sdes#endif /* OPENSSL_HAS_ECC */
100