1262566Sdes/* $OpenBSD: kexecdh.c,v 1.5 2014/01/09 23:20:00 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" 47262566Sdes#include "digest.h" 48218767Sdes 49218767Sdesvoid 50218767Sdeskex_ecdh_hash( 51262566Sdes int hash_alg, 52218767Sdes const EC_GROUP *ec_group, 53218767Sdes char *client_version_string, 54218767Sdes char *server_version_string, 55218767Sdes char *ckexinit, int ckexinitlen, 56218767Sdes char *skexinit, int skexinitlen, 57218767Sdes u_char *serverhostkeyblob, int sbloblen, 58218767Sdes const EC_POINT *client_dh_pub, 59218767Sdes const EC_POINT *server_dh_pub, 60218767Sdes const BIGNUM *shared_secret, 61218767Sdes u_char **hash, u_int *hashlen) 62218767Sdes{ 63218767Sdes Buffer b; 64262566Sdes static u_char digest[SSH_DIGEST_MAX_LENGTH]; 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 86262566Sdes if (ssh_digest_buffer(hash_alg, &b, digest, sizeof(digest)) != 0) 87262566Sdes fatal("%s: ssh_digest_buffer failed", __func__); 88218767Sdes 89218767Sdes buffer_free(&b); 90218767Sdes 91218767Sdes#ifdef DEBUG_KEX 92262566Sdes dump_digest("hash", digest, ssh_digest_bytes(hash_alg)); 93218767Sdes#endif 94218767Sdes *hash = digest; 95262566Sdes *hashlen = ssh_digest_bytes(hash_alg); 96218767Sdes} 97218767Sdes#endif /* OPENSSL_HAS_ECC */ 98