blob: ce3f6f3ce2088306a9813d6e88c2357e0485f382 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10005 *
Damien Millere4340be2000-09-16 13:29:08 +11006 * 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".
Damien Miller4af51302000-04-16 11:18:38 +100011 *
Damien Millere4340be2000-09-16 13:29:08 +110012 *
13 * Copyright (c) 1999 Niels Provos. All rights reserved.
Ben Lindstrom44697232001-07-04 03:32:30 +000014 * Copyright (c) 1999, 2000 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110015 *
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.
Damien Miller95def091999-11-25 00:26:21 +110035 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036
37#include "includes.h"
Damien Miller21cf4e02002-02-19 15:26:42 +110038RCSID("$OpenBSD: cipher.c,v 1.52 2002/02/18 13:05:32 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039
Damien Miller78928792000-04-12 20:17:38 +100040#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000041#include "log.h"
42#include "cipher.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043
44#include <openssl/md5.h>
Damien Miller21cf4e02002-02-19 15:26:42 +110045#include "rijndael.h"
46
47static EVP_CIPHER *evp_ssh1_3des(void);
48static EVP_CIPHER *evp_ssh1_bf(void);
49static EVP_CIPHER *evp_rijndael(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050
Damien Miller963f6b22002-02-19 15:21:23 +110051struct Cipher {
52 char *name;
53 int number; /* for ssh1 only */
54 u_int block_size;
55 u_int key_len;
Damien Miller21cf4e02002-02-19 15:26:42 +110056 EVP_CIPHER *(*evptype)(void);
57} ciphers[] = {
58 { "none", SSH_CIPHER_NONE, 8, 0, EVP_enc_null },
59 { "des", SSH_CIPHER_DES, 8, 8, EVP_des_cbc },
60 { "3des", SSH_CIPHER_3DES, 8, 16, evp_ssh1_3des },
61 { "blowfish", SSH_CIPHER_BLOWFISH, 8, 32, evp_ssh1_bf },
Damien Miller963f6b22002-02-19 15:21:23 +110062
Damien Miller21cf4e02002-02-19 15:26:42 +110063 { "3des-cbc", SSH_CIPHER_SSH2, 8, 24, EVP_des_ede3_cbc },
64 { "blowfish-cbc", SSH_CIPHER_SSH2, 8, 16, EVP_bf_cbc },
65 { "cast128-cbc", SSH_CIPHER_SSH2, 8, 16, EVP_cast5_cbc },
66 { "arcfour", SSH_CIPHER_SSH2, 8, 16, EVP_rc4 },
67 { "aes128-cbc", SSH_CIPHER_SSH2, 16, 16, evp_rijndael },
68 { "aes192-cbc", SSH_CIPHER_SSH2, 16, 24, evp_rijndael },
69 { "aes256-cbc", SSH_CIPHER_SSH2, 16, 32, evp_rijndael },
Damien Miller874d77b2000-10-14 16:23:11 +110070
Damien Miller21cf4e02002-02-19 15:26:42 +110071 { NULL, SSH_CIPHER_ILLEGAL, 0, 0, NULL }
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072};
73
Damien Miller874d77b2000-10-14 16:23:11 +110074/*--*/
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075
Damien Miller21cf4e02002-02-19 15:26:42 +110076u_int
Damien Miller963f6b22002-02-19 15:21:23 +110077cipher_blocksize(Cipher *c)
78{
79 return (c->block_size);
80}
Damien Miller21cf4e02002-02-19 15:26:42 +110081u_int
Damien Miller963f6b22002-02-19 15:21:23 +110082cipher_keylen(Cipher *c)
83{
84 return (c->key_len);
85}
86
Ben Lindstrom46c16222000-12-22 01:43:59 +000087u_int
Damien Miller874d77b2000-10-14 16:23:11 +110088cipher_mask_ssh1(int client)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089{
Ben Lindstrom46c16222000-12-22 01:43:59 +000090 u_int mask = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +110091 mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */
Damien Miller95def091999-11-25 00:26:21 +110092 mask |= 1 << SSH_CIPHER_BLOWFISH;
Damien Miller874d77b2000-10-14 16:23:11 +110093 if (client) {
94 mask |= 1 << SSH_CIPHER_DES;
95 }
Damien Miller1383bd82000-04-06 12:32:37 +100096 return mask;
97}
Damien Miller874d77b2000-10-14 16:23:11 +110098
99Cipher *
100cipher_by_name(const char *name)
Damien Miller1383bd82000-04-06 12:32:37 +1000101{
Damien Miller874d77b2000-10-14 16:23:11 +1100102 Cipher *c;
103 for (c = ciphers; c->name != NULL; c++)
104 if (strcasecmp(c->name, name) == 0)
105 return c;
106 return NULL;
Damien Miller1383bd82000-04-06 12:32:37 +1000107}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108
Damien Miller874d77b2000-10-14 16:23:11 +1100109Cipher *
110cipher_by_number(int id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111{
Damien Miller874d77b2000-10-14 16:23:11 +1100112 Cipher *c;
113 for (c = ciphers; c->name != NULL; c++)
114 if (c->number == id)
115 return c;
116 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117}
118
Damien Miller78928792000-04-12 20:17:38 +1000119#define CIPHER_SEP ","
120int
121ciphers_valid(const char *names)
122{
Damien Miller874d77b2000-10-14 16:23:11 +1100123 Cipher *c;
Damien Miller37023962000-07-11 17:31:38 +1000124 char *ciphers, *cp;
Damien Miller78928792000-04-12 20:17:38 +1000125 char *p;
Damien Miller78928792000-04-12 20:17:38 +1000126
Damien Millerb1715dc2000-05-30 13:44:51 +1000127 if (names == NULL || strcmp(names, "") == 0)
Damien Miller78928792000-04-12 20:17:38 +1000128 return 0;
Damien Miller37023962000-07-11 17:31:38 +1000129 ciphers = cp = xstrdup(names);
Damien Miller874d77b2000-10-14 16:23:11 +1100130 for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +1100131 (p = strsep(&cp, CIPHER_SEP))) {
Damien Miller874d77b2000-10-14 16:23:11 +1100132 c = cipher_by_name(p);
133 if (c == NULL || c->number != SSH_CIPHER_SSH2) {
134 debug("bad cipher %s [%s]", p, names);
Damien Miller78928792000-04-12 20:17:38 +1000135 xfree(ciphers);
136 return 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100137 } else {
Damien Miller50a41ed2000-10-16 12:14:42 +1100138 debug3("cipher ok: %s [%s]", p, names);
Damien Miller78928792000-04-12 20:17:38 +1000139 }
140 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100141 debug3("ciphers ok: [%s]", names);
Damien Miller78928792000-04-12 20:17:38 +1000142 xfree(ciphers);
143 return 1;
144}
145
Damien Miller5428f641999-11-25 11:54:57 +1100146/*
147 * Parses the name of the cipher. Returns the number of the corresponding
148 * cipher, or -1 on error.
149 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150
151int
152cipher_number(const char *name)
153{
Damien Miller874d77b2000-10-14 16:23:11 +1100154 Cipher *c;
Damien Millerb1715dc2000-05-30 13:44:51 +1000155 if (name == NULL)
156 return -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100157 c = cipher_by_name(name);
158 return (c==NULL) ? -1 : c->number;
159}
160
161char *
162cipher_name(int id)
163{
164 Cipher *c = cipher_by_number(id);
165 return (c==NULL) ? "<unknown>" : c->name;
166}
167
168void
Damien Miller21cf4e02002-02-19 15:26:42 +1100169cipher_init(CipherContext *cc, Cipher *cipher,
170 const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
171 int encrypt)
Damien Miller874d77b2000-10-14 16:23:11 +1100172{
Damien Miller21cf4e02002-02-19 15:26:42 +1100173 static int dowarn = 1;
174 const EVP_CIPHER *type;
175 int klen;
176
177 if (cipher->number == SSH_CIPHER_DES) {
178 if (dowarn) {
179 error("Warning: use of DES is strongly discouraged "
180 "due to cryptographic weaknesses");
181 dowarn = 0;
182 }
183 if (keylen > 8)
184 keylen = 8;
185 }
186 cc->plaintext = (cipher->number == SSH_CIPHER_NONE);
187
Damien Miller874d77b2000-10-14 16:23:11 +1100188 if (keylen < cipher->key_len)
189 fatal("cipher_init: key length %d is insufficient for %s.",
190 keylen, cipher->name);
191 if (iv != NULL && ivlen < cipher->block_size)
192 fatal("cipher_init: iv length %d is insufficient for %s.",
193 ivlen, cipher->name);
194 cc->cipher = cipher;
Damien Miller21cf4e02002-02-19 15:26:42 +1100195
196 type = (*cipher->evptype)();
197
198 EVP_CIPHER_CTX_init(&cc->evp);
199 if (EVP_CipherInit(&cc->evp, type, NULL, (u_char *)iv,
200 (encrypt == CIPHER_ENCRYPT)) == 0)
201 fatal("cipher_init: EVP_CipherInit failed for %s",
202 cipher->name);
203 klen = EVP_CIPHER_CTX_key_length(&cc->evp);
204 if (klen > 0 && keylen != klen) {
205 debug("cipher_init: set keylen (%d -> %d)", klen, keylen);
206 if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0)
207 fatal("cipher_init: set keylen failed (%d -> %d)",
208 klen, keylen);
209 }
210 if (EVP_CipherInit(&cc->evp, NULL, (u_char *)key, NULL, -1) == 0)
211 fatal("cipher_init: EVP_CipherInit: set key failed for %s",
212 cipher->name);
Damien Miller874d77b2000-10-14 16:23:11 +1100213}
214
215void
Damien Miller963f6b22002-02-19 15:21:23 +1100216cipher_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
Damien Miller874d77b2000-10-14 16:23:11 +1100217{
218 if (len % cc->cipher->block_size)
219 fatal("cipher_encrypt: bad plaintext length %d", len);
Damien Miller21cf4e02002-02-19 15:26:42 +1100220 if (EVP_Cipher(&cc->evp, dest, (u_char *)src, len) == 0)
221 fatal("evp_crypt: EVP_Cipher failed");
Damien Miller874d77b2000-10-14 16:23:11 +1100222}
223
224void
Damien Miller963f6b22002-02-19 15:21:23 +1100225cipher_cleanup(CipherContext *cc)
Damien Miller874d77b2000-10-14 16:23:11 +1100226{
Damien Miller21cf4e02002-02-19 15:26:42 +1100227 if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0)
228 error("cipher_cleanup: EVP_CIPHER_CTX_cleanup failed");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000229}
230
Damien Miller5428f641999-11-25 11:54:57 +1100231/*
232 * Selects the cipher, and keys if by computing the MD5 checksum of the
233 * passphrase and using the resulting 16 bytes as the key.
234 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000235
Damien Miller4af51302000-04-16 11:18:38 +1000236void
Damien Miller874d77b2000-10-14 16:23:11 +1100237cipher_set_key_string(CipherContext *cc, Cipher *cipher,
Damien Miller963f6b22002-02-19 15:21:23 +1100238 const char *passphrase, int encrypt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239{
Damien Miller95def091999-11-25 00:26:21 +1100240 MD5_CTX md;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000241 u_char digest[16];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242
Damien Miller95def091999-11-25 00:26:21 +1100243 MD5_Init(&md);
Damien Miller874d77b2000-10-14 16:23:11 +1100244 MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
Damien Miller95def091999-11-25 00:26:21 +1100245 MD5_Final(digest, &md);
246
Damien Miller963f6b22002-02-19 15:21:23 +1100247 cipher_init(cc, cipher, digest, 16, NULL, 0, encrypt);
Damien Miller95def091999-11-25 00:26:21 +1100248
249 memset(digest, 0, sizeof(digest));
250 memset(&md, 0, sizeof(md));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251}
Damien Miller21cf4e02002-02-19 15:26:42 +1100252
253/* Implementations for other non-EVP ciphers */
254
255/*
256 * This is used by SSH1:
257 *
258 * What kind of triple DES are these 2 routines?
259 *
260 * Why is there a redundant initialization vector?
261 *
262 * If only iv3 was used, then, this would till effect have been
263 * outer-cbc. However, there is also a private iv1 == iv2 which
264 * perhaps makes differential analysis easier. On the other hand, the
265 * private iv1 probably makes the CRC-32 attack ineffective. This is a
266 * result of that there is no longer any known iv1 to use when
267 * choosing the X block.
268 */
269struct ssh1_3des_ctx
270{
271 EVP_CIPHER_CTX k1, k2, k3;
272};
273static int
274ssh1_3des_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
275 int enc)
276{
277 struct ssh1_3des_ctx *c;
278 u_char *k1, *k2, *k3;
279
280 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
281 c = xmalloc(sizeof(*c));
282 EVP_CIPHER_CTX_set_app_data(ctx, c);
283 }
284 if (key == NULL)
285 return (1);
286 if (enc == -1)
287 enc = ctx->encrypt;
288 k1 = k2 = k3 = (u_char *) key;
289 k2 += 8;
290 if (EVP_CIPHER_CTX_key_length(ctx) >= 16+8) {
291 if (enc)
292 k3 += 16;
293 else
294 k1 += 16;
295 }
296 EVP_CIPHER_CTX_init(&c->k1);
297 EVP_CIPHER_CTX_init(&c->k2);
298 EVP_CIPHER_CTX_init(&c->k3);
299 if (EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc) == 0 ||
300 EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc) == 0 ||
301 EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc) == 0) {
302 memset(c, 0, sizeof(*c));
303 xfree(c);
304 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
305 return (0);
306 }
307 return (1);
308}
309static int
310ssh1_3des_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src, u_int len)
311{
312 struct ssh1_3des_ctx *c;
313
314 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
315 error("ssh1_3des_cbc: no context");
316 return (0);
317 }
318 if (EVP_Cipher(&c->k1, dest, (u_char *)src, len) == 0 ||
319 EVP_Cipher(&c->k2, dest, dest, len) == 0 ||
320 EVP_Cipher(&c->k3, dest, dest, len) == 0)
321 return (0);
322 return (1);
323}
324static int
325ssh1_3des_cleanup(EVP_CIPHER_CTX *ctx)
326{
327 struct ssh1_3des_ctx *c;
328
329 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
330 memset(c, 0, sizeof(*c));
331 xfree(c);
332 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
333 }
334 return (1);
335}
336static EVP_CIPHER *
337evp_ssh1_3des(void)
338{
339 static EVP_CIPHER ssh1_3des;
340
341 memset(&ssh1_3des, 0, sizeof(EVP_CIPHER));
342 ssh1_3des.nid = NID_undef;
343 ssh1_3des.block_size = 8;
344 ssh1_3des.iv_len = 0;
345 ssh1_3des.key_len = 16;
346 ssh1_3des.init = ssh1_3des_init;
347 ssh1_3des.cleanup = ssh1_3des_cleanup;
348 ssh1_3des.do_cipher = ssh1_3des_cbc;
349 ssh1_3des.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH;
350 return (&ssh1_3des);
351}
352
353/*
354 * SSH1 uses a variation on Blowfish, all bytes must be swapped before
355 * and after encryption/decryption. Thus the swap_bytes stuff (yuk).
356 */
357static void
358swap_bytes(const u_char *src, u_char *dst, int n)
359{
360 u_char c[4];
361
362 /* Process 4 bytes every lap. */
363 for (n = n / 4; n > 0; n--) {
364 c[3] = *src++;
365 c[2] = *src++;
366 c[1] = *src++;
367 c[0] = *src++;
368
369 *dst++ = c[0];
370 *dst++ = c[1];
371 *dst++ = c[2];
372 *dst++ = c[3];
373 }
374}
375static int (*orig_bf)(EVP_CIPHER_CTX *, u_char *, const u_char *, u_int) = NULL;
376static int
377bf_ssh1_cipher(EVP_CIPHER_CTX *ctx, u_char *out, const u_char *in, u_int len)
378{
379 int ret;
380
381 swap_bytes(in, out, len);
382 ret = (*orig_bf)(ctx, out, out, len);
383 swap_bytes(out, out, len);
384 return (ret);
385}
386static EVP_CIPHER *
387evp_ssh1_bf(void)
388{
389 static EVP_CIPHER ssh1_bf;
390
391 memcpy(&ssh1_bf, EVP_bf_cbc(), sizeof(EVP_CIPHER));
392 orig_bf = ssh1_bf.do_cipher;
393 ssh1_bf.nid = NID_undef;
394 ssh1_bf.do_cipher = bf_ssh1_cipher;
395 ssh1_bf.key_len = 32;
396 return (&ssh1_bf);
397}
398
399/* RIJNDAEL */
400#define RIJNDAEL_BLOCKSIZE 16
401struct ssh_rijndael_ctx
402{
403 rijndael_ctx r_ctx;
404 u_char r_iv[RIJNDAEL_BLOCKSIZE];
405};
406
407static int
408ssh_rijndael_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
409 int enc)
410{
411 struct ssh_rijndael_ctx *c;
412
413 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
414 c = xmalloc(sizeof(*c));
415 EVP_CIPHER_CTX_set_app_data(ctx, c);
416 }
417 if (key != NULL) {
418 if (enc == -1)
419 enc = ctx->encrypt;
420 rijndael_set_key(&c->r_ctx, (u_char *)key,
421 8*EVP_CIPHER_CTX_key_length(ctx), enc);
422 }
423 if (iv != NULL)
424 memcpy(c->r_iv, iv, RIJNDAEL_BLOCKSIZE);
425 return (1);
426}
427static int
428ssh_rijndael_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src,
429 u_int len)
430{
431 struct ssh_rijndael_ctx *c;
432 u_char buf[RIJNDAEL_BLOCKSIZE];
433 u_char *cprev, *cnow, *plain, *ivp;
434 int i, j, blocks = len / RIJNDAEL_BLOCKSIZE;
435
436 if (len == 0)
437 return (1);
438 if (len % RIJNDAEL_BLOCKSIZE)
439 fatal("ssh_rijndael_cbc: bad len %d", len);
440 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
441 error("ssh_rijndael_cbc: no context");
442 return (0);
443 }
444 if (ctx->encrypt) {
445 cnow = dest;
446 plain = (u_char *)src;
447 cprev = c->r_iv;
448 for (i = 0; i < blocks; i++, plain+=RIJNDAEL_BLOCKSIZE,
449 cnow+=RIJNDAEL_BLOCKSIZE) {
450 for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++)
451 buf[j] = plain[j] ^ cprev[j];
452 rijndael_encrypt(&c->r_ctx, buf, cnow);
453 cprev = cnow;
454 }
455 memcpy(c->r_iv, cprev, RIJNDAEL_BLOCKSIZE);
456 } else {
457 cnow = (u_char *) (src+len-RIJNDAEL_BLOCKSIZE);
458 plain = dest+len-RIJNDAEL_BLOCKSIZE;
459
460 memcpy(buf, cnow, RIJNDAEL_BLOCKSIZE);
461 for (i = blocks; i > 0; i--, cnow-=RIJNDAEL_BLOCKSIZE,
462 plain-=RIJNDAEL_BLOCKSIZE) {
463 rijndael_decrypt(&c->r_ctx, cnow, plain);
464 ivp = (i == 1) ? c->r_iv : cnow-RIJNDAEL_BLOCKSIZE;
465 for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++)
466 plain[j] ^= ivp[j];
467 }
468 memcpy(c->r_iv, buf, RIJNDAEL_BLOCKSIZE);
469 }
470 return (1);
471}
472static int
473ssh_rijndael_cleanup(EVP_CIPHER_CTX *ctx)
474{
475 struct ssh_rijndael_ctx *c;
476
477 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
478 memset(c, 0, sizeof(*c));
479 xfree(c);
480 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
481 }
482 return (1);
483}
484static EVP_CIPHER *
485evp_rijndael(void)
486{
487 static EVP_CIPHER rijndal_cbc;
488
489 memset(&rijndal_cbc, 0, sizeof(EVP_CIPHER));
490 rijndal_cbc.nid = NID_undef;
491 rijndal_cbc.block_size = RIJNDAEL_BLOCKSIZE;
492 rijndal_cbc.iv_len = RIJNDAEL_BLOCKSIZE;
493 rijndal_cbc.key_len = 16;
494 rijndal_cbc.init = ssh_rijndael_init;
495 rijndal_cbc.cleanup = ssh_rijndael_cleanup;
496 rijndal_cbc.do_cipher = ssh_rijndael_cbc;
497 rijndal_cbc.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH |
498 EVP_CIPH_ALWAYS_CALL_INIT;
499 return (&rijndal_cbc);
500}