Mercurial > dropbear
comparison common-algo.c @ 4:fe6bca95afa7
Makefile.in contains updated files required
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Tue, 01 Jun 2004 02:46:09 +0000 |
parents | |
children | e3adf4cf5465 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 4:fe6bca95afa7 |
---|---|
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 #include "dbutil.h" | |
27 | |
28 /* This file (algo.c) organises the ciphers which can be used, and is used to | |
29 * decide which ciphers/hashes/compression/signing to use during key exchange*/ | |
30 | |
31 /* Mappings for ciphers, parameters are | |
32 {&cipher_desc, keysize, blocksize} */ | |
33 | |
34 #ifdef DROPBEAR_AES128_CBC | |
35 const struct dropbear_cipher dropbear_aes128 = | |
36 {&rijndael_desc, 16, 16}; | |
37 #endif | |
38 #ifdef DROPBEAR_BLOWFISH_CBC | |
39 const struct dropbear_cipher dropbear_blowfish = | |
40 {&blowfish_desc, 16, 8}; | |
41 #endif | |
42 #ifdef DROPBEAR_TWOFISH128_CBC | |
43 const struct dropbear_cipher dropbear_twofish128 = | |
44 {&twofish_desc, 16, 16}; | |
45 #endif | |
46 #ifdef DROPBEAR_3DES_CBC | |
47 const struct dropbear_cipher dropbear_3des = | |
48 {&des3_desc, 24, 8}; | |
49 #endif | |
50 | |
51 /* used to indicate no encryption, as defined in rfc2410 */ | |
52 const struct dropbear_cipher dropbear_nocipher = | |
53 {NULL, 16, 8}; | |
54 | |
55 /* Mapping of ssh hashes to libtomcrypt hashes, including keysize etc. | |
56 {&hash_desc, keysize, hashsize} */ | |
57 | |
58 #ifdef DROPBEAR_SHA1_HMAC | |
59 const struct dropbear_hash dropbear_sha1 = | |
60 {&sha1_desc, 20, 20}; | |
61 #endif | |
62 #ifdef DROPBEAR_MD5_HMAC | |
63 const struct dropbear_hash dropbear_md5 = | |
64 {&md5_desc, 16, 16}; | |
65 #endif | |
66 | |
67 const struct dropbear_hash dropbear_nohash = | |
68 {NULL, 16, 0}; /* used initially */ | |
69 | |
70 | |
71 /* The following map ssh names to internal values */ | |
72 | |
73 algo_type sshciphers[] = { | |
74 #ifdef DROPBEAR_AES128_CBC | |
75 {"aes128-cbc", 0, (void*)&dropbear_aes128, 1}, | |
76 #endif | |
77 #ifdef DROPBEAR_BLOWFISH_CBC | |
78 {"blowfish-cbc", 0, (void*)&dropbear_blowfish, 1}, | |
79 #endif | |
80 #ifdef DROPBEAR_TWOFISH128_CBC | |
81 {"twofish-cbc", 0, (void*)&dropbear_twofish128, 1}, | |
82 #endif | |
83 #ifdef DROPBEAR_3DES_CBC | |
84 {"3des-cbc", 0, (void*)&dropbear_3des, 1}, | |
85 #endif | |
86 {NULL, 0, NULL, 0} | |
87 }; | |
88 | |
89 algo_type sshhashes[] = { | |
90 #ifdef DROPBEAR_SHA1_HMAC | |
91 {"hmac-sha1", 0, (void*)&dropbear_sha1, 1}, | |
92 #endif | |
93 #ifdef DROPBEAR_MD5_HMAC | |
94 {"hmac-md5", 0, (void*)&dropbear_md5, 1}, | |
95 #endif | |
96 {NULL, 0, NULL, 0} | |
97 }; | |
98 | |
99 algo_type sshcompress[] = { | |
100 {"none", DROPBEAR_COMP_NONE, NULL, 1}, | |
101 #ifndef DISABLE_ZLIB | |
102 {"zlib", DROPBEAR_COMP_ZLIB, NULL, 1}, | |
103 #endif | |
104 {NULL, 0, NULL, 0} | |
105 }; | |
106 | |
107 algo_type sshhostkey[] = { | |
108 #ifdef DROPBEAR_RSA | |
109 {"ssh-rsa", DROPBEAR_SIGNKEY_RSA, NULL, 1}, | |
110 #endif | |
111 #ifdef DROPBEAR_DSS | |
112 {"ssh-dss", DROPBEAR_SIGNKEY_DSS, NULL, 1}, | |
113 #endif | |
114 {NULL, 0, NULL, 0} | |
115 }; | |
116 | |
117 algo_type sshkex[] = { | |
118 {"diffie-hellman-group1-sha1", DROPBEAR_KEX_DH_GROUP1, NULL, 1}, | |
119 {NULL, 0, NULL, 0} | |
120 }; | |
121 | |
122 | |
123 /* Register the compiled in ciphers. | |
124 * This should be run before using any of the ciphers/hashes */ | |
125 void crypto_init() { | |
126 | |
127 const struct _cipher_descriptor *regciphers[] = { | |
128 #ifdef DROPBEAR_AES128_CBC | |
129 &rijndael_desc, | |
130 #endif | |
131 #ifdef DROPBEAR_BLOWFISH_CBC | |
132 &blowfish_desc, | |
133 #endif | |
134 #ifdef DROPBEAR_TWOFISH128_CBC | |
135 &twofish_desc, | |
136 #endif | |
137 #ifdef DROPBEAR_3DES_CBC | |
138 &des3_desc, | |
139 #endif | |
140 NULL | |
141 }; | |
142 | |
143 const struct _hash_descriptor *reghashes[] = { | |
144 /* we need sha1 for hostkey stuff regardless */ | |
145 &sha1_desc, | |
146 #ifdef DROPBEAR_MD5_HMAC | |
147 &md5_desc, | |
148 #endif | |
149 NULL | |
150 }; | |
151 int i; | |
152 | |
153 for (i = 0; regciphers[i] != NULL; i++) { | |
154 if (register_cipher(regciphers[i]) == -1) { | |
155 dropbear_exit("error registering crypto"); | |
156 } | |
157 } | |
158 | |
159 for (i = 0; reghashes[i] != NULL; i++) { | |
160 if (register_hash(reghashes[i]) == -1) { | |
161 dropbear_exit("error registering crypto"); | |
162 } | |
163 } | |
164 } | |
165 | |
166 /* algolen specifies the length of algo, algos is our local list to match | |
167 * against. | |
168 * Returns DROPBEAR_SUCCESS if we have a match for algo, DROPBEAR_FAILURE | |
169 * otherwise */ | |
170 int have_algo(char* algo, size_t algolen, algo_type algos[]) { | |
171 | |
172 int i; | |
173 | |
174 for (i = 0; algos[i].name != NULL; i++) { | |
175 if (strlen(algos[i].name) == algolen | |
176 && (strncmp(algos[i].name, algo, algolen) == 0)) { | |
177 return DROPBEAR_SUCCESS; | |
178 } | |
179 } | |
180 | |
181 return DROPBEAR_FAILURE; | |
182 } | |
183 | |
184 | |
185 | |
186 /* Output a comma separated list of algorithms to a buffer */ | |
187 void buf_put_algolist(buffer * buf, algo_type localalgos[]) { | |
188 | |
189 unsigned int pos = 0, i, len; | |
190 char str[50]; /* enough for local algo storage */ | |
191 | |
192 for (i = 0; localalgos[i].name != NULL; i++) { | |
193 if (localalgos[i].usable) { | |
194 /* Avoid generating a trailing comma */ | |
195 if (pos) | |
196 str[pos++] = ','; | |
197 len = strlen(localalgos[i].name); | |
198 memcpy(&str[pos], localalgos[i].name, len); | |
199 pos += len; | |
200 } | |
201 } | |
202 str[pos]=0; | |
203 /* Debug this */ | |
204 TRACE(("buf_put_algolist: %s", str)); | |
205 buf_putstring(buf, str, pos); | |
206 } |