1 /*
2  * Dropbear - a SSH2 server
3  *
4  * Copyright (c) 2002,2003 Matt Johnston
5  * All rights reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE. */
24 
25 #ifndef DROPBEAR_ALGO_H_
26 
27 #define DROPBEAR_ALGO_H_
28 
29 #include "includes.h"
30 #include "buffer.h"
31 
32 #define DROPBEAR_MODE_UNUSED 0
33 #define DROPBEAR_MODE_CBC 1
34 #define DROPBEAR_MODE_CTR 2
35 
36 struct Algo_Type {
37 
38 	const char *name; /* identifying name */
39 	char val; /* a value for this cipher, or -1 for invalid */
40 	const void *data; /* algorithm specific data */
41 	char usable; /* whether we can use this algorithm */
42 	const void *mode; /* the mode, currently only used for ciphers,
43 						 points to a 'struct dropbear_cipher_mode' */
44 };
45 
46 typedef struct Algo_Type algo_type;
47 
48 /* lists mapping ssh types of algorithms to internal values */
49 extern algo_type sshkex[];
50 extern algo_type sigalgs[];
51 extern algo_type sshciphers[];
52 extern algo_type sshhashes[];
53 extern algo_type ssh_compress[];
54 extern algo_type ssh_delaycompress[];
55 extern algo_type ssh_nocompress[];
56 
57 extern const struct dropbear_cipher dropbear_nocipher;
58 extern const struct dropbear_cipher_mode dropbear_mode_none;
59 extern const struct dropbear_hash dropbear_nohash;
60 
61 struct dropbear_cipher {
62 	const struct ltc_cipher_descriptor *cipherdesc;
63 	const unsigned long keysize;
64 	const unsigned char blocksize;
65 };
66 
67 struct dropbear_cipher_mode {
68 	int (*start)(int cipher, const unsigned char *IV,
69 			const unsigned char *key,
70 			int keylen, int num_rounds, void *cipher_state);
71 	int (*encrypt)(const unsigned char *pt, unsigned char *ct,
72 			unsigned long len, void *cipher_state);
73 	int (*decrypt)(const unsigned char *ct, unsigned char *pt,
74 			unsigned long len, void *cipher_state);
75 	int (*aead_crypt)(unsigned int seq,
76 			const unsigned char *in, unsigned char *out,
77 			unsigned long len, unsigned long taglen,
78 			void *cipher_state, int direction);
79 	int (*aead_getlength)(unsigned int seq,
80 			const unsigned char *in, unsigned int *outlen,
81 			unsigned long len, void *cipher_state);
82 	const struct dropbear_hash *aead_mac;
83 };
84 
85 struct dropbear_hash {
86 	const struct ltc_hash_descriptor *hash_desc;
87 	const unsigned long keysize;
88 	/* hashsize may be truncated from the size returned by hash_desc,
89 	   eg sha1-96 */
90 	const unsigned char hashsize;
91 };
92 
93 enum dropbear_kex_mode {
94 #if DROPBEAR_NORMAL_DH
95 	DROPBEAR_KEX_NORMAL_DH,
96 #endif
97 #if DROPBEAR_ECDH
98 	DROPBEAR_KEX_ECDH,
99 #endif
100 #if DROPBEAR_CURVE25519
101 	DROPBEAR_KEX_CURVE25519,
102 #endif
103 };
104 
105 struct dropbear_kex {
106 	enum dropbear_kex_mode mode;
107 
108 	/* "normal" DH KEX */
109 	const unsigned char *dh_p_bytes;
110 	const int dh_p_len;
111 
112 	/* elliptic curve DH KEX */
113 #if DROPBEAR_ECDH
114 	const struct dropbear_ecc_curve *ecc_curve;
115 #else
116 	const void* dummy;
117 #endif
118 
119 	/* both */
120 	const struct ltc_hash_descriptor *hash_desc;
121 };
122 
123 /* Includes all algorithms is useall is set */
124 void buf_put_algolist_all(buffer * buf, const algo_type localalgos[], int useall);
125 /* Includes "usable" algorithms */
126 void buf_put_algolist(buffer * buf, const algo_type localalgos[]);
127 
128 #define KEXGUESS2_ALGO_NAME "kexguess2@matt.ucc.asn.au"
129 
130 int buf_has_algo(buffer *buf, const char *algo);
131 algo_type * first_usable_algo(algo_type algos[]);
132 algo_type * buf_match_algo(buffer* buf, algo_type localalgos[],
133 		int kexguess2, int *goodguess);
134 
135 #if DROPBEAR_USER_ALGO_LIST
136 int check_user_algos(const char* user_algo_list, algo_type * algos,
137 		const char *algo_desc);
138 char * algolist_string(const algo_type algos[]);
139 #endif
140 
141 enum {
142 	DROPBEAR_COMP_NONE,
143 	DROPBEAR_COMP_ZLIB,
144 	DROPBEAR_COMP_ZLIB_DELAY,
145 };
146 
147 #endif /* DROPBEAR_ALGO_H_ */
148