blob: 348a7d50d0cf79d576f7a66c564e3cf43cab2c0d [file] [log] [blame]
Damien Miller094003f2013-11-04 22:59:27 +11001/* $OpenBSD: kexc25519.c,v 1.2 2013/11/02 22:02:14 markus Exp $ */
2/*
3 * Copyright (c) 2001, 2013 Markus Friedl. All rights reserved.
4 * Copyright (c) 2010 Damien Miller. All rights reserved.
5 * Copyright (c) 2013 Aris Adamantiadis. 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
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
38#include "buffer.h"
39#include "ssh2.h"
40#include "key.h"
41#include "cipher.h"
42#include "kex.h"
43#include "log.h"
44
45extern int crypto_scalarmult_curve25519(u_char a[CURVE25519_SIZE],
46 const u_char b[CURVE25519_SIZE], const u_char c[CURVE25519_SIZE])
47 __attribute__((__bounded__(__minbytes__, 1, CURVE25519_SIZE)))
48 __attribute__((__bounded__(__minbytes__, 2, CURVE25519_SIZE)))
49 __attribute__((__bounded__(__minbytes__, 3, CURVE25519_SIZE)));
50
51void
52kexc25519_keygen(u_char key[CURVE25519_SIZE], u_char pub[CURVE25519_SIZE])
53{
54 static const u_char basepoint[CURVE25519_SIZE] = {9};
55
56 arc4random_buf(key, CURVE25519_SIZE);
57 crypto_scalarmult_curve25519(pub, key, basepoint);
58}
59
60BIGNUM *
61kexc25519_shared_key(const u_char key[CURVE25519_SIZE],
62 const u_char pub[CURVE25519_SIZE])
63{
64 u_char shared_key[CURVE25519_SIZE];
65 BIGNUM *shared_secret;
66
67 crypto_scalarmult_curve25519(shared_key, key, pub);
68#ifdef DEBUG_KEXECDH
69 dump_digest("shared secret", shared_key, CURVE25519_SIZE);
70#endif
71 if ((shared_secret = BN_new()) == NULL)
72 fatal("%s: BN_new failed", __func__);
73 if (BN_bin2bn(shared_key, sizeof(shared_key), shared_secret) == NULL)
74 fatal("%s: BN_bin2bn failed", __func__);
75 memset(shared_key, 0, CURVE25519_SIZE); /* XXX explicit_bzero() */
76 return (shared_secret);
77}
78
79void
80kex_c25519_hash(
81 const EVP_MD *evp_md,
82 char *client_version_string,
83 char *server_version_string,
84 char *ckexinit, int ckexinitlen,
85 char *skexinit, int skexinitlen,
86 u_char *serverhostkeyblob, int sbloblen,
87 const u_char client_dh_pub[CURVE25519_SIZE],
88 const u_char server_dh_pub[CURVE25519_SIZE],
89 const BIGNUM *shared_secret,
90 u_char **hash, u_int *hashlen)
91{
92 Buffer b;
93 EVP_MD_CTX md;
94 static u_char digest[EVP_MAX_MD_SIZE];
95
96 buffer_init(&b);
97 buffer_put_cstring(&b, client_version_string);
98 buffer_put_cstring(&b, server_version_string);
99
100 /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
101 buffer_put_int(&b, ckexinitlen+1);
102 buffer_put_char(&b, SSH2_MSG_KEXINIT);
103 buffer_append(&b, ckexinit, ckexinitlen);
104 buffer_put_int(&b, skexinitlen+1);
105 buffer_put_char(&b, SSH2_MSG_KEXINIT);
106 buffer_append(&b, skexinit, skexinitlen);
107
108 buffer_put_string(&b, serverhostkeyblob, sbloblen);
109 buffer_put_string(&b, client_dh_pub, CURVE25519_SIZE);
110 buffer_put_string(&b, server_dh_pub, CURVE25519_SIZE);
111 buffer_put_bignum2(&b, shared_secret);
112
113#ifdef DEBUG_KEX
114 buffer_dump(&b);
115#endif
116 EVP_DigestInit(&md, evp_md);
117 EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
118 EVP_DigestFinal(&md, digest, NULL);
119
120 buffer_free(&b);
121
122#ifdef DEBUG_KEX
123 dump_digest("hash", digest, EVP_MD_size(evp_md));
124#endif
125 *hash = digest;
126 *hashlen = EVP_MD_size(evp_md);
127}