1*19261079SEd Maste /* $OpenBSD: kexgex.c,v 1.32 2019/01/23 00:30:41 djm Exp $ */
21e8db6e2SBrian Feldman /*
31e8db6e2SBrian Feldman * Copyright (c) 2000 Niels Provos. All rights reserved.
41e8db6e2SBrian Feldman * Copyright (c) 2001 Markus Friedl. All rights reserved.
51e8db6e2SBrian Feldman *
61e8db6e2SBrian Feldman * Redistribution and use in source and binary forms, with or without
71e8db6e2SBrian Feldman * modification, are permitted provided that the following conditions
81e8db6e2SBrian Feldman * are met:
91e8db6e2SBrian Feldman * 1. Redistributions of source code must retain the above copyright
101e8db6e2SBrian Feldman * notice, this list of conditions and the following disclaimer.
111e8db6e2SBrian Feldman * 2. Redistributions in binary form must reproduce the above copyright
121e8db6e2SBrian Feldman * notice, this list of conditions and the following disclaimer in the
131e8db6e2SBrian Feldman * documentation and/or other materials provided with the distribution.
141e8db6e2SBrian Feldman *
151e8db6e2SBrian Feldman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
161e8db6e2SBrian Feldman * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
171e8db6e2SBrian Feldman * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
181e8db6e2SBrian Feldman * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
191e8db6e2SBrian Feldman * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
201e8db6e2SBrian Feldman * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
211e8db6e2SBrian Feldman * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
221e8db6e2SBrian Feldman * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
231e8db6e2SBrian Feldman * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
241e8db6e2SBrian Feldman * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
251e8db6e2SBrian Feldman */
261e8db6e2SBrian Feldman
271e8db6e2SBrian Feldman #include "includes.h"
28761efaa7SDag-Erling Smørgrav
29bc5531deSDag-Erling Smørgrav #ifdef WITH_OPENSSL
30bc5531deSDag-Erling Smørgrav
31761efaa7SDag-Erling Smørgrav #include <sys/types.h>
321e8db6e2SBrian Feldman
33d0c8c0bcSDag-Erling Smørgrav #include <openssl/evp.h>
34761efaa7SDag-Erling Smørgrav #include <signal.h>
351e8db6e2SBrian Feldman
362a01feabSEd Maste #include "openbsd-compat/openssl-compat.h"
372a01feabSEd Maste
38bc5531deSDag-Erling Smørgrav #include "sshkey.h"
39761efaa7SDag-Erling Smørgrav #include "cipher.h"
401e8db6e2SBrian Feldman #include "kex.h"
411e8db6e2SBrian Feldman #include "ssh2.h"
42bc5531deSDag-Erling Smørgrav #include "ssherr.h"
43bc5531deSDag-Erling Smørgrav #include "sshbuf.h"
44f7167e0eSDag-Erling Smørgrav #include "digest.h"
451e8db6e2SBrian Feldman
46bc5531deSDag-Erling Smørgrav int
kexgex_hash(int hash_alg,const struct sshbuf * client_version,const struct sshbuf * server_version,const struct sshbuf * client_kexinit,const struct sshbuf * server_kexinit,const struct sshbuf * server_host_key_blob,int min,int wantbits,int max,const BIGNUM * prime,const BIGNUM * gen,const BIGNUM * client_dh_pub,const BIGNUM * server_dh_pub,const u_char * shared_secret,size_t secretlen,u_char * hash,size_t * hashlen)471e8db6e2SBrian Feldman kexgex_hash(
48f7167e0eSDag-Erling Smørgrav int hash_alg,
49*19261079SEd Maste const struct sshbuf *client_version,
50*19261079SEd Maste const struct sshbuf *server_version,
51*19261079SEd Maste const struct sshbuf *client_kexinit,
52*19261079SEd Maste const struct sshbuf *server_kexinit,
53*19261079SEd Maste const struct sshbuf *server_host_key_blob,
54bc5531deSDag-Erling Smørgrav int min, int wantbits, int max,
55bc5531deSDag-Erling Smørgrav const BIGNUM *prime,
56bc5531deSDag-Erling Smørgrav const BIGNUM *gen,
57bc5531deSDag-Erling Smørgrav const BIGNUM *client_dh_pub,
58bc5531deSDag-Erling Smørgrav const BIGNUM *server_dh_pub,
59*19261079SEd Maste const u_char *shared_secret, size_t secretlen,
60bc5531deSDag-Erling Smørgrav u_char *hash, size_t *hashlen)
611e8db6e2SBrian Feldman {
62bc5531deSDag-Erling Smørgrav struct sshbuf *b;
63bc5531deSDag-Erling Smørgrav int r;
641e8db6e2SBrian Feldman
65bc5531deSDag-Erling Smørgrav if (*hashlen < ssh_digest_bytes(SSH_DIGEST_SHA1))
66bc5531deSDag-Erling Smørgrav return SSH_ERR_INVALID_ARGUMENT;
67bc5531deSDag-Erling Smørgrav if ((b = sshbuf_new()) == NULL)
68bc5531deSDag-Erling Smørgrav return SSH_ERR_ALLOC_FAIL;
69*19261079SEd Maste if ((r = sshbuf_put_stringb(b, client_version)) < 0 ||
70*19261079SEd Maste (r = sshbuf_put_stringb(b, server_version)) < 0 ||
711e8db6e2SBrian Feldman /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
72*19261079SEd Maste (r = sshbuf_put_u32(b, sshbuf_len(client_kexinit) + 1)) != 0 ||
73bc5531deSDag-Erling Smørgrav (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
74*19261079SEd Maste (r = sshbuf_putb(b, client_kexinit)) != 0 ||
75*19261079SEd Maste (r = sshbuf_put_u32(b, sshbuf_len(server_kexinit) + 1)) != 0 ||
76bc5531deSDag-Erling Smørgrav (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
77*19261079SEd Maste (r = sshbuf_putb(b, server_kexinit)) != 0 ||
78*19261079SEd Maste (r = sshbuf_put_stringb(b, server_host_key_blob)) != 0 ||
79bc5531deSDag-Erling Smørgrav (min != -1 && (r = sshbuf_put_u32(b, min)) != 0) ||
80bc5531deSDag-Erling Smørgrav (r = sshbuf_put_u32(b, wantbits)) != 0 ||
81bc5531deSDag-Erling Smørgrav (max != -1 && (r = sshbuf_put_u32(b, max)) != 0) ||
82bc5531deSDag-Erling Smørgrav (r = sshbuf_put_bignum2(b, prime)) != 0 ||
83bc5531deSDag-Erling Smørgrav (r = sshbuf_put_bignum2(b, gen)) != 0 ||
84bc5531deSDag-Erling Smørgrav (r = sshbuf_put_bignum2(b, client_dh_pub)) != 0 ||
85bc5531deSDag-Erling Smørgrav (r = sshbuf_put_bignum2(b, server_dh_pub)) != 0 ||
86*19261079SEd Maste (r = sshbuf_put(b, shared_secret, secretlen)) != 0) {
87bc5531deSDag-Erling Smørgrav sshbuf_free(b);
88bc5531deSDag-Erling Smørgrav return r;
891e8db6e2SBrian Feldman }
901e8db6e2SBrian Feldman #ifdef DEBUG_KEXDH
91bc5531deSDag-Erling Smørgrav sshbuf_dump(b, stderr);
921e8db6e2SBrian Feldman #endif
93bc5531deSDag-Erling Smørgrav if (ssh_digest_buffer(hash_alg, b, hash, *hashlen) != 0) {
94bc5531deSDag-Erling Smørgrav sshbuf_free(b);
95bc5531deSDag-Erling Smørgrav return SSH_ERR_LIBCRYPTO_ERROR;
961e8db6e2SBrian Feldman }
97bc5531deSDag-Erling Smørgrav sshbuf_free(b);
98bc5531deSDag-Erling Smørgrav *hashlen = ssh_digest_bytes(hash_alg);
99bc5531deSDag-Erling Smørgrav #ifdef DEBUG_KEXDH
100bc5531deSDag-Erling Smørgrav dump_digest("hash", hash, *hashlen);
101bc5531deSDag-Erling Smørgrav #endif
102bc5531deSDag-Erling Smørgrav return 0;
103bc5531deSDag-Erling Smørgrav }
104bc5531deSDag-Erling Smørgrav #endif /* WITH_OPENSSL */
105