comparison common-algo.c @ 502:43bbe17d6ba0

- Add Counter Mode support
author Matt Johnston <matt@ucc.asn.au>
date Mon, 29 Sep 2008 13:53:31 +0000
parents d58c478bd399
children 0cdbc95bb3d2
comparison
equal deleted inserted replaced
501:d58c478bd399 502:43bbe17d6ba0
27 #include "dbutil.h" 27 #include "dbutil.h"
28 28
29 /* This file (algo.c) organises the ciphers which can be used, and is used to 29 /* This file (algo.c) organises the ciphers which can be used, and is used to
30 * decide which ciphers/hashes/compression/signing to use during key exchange*/ 30 * decide which ciphers/hashes/compression/signing to use during key exchange*/
31 31
32 static int void_cipher(const unsigned char* in, unsigned char* out,
33 unsigned long len, void *cipher_state) {
34 memcpy(out, in, len);
35 return CRYPT_OK;
36 }
37
38 static int void_start(int cipher, const unsigned char *IV,
39 const unsigned char *key,
40 int keylen, int num_rounds, void *cipher_state) {
41 return CRYPT_OK;
42 }
43
32 /* Mappings for ciphers, parameters are 44 /* Mappings for ciphers, parameters are
33 {&cipher_desc, keysize, blocksize} */ 45 {&cipher_desc, keysize, blocksize} */
34 /* NOTE: if keysize > 2*SHA1_HASH_SIZE, code such as hashkeys() 46 /* NOTE: if keysize > 2*SHA1_HASH_SIZE, code such as hashkeys()
35 needs revisiting */ 47 needs revisiting */
36 48
37 #ifdef DROPBEAR_AES256_CBC 49 #ifdef DROPBEAR_AES256
38 static const struct dropbear_cipher dropbear_aes256 = 50 static const struct dropbear_cipher dropbear_aes256 =
39 {&aes_desc, 32, 16}; 51 {&aes_desc, 32, 16};
40 #endif 52 #endif
41 #ifdef DROPBEAR_AES128_CBC 53 #ifdef DROPBEAR_AES128
42 static const struct dropbear_cipher dropbear_aes128 = 54 static const struct dropbear_cipher dropbear_aes128 =
43 {&aes_desc, 16, 16}; 55 {&aes_desc, 16, 16};
44 #endif 56 #endif
45 #ifdef DROPBEAR_BLOWFISH_CBC 57 #ifdef DROPBEAR_BLOWFISH
46 static const struct dropbear_cipher dropbear_blowfish = 58 static const struct dropbear_cipher dropbear_blowfish =
47 {&blowfish_desc, 16, 8}; 59 {&blowfish_desc, 16, 8};
48 #endif 60 #endif
49 #ifdef DROPBEAR_TWOFISH256_CBC 61 #ifdef DROPBEAR_TWOFISH256
50 static const struct dropbear_cipher dropbear_twofish256 = 62 static const struct dropbear_cipher dropbear_twofish256 =
51 {&twofish_desc, 32, 16}; 63 {&twofish_desc, 32, 16};
52 #endif 64 #endif
53 #ifdef DROPBEAR_TWOFISH128_CBC 65 #ifdef DROPBEAR_TWOFISH128
54 static const struct dropbear_cipher dropbear_twofish128 = 66 static const struct dropbear_cipher dropbear_twofish128 =
55 {&twofish_desc, 16, 16}; 67 {&twofish_desc, 16, 16};
56 #endif 68 #endif
57 #ifdef DROPBEAR_3DES_CBC 69 #ifdef DROPBEAR_3DES
58 static const struct dropbear_cipher dropbear_3des = 70 static const struct dropbear_cipher dropbear_3des =
59 {&des3_desc, 24, 8}; 71 {&des3_desc, 24, 8};
60 #endif 72 #endif
61 73
62 /* used to indicate no encryption, as defined in rfc2410 */ 74 /* used to indicate no encryption, as defined in rfc2410 */
63 const struct dropbear_cipher dropbear_nocipher = 75 const struct dropbear_cipher dropbear_nocipher =
64 {NULL, 16, 8}; 76 {NULL, 16, 8};
65 77
78 /* A few void* s are required to silence warnings
79 * about the symmetric_CBC vs symmetric_CTR cipher_state pointer */
80 const struct dropbear_cipher_mode dropbear_mode_cbc =
81 {(void*)cbc_start, (void*)cbc_encrypt, (void*)cbc_decrypt};
82 const struct dropbear_cipher_mode dropbear_mode_none =
83 {void_start, void_cipher, void_cipher};
84 #ifdef DROPBEAR_ENABLE_CTR_MODE
85 /* a wrapper to make ctr_start and cbc_start look the same */
86 static int dropbear_big_endian_ctr_start(int cipher,
87 const unsigned char *IV,
88 const unsigned char *key, int keylen,
89 int num_rounds, symmetric_CTR *ctr) {
90 return ctr_start(cipher, IV, key, keylen, num_rounds, CTR_COUNTER_BIG_ENDIAN, ctr);
91 }
92 const struct dropbear_cipher_mode dropbear_mode_ctr =
93 {(void*)dropbear_big_endian_ctr_start, (void*)ctr_encrypt, (void*)ctr_decrypt};
94 #endif
95
66 /* Mapping of ssh hashes to libtomcrypt hashes, including keysize etc. 96 /* Mapping of ssh hashes to libtomcrypt hashes, including keysize etc.
67 {&hash_desc, keysize, hashsize} */ 97 {&hash_desc, keysize, hashsize} */
68 98
69 #ifdef DROPBEAR_SHA1_HMAC 99 #ifdef DROPBEAR_SHA1_HMAC
70 static const struct dropbear_hash dropbear_sha1 = 100 static const struct dropbear_hash dropbear_sha1 =
81 111
82 const struct dropbear_hash dropbear_nohash = 112 const struct dropbear_hash dropbear_nohash =
83 {NULL, 16, 0}; /* used initially */ 113 {NULL, 16, 0}; /* used initially */
84 114
85 115
86 /* The following map ssh names to internal values */ 116 /* The following map ssh names to internal values.
117 * The ordering here is important for the client - the first mode
118 * that is also supported by the server will get used. */
87 119
88 algo_type sshciphers[] = { 120 algo_type sshciphers[] = {
89 #ifdef DROPBEAR_AES128_CBC 121 #ifdef DROPBEAR_ENABLE_CTR_MODE
90 {"aes128-cbc", 0, (void*)&dropbear_aes128, 1}, 122 #ifdef DROPBEAR_AES128
91 #endif 123 {"aes128-ctr", 0, &dropbear_aes128, 1, &dropbear_mode_ctr},
92 #ifdef DROPBEAR_3DES_CBC 124 #endif
93 {"3des-cbc", 0, (void*)&dropbear_3des, 1}, 125 #ifdef DROPBEAR_3DES
94 #endif 126 {"3des-ctr", 0, &dropbear_3des, 1, &dropbear_mode_ctr},
95 #ifdef DROPBEAR_AES256_CBC 127 #endif
96 {"aes256-cbc", 0, (void*)&dropbear_aes256, 1}, 128 #ifdef DROPBEAR_AES256
97 #endif 129 {"aes256-ctr", 0, &dropbear_aes256, 1, &dropbear_mode_ctr},
98 #ifdef DROPBEAR_TWOFISH256_CBC 130 #endif
99 {"twofish256-cbc", 0, (void*)&dropbear_twofish256, 1}, 131 #ifdef DROPBEAR_TWOFISH256
100 {"twofish-cbc", 0, (void*)&dropbear_twofish256, 1}, 132 {"twofish256-ctr", 0, &dropbear_twofish256, 1, &dropbear_mode_ctr},
101 #endif 133 #endif
102 #ifdef DROPBEAR_TWOFISH128_CBC 134 #ifdef DROPBEAR_TWOFISH128
103 {"twofish128-cbc", 0, (void*)&dropbear_twofish128, 1}, 135 {"twofish128-ctr", 0, &dropbear_twofish128, 1, &dropbear_mode_ctr},
104 #endif 136 #endif
105 #ifdef DROPBEAR_BLOWFISH_CBC 137 #ifdef DROPBEAR_BLOWFISH
106 {"blowfish-cbc", 0, (void*)&dropbear_blowfish, 1}, 138 {"blowfish-ctr", 0, &dropbear_blowfish, 1, &dropbear_mode_ctr},
107 #endif 139 #endif
108 {NULL, 0, NULL, 0} 140 #endif /* DROPBEAR_ENABLE_CTR_MODE */
141
142 /* CBC modes are always enabled */
143 #ifdef DROPBEAR_AES128
144 {"aes128-cbc", 0, &dropbear_aes128, 1, &dropbear_mode_cbc},
145 #endif
146 #ifdef DROPBEAR_3DES
147 {"3des-cbc", 0, &dropbear_3des, 1, &dropbear_mode_cbc},
148 #endif
149 #ifdef DROPBEAR_AES256
150 {"aes256-cbc", 0, &dropbear_aes256, 1, &dropbear_mode_cbc},
151 #endif
152 #ifdef DROPBEAR_TWOFISH256
153 {"twofish256-cbc", 0, &dropbear_twofish256, 1, &dropbear_mode_cbc},
154 {"twofish-cbc", 0, &dropbear_twofish256, 1, &dropbear_mode_cbc},
155 #endif
156 #ifdef DROPBEAR_TWOFISH128
157 {"twofish128-cbc", 0, &dropbear_twofish128, 1, &dropbear_mode_cbc},
158 #endif
159 #ifdef DROPBEAR_BLOWFISH
160 {"blowfish-cbc", 0, &dropbear_blowfish, 1, &dropbear_mode_cbc},
161 #endif
162 {NULL, 0, NULL, 0, NULL}
109 }; 163 };
110 164
111 algo_type sshhashes[] = { 165 algo_type sshhashes[] = {
112 #ifdef DROPBEAR_SHA1_96_HMAC 166 #ifdef DROPBEAR_SHA1_96_HMAC
113 {"hmac-sha1-96", 0, (void*)&dropbear_sha1_96, 1}, 167 {"hmac-sha1-96", 0, &dropbear_sha1_96, 1, NULL},
114 #endif 168 #endif
115 #ifdef DROPBEAR_SHA1_HMAC 169 #ifdef DROPBEAR_SHA1_HMAC
116 {"hmac-sha1", 0, (void*)&dropbear_sha1, 1}, 170 {"hmac-sha1", 0, &dropbear_sha1, 1, NULL},
117 #endif 171 #endif
118 #ifdef DROPBEAR_MD5_HMAC 172 #ifdef DROPBEAR_MD5_HMAC
119 {"hmac-md5", 0, (void*)&dropbear_md5, 1}, 173 {"hmac-md5", 0, &dropbear_md5, 1, NULL},
120 #endif 174 #endif
121 {NULL, 0, NULL, 0} 175 {NULL, 0, NULL, 0, NULL}
122 }; 176 };
123 177
124 algo_type sshcompress[] = { 178 algo_type sshcompress[] = {
125 #ifndef DISABLE_ZLIB 179 #ifndef DISABLE_ZLIB
126 {"zlib", DROPBEAR_COMP_ZLIB, NULL, 1}, 180 {"zlib", DROPBEAR_COMP_ZLIB, NULL, 1, NULL},
127 {"[email protected]", DROPBEAR_COMP_ZLIB_DELAY, NULL, 1}, 181 {"[email protected]", DROPBEAR_COMP_ZLIB_DELAY, NULL, 1, NULL},
128 #endif 182 #endif
129 {"none", DROPBEAR_COMP_NONE, NULL, 1}, 183 {"none", DROPBEAR_COMP_NONE, NULL, 1, NULL},
130 {NULL, 0, NULL, 0} 184 {NULL, 0, NULL, 0, NULL}
131 }; 185 };
132 186
133 algo_type sshhostkey[] = { 187 algo_type sshhostkey[] = {
134 #ifdef DROPBEAR_RSA 188 #ifdef DROPBEAR_RSA
135 {"ssh-rsa", DROPBEAR_SIGNKEY_RSA, NULL, 1}, 189 {"ssh-rsa", DROPBEAR_SIGNKEY_RSA, NULL, 1, NULL},
136 #endif 190 #endif
137 #ifdef DROPBEAR_DSS 191 #ifdef DROPBEAR_DSS
138 {"ssh-dss", DROPBEAR_SIGNKEY_DSS, NULL, 1}, 192 {"ssh-dss", DROPBEAR_SIGNKEY_DSS, NULL, 1, NULL},
139 #endif 193 #endif
140 {NULL, 0, NULL, 0} 194 {NULL, 0, NULL, 0, NULL}
141 }; 195 };
142 196
143 algo_type sshkex[] = { 197 algo_type sshkex[] = {
144 {"diffie-hellman-group1-sha1", DROPBEAR_KEX_DH_GROUP1, NULL, 1}, 198 {"diffie-hellman-group1-sha1", DROPBEAR_KEX_DH_GROUP1, NULL, 1, NULL},
145 {NULL, 0, NULL, 0} 199 {NULL, 0, NULL, 0, NULL}
146 }; 200 };
147 201
148 202
149 /* Register the compiled in ciphers. 203 /* Register the compiled in ciphers.
150 * This should be run before using any of the ciphers/hashes */ 204 * This should be run before using any of the ciphers/hashes */
151 void crypto_init() { 205 void crypto_init() {
152 206
153 const struct ltc_cipher_descriptor *regciphers[] = { 207 const struct ltc_cipher_descriptor *regciphers[] = {
154 #ifdef DROPBEAR_AES_CBC 208 #ifdef DROPBEAR_AES
155 &aes_desc, 209 &aes_desc,
156 #endif 210 #endif
157 #ifdef DROPBEAR_BLOWFISH_CBC 211 #ifdef DROPBEAR_BLOWFISH
158 &blowfish_desc, 212 &blowfish_desc,
159 #endif 213 #endif
160 #ifdef DROPBEAR_TWOFISH_CBC 214 #ifdef DROPBEAR_TWOFISH
161 &twofish_desc, 215 &twofish_desc,
162 #endif 216 #endif
163 #ifdef DROPBEAR_3DES_CBC 217 #ifdef DROPBEAR_3DES
164 &des3_desc, 218 &des3_desc,
165 #endif 219 #endif
166 NULL 220 NULL
167 }; 221 };
168 222
214 268
215 unsigned int i, len; 269 unsigned int i, len;
216 unsigned int donefirst = 0; 270 unsigned int donefirst = 0;
217 buffer *algolist = NULL; 271 buffer *algolist = NULL;
218 272
219 algolist = buf_new(100); 273 algolist = buf_new(160);
220 for (i = 0; localalgos[i].name != NULL; i++) { 274 for (i = 0; localalgos[i].name != NULL; i++) {
221 if (localalgos[i].usable) { 275 if (localalgos[i].usable) {
222 if (donefirst) 276 if (donefirst)
223 buf_putbyte(algolist, ','); 277 buf_putbyte(algolist, ',');
224 donefirst = 1; 278 donefirst = 1;