]>
Commit | Line | Data |
---|---|---|
8efc0c15 | 1 | /* |
5260325f | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
5260325f | 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
4 | * All rights reserved | |
6ae2364d | 5 | * |
bcbf86ec | 6 | * As far as I am concerned, the code I have written for this software |
7 | * can be used freely for any purpose. Any derived versions of this | |
8 | * software must be clearly marked as such, and if the derived work is | |
9 | * incompatible with the protocol description in the RFC file, it must be | |
10 | * called by a name other than "ssh" or "Secure Shell". | |
11 | * | |
12 | * | |
13 | * Copyright (c) 1999 Niels Provos. All rights reserved. | |
14 | * Copyright (c) 1999,2000 Markus Friedl. All rights reserved. | |
15 | * | |
16 | * Redistribution and use in source and binary forms, with or without | |
17 | * modification, are permitted provided that the following conditions | |
18 | * are met: | |
19 | * 1. Redistributions of source code must retain the above copyright | |
20 | * notice, this list of conditions and the following disclaimer. | |
21 | * 2. Redistributions in binary form must reproduce the above copyright | |
22 | * notice, this list of conditions and the following disclaimer in the | |
23 | * documentation and/or other materials provided with the distribution. | |
6ae2364d | 24 | * |
bcbf86ec | 25 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
26 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
27 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
28 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
29 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
30 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
31 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
32 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
33 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
34 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
5260325f | 35 | */ |
8efc0c15 | 36 | |
37 | #include "includes.h" | |
b35eb612 | 38 | RCSID("$OpenBSD: cipher.c,v 1.43 2001/02/04 15:32:23 stevesk Exp $"); |
8efc0c15 | 39 | |
a8be9f80 | 40 | #include "xmalloc.h" |
42f11eb2 | 41 | #include "log.h" |
42 | #include "cipher.h" | |
8efc0c15 | 43 | |
44 | #include <openssl/md5.h> | |
45 | ||
94ec8c6b | 46 | |
47 | /* no encryption */ | |
48 | void | |
49 | none_setkey(CipherContext *cc, const u_char *key, u_int keylen) | |
50 | { | |
51 | } | |
52 | void | |
53 | none_setiv(CipherContext *cc, const u_char *iv, u_int ivlen) | |
54 | { | |
55 | } | |
56 | void | |
57 | none_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) | |
58 | { | |
59 | memcpy(dest, src, len); | |
60 | } | |
61 | ||
62 | /* DES */ | |
63 | void | |
64 | des_ssh1_setkey(CipherContext *cc, const u_char *key, u_int keylen) | |
65 | { | |
66 | static int dowarn = 1; | |
67 | if (dowarn) { | |
68 | error("Warning: use of DES is strongly discouraged " | |
69 | "due to cryptographic weaknesses"); | |
70 | dowarn = 0; | |
71 | } | |
72 | des_set_key((void *)key, cc->u.des.key); | |
73 | } | |
74 | void | |
75 | des_ssh1_setiv(CipherContext *cc, const u_char *iv, u_int ivlen) | |
76 | { | |
77 | memset(cc->u.des.iv, 0, sizeof(cc->u.des.iv)); | |
78 | } | |
79 | void | |
80 | des_ssh1_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) | |
81 | { | |
82 | des_ncbc_encrypt(src, dest, len, cc->u.des.key, &cc->u.des.iv, | |
83 | DES_ENCRYPT); | |
84 | } | |
85 | void | |
86 | des_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) | |
87 | { | |
88 | des_ncbc_encrypt(src, dest, len, cc->u.des.key, &cc->u.des.iv, | |
89 | DES_DECRYPT); | |
90 | } | |
91 | ||
92 | /* 3DES */ | |
93 | void | |
94 | des3_setkey(CipherContext *cc, const u_char *key, u_int keylen) | |
95 | { | |
96 | des_set_key((void *) key, cc->u.des3.key1); | |
97 | des_set_key((void *) (key+8), cc->u.des3.key2); | |
98 | des_set_key((void *) (key+16), cc->u.des3.key3); | |
99 | } | |
100 | void | |
101 | des3_setiv(CipherContext *cc, const u_char *iv, u_int ivlen) | |
102 | { | |
103 | memset(cc->u.des3.iv2, 0, sizeof(cc->u.des3.iv2)); | |
104 | memset(cc->u.des3.iv3, 0, sizeof(cc->u.des3.iv3)); | |
105 | if (iv == NULL) | |
106 | return; | |
107 | memcpy(cc->u.des3.iv3, (char *)iv, 8); | |
108 | } | |
109 | void | |
110 | des3_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) | |
111 | { | |
112 | des_ede3_cbc_encrypt(src, dest, len, | |
113 | cc->u.des3.key1, cc->u.des3.key2, cc->u.des3.key3, | |
114 | &cc->u.des3.iv3, DES_ENCRYPT); | |
115 | } | |
116 | void | |
117 | des3_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) | |
118 | { | |
119 | des_ede3_cbc_encrypt(src, dest, len, | |
120 | cc->u.des3.key1, cc->u.des3.key2, cc->u.des3.key3, | |
121 | &cc->u.des3.iv3, DES_DECRYPT); | |
122 | } | |
123 | ||
8efc0c15 | 124 | /* |
a8be9f80 | 125 | * This is used by SSH1: |
126 | * | |
127 | * What kind of triple DES are these 2 routines? | |
8efc0c15 | 128 | * |
129 | * Why is there a redundant initialization vector? | |
130 | * | |
131 | * If only iv3 was used, then, this would till effect have been | |
132 | * outer-cbc. However, there is also a private iv1 == iv2 which | |
133 | * perhaps makes differential analysis easier. On the other hand, the | |
134 | * private iv1 probably makes the CRC-32 attack ineffective. This is a | |
135 | * result of that there is no longer any known iv1 to use when | |
136 | * choosing the X block. | |
137 | */ | |
138 | void | |
94ec8c6b | 139 | des3_ssh1_setkey(CipherContext *cc, const u_char *key, u_int keylen) |
140 | { | |
141 | des_set_key((void *) key, cc->u.des3.key1); | |
142 | des_set_key((void *) (key+8), cc->u.des3.key2); | |
143 | if (keylen <= 16) | |
144 | des_set_key((void *) key, cc->u.des3.key3); | |
145 | else | |
146 | des_set_key((void *) (key+16), cc->u.des3.key3); | |
147 | } | |
148 | void | |
149 | des3_ssh1_encrypt(CipherContext *cc, u_char *dest, const u_char *src, | |
150 | u_int len) | |
8efc0c15 | 151 | { |
5260325f | 152 | des_cblock iv1; |
94ec8c6b | 153 | des_cblock *iv2 = &cc->u.des3.iv2; |
154 | des_cblock *iv3 = &cc->u.des3.iv3; | |
8efc0c15 | 155 | |
5260325f | 156 | memcpy(&iv1, iv2, 8); |
8efc0c15 | 157 | |
0b6fbf03 | 158 | des_ncbc_encrypt(src, dest, len, cc->u.des3.key1, &iv1, DES_ENCRYPT); |
159 | des_ncbc_encrypt(dest, dest, len, cc->u.des3.key2, iv2, DES_DECRYPT); | |
160 | des_ncbc_encrypt(dest, dest, len, cc->u.des3.key3, iv3, DES_ENCRYPT); | |
8efc0c15 | 161 | } |
8efc0c15 | 162 | void |
94ec8c6b | 163 | des3_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src, |
164 | u_int len) | |
8efc0c15 | 165 | { |
5260325f | 166 | des_cblock iv1; |
94ec8c6b | 167 | des_cblock *iv2 = &cc->u.des3.iv2; |
168 | des_cblock *iv3 = &cc->u.des3.iv3; | |
8efc0c15 | 169 | |
5260325f | 170 | memcpy(&iv1, iv2, 8); |
8efc0c15 | 171 | |
0b6fbf03 | 172 | des_ncbc_encrypt(src, dest, len, cc->u.des3.key3, iv3, DES_DECRYPT); |
173 | des_ncbc_encrypt(dest, dest, len, cc->u.des3.key2, iv2, DES_ENCRYPT); | |
174 | des_ncbc_encrypt(dest, dest, len, cc->u.des3.key1, &iv1, DES_DECRYPT); | |
8efc0c15 | 175 | } |
176 | ||
94ec8c6b | 177 | /* Blowfish */ |
178 | void | |
179 | blowfish_setkey(CipherContext *cc, const u_char *key, u_int keylen) | |
180 | { | |
1e3b8b07 | 181 | BF_set_key(&cc->u.bf.key, keylen, (u_char *)key); |
94ec8c6b | 182 | } |
183 | void | |
184 | blowfish_setiv(CipherContext *cc, const u_char *iv, u_int ivlen) | |
185 | { | |
186 | if (iv == NULL) | |
187 | memset(cc->u.bf.iv, 0, 8); | |
188 | else | |
189 | memcpy(cc->u.bf.iv, (char *)iv, 8); | |
190 | } | |
191 | void | |
192 | blowfish_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src, | |
193 | u_int len) | |
194 | { | |
195 | BF_cbc_encrypt((void *)src, dest, len, &cc->u.bf.key, cc->u.bf.iv, | |
196 | BF_ENCRYPT); | |
197 | } | |
198 | void | |
199 | blowfish_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, | |
200 | u_int len) | |
201 | { | |
202 | BF_cbc_encrypt((void *)src, dest, len, &cc->u.bf.key, cc->u.bf.iv, | |
203 | BF_DECRYPT); | |
204 | } | |
205 | ||
8efc0c15 | 206 | /* |
a8be9f80 | 207 | * SSH1 uses a variation on Blowfish, all bytes must be swapped before |
8efc0c15 | 208 | * and after encryption/decryption. Thus the swap_bytes stuff (yuk). |
209 | */ | |
5260325f | 210 | static void |
1e3b8b07 | 211 | swap_bytes(const u_char *src, u_char *dst, int n) |
8efc0c15 | 212 | { |
a22aff1f | 213 | char c[4]; |
5260325f | 214 | |
a22aff1f | 215 | /* Process 4 bytes every lap. */ |
216 | for (n = n / 4; n > 0; n--) { | |
217 | c[3] = *src++; | |
218 | c[2] = *src++; | |
219 | c[1] = *src++; | |
220 | c[0] = *src++; | |
5260325f | 221 | |
a22aff1f | 222 | *dst++ = c[0]; |
223 | *dst++ = c[1]; | |
224 | *dst++ = c[2]; | |
225 | *dst++ = c[3]; | |
5260325f | 226 | } |
8efc0c15 | 227 | } |
228 | ||
94ec8c6b | 229 | void |
230 | blowfish_ssh1_encrypt(CipherContext *cc, u_char *dest, const u_char *src, | |
231 | u_int len) | |
232 | { | |
233 | swap_bytes(src, dest, len); | |
234 | BF_cbc_encrypt((void *)dest, dest, len, &cc->u.bf.key, cc->u.bf.iv, | |
235 | BF_ENCRYPT); | |
236 | swap_bytes(dest, dest, len); | |
237 | } | |
238 | void | |
239 | blowfish_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src, | |
240 | u_int len) | |
241 | { | |
242 | swap_bytes(src, dest, len); | |
243 | BF_cbc_encrypt((void *)dest, dest, len, &cc->u.bf.key, cc->u.bf.iv, | |
244 | BF_DECRYPT); | |
245 | swap_bytes(dest, dest, len); | |
246 | } | |
8efc0c15 | 247 | |
94ec8c6b | 248 | /* alleged rc4 */ |
249 | void | |
250 | arcfour_setkey(CipherContext *cc, const u_char *key, u_int keylen) | |
251 | { | |
252 | RC4_set_key(&cc->u.rc4, keylen, (u_char *)key); | |
253 | } | |
254 | void | |
255 | arcfour_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) | |
256 | { | |
257 | RC4(&cc->u.rc4, len, (u_char *)src, dest); | |
258 | } | |
8efc0c15 | 259 | |
94ec8c6b | 260 | /* CAST */ |
261 | void | |
262 | cast_setkey(CipherContext *cc, const u_char *key, u_int keylen) | |
8efc0c15 | 263 | { |
1e3b8b07 | 264 | CAST_set_key(&cc->u.cast.key, keylen, (u_char *) key); |
94ec8c6b | 265 | } |
266 | void | |
267 | cast_setiv(CipherContext *cc, const u_char *iv, u_int ivlen) | |
268 | { | |
2b87da3b | 269 | if (iv == NULL) |
94ec8c6b | 270 | fatal("no IV for %s.", cc->cipher->name); |
271 | memcpy(cc->u.cast.iv, (char *)iv, 8); | |
272 | } | |
273 | void | |
274 | cast_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) | |
275 | { | |
276 | CAST_cbc_encrypt(src, dest, len, &cc->u.cast.key, cc->u.cast.iv, | |
277 | CAST_ENCRYPT); | |
278 | } | |
279 | void | |
280 | cast_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) | |
281 | { | |
282 | CAST_cbc_encrypt(src, dest, len, &cc->u.cast.key, cc->u.cast.iv, | |
283 | CAST_DECRYPT); | |
284 | } | |
285 | ||
286 | /* RIJNDAEL */ | |
287 | ||
288 | #define RIJNDAEL_BLOCKSIZE 16 | |
289 | void | |
290 | rijndael_setkey(CipherContext *cc, const u_char *key, u_int keylen) | |
291 | { | |
6b523bae | 292 | rijndael_set_key(&cc->u.rijndael.enc, (u4byte *)key, 8*keylen, 1); |
293 | rijndael_set_key(&cc->u.rijndael.dec, (u4byte *)key, 8*keylen, 0); | |
94ec8c6b | 294 | } |
295 | void | |
296 | rijndael_setiv(CipherContext *cc, const u_char *iv, u_int ivlen) | |
297 | { | |
2b87da3b | 298 | if (iv == NULL) |
6b523bae | 299 | fatal("no IV for %s.", cc->cipher->name); |
300 | memcpy((u_char *)cc->u.rijndael.iv, iv, RIJNDAEL_BLOCKSIZE); | |
8ce64345 | 301 | } |
94ec8c6b | 302 | void |
303 | rijndael_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src, | |
304 | u_int len) | |
305 | { | |
6b523bae | 306 | rijndael_ctx *ctx = &cc->u.rijndael.enc; |
307 | u4byte *iv = cc->u.rijndael.iv; | |
308 | u4byte in[4]; | |
309 | u4byte *cprev, *cnow, *plain; | |
310 | int i, blocks = len / RIJNDAEL_BLOCKSIZE; | |
94ec8c6b | 311 | if (len == 0) |
312 | return; | |
313 | if (len % RIJNDAEL_BLOCKSIZE) | |
314 | fatal("rijndael_cbc_encrypt: bad len %d", len); | |
6b523bae | 315 | cnow = (u4byte*) dest; |
316 | plain = (u4byte*) src; | |
94ec8c6b | 317 | cprev = iv; |
6b523bae | 318 | for(i = 0; i < blocks; i++, plain+=4, cnow+=4) { |
319 | in[0] = plain[0] ^ cprev[0]; | |
320 | in[1] = plain[1] ^ cprev[1]; | |
321 | in[2] = plain[2] ^ cprev[2]; | |
322 | in[3] = plain[3] ^ cprev[3]; | |
94ec8c6b | 323 | rijndael_encrypt(ctx, in, cnow); |
324 | cprev = cnow; | |
325 | } | |
326 | memcpy(iv, cprev, RIJNDAEL_BLOCKSIZE); | |
327 | } | |
328 | ||
329 | void | |
330 | rijndael_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, | |
331 | u_int len) | |
332 | { | |
6b523bae | 333 | rijndael_ctx *ctx = &cc->u.rijndael.dec; |
334 | u4byte *iv = cc->u.rijndael.iv; | |
335 | u4byte ivsaved[4]; | |
336 | u4byte *cnow = (u4byte*) (src+len-RIJNDAEL_BLOCKSIZE); | |
337 | u4byte *plain = (u4byte*) (dest+len-RIJNDAEL_BLOCKSIZE); | |
338 | u4byte *ivp; | |
339 | int i, blocks = len / RIJNDAEL_BLOCKSIZE; | |
94ec8c6b | 340 | if (len == 0) |
341 | return; | |
342 | if (len % RIJNDAEL_BLOCKSIZE) | |
343 | fatal("rijndael_cbc_decrypt: bad len %d", len); | |
344 | memcpy(ivsaved, cnow, RIJNDAEL_BLOCKSIZE); | |
6b523bae | 345 | for(i = blocks; i > 0; i--, cnow-=4, plain-=4) { |
94ec8c6b | 346 | rijndael_decrypt(ctx, cnow, plain); |
6b523bae | 347 | ivp = (i == 1) ? iv : cnow-4; |
348 | plain[0] ^= ivp[0]; | |
349 | plain[1] ^= ivp[1]; | |
350 | plain[2] ^= ivp[2]; | |
351 | plain[3] ^= ivp[3]; | |
94ec8c6b | 352 | } |
353 | memcpy(iv, ivsaved, RIJNDAEL_BLOCKSIZE); | |
354 | } | |
355 | ||
356 | Cipher ciphers[] = { | |
357 | { "none", | |
358 | SSH_CIPHER_NONE, 8, 0, | |
359 | none_setkey, none_setiv, | |
360 | none_crypt, none_crypt }, | |
361 | { "des", | |
362 | SSH_CIPHER_DES, 8, 8, | |
363 | des_ssh1_setkey, des_ssh1_setiv, | |
364 | des_ssh1_encrypt, des_ssh1_decrypt }, | |
365 | { "3des", | |
366 | SSH_CIPHER_3DES, 8, 16, | |
367 | des3_ssh1_setkey, des3_setiv, | |
368 | des3_ssh1_encrypt, des3_ssh1_decrypt }, | |
369 | { "blowfish", | |
370 | SSH_CIPHER_BLOWFISH, 8, 16, | |
371 | blowfish_setkey, blowfish_setiv, | |
372 | blowfish_ssh1_encrypt, blowfish_ssh1_decrypt }, | |
373 | ||
374 | { "3des-cbc", | |
375 | SSH_CIPHER_SSH2, 8, 24, | |
376 | des3_setkey, des3_setiv, | |
377 | des3_cbc_encrypt, des3_cbc_decrypt }, | |
378 | { "blowfish-cbc", | |
379 | SSH_CIPHER_SSH2, 8, 16, | |
380 | blowfish_setkey, blowfish_setiv, | |
381 | blowfish_cbc_encrypt, blowfish_cbc_decrypt }, | |
382 | { "cast128-cbc", | |
383 | SSH_CIPHER_SSH2, 8, 16, | |
384 | cast_setkey, cast_setiv, | |
385 | cast_cbc_encrypt, cast_cbc_decrypt }, | |
386 | { "arcfour", | |
387 | SSH_CIPHER_SSH2, 8, 16, | |
388 | arcfour_setkey, none_setiv, | |
389 | arcfour_crypt, arcfour_crypt }, | |
390 | { "aes128-cbc", | |
391 | SSH_CIPHER_SSH2, 16, 16, | |
392 | rijndael_setkey, rijndael_setiv, | |
393 | rijndael_cbc_encrypt, rijndael_cbc_decrypt }, | |
394 | { "aes192-cbc", | |
395 | SSH_CIPHER_SSH2, 16, 24, | |
396 | rijndael_setkey, rijndael_setiv, | |
397 | rijndael_cbc_encrypt, rijndael_cbc_decrypt }, | |
398 | { "aes256-cbc", | |
399 | SSH_CIPHER_SSH2, 16, 32, | |
400 | rijndael_setkey, rijndael_setiv, | |
401 | rijndael_cbc_encrypt, rijndael_cbc_decrypt }, | |
402 | { "rijndael128-cbc", | |
403 | SSH_CIPHER_SSH2, 16, 16, | |
404 | rijndael_setkey, rijndael_setiv, | |
405 | rijndael_cbc_encrypt, rijndael_cbc_decrypt }, | |
406 | { "rijndael192-cbc", | |
407 | SSH_CIPHER_SSH2, 16, 24, | |
408 | rijndael_setkey, rijndael_setiv, | |
409 | rijndael_cbc_encrypt, rijndael_cbc_decrypt }, | |
410 | { "rijndael256-cbc", | |
411 | SSH_CIPHER_SSH2, 16, 32, | |
412 | rijndael_setkey, rijndael_setiv, | |
413 | rijndael_cbc_encrypt, rijndael_cbc_decrypt }, | |
414 | { "rijndael-cbc@lysator.liu.se", | |
415 | SSH_CIPHER_SSH2, 16, 32, | |
416 | rijndael_setkey, rijndael_setiv, | |
417 | rijndael_cbc_encrypt, rijndael_cbc_decrypt }, | |
2b87da3b | 418 | { NULL, SSH_CIPHER_ILLEGAL, 0, 0, NULL, NULL, NULL, NULL } |
94ec8c6b | 419 | }; |
420 | ||
421 | /*--*/ | |
422 | ||
1e3b8b07 | 423 | u_int |
94ec8c6b | 424 | cipher_mask_ssh1(int client) |
8ce64345 | 425 | { |
1e3b8b07 | 426 | u_int mask = 0; |
94ec8c6b | 427 | mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */ |
428 | mask |= 1 << SSH_CIPHER_BLOWFISH; | |
429 | if (client) { | |
430 | mask |= 1 << SSH_CIPHER_DES; | |
431 | } | |
5260325f | 432 | return mask; |
8efc0c15 | 433 | } |
94ec8c6b | 434 | |
435 | Cipher * | |
436 | cipher_by_name(const char *name) | |
8ce64345 | 437 | { |
94ec8c6b | 438 | Cipher *c; |
439 | for (c = ciphers; c->name != NULL; c++) | |
440 | if (strcasecmp(c->name, name) == 0) | |
441 | return c; | |
442 | return NULL; | |
8ce64345 | 443 | } |
8efc0c15 | 444 | |
94ec8c6b | 445 | Cipher * |
446 | cipher_by_number(int id) | |
8efc0c15 | 447 | { |
94ec8c6b | 448 | Cipher *c; |
449 | for (c = ciphers; c->name != NULL; c++) | |
450 | if (c->number == id) | |
451 | return c; | |
452 | return NULL; | |
8efc0c15 | 453 | } |
454 | ||
a8be9f80 | 455 | #define CIPHER_SEP "," |
456 | int | |
457 | ciphers_valid(const char *names) | |
458 | { | |
94ec8c6b | 459 | Cipher *c; |
089fbbd2 | 460 | char *ciphers, *cp; |
a8be9f80 | 461 | char *p; |
a8be9f80 | 462 | |
71276795 | 463 | if (names == NULL || strcmp(names, "") == 0) |
a8be9f80 | 464 | return 0; |
089fbbd2 | 465 | ciphers = cp = xstrdup(names); |
94ec8c6b | 466 | for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0'; |
089fbbd2 | 467 | (p = strsep(&cp, CIPHER_SEP))) { |
94ec8c6b | 468 | c = cipher_by_name(p); |
469 | if (c == NULL || c->number != SSH_CIPHER_SSH2) { | |
470 | debug("bad cipher %s [%s]", p, names); | |
a8be9f80 | 471 | xfree(ciphers); |
472 | return 0; | |
94ec8c6b | 473 | } else { |
33de75a3 | 474 | debug3("cipher ok: %s [%s]", p, names); |
a8be9f80 | 475 | } |
476 | } | |
33de75a3 | 477 | debug3("ciphers ok: [%s]", names); |
a8be9f80 | 478 | xfree(ciphers); |
479 | return 1; | |
480 | } | |
481 | ||
aa3378df | 482 | /* |
483 | * Parses the name of the cipher. Returns the number of the corresponding | |
484 | * cipher, or -1 on error. | |
485 | */ | |
8efc0c15 | 486 | |
487 | int | |
488 | cipher_number(const char *name) | |
489 | { | |
94ec8c6b | 490 | Cipher *c; |
71276795 | 491 | if (name == NULL) |
492 | return -1; | |
94ec8c6b | 493 | c = cipher_by_name(name); |
494 | return (c==NULL) ? -1 : c->number; | |
495 | } | |
496 | ||
497 | char * | |
498 | cipher_name(int id) | |
499 | { | |
500 | Cipher *c = cipher_by_number(id); | |
501 | return (c==NULL) ? "<unknown>" : c->name; | |
502 | } | |
503 | ||
504 | void | |
505 | cipher_init(CipherContext *cc, Cipher *cipher, | |
506 | const u_char *key, u_int keylen, const u_char *iv, u_int ivlen) | |
507 | { | |
508 | if (keylen < cipher->key_len) | |
509 | fatal("cipher_init: key length %d is insufficient for %s.", | |
510 | keylen, cipher->name); | |
511 | if (iv != NULL && ivlen < cipher->block_size) | |
512 | fatal("cipher_init: iv length %d is insufficient for %s.", | |
513 | ivlen, cipher->name); | |
514 | cc->cipher = cipher; | |
515 | cipher->setkey(cc, key, keylen); | |
516 | cipher->setiv(cc, iv, ivlen); | |
517 | } | |
518 | ||
519 | void | |
520 | cipher_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) | |
521 | { | |
522 | if (len % cc->cipher->block_size) | |
523 | fatal("cipher_encrypt: bad plaintext length %d", len); | |
524 | cc->cipher->encrypt(cc, dest, src, len); | |
525 | } | |
526 | ||
527 | void | |
528 | cipher_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) | |
529 | { | |
530 | if (len % cc->cipher->block_size) | |
531 | fatal("cipher_decrypt: bad ciphertext length %d", len); | |
532 | cc->cipher->decrypt(cc, dest, src, len); | |
8efc0c15 | 533 | } |
534 | ||
aa3378df | 535 | /* |
536 | * Selects the cipher, and keys if by computing the MD5 checksum of the | |
537 | * passphrase and using the resulting 16 bytes as the key. | |
538 | */ | |
8efc0c15 | 539 | |
6ae2364d | 540 | void |
94ec8c6b | 541 | cipher_set_key_string(CipherContext *cc, Cipher *cipher, |
542 | const char *passphrase) | |
8efc0c15 | 543 | { |
5260325f | 544 | MD5_CTX md; |
1e3b8b07 | 545 | u_char digest[16]; |
5260325f | 546 | |
547 | MD5_Init(&md); | |
94ec8c6b | 548 | MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase)); |
5260325f | 549 | MD5_Final(digest, &md); |
550 | ||
94ec8c6b | 551 | cipher_init(cc, cipher, digest, 16, NULL, 0); |
5260325f | 552 | |
553 | memset(digest, 0, sizeof(digest)); | |
554 | memset(&md, 0, sizeof(md)); | |
8efc0c15 | 555 | } |