]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> | |
3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | |
4 | * All rights reserved | |
5 | * | |
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. | |
24 | * | |
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. | |
35 | */ | |
36 | ||
37 | #include "includes.h" | |
38 | RCSID("$OpenBSD: cipher.c,v 1.51 2002/02/14 23:41:01 markus Exp $"); | |
39 | ||
40 | #include "xmalloc.h" | |
41 | #include "log.h" | |
42 | #include "cipher.h" | |
43 | ||
44 | #include <openssl/md5.h> | |
45 | ||
46 | struct Cipher { | |
47 | char *name; | |
48 | int number; /* for ssh1 only */ | |
49 | u_int block_size; | |
50 | u_int key_len; | |
51 | void (*setkey)(CipherContext *, const u_char *, u_int); | |
52 | void (*setiv)(CipherContext *, const u_char *, u_int); | |
53 | void (*encrypt)(CipherContext *, u_char *, const u_char *, u_int); | |
54 | void (*decrypt)(CipherContext *, u_char *, const u_char *, u_int); | |
55 | }; | |
56 | ||
57 | /* no encryption */ | |
58 | static void | |
59 | none_setkey(CipherContext *cc, const u_char *key, u_int keylen) | |
60 | { | |
61 | } | |
62 | static void | |
63 | none_setiv(CipherContext *cc, const u_char *iv, u_int ivlen) | |
64 | { | |
65 | } | |
66 | static void | |
67 | none_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) | |
68 | { | |
69 | memcpy(dest, src, len); | |
70 | } | |
71 | ||
72 | /* DES */ | |
73 | static void | |
74 | des_ssh1_setkey(CipherContext *cc, const u_char *key, u_int keylen) | |
75 | { | |
76 | static int dowarn = 1; | |
77 | if (dowarn) { | |
78 | error("Warning: use of DES is strongly discouraged " | |
79 | "due to cryptographic weaknesses"); | |
80 | dowarn = 0; | |
81 | } | |
82 | des_set_key((void *)key, cc->u.des.key); | |
83 | } | |
84 | static void | |
85 | des_ssh1_setiv(CipherContext *cc, const u_char *iv, u_int ivlen) | |
86 | { | |
87 | memset(cc->u.des.iv, 0, sizeof(cc->u.des.iv)); | |
88 | } | |
89 | static void | |
90 | des_ssh1_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) | |
91 | { | |
92 | des_ncbc_encrypt(src, dest, len, cc->u.des.key, &cc->u.des.iv, | |
93 | DES_ENCRYPT); | |
94 | } | |
95 | static void | |
96 | des_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) | |
97 | { | |
98 | des_ncbc_encrypt(src, dest, len, cc->u.des.key, &cc->u.des.iv, | |
99 | DES_DECRYPT); | |
100 | } | |
101 | ||
102 | /* 3DES */ | |
103 | static void | |
104 | des3_setkey(CipherContext *cc, const u_char *key, u_int keylen) | |
105 | { | |
106 | des_set_key((void *) key, cc->u.des3.key1); | |
107 | des_set_key((void *) (key+8), cc->u.des3.key2); | |
108 | des_set_key((void *) (key+16), cc->u.des3.key3); | |
109 | } | |
110 | static void | |
111 | des3_setiv(CipherContext *cc, const u_char *iv, u_int ivlen) | |
112 | { | |
113 | memset(cc->u.des3.iv1, 0, sizeof(cc->u.des3.iv1)); | |
114 | memset(cc->u.des3.iv2, 0, sizeof(cc->u.des3.iv2)); | |
115 | memset(cc->u.des3.iv3, 0, sizeof(cc->u.des3.iv3)); | |
116 | if (iv == NULL) | |
117 | return; | |
118 | memcpy(cc->u.des3.iv3, (char *)iv, 8); | |
119 | } | |
120 | static void | |
121 | des3_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) | |
122 | { | |
123 | des_ede3_cbc_encrypt(src, dest, len, | |
124 | cc->u.des3.key1, cc->u.des3.key2, cc->u.des3.key3, | |
125 | &cc->u.des3.iv3, DES_ENCRYPT); | |
126 | } | |
127 | static void | |
128 | des3_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) | |
129 | { | |
130 | des_ede3_cbc_encrypt(src, dest, len, | |
131 | cc->u.des3.key1, cc->u.des3.key2, cc->u.des3.key3, | |
132 | &cc->u.des3.iv3, DES_DECRYPT); | |
133 | } | |
134 | ||
135 | /* | |
136 | * This is used by SSH1: | |
137 | * | |
138 | * What kind of triple DES are these 2 routines? | |
139 | * | |
140 | * Why is there a redundant initialization vector? | |
141 | * | |
142 | * If only iv3 was used, then, this would till effect have been | |
143 | * outer-cbc. However, there is also a private iv1 == iv2 which | |
144 | * perhaps makes differential analysis easier. On the other hand, the | |
145 | * private iv1 probably makes the CRC-32 attack ineffective. This is a | |
146 | * result of that there is no longer any known iv1 to use when | |
147 | * choosing the X block. | |
148 | */ | |
149 | static void | |
150 | des3_ssh1_setkey(CipherContext *cc, const u_char *key, u_int keylen) | |
151 | { | |
152 | des_set_key((void *) key, cc->u.des3.key1); | |
153 | des_set_key((void *) (key+8), cc->u.des3.key2); | |
154 | if (keylen <= 16) | |
155 | des_set_key((void *) key, cc->u.des3.key3); | |
156 | else | |
157 | des_set_key((void *) (key+16), cc->u.des3.key3); | |
158 | } | |
159 | static void | |
160 | des3_ssh1_encrypt(CipherContext *cc, u_char *dest, const u_char *src, | |
161 | u_int len) | |
162 | { | |
163 | des_ncbc_encrypt(src, dest, len, cc->u.des3.key1, &cc->u.des3.iv1, | |
164 | DES_ENCRYPT); | |
165 | des_ncbc_encrypt(dest, dest, len, cc->u.des3.key2, &cc->u.des3.iv2, | |
166 | DES_DECRYPT); | |
167 | des_ncbc_encrypt(dest, dest, len, cc->u.des3.key3, &cc->u.des3.iv3, | |
168 | DES_ENCRYPT); | |
169 | } | |
170 | static void | |
171 | des3_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src, | |
172 | u_int len) | |
173 | { | |
174 | des_ncbc_encrypt(src, dest, len, cc->u.des3.key3, &cc->u.des3.iv3, | |
175 | DES_DECRYPT); | |
176 | des_ncbc_encrypt(dest, dest, len, cc->u.des3.key2, &cc->u.des3.iv2, | |
177 | DES_ENCRYPT); | |
178 | des_ncbc_encrypt(dest, dest, len, cc->u.des3.key1, &cc->u.des3.iv1, | |
179 | DES_DECRYPT); | |
180 | } | |
181 | ||
182 | /* Blowfish */ | |
183 | static void | |
184 | blowfish_setkey(CipherContext *cc, const u_char *key, u_int keylen) | |
185 | { | |
186 | BF_set_key(&cc->u.bf.key, keylen, (u_char *)key); | |
187 | } | |
188 | static void | |
189 | blowfish_setiv(CipherContext *cc, const u_char *iv, u_int ivlen) | |
190 | { | |
191 | if (iv == NULL) | |
192 | memset(cc->u.bf.iv, 0, 8); | |
193 | else | |
194 | memcpy(cc->u.bf.iv, (char *)iv, 8); | |
195 | } | |
196 | static void | |
197 | blowfish_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src, | |
198 | u_int len) | |
199 | { | |
200 | BF_cbc_encrypt((void *)src, dest, len, &cc->u.bf.key, cc->u.bf.iv, | |
201 | BF_ENCRYPT); | |
202 | } | |
203 | static void | |
204 | blowfish_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, | |
205 | u_int len) | |
206 | { | |
207 | BF_cbc_encrypt((void *)src, dest, len, &cc->u.bf.key, cc->u.bf.iv, | |
208 | BF_DECRYPT); | |
209 | } | |
210 | ||
211 | /* | |
212 | * SSH1 uses a variation on Blowfish, all bytes must be swapped before | |
213 | * and after encryption/decryption. Thus the swap_bytes stuff (yuk). | |
214 | */ | |
215 | static void | |
216 | swap_bytes(const u_char *src, u_char *dst, int n) | |
217 | { | |
218 | char c[4]; | |
219 | ||
220 | /* Process 4 bytes every lap. */ | |
221 | for (n = n / 4; n > 0; n--) { | |
222 | c[3] = *src++; | |
223 | c[2] = *src++; | |
224 | c[1] = *src++; | |
225 | c[0] = *src++; | |
226 | ||
227 | *dst++ = c[0]; | |
228 | *dst++ = c[1]; | |
229 | *dst++ = c[2]; | |
230 | *dst++ = c[3]; | |
231 | } | |
232 | } | |
233 | ||
234 | static void | |
235 | blowfish_ssh1_encrypt(CipherContext *cc, u_char *dest, const u_char *src, | |
236 | u_int len) | |
237 | { | |
238 | swap_bytes(src, dest, len); | |
239 | BF_cbc_encrypt((void *)dest, dest, len, &cc->u.bf.key, cc->u.bf.iv, | |
240 | BF_ENCRYPT); | |
241 | swap_bytes(dest, dest, len); | |
242 | } | |
243 | static void | |
244 | blowfish_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src, | |
245 | u_int len) | |
246 | { | |
247 | swap_bytes(src, dest, len); | |
248 | BF_cbc_encrypt((void *)dest, dest, len, &cc->u.bf.key, cc->u.bf.iv, | |
249 | BF_DECRYPT); | |
250 | swap_bytes(dest, dest, len); | |
251 | } | |
252 | ||
253 | /* alleged rc4 */ | |
254 | static void | |
255 | arcfour_setkey(CipherContext *cc, const u_char *key, u_int keylen) | |
256 | { | |
257 | RC4_set_key(&cc->u.rc4, keylen, (u_char *)key); | |
258 | } | |
259 | static void | |
260 | arcfour_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) | |
261 | { | |
262 | RC4(&cc->u.rc4, len, (u_char *)src, dest); | |
263 | } | |
264 | ||
265 | /* CAST */ | |
266 | static void | |
267 | cast_setkey(CipherContext *cc, const u_char *key, u_int keylen) | |
268 | { | |
269 | CAST_set_key(&cc->u.cast.key, keylen, (u_char *) key); | |
270 | } | |
271 | static void | |
272 | cast_setiv(CipherContext *cc, const u_char *iv, u_int ivlen) | |
273 | { | |
274 | if (iv == NULL) | |
275 | fatal("no IV for %s.", cc->cipher->name); | |
276 | memcpy(cc->u.cast.iv, (char *)iv, 8); | |
277 | } | |
278 | static void | |
279 | cast_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) | |
280 | { | |
281 | CAST_cbc_encrypt(src, dest, len, &cc->u.cast.key, cc->u.cast.iv, | |
282 | CAST_ENCRYPT); | |
283 | } | |
284 | static void | |
285 | cast_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) | |
286 | { | |
287 | CAST_cbc_encrypt(src, dest, len, &cc->u.cast.key, cc->u.cast.iv, | |
288 | CAST_DECRYPT); | |
289 | } | |
290 | ||
291 | /* RIJNDAEL */ | |
292 | ||
293 | #define RIJNDAEL_BLOCKSIZE 16 | |
294 | static void | |
295 | rijndael_setkey(CipherContext *cc, const u_char *key, u_int keylen) | |
296 | { | |
297 | rijndael_set_key(&cc->u.rijndael.enc, (char *)key, 8*keylen, 1); | |
298 | rijndael_set_key(&cc->u.rijndael.dec, (char *)key, 8*keylen, 0); | |
299 | } | |
300 | static void | |
301 | rijndael_setiv(CipherContext *cc, const u_char *iv, u_int ivlen) | |
302 | { | |
303 | if (iv == NULL || ivlen != RIJNDAEL_BLOCKSIZE) | |
304 | fatal("bad/no IV for %s.", cc->cipher->name); | |
305 | memcpy(cc->u.rijndael.iv, iv, RIJNDAEL_BLOCKSIZE); | |
306 | } | |
307 | static void | |
308 | rijndael_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src, | |
309 | u_int len) | |
310 | { | |
311 | rijndael_ctx *ctx = &cc->u.rijndael.enc; | |
312 | u_char *iv = cc->u.rijndael.iv; | |
313 | u_char in[RIJNDAEL_BLOCKSIZE]; | |
314 | u_char *cprev, *cnow, *plain; | |
315 | int i, j, blocks = len / RIJNDAEL_BLOCKSIZE; | |
316 | ||
317 | if (len == 0) | |
318 | return; | |
319 | if (len % RIJNDAEL_BLOCKSIZE) | |
320 | fatal("rijndael_cbc_encrypt: bad len %d", len); | |
321 | cnow = dest; | |
322 | plain = (u_char *)src; | |
323 | cprev = iv; | |
324 | for (i = 0; i < blocks; i++, plain+=RIJNDAEL_BLOCKSIZE, | |
325 | cnow+=RIJNDAEL_BLOCKSIZE) { | |
326 | for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++) | |
327 | in[j] = plain[j] ^ cprev[j]; | |
328 | rijndael_encrypt(ctx, in, cnow); | |
329 | cprev = cnow; | |
330 | } | |
331 | memcpy(iv, cprev, RIJNDAEL_BLOCKSIZE); | |
332 | } | |
333 | static void | |
334 | rijndael_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, | |
335 | u_int len) | |
336 | { | |
337 | rijndael_ctx *ctx = &cc->u.rijndael.dec; | |
338 | u_char *iv = cc->u.rijndael.iv; | |
339 | u_char ivsaved[RIJNDAEL_BLOCKSIZE]; | |
340 | u_char *cnow = (u_char *) (src+len-RIJNDAEL_BLOCKSIZE); | |
341 | u_char *plain = dest+len-RIJNDAEL_BLOCKSIZE; | |
342 | u_char *ivp; | |
343 | int i, j, blocks = len / RIJNDAEL_BLOCKSIZE; | |
344 | ||
345 | if (len == 0) | |
346 | return; | |
347 | if (len % RIJNDAEL_BLOCKSIZE) | |
348 | fatal("rijndael_cbc_decrypt: bad len %d", len); | |
349 | memcpy(ivsaved, cnow, RIJNDAEL_BLOCKSIZE); | |
350 | for (i = blocks; i > 0; i--, cnow-=RIJNDAEL_BLOCKSIZE, | |
351 | plain-=RIJNDAEL_BLOCKSIZE) { | |
352 | rijndael_decrypt(ctx, cnow, plain); | |
353 | ivp = (i == 1) ? iv : cnow-RIJNDAEL_BLOCKSIZE; | |
354 | for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++) | |
355 | plain[j] ^= ivp[j]; | |
356 | } | |
357 | memcpy(iv, ivsaved, RIJNDAEL_BLOCKSIZE); | |
358 | } | |
359 | ||
360 | Cipher ciphers[] = { | |
361 | { "none", | |
362 | SSH_CIPHER_NONE, 8, 0, | |
363 | none_setkey, none_setiv, | |
364 | none_crypt, none_crypt }, | |
365 | { "des", | |
366 | SSH_CIPHER_DES, 8, 8, | |
367 | des_ssh1_setkey, des_ssh1_setiv, | |
368 | des_ssh1_encrypt, des_ssh1_decrypt }, | |
369 | { "3des", | |
370 | SSH_CIPHER_3DES, 8, 16, | |
371 | des3_ssh1_setkey, des3_setiv, | |
372 | des3_ssh1_encrypt, des3_ssh1_decrypt }, | |
373 | { "blowfish", | |
374 | SSH_CIPHER_BLOWFISH, 8, 16, | |
375 | blowfish_setkey, blowfish_setiv, | |
376 | blowfish_ssh1_encrypt, blowfish_ssh1_decrypt }, | |
377 | ||
378 | { "3des-cbc", | |
379 | SSH_CIPHER_SSH2, 8, 24, | |
380 | des3_setkey, des3_setiv, | |
381 | des3_cbc_encrypt, des3_cbc_decrypt }, | |
382 | { "blowfish-cbc", | |
383 | SSH_CIPHER_SSH2, 8, 16, | |
384 | blowfish_setkey, blowfish_setiv, | |
385 | blowfish_cbc_encrypt, blowfish_cbc_decrypt }, | |
386 | { "cast128-cbc", | |
387 | SSH_CIPHER_SSH2, 8, 16, | |
388 | cast_setkey, cast_setiv, | |
389 | cast_cbc_encrypt, cast_cbc_decrypt }, | |
390 | { "arcfour", | |
391 | SSH_CIPHER_SSH2, 8, 16, | |
392 | arcfour_setkey, none_setiv, | |
393 | arcfour_crypt, arcfour_crypt }, | |
394 | { "aes128-cbc", | |
395 | SSH_CIPHER_SSH2, 16, 16, | |
396 | rijndael_setkey, rijndael_setiv, | |
397 | rijndael_cbc_encrypt, rijndael_cbc_decrypt }, | |
398 | { "aes192-cbc", | |
399 | SSH_CIPHER_SSH2, 16, 24, | |
400 | rijndael_setkey, rijndael_setiv, | |
401 | rijndael_cbc_encrypt, rijndael_cbc_decrypt }, | |
402 | { "aes256-cbc", | |
403 | SSH_CIPHER_SSH2, 16, 32, | |
404 | rijndael_setkey, rijndael_setiv, | |
405 | rijndael_cbc_encrypt, rijndael_cbc_decrypt }, | |
406 | { NULL, SSH_CIPHER_ILLEGAL, 0, 0, NULL, NULL, NULL, NULL } | |
407 | }; | |
408 | ||
409 | /*--*/ | |
410 | ||
411 | u_int | |
412 | cipher_blocksize(Cipher *c) | |
413 | { | |
414 | return (c->block_size); | |
415 | } | |
416 | ||
417 | u_int | |
418 | cipher_keylen(Cipher *c) | |
419 | { | |
420 | return (c->key_len); | |
421 | } | |
422 | ||
423 | u_int | |
424 | cipher_mask_ssh1(int client) | |
425 | { | |
426 | u_int mask = 0; | |
427 | mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */ | |
428 | mask |= 1 << SSH_CIPHER_BLOWFISH; | |
429 | if (client) { | |
430 | mask |= 1 << SSH_CIPHER_DES; | |
431 | } | |
432 | return mask; | |
433 | } | |
434 | ||
435 | Cipher * | |
436 | cipher_by_name(const char *name) | |
437 | { | |
438 | Cipher *c; | |
439 | for (c = ciphers; c->name != NULL; c++) | |
440 | if (strcasecmp(c->name, name) == 0) | |
441 | return c; | |
442 | return NULL; | |
443 | } | |
444 | ||
445 | Cipher * | |
446 | cipher_by_number(int id) | |
447 | { | |
448 | Cipher *c; | |
449 | for (c = ciphers; c->name != NULL; c++) | |
450 | if (c->number == id) | |
451 | return c; | |
452 | return NULL; | |
453 | } | |
454 | ||
455 | #define CIPHER_SEP "," | |
456 | int | |
457 | ciphers_valid(const char *names) | |
458 | { | |
459 | Cipher *c; | |
460 | char *ciphers, *cp; | |
461 | char *p; | |
462 | ||
463 | if (names == NULL || strcmp(names, "") == 0) | |
464 | return 0; | |
465 | ciphers = cp = xstrdup(names); | |
466 | for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0'; | |
467 | (p = strsep(&cp, CIPHER_SEP))) { | |
468 | c = cipher_by_name(p); | |
469 | if (c == NULL || c->number != SSH_CIPHER_SSH2) { | |
470 | debug("bad cipher %s [%s]", p, names); | |
471 | xfree(ciphers); | |
472 | return 0; | |
473 | } else { | |
474 | debug3("cipher ok: %s [%s]", p, names); | |
475 | } | |
476 | } | |
477 | debug3("ciphers ok: [%s]", names); | |
478 | xfree(ciphers); | |
479 | return 1; | |
480 | } | |
481 | ||
482 | /* | |
483 | * Parses the name of the cipher. Returns the number of the corresponding | |
484 | * cipher, or -1 on error. | |
485 | */ | |
486 | ||
487 | int | |
488 | cipher_number(const char *name) | |
489 | { | |
490 | Cipher *c; | |
491 | if (name == NULL) | |
492 | return -1; | |
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, const u_char *key, | |
506 | u_int keylen, const u_char *iv, u_int ivlen, int encrypt) | |
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 | cc->encrypt = (encrypt == CIPHER_ENCRYPT); | |
516 | cipher->setkey(cc, key, keylen); | |
517 | cipher->setiv(cc, iv, ivlen); | |
518 | } | |
519 | ||
520 | void | |
521 | cipher_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) | |
522 | { | |
523 | if (len % cc->cipher->block_size) | |
524 | fatal("cipher_encrypt: bad plaintext length %d", len); | |
525 | if (cc->encrypt) | |
526 | cc->cipher->encrypt(cc, dest, src, len); | |
527 | else | |
528 | cc->cipher->decrypt(cc, dest, src, len); | |
529 | } | |
530 | ||
531 | void | |
532 | cipher_cleanup(CipherContext *cc) | |
533 | { | |
534 | memset(cc, 0, sizeof(*cc)); | |
535 | } | |
536 | ||
537 | /* | |
538 | * Selects the cipher, and keys if by computing the MD5 checksum of the | |
539 | * passphrase and using the resulting 16 bytes as the key. | |
540 | */ | |
541 | ||
542 | void | |
543 | cipher_set_key_string(CipherContext *cc, Cipher *cipher, | |
544 | const char *passphrase, int encrypt) | |
545 | { | |
546 | MD5_CTX md; | |
547 | u_char digest[16]; | |
548 | ||
549 | MD5_Init(&md); | |
550 | MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase)); | |
551 | MD5_Final(digest, &md); | |
552 | ||
553 | cipher_init(cc, cipher, digest, 16, NULL, 0, encrypt); | |
554 | ||
555 | memset(digest, 0, sizeof(digest)); | |
556 | memset(&md, 0, sizeof(md)); | |
557 | } |