xref: /netbsd-src/crypto/external/bsd/openssh/dist/kexecdh.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: kexecdh.c,v 1.7 2017/04/18 18:41:46 christos Exp $	*/
2 /* $OpenBSD: kexecdh.c,v 1.6 2015/01/19 20:16:15 markus 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.7 2017/04/18 18:41:46 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 "ssh2.h"
41 #include "sshkey.h"
42 #include "cipher.h"
43 #include "kex.h"
44 #include "sshbuf.h"
45 #include "digest.h"
46 #include "ssherr.h"
47 
48 int
49 kex_ecdh_hash(
50     int hash_alg,
51     const EC_GROUP *ec_group,
52     const char *client_version_string,
53     const char *server_version_string,
54     const u_char *ckexinit, size_t ckexinitlen,
55     const u_char *skexinit, size_t skexinitlen,
56     const u_char *serverhostkeyblob, size_t sbloblen,
57     const EC_POINT *client_dh_pub,
58     const EC_POINT *server_dh_pub,
59     const BIGNUM *shared_secret,
60     u_char *hash, size_t *hashlen)
61 {
62 	struct sshbuf *b;
63 	int r;
64 
65 	if (*hashlen < ssh_digest_bytes(hash_alg))
66 		return SSH_ERR_INVALID_ARGUMENT;
67 	if ((b = sshbuf_new()) == NULL)
68 		return SSH_ERR_ALLOC_FAIL;
69 	if ((r = sshbuf_put_cstring(b, client_version_string)) != 0 ||
70 	    (r = sshbuf_put_cstring(b, server_version_string)) != 0 ||
71 	    /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
72 	    (r = sshbuf_put_u32(b, ckexinitlen+1)) != 0 ||
73 	    (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
74 	    (r = sshbuf_put(b, ckexinit, ckexinitlen)) != 0 ||
75 	    (r = sshbuf_put_u32(b, skexinitlen+1)) != 0 ||
76 	    (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
77 	    (r = sshbuf_put(b, skexinit, skexinitlen)) != 0 ||
78 	    (r = sshbuf_put_string(b, serverhostkeyblob, sbloblen)) != 0 ||
79 	    (r = sshbuf_put_ec(b, client_dh_pub, ec_group)) != 0 ||
80 	    (r = sshbuf_put_ec(b, server_dh_pub, ec_group)) != 0 ||
81 	    (r = sshbuf_put_bignum2(b, shared_secret)) != 0) {
82 		sshbuf_free(b);
83 		return r;
84 	}
85 #ifdef DEBUG_KEX
86 	sshbuf_dump(b, stderr);
87 #endif
88 	if (ssh_digest_buffer(hash_alg, b, hash, *hashlen) != 0) {
89 		sshbuf_free(b);
90 		return SSH_ERR_LIBCRYPTO_ERROR;
91 	}
92 	sshbuf_free(b);
93 	*hashlen = ssh_digest_bytes(hash_alg);
94 #ifdef DEBUG_KEX
95 	dump_digest("hash", hash, *hashlen);
96 #endif
97 	return 0;
98 }
99