]>
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. | |
a96070d4 | 14 | * Copyright (c) 1999, 2000 Markus Friedl. All rights reserved. |
bcbf86ec | 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" | |
70fc1609 | 38 | RCSID("$OpenBSD: cipher.c,v 1.52 2002/02/18 13:05:32 markus Exp $"); |
8efc0c15 | 39 | |
a8be9f80 | 40 | #include "xmalloc.h" |
42f11eb2 | 41 | #include "log.h" |
42 | #include "cipher.h" | |
8efc0c15 | 43 | |
492a3893 | 44 | #if OPENSSL_VERSION_NUMBER <= 0x0090600fL |
45 | #define EVP_CIPHER_CTX_get_app_data(e) ((e)->app_data) | |
46 | #endif | |
47 | ||
8efc0c15 | 48 | #include <openssl/md5.h> |
70fc1609 | 49 | #include "rijndael.h" |
50 | ||
51 | static EVP_CIPHER *evp_ssh1_3des(void); | |
52 | static EVP_CIPHER *evp_ssh1_bf(void); | |
53 | static EVP_CIPHER *evp_rijndael(void); | |
8efc0c15 | 54 | |
3ee832e5 | 55 | struct Cipher { |
56 | char *name; | |
57 | int number; /* for ssh1 only */ | |
58 | u_int block_size; | |
59 | u_int key_len; | |
70fc1609 | 60 | EVP_CIPHER *(*evptype)(void); |
61 | } ciphers[] = { | |
62 | { "none", SSH_CIPHER_NONE, 8, 0, EVP_enc_null }, | |
63 | { "des", SSH_CIPHER_DES, 8, 8, EVP_des_cbc }, | |
64 | { "3des", SSH_CIPHER_3DES, 8, 16, evp_ssh1_3des }, | |
65 | { "blowfish", SSH_CIPHER_BLOWFISH, 8, 32, evp_ssh1_bf }, | |
66 | ||
67 | { "3des-cbc", SSH_CIPHER_SSH2, 8, 24, EVP_des_ede3_cbc }, | |
68 | { "blowfish-cbc", SSH_CIPHER_SSH2, 8, 16, EVP_bf_cbc }, | |
69 | { "cast128-cbc", SSH_CIPHER_SSH2, 8, 16, EVP_cast5_cbc }, | |
70 | { "arcfour", SSH_CIPHER_SSH2, 8, 16, EVP_rc4 }, | |
71 | { "aes128-cbc", SSH_CIPHER_SSH2, 16, 16, evp_rijndael }, | |
72 | { "aes192-cbc", SSH_CIPHER_SSH2, 16, 24, evp_rijndael }, | |
73 | { "aes256-cbc", SSH_CIPHER_SSH2, 16, 32, evp_rijndael }, | |
74 | ||
75 | { NULL, SSH_CIPHER_ILLEGAL, 0, 0, NULL } | |
94ec8c6b | 76 | }; |
77 | ||
78 | /*--*/ | |
79 | ||
70fc1609 | 80 | u_int |
3ee832e5 | 81 | cipher_blocksize(Cipher *c) |
82 | { | |
83 | return (c->block_size); | |
84 | } | |
70fc1609 | 85 | u_int |
3ee832e5 | 86 | cipher_keylen(Cipher *c) |
87 | { | |
88 | return (c->key_len); | |
89 | } | |
90 | ||
1e3b8b07 | 91 | u_int |
94ec8c6b | 92 | cipher_mask_ssh1(int client) |
8ce64345 | 93 | { |
1e3b8b07 | 94 | u_int mask = 0; |
184eed6a | 95 | mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */ |
94ec8c6b | 96 | mask |= 1 << SSH_CIPHER_BLOWFISH; |
97 | if (client) { | |
98 | mask |= 1 << SSH_CIPHER_DES; | |
99 | } | |
5260325f | 100 | return mask; |
8efc0c15 | 101 | } |
94ec8c6b | 102 | |
103 | Cipher * | |
104 | cipher_by_name(const char *name) | |
8ce64345 | 105 | { |
94ec8c6b | 106 | Cipher *c; |
107 | for (c = ciphers; c->name != NULL; c++) | |
108 | if (strcasecmp(c->name, name) == 0) | |
109 | return c; | |
110 | return NULL; | |
8ce64345 | 111 | } |
8efc0c15 | 112 | |
94ec8c6b | 113 | Cipher * |
114 | cipher_by_number(int id) | |
8efc0c15 | 115 | { |
94ec8c6b | 116 | Cipher *c; |
117 | for (c = ciphers; c->name != NULL; c++) | |
118 | if (c->number == id) | |
119 | return c; | |
120 | return NULL; | |
8efc0c15 | 121 | } |
122 | ||
a8be9f80 | 123 | #define CIPHER_SEP "," |
124 | int | |
125 | ciphers_valid(const char *names) | |
126 | { | |
94ec8c6b | 127 | Cipher *c; |
089fbbd2 | 128 | char *ciphers, *cp; |
a8be9f80 | 129 | char *p; |
a8be9f80 | 130 | |
71276795 | 131 | if (names == NULL || strcmp(names, "") == 0) |
a8be9f80 | 132 | return 0; |
089fbbd2 | 133 | ciphers = cp = xstrdup(names); |
94ec8c6b | 134 | for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0'; |
184eed6a | 135 | (p = strsep(&cp, CIPHER_SEP))) { |
94ec8c6b | 136 | c = cipher_by_name(p); |
137 | if (c == NULL || c->number != SSH_CIPHER_SSH2) { | |
138 | debug("bad cipher %s [%s]", p, names); | |
a8be9f80 | 139 | xfree(ciphers); |
140 | return 0; | |
94ec8c6b | 141 | } else { |
33de75a3 | 142 | debug3("cipher ok: %s [%s]", p, names); |
a8be9f80 | 143 | } |
144 | } | |
33de75a3 | 145 | debug3("ciphers ok: [%s]", names); |
a8be9f80 | 146 | xfree(ciphers); |
147 | return 1; | |
148 | } | |
149 | ||
aa3378df | 150 | /* |
151 | * Parses the name of the cipher. Returns the number of the corresponding | |
152 | * cipher, or -1 on error. | |
153 | */ | |
8efc0c15 | 154 | |
155 | int | |
156 | cipher_number(const char *name) | |
157 | { | |
94ec8c6b | 158 | Cipher *c; |
71276795 | 159 | if (name == NULL) |
160 | return -1; | |
94ec8c6b | 161 | c = cipher_by_name(name); |
162 | return (c==NULL) ? -1 : c->number; | |
163 | } | |
164 | ||
165 | char * | |
166 | cipher_name(int id) | |
167 | { | |
168 | Cipher *c = cipher_by_number(id); | |
169 | return (c==NULL) ? "<unknown>" : c->name; | |
170 | } | |
171 | ||
172 | void | |
70fc1609 | 173 | cipher_init(CipherContext *cc, Cipher *cipher, |
174 | const u_char *key, u_int keylen, const u_char *iv, u_int ivlen, | |
175 | int encrypt) | |
94ec8c6b | 176 | { |
70fc1609 | 177 | static int dowarn = 1; |
178 | const EVP_CIPHER *type; | |
179 | int klen; | |
180 | ||
181 | if (cipher->number == SSH_CIPHER_DES) { | |
182 | if (dowarn) { | |
183 | error("Warning: use of DES is strongly discouraged " | |
184 | "due to cryptographic weaknesses"); | |
185 | dowarn = 0; | |
186 | } | |
187 | if (keylen > 8) | |
188 | keylen = 8; | |
189 | } | |
190 | cc->plaintext = (cipher->number == SSH_CIPHER_NONE); | |
191 | ||
94ec8c6b | 192 | if (keylen < cipher->key_len) |
193 | fatal("cipher_init: key length %d is insufficient for %s.", | |
194 | keylen, cipher->name); | |
195 | if (iv != NULL && ivlen < cipher->block_size) | |
196 | fatal("cipher_init: iv length %d is insufficient for %s.", | |
197 | ivlen, cipher->name); | |
198 | cc->cipher = cipher; | |
70fc1609 | 199 | |
200 | type = (*cipher->evptype)(); | |
201 | ||
202 | EVP_CIPHER_CTX_init(&cc->evp); | |
492a3893 | 203 | #if OPENSSL_VERSION_NUMBER > 0x0090600fL |
70fc1609 | 204 | if (EVP_CipherInit(&cc->evp, type, NULL, (u_char *)iv, |
205 | (encrypt == CIPHER_ENCRYPT)) == 0) | |
206 | fatal("cipher_init: EVP_CipherInit failed for %s", | |
207 | cipher->name); | |
208 | klen = EVP_CIPHER_CTX_key_length(&cc->evp); | |
209 | if (klen > 0 && keylen != klen) { | |
210 | debug("cipher_init: set keylen (%d -> %d)", klen, keylen); | |
211 | if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0) | |
212 | fatal("cipher_init: set keylen failed (%d -> %d)", | |
213 | klen, keylen); | |
214 | } | |
215 | if (EVP_CipherInit(&cc->evp, NULL, (u_char *)key, NULL, -1) == 0) | |
216 | fatal("cipher_init: EVP_CipherInit: set key failed for %s", | |
217 | cipher->name); | |
492a3893 | 218 | #else |
219 | EVP_CipherInit(&cc->evp, type, (u_char *)key, (u_char *)iv, | |
220 | (encrypt == CIPHER_ENCRYPT)); | |
221 | #endif | |
94ec8c6b | 222 | } |
223 | ||
224 | void | |
3ee832e5 | 225 | cipher_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) |
94ec8c6b | 226 | { |
227 | if (len % cc->cipher->block_size) | |
228 | fatal("cipher_encrypt: bad plaintext length %d", len); | |
492a3893 | 229 | #if OPENSSL_VERSION_NUMBER > 0x0090600fL |
70fc1609 | 230 | if (EVP_Cipher(&cc->evp, dest, (u_char *)src, len) == 0) |
231 | fatal("evp_crypt: EVP_Cipher failed"); | |
492a3893 | 232 | #else |
233 | EVP_Cipher(&cc->evp, dest, (u_char *)src, len); | |
234 | #endif | |
94ec8c6b | 235 | } |
236 | ||
237 | void | |
3ee832e5 | 238 | cipher_cleanup(CipherContext *cc) |
94ec8c6b | 239 | { |
492a3893 | 240 | #if OPENSSL_VERSION_NUMBER > 0x0090600fL |
70fc1609 | 241 | if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0) |
242 | error("cipher_cleanup: EVP_CIPHER_CTX_cleanup failed"); | |
492a3893 | 243 | #else |
244 | EVP_CIPHER_CTX_cleanup(&cc->evp); | |
245 | #endif | |
8efc0c15 | 246 | } |
247 | ||
aa3378df | 248 | /* |
249 | * Selects the cipher, and keys if by computing the MD5 checksum of the | |
250 | * passphrase and using the resulting 16 bytes as the key. | |
251 | */ | |
8efc0c15 | 252 | |
6ae2364d | 253 | void |
94ec8c6b | 254 | cipher_set_key_string(CipherContext *cc, Cipher *cipher, |
3ee832e5 | 255 | const char *passphrase, int encrypt) |
8efc0c15 | 256 | { |
5260325f | 257 | MD5_CTX md; |
1e3b8b07 | 258 | u_char digest[16]; |
5260325f | 259 | |
260 | MD5_Init(&md); | |
94ec8c6b | 261 | MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase)); |
5260325f | 262 | MD5_Final(digest, &md); |
263 | ||
3ee832e5 | 264 | cipher_init(cc, cipher, digest, 16, NULL, 0, encrypt); |
5260325f | 265 | |
266 | memset(digest, 0, sizeof(digest)); | |
267 | memset(&md, 0, sizeof(md)); | |
8efc0c15 | 268 | } |
70fc1609 | 269 | |
270 | /* Implementations for other non-EVP ciphers */ | |
271 | ||
272 | /* | |
273 | * This is used by SSH1: | |
274 | * | |
275 | * What kind of triple DES are these 2 routines? | |
276 | * | |
277 | * Why is there a redundant initialization vector? | |
278 | * | |
279 | * If only iv3 was used, then, this would till effect have been | |
280 | * outer-cbc. However, there is also a private iv1 == iv2 which | |
281 | * perhaps makes differential analysis easier. On the other hand, the | |
282 | * private iv1 probably makes the CRC-32 attack ineffective. This is a | |
283 | * result of that there is no longer any known iv1 to use when | |
284 | * choosing the X block. | |
285 | */ | |
286 | struct ssh1_3des_ctx | |
287 | { | |
288 | EVP_CIPHER_CTX k1, k2, k3; | |
289 | }; | |
290 | static int | |
291 | ssh1_3des_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv, | |
292 | int enc) | |
293 | { | |
294 | struct ssh1_3des_ctx *c; | |
295 | u_char *k1, *k2, *k3; | |
296 | ||
297 | if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) { | |
298 | c = xmalloc(sizeof(*c)); | |
299 | EVP_CIPHER_CTX_set_app_data(ctx, c); | |
300 | } | |
301 | if (key == NULL) | |
302 | return (1); | |
303 | if (enc == -1) | |
304 | enc = ctx->encrypt; | |
305 | k1 = k2 = k3 = (u_char *) key; | |
306 | k2 += 8; | |
307 | if (EVP_CIPHER_CTX_key_length(ctx) >= 16+8) { | |
308 | if (enc) | |
309 | k3 += 16; | |
310 | else | |
311 | k1 += 16; | |
312 | } | |
313 | EVP_CIPHER_CTX_init(&c->k1); | |
314 | EVP_CIPHER_CTX_init(&c->k2); | |
315 | EVP_CIPHER_CTX_init(&c->k3); | |
492a3893 | 316 | #if OPENSSL_VERSION_NUMBER > 0x0090600fL |
70fc1609 | 317 | if (EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc) == 0 || |
318 | EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc) == 0 || | |
319 | EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc) == 0) { | |
320 | memset(c, 0, sizeof(*c)); | |
321 | xfree(c); | |
322 | EVP_CIPHER_CTX_set_app_data(ctx, NULL); | |
323 | return (0); | |
324 | } | |
492a3893 | 325 | #else |
326 | EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc); | |
327 | EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc); | |
328 | EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc); | |
329 | #endif | |
70fc1609 | 330 | return (1); |
331 | } | |
332 | static int | |
333 | ssh1_3des_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src, u_int len) | |
334 | { | |
335 | struct ssh1_3des_ctx *c; | |
336 | ||
337 | if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) { | |
338 | error("ssh1_3des_cbc: no context"); | |
339 | return (0); | |
340 | } | |
492a3893 | 341 | #if OPENSSL_VERSION_NUMBER > 0x0090600fL |
70fc1609 | 342 | if (EVP_Cipher(&c->k1, dest, (u_char *)src, len) == 0 || |
343 | EVP_Cipher(&c->k2, dest, dest, len) == 0 || | |
344 | EVP_Cipher(&c->k3, dest, dest, len) == 0) | |
345 | return (0); | |
492a3893 | 346 | #else |
347 | EVP_Cipher(&c->k1, dest, (u_char *)src, len); | |
348 | EVP_Cipher(&c->k2, dest, dest, len); | |
349 | EVP_Cipher(&c->k3, dest, dest, len); | |
350 | #endif | |
70fc1609 | 351 | return (1); |
352 | } | |
353 | static int | |
354 | ssh1_3des_cleanup(EVP_CIPHER_CTX *ctx) | |
355 | { | |
356 | struct ssh1_3des_ctx *c; | |
357 | ||
358 | if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) { | |
359 | memset(c, 0, sizeof(*c)); | |
360 | xfree(c); | |
361 | EVP_CIPHER_CTX_set_app_data(ctx, NULL); | |
362 | } | |
363 | return (1); | |
364 | } | |
365 | static EVP_CIPHER * | |
366 | evp_ssh1_3des(void) | |
367 | { | |
368 | static EVP_CIPHER ssh1_3des; | |
369 | ||
370 | memset(&ssh1_3des, 0, sizeof(EVP_CIPHER)); | |
371 | ssh1_3des.nid = NID_undef; | |
372 | ssh1_3des.block_size = 8; | |
373 | ssh1_3des.iv_len = 0; | |
374 | ssh1_3des.key_len = 16; | |
375 | ssh1_3des.init = ssh1_3des_init; | |
376 | ssh1_3des.cleanup = ssh1_3des_cleanup; | |
377 | ssh1_3des.do_cipher = ssh1_3des_cbc; | |
492a3893 | 378 | #if OPENSSL_VERSION_NUMBER > 0x0090600fL |
70fc1609 | 379 | ssh1_3des.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH; |
492a3893 | 380 | #endif |
70fc1609 | 381 | return (&ssh1_3des); |
382 | } | |
383 | ||
384 | /* | |
385 | * SSH1 uses a variation on Blowfish, all bytes must be swapped before | |
386 | * and after encryption/decryption. Thus the swap_bytes stuff (yuk). | |
387 | */ | |
388 | static void | |
389 | swap_bytes(const u_char *src, u_char *dst, int n) | |
390 | { | |
391 | u_char c[4]; | |
392 | ||
393 | /* Process 4 bytes every lap. */ | |
394 | for (n = n / 4; n > 0; n--) { | |
395 | c[3] = *src++; | |
396 | c[2] = *src++; | |
397 | c[1] = *src++; | |
398 | c[0] = *src++; | |
399 | ||
400 | *dst++ = c[0]; | |
401 | *dst++ = c[1]; | |
402 | *dst++ = c[2]; | |
403 | *dst++ = c[3]; | |
404 | } | |
405 | } | |
406 | static int (*orig_bf)(EVP_CIPHER_CTX *, u_char *, const u_char *, u_int) = NULL; | |
407 | static int | |
408 | bf_ssh1_cipher(EVP_CIPHER_CTX *ctx, u_char *out, const u_char *in, u_int len) | |
409 | { | |
410 | int ret; | |
411 | ||
412 | swap_bytes(in, out, len); | |
413 | ret = (*orig_bf)(ctx, out, out, len); | |
414 | swap_bytes(out, out, len); | |
415 | return (ret); | |
416 | } | |
417 | static EVP_CIPHER * | |
418 | evp_ssh1_bf(void) | |
419 | { | |
420 | static EVP_CIPHER ssh1_bf; | |
421 | ||
422 | memcpy(&ssh1_bf, EVP_bf_cbc(), sizeof(EVP_CIPHER)); | |
423 | orig_bf = ssh1_bf.do_cipher; | |
424 | ssh1_bf.nid = NID_undef; | |
425 | ssh1_bf.do_cipher = bf_ssh1_cipher; | |
426 | ssh1_bf.key_len = 32; | |
427 | return (&ssh1_bf); | |
428 | } | |
429 | ||
430 | /* RIJNDAEL */ | |
431 | #define RIJNDAEL_BLOCKSIZE 16 | |
432 | struct ssh_rijndael_ctx | |
433 | { | |
434 | rijndael_ctx r_ctx; | |
435 | u_char r_iv[RIJNDAEL_BLOCKSIZE]; | |
436 | }; | |
437 | ||
438 | static int | |
439 | ssh_rijndael_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv, | |
440 | int enc) | |
441 | { | |
442 | struct ssh_rijndael_ctx *c; | |
443 | ||
444 | if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) { | |
445 | c = xmalloc(sizeof(*c)); | |
446 | EVP_CIPHER_CTX_set_app_data(ctx, c); | |
447 | } | |
448 | if (key != NULL) { | |
449 | if (enc == -1) | |
450 | enc = ctx->encrypt; | |
451 | rijndael_set_key(&c->r_ctx, (u_char *)key, | |
452 | 8*EVP_CIPHER_CTX_key_length(ctx), enc); | |
453 | } | |
454 | if (iv != NULL) | |
455 | memcpy(c->r_iv, iv, RIJNDAEL_BLOCKSIZE); | |
456 | return (1); | |
457 | } | |
458 | static int | |
459 | ssh_rijndael_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src, | |
460 | u_int len) | |
461 | { | |
462 | struct ssh_rijndael_ctx *c; | |
463 | u_char buf[RIJNDAEL_BLOCKSIZE]; | |
464 | u_char *cprev, *cnow, *plain, *ivp; | |
465 | int i, j, blocks = len / RIJNDAEL_BLOCKSIZE; | |
466 | ||
467 | if (len == 0) | |
468 | return (1); | |
469 | if (len % RIJNDAEL_BLOCKSIZE) | |
470 | fatal("ssh_rijndael_cbc: bad len %d", len); | |
471 | if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) { | |
472 | error("ssh_rijndael_cbc: no context"); | |
473 | return (0); | |
474 | } | |
475 | if (ctx->encrypt) { | |
476 | cnow = dest; | |
477 | plain = (u_char *)src; | |
478 | cprev = c->r_iv; | |
479 | for (i = 0; i < blocks; i++, plain+=RIJNDAEL_BLOCKSIZE, | |
480 | cnow+=RIJNDAEL_BLOCKSIZE) { | |
481 | for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++) | |
482 | buf[j] = plain[j] ^ cprev[j]; | |
483 | rijndael_encrypt(&c->r_ctx, buf, cnow); | |
484 | cprev = cnow; | |
485 | } | |
486 | memcpy(c->r_iv, cprev, RIJNDAEL_BLOCKSIZE); | |
487 | } else { | |
488 | cnow = (u_char *) (src+len-RIJNDAEL_BLOCKSIZE); | |
489 | plain = dest+len-RIJNDAEL_BLOCKSIZE; | |
490 | ||
491 | memcpy(buf, cnow, RIJNDAEL_BLOCKSIZE); | |
492 | for (i = blocks; i > 0; i--, cnow-=RIJNDAEL_BLOCKSIZE, | |
493 | plain-=RIJNDAEL_BLOCKSIZE) { | |
494 | rijndael_decrypt(&c->r_ctx, cnow, plain); | |
495 | ivp = (i == 1) ? c->r_iv : cnow-RIJNDAEL_BLOCKSIZE; | |
496 | for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++) | |
497 | plain[j] ^= ivp[j]; | |
498 | } | |
499 | memcpy(c->r_iv, buf, RIJNDAEL_BLOCKSIZE); | |
500 | } | |
501 | return (1); | |
502 | } | |
503 | static int | |
504 | ssh_rijndael_cleanup(EVP_CIPHER_CTX *ctx) | |
505 | { | |
506 | struct ssh_rijndael_ctx *c; | |
507 | ||
508 | if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) { | |
509 | memset(c, 0, sizeof(*c)); | |
510 | xfree(c); | |
511 | EVP_CIPHER_CTX_set_app_data(ctx, NULL); | |
512 | } | |
513 | return (1); | |
514 | } | |
515 | static EVP_CIPHER * | |
516 | evp_rijndael(void) | |
517 | { | |
518 | static EVP_CIPHER rijndal_cbc; | |
519 | ||
520 | memset(&rijndal_cbc, 0, sizeof(EVP_CIPHER)); | |
521 | rijndal_cbc.nid = NID_undef; | |
522 | rijndal_cbc.block_size = RIJNDAEL_BLOCKSIZE; | |
523 | rijndal_cbc.iv_len = RIJNDAEL_BLOCKSIZE; | |
524 | rijndal_cbc.key_len = 16; | |
525 | rijndal_cbc.init = ssh_rijndael_init; | |
526 | rijndal_cbc.cleanup = ssh_rijndael_cleanup; | |
527 | rijndal_cbc.do_cipher = ssh_rijndael_cbc; | |
492a3893 | 528 | #if OPENSSL_VERSION_NUMBER > 0x0090600fL |
70fc1609 | 529 | rijndal_cbc.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | |
530 | EVP_CIPH_ALWAYS_CALL_INIT; | |
492a3893 | 531 | #endif |
70fc1609 | 532 | return (&rijndal_cbc); |
533 | } |