1323124Sdes/* $OpenBSD: kexc25519.c,v 1.10 2016/05/02 08:49:03 djm Exp $ */
2261287Sdes/*
3261287Sdes * Copyright (c) 2001, 2013 Markus Friedl.  All rights reserved.
4261287Sdes * Copyright (c) 2010 Damien Miller.  All rights reserved.
5261287Sdes * Copyright (c) 2013 Aris Adamantiadis.  All rights reserved.
6261287Sdes *
7261287Sdes * Redistribution and use in source and binary forms, with or without
8261287Sdes * modification, are permitted provided that the following conditions
9261287Sdes * are met:
10261287Sdes * 1. Redistributions of source code must retain the above copyright
11261287Sdes *    notice, this list of conditions and the following disclaimer.
12261287Sdes * 2. Redistributions in binary form must reproduce the above copyright
13261287Sdes *    notice, this list of conditions and the following disclaimer in the
14261287Sdes *    documentation and/or other materials provided with the distribution.
15261287Sdes *
16261287Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17261287Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18261287Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19261287Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20261287Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21261287Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22261287Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23261287Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24261287Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25261287Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26261287Sdes */
27261287Sdes
28261287Sdes#include "includes.h"
29261287Sdes
30261287Sdes#include <sys/types.h>
31261287Sdes
32261287Sdes#include <signal.h>
33261287Sdes#include <string.h>
34261287Sdes
35261287Sdes#include <openssl/bn.h>
36261287Sdes#include <openssl/evp.h>
37261287Sdes
38295367Sdes#include "sshbuf.h"
39261287Sdes#include "ssh2.h"
40295367Sdes#include "sshkey.h"
41261287Sdes#include "cipher.h"
42261287Sdes#include "kex.h"
43261287Sdes#include "log.h"
44261287Sdes#include "digest.h"
45295367Sdes#include "ssherr.h"
46261287Sdes
47261287Sdesextern int crypto_scalarmult_curve25519(u_char a[CURVE25519_SIZE],
48261287Sdes    const u_char b[CURVE25519_SIZE], const u_char c[CURVE25519_SIZE])
49261287Sdes	__attribute__((__bounded__(__minbytes__, 1, CURVE25519_SIZE)))
50261287Sdes	__attribute__((__bounded__(__minbytes__, 2, CURVE25519_SIZE)))
51261287Sdes	__attribute__((__bounded__(__minbytes__, 3, CURVE25519_SIZE)));
52261287Sdes
53261287Sdesvoid
54261287Sdeskexc25519_keygen(u_char key[CURVE25519_SIZE], u_char pub[CURVE25519_SIZE])
55261287Sdes{
56261287Sdes	static const u_char basepoint[CURVE25519_SIZE] = {9};
57261287Sdes
58261287Sdes	arc4random_buf(key, CURVE25519_SIZE);
59261287Sdes	crypto_scalarmult_curve25519(pub, key, basepoint);
60261287Sdes}
61261287Sdes
62295367Sdesint
63261287Sdeskexc25519_shared_key(const u_char key[CURVE25519_SIZE],
64295367Sdes    const u_char pub[CURVE25519_SIZE], struct sshbuf *out)
65261287Sdes{
66261287Sdes	u_char shared_key[CURVE25519_SIZE];
67295367Sdes	int r;
68261287Sdes
69295367Sdes	/* Check for all-zero public key */
70295367Sdes	explicit_bzero(shared_key, CURVE25519_SIZE);
71295367Sdes	if (timingsafe_bcmp(pub, shared_key, CURVE25519_SIZE) == 0)
72295367Sdes		return SSH_ERR_KEY_INVALID_EC_VALUE;
73295367Sdes
74261287Sdes	crypto_scalarmult_curve25519(shared_key, key, pub);
75261287Sdes#ifdef DEBUG_KEXECDH
76261287Sdes	dump_digest("shared secret", shared_key, CURVE25519_SIZE);
77261287Sdes#endif
78295367Sdes	sshbuf_reset(out);
79295367Sdes	r = sshbuf_put_bignum2_bytes(out, shared_key, CURVE25519_SIZE);
80264377Sdes	explicit_bzero(shared_key, CURVE25519_SIZE);
81295367Sdes	return r;
82261287Sdes}
83261287Sdes
84295367Sdesint
85261287Sdeskex_c25519_hash(
86261287Sdes    int hash_alg,
87295367Sdes    const char *client_version_string,
88295367Sdes    const char *server_version_string,
89323124Sdes    const u_char *ckexinit, size_t ckexinitlen,
90323124Sdes    const u_char *skexinit, size_t skexinitlen,
91295367Sdes    const u_char *serverhostkeyblob, size_t sbloblen,
92261287Sdes    const u_char client_dh_pub[CURVE25519_SIZE],
93261287Sdes    const u_char server_dh_pub[CURVE25519_SIZE],
94295367Sdes    const u_char *shared_secret, size_t secretlen,
95295367Sdes    u_char *hash, size_t *hashlen)
96261287Sdes{
97295367Sdes	struct sshbuf *b;
98295367Sdes	int r;
99261287Sdes
100295367Sdes	if (*hashlen < ssh_digest_bytes(hash_alg))
101295367Sdes		return SSH_ERR_INVALID_ARGUMENT;
102295367Sdes	if ((b = sshbuf_new()) == NULL)
103295367Sdes		return SSH_ERR_ALLOC_FAIL;
104295367Sdes	if ((r = sshbuf_put_cstring(b, client_version_string)) < 0 ||
105295367Sdes	    (r = sshbuf_put_cstring(b, server_version_string)) < 0 ||
106295367Sdes	    /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
107295367Sdes	    (r = sshbuf_put_u32(b, ckexinitlen+1)) < 0 ||
108295367Sdes	    (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) < 0 ||
109295367Sdes	    (r = sshbuf_put(b, ckexinit, ckexinitlen)) < 0 ||
110295367Sdes	    (r = sshbuf_put_u32(b, skexinitlen+1)) < 0 ||
111295367Sdes	    (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) < 0 ||
112295367Sdes	    (r = sshbuf_put(b, skexinit, skexinitlen)) < 0 ||
113295367Sdes	    (r = sshbuf_put_string(b, serverhostkeyblob, sbloblen)) < 0 ||
114295367Sdes	    (r = sshbuf_put_string(b, client_dh_pub, CURVE25519_SIZE)) < 0 ||
115295367Sdes	    (r = sshbuf_put_string(b, server_dh_pub, CURVE25519_SIZE)) < 0 ||
116295367Sdes	    (r = sshbuf_put(b, shared_secret, secretlen)) < 0) {
117295367Sdes		sshbuf_free(b);
118295367Sdes		return r;
119295367Sdes	}
120261287Sdes#ifdef DEBUG_KEX
121295367Sdes	sshbuf_dump(b, stderr);
122261287Sdes#endif
123295367Sdes	if (ssh_digest_buffer(hash_alg, b, hash, *hashlen) != 0) {
124295367Sdes		sshbuf_free(b);
125295367Sdes		return SSH_ERR_LIBCRYPTO_ERROR;
126295367Sdes	}
127295367Sdes	sshbuf_free(b);
128295367Sdes	*hashlen = ssh_digest_bytes(hash_alg);
129261287Sdes#ifdef DEBUG_KEX
130295367Sdes	dump_digest("hash", hash, *hashlen);
131261287Sdes#endif
132295367Sdes	return 0;
133261287Sdes}
134