xref: /netbsd-src/crypto/external/bsd/openssh/dist/kexecdh.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: kexecdh.c,v 1.3 2013/11/08 19:18:25 christos Exp $	*/
2 /* $OpenBSD: kexecdh.c,v 1.4 2013/04/19 01:06:50 djm Exp $ */
3 /*
4  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
5  * Copyright (c) 2010 Damien Miller.  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: kexecdh.c,v 1.3 2013/11/08 19:18:25 christos Exp $");
30 #include <sys/types.h>
31 
32 #include <signal.h>
33 #include <string.h>
34 
35 #include <openssl/bn.h>
36 #include <openssl/evp.h>
37 #include <openssl/ec.h>
38 #include <openssl/ecdh.h>
39 
40 #include "buffer.h"
41 #include "ssh2.h"
42 #include "key.h"
43 #include "cipher.h"
44 #include "kex.h"
45 #include "log.h"
46 
47 void
48 kex_ecdh_hash(
49     const EVP_MD *evp_md,
50     const EC_GROUP *ec_group,
51     char *client_version_string,
52     char *server_version_string,
53     char *ckexinit, int ckexinitlen,
54     char *skexinit, int skexinitlen,
55     u_char *serverhostkeyblob, int sbloblen,
56     const EC_POINT *client_dh_pub,
57     const EC_POINT *server_dh_pub,
58     const BIGNUM *shared_secret,
59     u_char **hash, u_int *hashlen)
60 {
61 	Buffer b;
62 	EVP_MD_CTX md;
63 	static u_char digest[EVP_MAX_MD_SIZE];
64 
65 	buffer_init(&b);
66 	buffer_put_cstring(&b, client_version_string);
67 	buffer_put_cstring(&b, server_version_string);
68 
69 	/* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
70 	buffer_put_int(&b, ckexinitlen+1);
71 	buffer_put_char(&b, SSH2_MSG_KEXINIT);
72 	buffer_append(&b, ckexinit, ckexinitlen);
73 	buffer_put_int(&b, skexinitlen+1);
74 	buffer_put_char(&b, SSH2_MSG_KEXINIT);
75 	buffer_append(&b, skexinit, skexinitlen);
76 
77 	buffer_put_string(&b, serverhostkeyblob, sbloblen);
78 	buffer_put_ecpoint(&b, ec_group, client_dh_pub);
79 	buffer_put_ecpoint(&b, ec_group, server_dh_pub);
80 	buffer_put_bignum2(&b, shared_secret);
81 
82 #ifdef DEBUG_KEX
83 	buffer_dump(&b);
84 #endif
85 	EVP_DigestInit(&md, evp_md);
86 	EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
87 	EVP_DigestFinal(&md, digest, NULL);
88 
89 	buffer_free(&b);
90 
91 #ifdef DEBUG_KEX
92 	dump_digest("hash", digest, EVP_MD_size(evp_md));
93 #endif
94 	*hash = digest;
95 	*hashlen = EVP_MD_size(evp_md);
96 }
97 
98