xref: /netbsd-src/crypto/external/bsd/openssh/dist/kex.h (revision b8ecfcfef0e343ad71faea7a54fb5fcb42ad4e27)
1 /*	$NetBSD: kex.h,v 1.8 2014/10/19 16:30:58 christos Exp $	*/
2 /* $OpenBSD: kex.h,v 1.64 2014/05/02 03:27:54 djm Exp $ */
3 
4 /*
5  * Copyright (c) 2000, 2001 Markus Friedl.  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 #ifndef KEX_H
28 #define KEX_H
29 
30 #include <openssl/evp.h>
31 #include <openssl/hmac.h>
32 #include <openssl/ec.h>
33 
34 #define KEX_COOKIE_LEN	16
35 
36 #define	KEX_DH1			"diffie-hellman-group1-sha1"
37 #define	KEX_DH14		"diffie-hellman-group14-sha1"
38 #define	KEX_DHGEX_SHA1		"diffie-hellman-group-exchange-sha1"
39 #define	KEX_DHGEX_SHA256	"diffie-hellman-group-exchange-sha256"
40 #define	KEX_RESUME		"resume@appgate.com"
41 #define	KEX_ECDH_SHA2_NISTP256	"ecdh-sha2-nistp256"
42 #define	KEX_ECDH_SHA2_NISTP384	"ecdh-sha2-nistp384"
43 #define	KEX_ECDH_SHA2_NISTP521	"ecdh-sha2-nistp521"
44 #define	KEX_CURVE25519_SHA256	"curve25519-sha256@libssh.org"
45 
46 #define COMP_NONE	0
47 #define COMP_ZLIB	1
48 #define COMP_DELAYED	2
49 
50 enum kex_init_proposals {
51 	PROPOSAL_KEX_ALGS,
52 	PROPOSAL_SERVER_HOST_KEY_ALGS,
53 	PROPOSAL_ENC_ALGS_CTOS,
54 	PROPOSAL_ENC_ALGS_STOC,
55 	PROPOSAL_MAC_ALGS_CTOS,
56 	PROPOSAL_MAC_ALGS_STOC,
57 	PROPOSAL_COMP_ALGS_CTOS,
58 	PROPOSAL_COMP_ALGS_STOC,
59 	PROPOSAL_LANG_CTOS,
60 	PROPOSAL_LANG_STOC,
61 	PROPOSAL_MAX
62 };
63 
64 enum kex_modes {
65 	MODE_IN,
66 	MODE_OUT,
67 	MODE_MAX
68 };
69 
70 enum kex_exchange {
71 	KEX_DH_GRP1_SHA1,
72 	KEX_DH_GRP14_SHA1,
73 	KEX_DH_GEX_SHA1,
74 	KEX_DH_GEX_SHA256,
75 	KEX_ECDH_SHA2,
76 	KEX_C25519_SHA256,
77 	KEX_MAX
78 };
79 
80 #define KEX_INIT_SENT	0x0001
81 
82 typedef struct Kex Kex;
83 typedef struct Mac Mac;
84 typedef struct Comp Comp;
85 typedef struct Enc Enc;
86 typedef struct Newkeys Newkeys;
87 
88 struct Enc {
89 	char	*name;
90 	const Cipher *cipher;
91 	int	enabled;
92 	u_int	key_len;
93 	u_int	iv_len;
94 	u_int	block_size;
95 	u_char	*key;
96 	u_char	*iv;
97 };
98 struct Mac {
99 	char	*name;
100 	int	enabled;
101 	u_int	mac_len;
102 	u_char	*key;
103 	u_int	key_len;
104 	int	type;
105 	int	etm;		/* Encrypt-then-MAC */
106 	struct ssh_hmac_ctx	*hmac_ctx;
107 	struct umac_ctx		*umac_ctx;
108 };
109 struct Comp {
110 	int	type;
111 	int	enabled;
112 	char	*name;
113 };
114 struct Newkeys {
115 	Enc	enc;
116 	Mac	mac;
117 	Comp	comp;
118 };
119 struct Kex {
120 	u_char	*session_id;
121 	u_int	session_id_len;
122 	Newkeys	*newkeys[MODE_MAX];
123 	u_int	we_need;
124 	u_int	dh_need;
125 	int	server;
126 	char	*name;
127 	int	hostkey_type;
128 	int	kex_type;
129 	int	roaming;
130 	Buffer	my;
131 	Buffer	peer;
132 	sig_atomic_t done;
133 	int	flags;
134 	int	hash_alg;
135 	int	ec_nid;
136 	char	*client_version_string;
137 	char	*server_version_string;
138 	int	(*verify_host_key)(Key *);
139 	Key	*(*load_host_public_key)(int);
140 	Key	*(*load_host_private_key)(int);
141 	int	(*host_key_index)(Key *);
142 	void    (*sign)(Key *, Key *, u_char **, u_int *, u_char *, u_int);
143 	void	(*kex[KEX_MAX])(Kex *);
144 };
145 
146 void	 kex_prop2buf(Buffer *, const char *proposal[PROPOSAL_MAX]);
147 
148 int	 kex_names_valid(const char *);
149 char	*kex_alg_list(char);
150 
151 Kex	*kex_setup(const char *[PROPOSAL_MAX]);
152 void	 kex_finish(Kex *);
153 
154 void	 kex_send_kexinit(Kex *);
155 void	 kex_input_kexinit(int, u_int32_t, void *);
156 void	 kex_derive_keys(Kex *, u_char *, u_int, const u_char *, u_int);
157 void	 kex_derive_keys_bn(Kex *, u_char *, u_int, const BIGNUM *);
158 
159 Newkeys *kex_get_newkeys(int);
160 
161 void	 kexdh_client(Kex *);
162 void	 kexdh_server(Kex *);
163 void	 kexgex_client(Kex *);
164 void	 kexgex_server(Kex *);
165 void	 kexecdh_client(Kex *);
166 void	 kexecdh_server(Kex *);
167 void	 kexc25519_client(Kex *);
168 void	 kexc25519_server(Kex *);
169 
170 void
171 kex_dh_hash(char *, char *, char *, int, char *, int, u_char *, int,
172     BIGNUM *, BIGNUM *, BIGNUM *, u_char **, u_int *);
173 void
174 kexgex_hash(int, char *, char *, char *, int, char *,
175     int, u_char *, int, int, int, int, BIGNUM *, BIGNUM *, BIGNUM *,
176     BIGNUM *, BIGNUM *, u_char **, u_int *);
177 void
178 kex_ecdh_hash(int, const EC_GROUP *, char *, char *, char *, int,
179     char *, int, u_char *, int, const EC_POINT *, const EC_POINT *,
180     const BIGNUM *, u_char **, u_int *);
181 void
182 kex_c25519_hash(int, char *, char *, char *, int,
183     char *, int, u_char *, int, const u_char *, const u_char *,
184     const u_char *, u_int, u_char **, u_int *);
185 
186 #define CURVE25519_SIZE 32
187 void	kexc25519_keygen(u_char[CURVE25519_SIZE], u_char[CURVE25519_SIZE])
188 	__attribute__((__bounded__(__minbytes__, 1, CURVE25519_SIZE)))
189 	__attribute__((__bounded__(__minbytes__, 2, CURVE25519_SIZE)));
190 void kexc25519_shared_key(const u_char key[CURVE25519_SIZE],
191     const u_char pub[CURVE25519_SIZE], Buffer *out)
192 	__attribute__((__bounded__(__minbytes__, 1, CURVE25519_SIZE)))
193 	__attribute__((__bounded__(__minbytes__, 2, CURVE25519_SIZE)));
194 
195 void
196 derive_ssh1_session_id(BIGNUM *, BIGNUM *, u_int8_t[8], u_int8_t[16]);
197 
198 #if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) || defined(DEBUG_KEXECDH)
199 void	dump_digest(char *, u_char *, int);
200 #endif
201 
202 #endif
203