xref: /openssh-portable/kex.h (revision 3dd0c64e)
1*3dd0c64eSdjm@openbsd.org /* $OpenBSD: kex.h,v 1.114 2021/01/31 22:55:29 djm Exp $ */
236579d3dSBen Lindstrom 
333b13568SDamien Miller /*
444697233SBen Lindstrom  * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
533b13568SDamien Miller  *
633b13568SDamien Miller  * Redistribution and use in source and binary forms, with or without
733b13568SDamien Miller  * modification, are permitted provided that the following conditions
833b13568SDamien Miller  * are met:
933b13568SDamien Miller  * 1. Redistributions of source code must retain the above copyright
1033b13568SDamien Miller  *    notice, this list of conditions and the following disclaimer.
1133b13568SDamien Miller  * 2. Redistributions in binary form must reproduce the above copyright
1233b13568SDamien Miller  *    notice, this list of conditions and the following disclaimer in the
1333b13568SDamien Miller  *    documentation and/or other materials provided with the distribution.
1433b13568SDamien Miller  *
1533b13568SDamien Miller  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1633b13568SDamien Miller  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1733b13568SDamien Miller  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1833b13568SDamien Miller  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1933b13568SDamien Miller  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2033b13568SDamien Miller  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2133b13568SDamien Miller  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2233b13568SDamien Miller  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2333b13568SDamien Miller  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2433b13568SDamien Miller  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2533b13568SDamien Miller  */
2633b13568SDamien Miller #ifndef KEX_H
2733b13568SDamien Miller #define KEX_H
2833b13568SDamien Miller 
29128343bcSmarkus@openbsd.org #include "mac.h"
30dfd59161Sdjm@openbsd.org #include "crypto_api.h"
31128343bcSmarkus@openbsd.org 
32773dda25SDamien Miller #ifdef WITH_OPENSSL
33edd1d3a6SDamien Miller # include <openssl/bn.h>
34edd1d3a6SDamien Miller # include <openssl/dh.h>
35edd1d3a6SDamien Miller # include <openssl/ecdsa.h>
36773dda25SDamien Miller # ifdef OPENSSL_HAS_ECC
37773dda25SDamien Miller #  include <openssl/ec.h>
38773dda25SDamien Miller # else /* OPENSSL_HAS_ECC */
39773dda25SDamien Miller #  define EC_KEY	void
40773dda25SDamien Miller #  define EC_GROUP	void
41773dda25SDamien Miller #  define EC_POINT	void
42773dda25SDamien Miller # endif /* OPENSSL_HAS_ECC */
43773dda25SDamien Miller #else /* WITH_OPENSSL */
44eef1447dSDamien Miller # define DH		void
45eef1447dSDamien Miller # define BIGNUM		void
46773dda25SDamien Miller # define EC_KEY		void
47773dda25SDamien Miller # define EC_GROUP	void
48773dda25SDamien Miller # define EC_POINT	void
49773dda25SDamien Miller #endif /* WITH_OPENSSL */
50773dda25SDamien Miller 
515b48cdd8SDarren Tucker #define KEX_COOKIE_LEN	16
525b48cdd8SDarren Tucker 
5333b13568SDamien Miller #define	KEX_DH1				"diffie-hellman-group1-sha1"
540e8eeec8Sdjm@openbsd.org #define	KEX_DH14_SHA1			"diffie-hellman-group14-sha1"
550e8eeec8Sdjm@openbsd.org #define	KEX_DH14_SHA256			"diffie-hellman-group14-sha256"
560e8eeec8Sdjm@openbsd.org #define	KEX_DH16_SHA512			"diffie-hellman-group16-sha512"
570e8eeec8Sdjm@openbsd.org #define	KEX_DH18_SHA512			"diffie-hellman-group18-sha512"
5819bb3a57SDamien Miller #define	KEX_DHGEX_SHA1			"diffie-hellman-group-exchange-sha1"
59a63128d1SDamien Miller #define	KEX_DHGEX_SHA256		"diffie-hellman-group-exchange-sha256"
60ea11119eSDamien Miller #define	KEX_ECDH_SHA2_NISTP256		"ecdh-sha2-nistp256"
61ea11119eSDamien Miller #define	KEX_ECDH_SHA2_NISTP384		"ecdh-sha2-nistp384"
62ea11119eSDamien Miller #define	KEX_ECDH_SHA2_NISTP521		"ecdh-sha2-nistp521"
63b97739dcSdjm@openbsd.org #define	KEX_CURVE25519_SHA256		"curve25519-sha256"
64b97739dcSdjm@openbsd.org #define	KEX_CURVE25519_SHA256_OLD	"curve25519-sha256@libssh.org"
652c71cec0Sdjm@openbsd.org #define	KEX_SNTRUP761X25519_SHA512	"sntrup761x25519-sha512@openssh.com"
6633b13568SDamien Miller 
679786e6e2SDamien Miller #define COMP_NONE	0
68984bacfaSsf@openbsd.org /* pre-auth compression (COMP_ZLIB) is only supported in the client */
69ab39267fSsf@openbsd.org #define COMP_ZLIB	1
70168b46f4Ssf@openbsd.org #define COMP_DELAYED	2
719786e6e2SDamien Miller 
7257d10cbeSmarkus@openbsd.org #define CURVE25519_SIZE 32
7357d10cbeSmarkus@openbsd.org 
7433b13568SDamien Miller enum kex_init_proposals {
7533b13568SDamien Miller 	PROPOSAL_KEX_ALGS,
7633b13568SDamien Miller 	PROPOSAL_SERVER_HOST_KEY_ALGS,
7733b13568SDamien Miller 	PROPOSAL_ENC_ALGS_CTOS,
7833b13568SDamien Miller 	PROPOSAL_ENC_ALGS_STOC,
7933b13568SDamien Miller 	PROPOSAL_MAC_ALGS_CTOS,
8033b13568SDamien Miller 	PROPOSAL_MAC_ALGS_STOC,
8133b13568SDamien Miller 	PROPOSAL_COMP_ALGS_CTOS,
8233b13568SDamien Miller 	PROPOSAL_COMP_ALGS_STOC,
8333b13568SDamien Miller 	PROPOSAL_LANG_CTOS,
8433b13568SDamien Miller 	PROPOSAL_LANG_STOC,
8533b13568SDamien Miller 	PROPOSAL_MAX
8633b13568SDamien Miller };
8733b13568SDamien Miller 
8833b13568SDamien Miller enum kex_modes {
8933b13568SDamien Miller 	MODE_IN,
9033b13568SDamien Miller 	MODE_OUT,
9133b13568SDamien Miller 	MODE_MAX
9233b13568SDamien Miller };
9333b13568SDamien Miller 
94874d77bbSDamien Miller enum kex_exchange {
958e7fb335SDamien Miller 	KEX_DH_GRP1_SHA1,
96f675fc49SDamien Miller 	KEX_DH_GRP14_SHA1,
970e8eeec8Sdjm@openbsd.org 	KEX_DH_GRP14_SHA256,
980e8eeec8Sdjm@openbsd.org 	KEX_DH_GRP16_SHA512,
990e8eeec8Sdjm@openbsd.org 	KEX_DH_GRP18_SHA512,
1008e7fb335SDamien Miller 	KEX_DH_GEX_SHA1,
101a63128d1SDamien Miller 	KEX_DH_GEX_SHA256,
102eb8b60e3SDamien Miller 	KEX_ECDH_SHA2,
1031e124260SDamien Miller 	KEX_C25519_SHA256,
1042c71cec0Sdjm@openbsd.org 	KEX_KEM_SNTRUP761X25519_SHA512,
1058e7fb335SDamien Miller 	KEX_MAX
106874d77bbSDamien Miller };
107874d77bbSDamien Miller 
108aa623142Sdjm@openbsd.org #define KEX_INIT_SENT	0x0001
109aa623142Sdjm@openbsd.org #define KEX_INITIAL	0x0002
1102d90e003SBen Lindstrom 
111091c3028Smarkus@openbsd.org struct sshenc {
112874d77bbSDamien Miller 	char	*name;
113091c3028Smarkus@openbsd.org 	const struct sshcipher *cipher;
11433b13568SDamien Miller 	int	enabled;
115963f6b25SDamien Miller 	u_int	key_len;
1161d75abfeSDamien Miller 	u_int	iv_len;
117963f6b25SDamien Miller 	u_int	block_size;
11846c16220SBen Lindstrom 	u_char	*key;
11946c16220SBen Lindstrom 	u_char	*iv;
12033b13568SDamien Miller };
121091c3028Smarkus@openbsd.org struct sshcomp {
122091c3028Smarkus@openbsd.org 	u_int	type;
12333b13568SDamien Miller 	int	enabled;
12433b13568SDamien Miller 	char	*name;
12533b13568SDamien Miller };
126091c3028Smarkus@openbsd.org struct newkeys {
127091c3028Smarkus@openbsd.org 	struct sshenc	enc;
128091c3028Smarkus@openbsd.org 	struct sshmac	mac;
129091c3028Smarkus@openbsd.org 	struct sshcomp  comp;
1302d90e003SBen Lindstrom };
13157d10cbeSmarkus@openbsd.org 
13257d10cbeSmarkus@openbsd.org struct ssh;
13357d10cbeSmarkus@openbsd.org 
134091c3028Smarkus@openbsd.org struct kex {
135091c3028Smarkus@openbsd.org 	struct newkeys	*newkeys[MODE_MAX];
136eccb9de7SDamien Miller 	u_int	we_need;
13776eea4abSDamien Miller 	u_int	dh_need;
13833b13568SDamien Miller 	int	server;
13933b13568SDamien Miller 	char	*name;
14076c9fbbeSmarkus@openbsd.org 	char	*hostkey_alg;
1410bc1bd81SDamien Miller 	int	hostkey_type;
1425104db7cSdjm@openbsd.org 	int	hostkey_nid;
14357d10cbeSmarkus@openbsd.org 	u_int	kex_type;
1444ba0d547Sdjm@openbsd.org 	char	*server_sig_algs;
14576c9fbbeSmarkus@openbsd.org 	int	ext_info_c;
146091c3028Smarkus@openbsd.org 	struct sshbuf *my;
147091c3028Smarkus@openbsd.org 	struct sshbuf *peer;
1480a843d9aSdjm@openbsd.org 	struct sshbuf *client_version;
1490a843d9aSdjm@openbsd.org 	struct sshbuf *server_version;
15039be3dc2Sdjm@openbsd.org 	struct sshbuf *session_id;
15197c91f68SDamien Miller 	sig_atomic_t done;
15257d10cbeSmarkus@openbsd.org 	u_int	flags;
153b3051d01SDamien Miller 	int	hash_alg;
154ea11119eSDamien Miller 	int	ec_nid;
155f319912bSdjm@openbsd.org 	char	*failed_choice;
15657d10cbeSmarkus@openbsd.org 	int	(*verify_host_key)(struct sshkey *, struct ssh *);
1575104db7cSdjm@openbsd.org 	struct sshkey *(*load_host_public_key)(int, int, struct ssh *);
1585104db7cSdjm@openbsd.org 	struct sshkey *(*load_host_private_key)(int, int, struct ssh *);
159523463a3Sdjm@openbsd.org 	int	(*host_key_index)(struct sshkey *, int, struct ssh *);
16004c091fcSdjm@openbsd.org 	int	(*sign)(struct ssh *, struct sshkey *, struct sshkey *,
16104c091fcSdjm@openbsd.org 	    u_char **, size_t *, const u_char *, size_t, const char *);
16257d10cbeSmarkus@openbsd.org 	int	(*kex[KEX_MAX])(struct ssh *);
16357d10cbeSmarkus@openbsd.org 	/* kex specific state */
16457d10cbeSmarkus@openbsd.org 	DH	*dh;			/* DH */
16557d10cbeSmarkus@openbsd.org 	u_int	min, max, nbits;	/* GEX */
16657d10cbeSmarkus@openbsd.org 	EC_KEY	*ec_client_key;		/* ECDH */
16757d10cbeSmarkus@openbsd.org 	const EC_GROUP *ec_group;	/* ECDH */
168dfd59161Sdjm@openbsd.org 	u_char c25519_client_key[CURVE25519_SIZE]; /* 25519 + KEM */
16957d10cbeSmarkus@openbsd.org 	u_char c25519_client_pubkey[CURVE25519_SIZE]; /* 25519 */
1702c71cec0Sdjm@openbsd.org 	u_char sntrup761_client_key[crypto_kem_sntrup761_SECRETKEYBYTES]; /* KEM */
171aaca72d6Sdjm@openbsd.org 	struct sshbuf *client_pub;
17233b13568SDamien Miller };
17333b13568SDamien Miller 
174d5f62bf2SDamien Miller int	 kex_names_valid(const char *);
175690d9890SDamien Miller char	*kex_alg_list(char);
176f9eca249Sdjm@openbsd.org char	*kex_names_cat(const char *, const char *);
177312d2f28Sdjm@openbsd.org int	 kex_assemble_names(char **, const char *, const char *);
178d5f62bf2SDamien Miller 
1790a843d9aSdjm@openbsd.org int	 kex_exchange_identification(struct ssh *, int, const char *);
1800a843d9aSdjm@openbsd.org 
1810a843d9aSdjm@openbsd.org struct kex *kex_new(void);
1820a843d9aSdjm@openbsd.org int	 kex_ready(struct ssh *, char *[PROPOSAL_MAX]);
18357d10cbeSmarkus@openbsd.org int	 kex_setup(struct ssh *, char *[PROPOSAL_MAX]);
184091c3028Smarkus@openbsd.org void	 kex_free_newkeys(struct newkeys *);
18557d10cbeSmarkus@openbsd.org void	 kex_free(struct kex *);
186238abf6aSBen Lindstrom 
18757d10cbeSmarkus@openbsd.org int	 kex_buf2prop(struct sshbuf *, int *, char ***);
18857d10cbeSmarkus@openbsd.org int	 kex_prop2buf(struct sshbuf *, char *proposal[PROPOSAL_MAX]);
18957d10cbeSmarkus@openbsd.org void	 kex_prop_free(char **);
190bb39bafbSdjm@openbsd.org int	 kex_load_hostkey(struct ssh *, struct sshkey **, struct sshkey **);
191b1b2ff4eSdjm@openbsd.org int	 kex_verify_host_key(struct ssh *, struct sshkey *);
19257d10cbeSmarkus@openbsd.org 
19357d10cbeSmarkus@openbsd.org int	 kex_send_kexinit(struct ssh *);
1942ae666a8Smarkus@openbsd.org int	 kex_input_kexinit(int, u_int32_t, struct ssh *);
1952ae666a8Smarkus@openbsd.org int	 kex_input_ext_info(int, u_int32_t, struct ssh *);
196*3dd0c64eSdjm@openbsd.org int	 kex_protocol_error(int, u_int32_t, struct ssh *);
19757d10cbeSmarkus@openbsd.org int	 kex_derive_keys(struct ssh *, u_char *, u_int, const struct sshbuf *);
19857d10cbeSmarkus@openbsd.org int	 kex_send_newkeys(struct ssh *);
19919bcf2eaSdjm@openbsd.org int	 kex_start_rekex(struct ssh *);
20033b13568SDamien Miller 
20157d10cbeSmarkus@openbsd.org int	 kexgex_client(struct ssh *);
20257d10cbeSmarkus@openbsd.org int	 kexgex_server(struct ssh *);
203aaca72d6Sdjm@openbsd.org int	 kex_gen_client(struct ssh *);
204aaca72d6Sdjm@openbsd.org int	 kex_gen_server(struct ssh *);
205dfd59161Sdjm@openbsd.org 
2069c9c97e1Sdjm@openbsd.org int	 kex_dh_keypair(struct kex *);
20771e67fffSdjm@openbsd.org int	 kex_dh_enc(struct kex *, const struct sshbuf *, struct sshbuf **,
2089c9c97e1Sdjm@openbsd.org     struct sshbuf **);
20971e67fffSdjm@openbsd.org int	 kex_dh_dec(struct kex *, const struct sshbuf *, struct sshbuf **);
2109c9c97e1Sdjm@openbsd.org 
21192dda34eSdjm@openbsd.org int	 kex_ecdh_keypair(struct kex *);
21271e67fffSdjm@openbsd.org int	 kex_ecdh_enc(struct kex *, const struct sshbuf *, struct sshbuf **,
21392dda34eSdjm@openbsd.org     struct sshbuf **);
21471e67fffSdjm@openbsd.org int	 kex_ecdh_dec(struct kex *, const struct sshbuf *, struct sshbuf **);
21592dda34eSdjm@openbsd.org 
2162f6a9ddbSdjm@openbsd.org int	 kex_c25519_keypair(struct kex *);
21771e67fffSdjm@openbsd.org int	 kex_c25519_enc(struct kex *, const struct sshbuf *, struct sshbuf **,
2182f6a9ddbSdjm@openbsd.org     struct sshbuf **);
21971e67fffSdjm@openbsd.org int	 kex_c25519_dec(struct kex *, const struct sshbuf *, struct sshbuf **);
2202f6a9ddbSdjm@openbsd.org 
2212c71cec0Sdjm@openbsd.org int	 kex_kem_sntrup761x25519_keypair(struct kex *);
2222c71cec0Sdjm@openbsd.org int	 kex_kem_sntrup761x25519_enc(struct kex *, const struct sshbuf *,
223dfd59161Sdjm@openbsd.org     struct sshbuf **, struct sshbuf **);
2242c71cec0Sdjm@openbsd.org int	 kex_kem_sntrup761x25519_dec(struct kex *, const struct sshbuf *,
225dfd59161Sdjm@openbsd.org     struct sshbuf **);
2268e7fb335SDamien Miller 
227e93bd98eSdjm@openbsd.org int	 kex_dh_keygen(struct kex *);
228dec5e9d3Sdjm@openbsd.org int	 kex_dh_compute_key(struct kex *, BIGNUM *, struct sshbuf *);
2291e124260SDamien Miller 
2300a843d9aSdjm@openbsd.org int	 kexgex_hash(int, const struct sshbuf *, const struct sshbuf *,
231bb956eaaSdjm@openbsd.org     const struct sshbuf *, const struct sshbuf *, const struct sshbuf *,
23257d10cbeSmarkus@openbsd.org     int, int, int,
23357d10cbeSmarkus@openbsd.org     const BIGNUM *, const BIGNUM *, const BIGNUM *,
234dec5e9d3Sdjm@openbsd.org     const BIGNUM *, const u_char *, size_t,
23557d10cbeSmarkus@openbsd.org     u_char *, size_t *);
23657d10cbeSmarkus@openbsd.org 
23757d10cbeSmarkus@openbsd.org void	kexc25519_keygen(u_char key[CURVE25519_SIZE], u_char pub[CURVE25519_SIZE])
238686c7d9eSDamien Miller 	__attribute__((__bounded__(__minbytes__, 1, CURVE25519_SIZE)))
239686c7d9eSDamien Miller 	__attribute__((__bounded__(__minbytes__, 2, CURVE25519_SIZE)));
24057d10cbeSmarkus@openbsd.org int	kexc25519_shared_key(const u_char key[CURVE25519_SIZE],
24157d10cbeSmarkus@openbsd.org     const u_char pub[CURVE25519_SIZE], struct sshbuf *out)
242686c7d9eSDamien Miller 	__attribute__((__bounded__(__minbytes__, 1, CURVE25519_SIZE)))
243686c7d9eSDamien Miller 	__attribute__((__bounded__(__minbytes__, 2, CURVE25519_SIZE)));
244dfd59161Sdjm@openbsd.org int	kexc25519_shared_key_ext(const u_char key[CURVE25519_SIZE],
245dfd59161Sdjm@openbsd.org     const u_char pub[CURVE25519_SIZE], struct sshbuf *out, int)
246dfd59161Sdjm@openbsd.org 	__attribute__((__bounded__(__minbytes__, 1, CURVE25519_SIZE)))
247dfd59161Sdjm@openbsd.org 	__attribute__((__bounded__(__minbytes__, 2, CURVE25519_SIZE)));
2488e7fb335SDamien Miller 
249eb8b60e3SDamien Miller #if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) || defined(DEBUG_KEXECDH)
250dfd59161Sdjm@openbsd.org void	dump_digest(const char *, const u_char *, int);
25120d7c7b0SBen Lindstrom #endif
25220d7c7b0SBen Lindstrom 
253773dda25SDamien Miller #if !defined(WITH_OPENSSL) || !defined(OPENSSL_HAS_ECC)
254773dda25SDamien Miller # undef EC_KEY
255773dda25SDamien Miller # undef EC_GROUP
256773dda25SDamien Miller # undef EC_POINT
257773dda25SDamien Miller #endif
258773dda25SDamien Miller 
25933b13568SDamien Miller #endif
260