Revert "Revert "and added files""
[bcm963xx.git] / userapps / opensource / sshd / algo.c
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 #include "algo.h"
26
27 /* This file (algo.c) organises the ciphers which can be used, and is used to
28  * decide which ciphers/hashes/compression/signing to use during key exchange*/
29
30 /* Mappings for ciphers, parameters are
31    {&cipher_desc, keysize, blocksize} */
32
33 #ifdef DROPBEAR_AES128_CBC
34 const struct dropbear_cipher dropbear_aes128 = 
35         {&rijndael_desc, 16, 16};
36 #endif
37 #ifdef DROPBEAR_BLOWFISH_CBC
38 const struct dropbear_cipher dropbear_blowfish = 
39         {&blowfish_desc, 16, 8};
40 #endif
41 #ifdef DROPBEAR_TWOFISH128_CBC
42 const struct dropbear_cipher dropbear_twofish128 = 
43         {&twofish_desc, 16, 16};
44 #endif
45 #ifdef DROPBEAR_3DES_CBC
46 const struct dropbear_cipher dropbear_3des = 
47         {&des3_desc, 24, 8};
48 #endif
49
50 /* used to indicate no encryption, as defined in rfc2410 */
51 const struct dropbear_cipher dropbear_nocipher =
52         {NULL, 16, 8}; 
53
54 /* Mapping of ssh hashes to libtomcrypt hashes, including keysize etc.
55    {&hash_desc, keysize, hashsize} */
56
57 #ifdef DROPBEAR_SHA1_HMAC
58 const struct dropbear_hash dropbear_sha1 = 
59         {&sha1_desc, 20, 20};
60 #endif
61 #ifdef DROPBEAR_MD5_HMAC
62 const struct dropbear_hash dropbear_md5 = 
63         {&md5_desc, 16, 16};
64 #endif
65
66 const struct dropbear_hash dropbear_nohash =
67         {NULL, 16, 0}; /* used initially */
68         
69
70 /* The following map ssh names to internal values */
71
72 algo_type sshciphers[] = {
73 #ifdef DROPBEAR_AES128_CBC
74         {"aes128-cbc", 0, (void*)&dropbear_aes128, 1},
75 #endif
76 #ifdef DROPBEAR_BLOWFISH_CBC
77         {"blowfish-cbc", 0, (void*)&dropbear_blowfish, 1},
78 #endif
79 #ifdef DROPBEAR_TWOFISH128_CBC
80         {"twofish-cbc", 0, (void*)&dropbear_twofish128, 1},
81 #endif
82 #ifdef DROPBEAR_3DES_CBC
83         {"3des-cbc", 0, (void*)&dropbear_3des, 1},
84 #endif
85         {NULL, 0, NULL, 0}
86 };
87
88 algo_type sshhashes[] = {
89 #ifdef DROPBEAR_SHA1_HMAC
90         {"hmac-sha1", 0, (void*)&dropbear_sha1, 1},
91 #endif
92 #ifdef DROPBEAR_MD5_HMAC
93         {"hmac-md5", 0, (void*)&dropbear_md5, 1},
94 #endif
95         {NULL, 0, NULL, 0}
96 };
97
98 algo_type sshcompress[] = {
99         {"none", DROPBEAR_COMP_NONE, NULL, 1},
100 #ifndef DISABLE_ZLIB
101         {"zlib", DROPBEAR_COMP_ZLIB, NULL, 1},
102 #endif
103         {NULL, 0, NULL, 0}
104 };
105
106 algo_type sshhostkey[] = {
107 #ifdef DROPBEAR_RSA
108         {"ssh-rsa", DROPBEAR_SIGNKEY_RSA, NULL, 1},
109 #endif
110 #ifdef DROPBEAR_DSS
111         {"ssh-dss", DROPBEAR_SIGNKEY_DSS, NULL, 1},
112 #endif
113         {NULL, 0, NULL, 0}
114 };
115
116 algo_type sshkex[] = {
117         {"diffie-hellman-group1-sha1", DROPBEAR_KEX_DH_GROUP1, NULL, 1},
118         {NULL, 0, NULL, 0}
119 };
120
121
122 /* Register the compiled in ciphers.
123  * This should be run before using any of the ciphers/hashes */
124 void crypto_init() {
125
126         const struct _cipher_descriptor *regciphers[] = {
127 #ifdef DROPBEAR_AES128_CBC
128                 &rijndael_desc,
129 #endif
130 #ifdef DROPBEAR_BLOWFISH_CBC
131                 &blowfish_desc,
132 #endif
133 #ifdef DROPBEAR_TWOFISH128_CBC
134                 &twofish_desc,
135 #endif
136 #ifdef DROPBEAR_3DES_CBC
137                 &des3_desc,
138 #endif
139                 NULL
140         };
141
142         const struct _hash_descriptor *reghashes[] = {
143                 /* we need sha1 for hostkey stuff regardless */
144                 &sha1_desc,
145 #ifdef DROPBEAR_MD5_HMAC
146                 &md5_desc,
147 #endif
148                 NULL
149         };      
150         int i;
151         
152         for (i = 0; regciphers[i] != NULL; i++) {
153                 if (register_cipher(regciphers[i]) == -1) {
154                         dropbear_exit("error registering crypto");
155                 }
156         }
157
158         for (i = 0; reghashes[i] != NULL; i++) {
159                 if (register_hash(reghashes[i]) == -1) {
160                         dropbear_exit("error registering crypto");
161                 }
162         }
163 }
164
165 /* returns DROPBEAR_SUCCESS if we have a match for algo, DROPBEAR_FAILURE
166  * otherwise */
167 int have_algo(char* algo, size_t algolen, algo_type algos[]) {
168
169         int i = 0;
170         while (algos[i].name != NULL) {
171                 if (strlen(algos[i].name) == algolen
172                                 && (strncmp(algos[i].name, algo, algolen) == 0)) {
173                         return DROPBEAR_SUCCESS;
174                 }
175                 i++;
176         }
177
178         return DROPBEAR_FAILURE;
179 }
180
181
182 /* match the first algorithm in the comma-seperated list in buf which is
183  * also in localalgos[], or return NULL on failure. */
184 algo_type * buf_match_algo(buffer* buf, algo_type localalgos[]) {
185
186         unsigned char * algolist = NULL;
187         unsigned char * remotealgos[MAX_PROPOSED_ALGO];
188         unsigned int len;
189         unsigned int count, i, j;
190         algo_type * ret = NULL;
191
192         /* get the comma-seperated list from the buffer ie "algo1,algo2,algo3" */
193         algolist = buf_getstring(buf, &len);
194         if (len > MAX_PROPOSED_ALGO*(MAX_NAME_LEN+1)) {
195                 goto out; /* just a sanity check, no other use */
196         }
197
198         /* remotealgos will contain a list of the strings parsed out */
199         /* We will have at least one string (even if it's just "") */
200         remotealgos[0] = algolist;
201         count = 1;
202         /* Iterate through, replacing ','s with NULs, to split it into
203          * words. */
204         for (i = 0; i < len; i++) {
205                 if (algolist[i] == '\0') {
206                         /* someone is trying something strange */
207                         goto out;
208                 }
209                 if (algolist[i] == ',' && i != len) {
210                         algolist[i] = '\0';
211                         remotealgos[count] = &algolist[i+1];
212                         count++;
213                 }
214                 if (count == MAX_PROPOSED_ALGO) {
215                         break;
216                 }
217         }
218
219         /* iterate and find the first match */
220         for (i = 0; i < count; i++) {
221                 len = strlen(remotealgos[i]);
222                 for (j = 0; localalgos[j].name != NULL; j++) {
223                         if (!localalgos[j].usable) {
224                                 continue;
225                         }
226                         if (len == strlen(localalgos[j].name) 
227                                         && strcmp(localalgos[j].name, remotealgos[i]) == 0) {
228                                 ret = &localalgos[j];
229                                 goto out;
230                         }
231                 }
232         }
233
234 out:
235         m_free(algolist);
236         return ret;
237 }
238
239 /* Output a comma seperated list of algorithms to a buffer */
240 void buf_put_algolist(buffer * buf, algo_type localalgos[]) {
241
242         unsigned int pos = 0, i, len;
243         char str[50]; /* enough for local algo storage */
244
245         for (i = 0; localalgos[i].name != NULL; i++) {
246                 if (localalgos[i].usable) {
247                         len = strlen(localalgos[i].name);
248                         memcpy(&str[pos], localalgos[i].name, len);
249                         pos += len;
250                         str[pos] = ',';
251                         pos++;
252                 }
253         }
254         buf_putstring(buf, str, pos-1);
255 }