1*664f4763Szrj /* $OpenBSD: kexgex.c,v 1.32 2019/01/23 00:30:41 djm Exp $ */
218de8d7fSPeter Avalos /*
318de8d7fSPeter Avalos * Copyright (c) 2000 Niels Provos. All rights reserved.
418de8d7fSPeter Avalos * Copyright (c) 2001 Markus Friedl. All rights reserved.
518de8d7fSPeter Avalos *
618de8d7fSPeter Avalos * Redistribution and use in source and binary forms, with or without
718de8d7fSPeter Avalos * modification, are permitted provided that the following conditions
818de8d7fSPeter Avalos * are met:
918de8d7fSPeter Avalos * 1. Redistributions of source code must retain the above copyright
1018de8d7fSPeter Avalos * notice, this list of conditions and the following disclaimer.
1118de8d7fSPeter Avalos * 2. Redistributions in binary form must reproduce the above copyright
1218de8d7fSPeter Avalos * notice, this list of conditions and the following disclaimer in the
1318de8d7fSPeter Avalos * documentation and/or other materials provided with the distribution.
1418de8d7fSPeter Avalos *
1518de8d7fSPeter Avalos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1618de8d7fSPeter Avalos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1718de8d7fSPeter Avalos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1818de8d7fSPeter Avalos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1918de8d7fSPeter Avalos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2018de8d7fSPeter Avalos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2118de8d7fSPeter Avalos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2218de8d7fSPeter Avalos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2318de8d7fSPeter Avalos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2418de8d7fSPeter Avalos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2518de8d7fSPeter Avalos */
2618de8d7fSPeter Avalos
2718de8d7fSPeter Avalos #include "includes.h"
2818de8d7fSPeter Avalos
29e9778795SPeter Avalos #ifdef WITH_OPENSSL
30e9778795SPeter Avalos
3118de8d7fSPeter Avalos #include <sys/types.h>
3218de8d7fSPeter Avalos
3318de8d7fSPeter Avalos #include <openssl/evp.h>
3418de8d7fSPeter Avalos #include <signal.h>
3518de8d7fSPeter Avalos
36*664f4763Szrj #include "openbsd-compat/openssl-compat.h"
37*664f4763Szrj
38e9778795SPeter Avalos #include "sshkey.h"
3918de8d7fSPeter Avalos #include "cipher.h"
4018de8d7fSPeter Avalos #include "kex.h"
4118de8d7fSPeter Avalos #include "ssh2.h"
42e9778795SPeter Avalos #include "ssherr.h"
43e9778795SPeter Avalos #include "sshbuf.h"
4436e94dc5SPeter Avalos #include "digest.h"
4518de8d7fSPeter Avalos
46e9778795SPeter Avalos 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)4718de8d7fSPeter Avalos kexgex_hash(
4836e94dc5SPeter Avalos int hash_alg,
49*664f4763Szrj const struct sshbuf *client_version,
50*664f4763Szrj const struct sshbuf *server_version,
51*664f4763Szrj const struct sshbuf *client_kexinit,
52*664f4763Szrj const struct sshbuf *server_kexinit,
53*664f4763Szrj const struct sshbuf *server_host_key_blob,
54e9778795SPeter Avalos int min, int wantbits, int max,
55e9778795SPeter Avalos const BIGNUM *prime,
56e9778795SPeter Avalos const BIGNUM *gen,
57e9778795SPeter Avalos const BIGNUM *client_dh_pub,
58e9778795SPeter Avalos const BIGNUM *server_dh_pub,
59*664f4763Szrj const u_char *shared_secret, size_t secretlen,
60e9778795SPeter Avalos u_char *hash, size_t *hashlen)
6118de8d7fSPeter Avalos {
62e9778795SPeter Avalos struct sshbuf *b;
63e9778795SPeter Avalos int r;
6418de8d7fSPeter Avalos
65e9778795SPeter Avalos if (*hashlen < ssh_digest_bytes(SSH_DIGEST_SHA1))
66e9778795SPeter Avalos return SSH_ERR_INVALID_ARGUMENT;
67e9778795SPeter Avalos if ((b = sshbuf_new()) == NULL)
68e9778795SPeter Avalos return SSH_ERR_ALLOC_FAIL;
69*664f4763Szrj if ((r = sshbuf_put_stringb(b, client_version)) < 0 ||
70*664f4763Szrj (r = sshbuf_put_stringb(b, server_version)) < 0 ||
7118de8d7fSPeter Avalos /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
72*664f4763Szrj (r = sshbuf_put_u32(b, sshbuf_len(client_kexinit) + 1)) != 0 ||
73e9778795SPeter Avalos (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
74*664f4763Szrj (r = sshbuf_putb(b, client_kexinit)) != 0 ||
75*664f4763Szrj (r = sshbuf_put_u32(b, sshbuf_len(server_kexinit) + 1)) != 0 ||
76e9778795SPeter Avalos (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
77*664f4763Szrj (r = sshbuf_putb(b, server_kexinit)) != 0 ||
78*664f4763Szrj (r = sshbuf_put_stringb(b, server_host_key_blob)) != 0 ||
79e9778795SPeter Avalos (min != -1 && (r = sshbuf_put_u32(b, min)) != 0) ||
80e9778795SPeter Avalos (r = sshbuf_put_u32(b, wantbits)) != 0 ||
81e9778795SPeter Avalos (max != -1 && (r = sshbuf_put_u32(b, max)) != 0) ||
82e9778795SPeter Avalos (r = sshbuf_put_bignum2(b, prime)) != 0 ||
83e9778795SPeter Avalos (r = sshbuf_put_bignum2(b, gen)) != 0 ||
84e9778795SPeter Avalos (r = sshbuf_put_bignum2(b, client_dh_pub)) != 0 ||
85e9778795SPeter Avalos (r = sshbuf_put_bignum2(b, server_dh_pub)) != 0 ||
86*664f4763Szrj (r = sshbuf_put(b, shared_secret, secretlen)) != 0) {
87e9778795SPeter Avalos sshbuf_free(b);
88e9778795SPeter Avalos return r;
8918de8d7fSPeter Avalos }
9018de8d7fSPeter Avalos #ifdef DEBUG_KEXDH
91e9778795SPeter Avalos sshbuf_dump(b, stderr);
9218de8d7fSPeter Avalos #endif
93e9778795SPeter Avalos if (ssh_digest_buffer(hash_alg, b, hash, *hashlen) != 0) {
94e9778795SPeter Avalos sshbuf_free(b);
95e9778795SPeter Avalos return SSH_ERR_LIBCRYPTO_ERROR;
9618de8d7fSPeter Avalos }
97e9778795SPeter Avalos sshbuf_free(b);
98e9778795SPeter Avalos *hashlen = ssh_digest_bytes(hash_alg);
99e9778795SPeter Avalos #ifdef DEBUG_KEXDH
100e9778795SPeter Avalos dump_digest("hash", hash, *hashlen);
101e9778795SPeter Avalos #endif
102e9778795SPeter Avalos return 0;
103e9778795SPeter Avalos }
104e9778795SPeter Avalos #endif /* WITH_OPENSSL */
105