]> andersk Git - openssh.git/blame - cipher.c
- markus@cvs.openbsd.org 2002/06/04 23:02:06
[openssh.git] / cipher.c
CommitLineData
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"
135247df 38RCSID("$OpenBSD: cipher.c,v 1.57 2002/05/30 08:07:31 markus Exp $");
8efc0c15 39
a8be9f80 40#include "xmalloc.h"
42f11eb2 41#include "log.h"
42#include "cipher.h"
8efc0c15 43
44#include <openssl/md5.h>
70fc1609 45
a068d86f 46#if OPENSSL_VERSION_NUMBER < 0x00906000L
47#define SSH_OLD_EVP
48#define EVP_CIPHER_CTX_get_app_data(e) ((e)->app_data)
49#endif
50
135247df 51#if OPENSSL_VERSION_NUMBER < 0x00907000L
52#include "rijndael.h"
53static const EVP_CIPHER *evp_rijndael(void);
54#endif
7b5edc2b 55static const EVP_CIPHER *evp_ssh1_3des(void);
56static const EVP_CIPHER *evp_ssh1_bf(void);
8efc0c15 57
3ee832e5 58struct Cipher {
59 char *name;
60 int number; /* for ssh1 only */
61 u_int block_size;
62 u_int key_len;
7b5edc2b 63 const EVP_CIPHER *(*evptype)(void);
70fc1609 64} ciphers[] = {
65 { "none", SSH_CIPHER_NONE, 8, 0, EVP_enc_null },
66 { "des", SSH_CIPHER_DES, 8, 8, EVP_des_cbc },
67 { "3des", SSH_CIPHER_3DES, 8, 16, evp_ssh1_3des },
68 { "blowfish", SSH_CIPHER_BLOWFISH, 8, 32, evp_ssh1_bf },
69
70 { "3des-cbc", SSH_CIPHER_SSH2, 8, 24, EVP_des_ede3_cbc },
71 { "blowfish-cbc", SSH_CIPHER_SSH2, 8, 16, EVP_bf_cbc },
72 { "cast128-cbc", SSH_CIPHER_SSH2, 8, 16, EVP_cast5_cbc },
73 { "arcfour", SSH_CIPHER_SSH2, 8, 16, EVP_rc4 },
135247df 74#if OPENSSL_VERSION_NUMBER < 0x00907000L
70fc1609 75 { "aes128-cbc", SSH_CIPHER_SSH2, 16, 16, evp_rijndael },
76 { "aes192-cbc", SSH_CIPHER_SSH2, 16, 24, evp_rijndael },
77 { "aes256-cbc", SSH_CIPHER_SSH2, 16, 32, evp_rijndael },
bf03f2da 78 { "rijndael-cbc@lysator.liu.se",
79 SSH_CIPHER_SSH2, 16, 32, evp_rijndael },
135247df 80#else
81 { "aes128-cbc", SSH_CIPHER_SSH2, 16, 16, EVP_aes_128_cbc },
82 { "aes192-cbc", SSH_CIPHER_SSH2, 16, 24, EVP_aes_192_cbc },
83 { "aes256-cbc", SSH_CIPHER_SSH2, 16, 32, EVP_aes_256_cbc },
84 { "rijndael-cbc@lysator.liu.se",
85 SSH_CIPHER_SSH2, 16, 32, EVP_aes_256_cbc },
86#endif
70fc1609 87
88 { NULL, SSH_CIPHER_ILLEGAL, 0, 0, NULL }
94ec8c6b 89};
90
91/*--*/
92
762715ce 93u_int
3ee832e5 94cipher_blocksize(Cipher *c)
95{
96 return (c->block_size);
97}
762715ce 98u_int
3ee832e5 99cipher_keylen(Cipher *c)
100{
101 return (c->key_len);
102}
762715ce 103u_int
bf8269a9 104cipher_get_number(Cipher *c)
105{
106 return (c->number);
107}
3ee832e5 108
1e3b8b07 109u_int
94ec8c6b 110cipher_mask_ssh1(int client)
8ce64345 111{
1e3b8b07 112 u_int mask = 0;
184eed6a 113 mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */
94ec8c6b 114 mask |= 1 << SSH_CIPHER_BLOWFISH;
115 if (client) {
116 mask |= 1 << SSH_CIPHER_DES;
117 }
5260325f 118 return mask;
8efc0c15 119}
94ec8c6b 120
121Cipher *
122cipher_by_name(const char *name)
8ce64345 123{
94ec8c6b 124 Cipher *c;
125 for (c = ciphers; c->name != NULL; c++)
126 if (strcasecmp(c->name, name) == 0)
127 return c;
128 return NULL;
8ce64345 129}
8efc0c15 130
94ec8c6b 131Cipher *
132cipher_by_number(int id)
8efc0c15 133{
94ec8c6b 134 Cipher *c;
135 for (c = ciphers; c->name != NULL; c++)
136 if (c->number == id)
137 return c;
138 return NULL;
8efc0c15 139}
140
a8be9f80 141#define CIPHER_SEP ","
142int
143ciphers_valid(const char *names)
144{
94ec8c6b 145 Cipher *c;
089fbbd2 146 char *ciphers, *cp;
a8be9f80 147 char *p;
a8be9f80 148
71276795 149 if (names == NULL || strcmp(names, "") == 0)
a8be9f80 150 return 0;
089fbbd2 151 ciphers = cp = xstrdup(names);
94ec8c6b 152 for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
184eed6a 153 (p = strsep(&cp, CIPHER_SEP))) {
94ec8c6b 154 c = cipher_by_name(p);
155 if (c == NULL || c->number != SSH_CIPHER_SSH2) {
156 debug("bad cipher %s [%s]", p, names);
a8be9f80 157 xfree(ciphers);
158 return 0;
94ec8c6b 159 } else {
33de75a3 160 debug3("cipher ok: %s [%s]", p, names);
a8be9f80 161 }
162 }
33de75a3 163 debug3("ciphers ok: [%s]", names);
a8be9f80 164 xfree(ciphers);
165 return 1;
166}
167
aa3378df 168/*
169 * Parses the name of the cipher. Returns the number of the corresponding
170 * cipher, or -1 on error.
171 */
8efc0c15 172
173int
174cipher_number(const char *name)
175{
94ec8c6b 176 Cipher *c;
71276795 177 if (name == NULL)
178 return -1;
94ec8c6b 179 c = cipher_by_name(name);
180 return (c==NULL) ? -1 : c->number;
181}
182
183char *
184cipher_name(int id)
185{
186 Cipher *c = cipher_by_number(id);
187 return (c==NULL) ? "<unknown>" : c->name;
188}
189
190void
70fc1609 191cipher_init(CipherContext *cc, Cipher *cipher,
192 const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
193 int encrypt)
94ec8c6b 194{
70fc1609 195 static int dowarn = 1;
a068d86f 196#ifdef SSH_OLD_EVP
197 EVP_CIPHER *type;
198#else
70fc1609 199 const EVP_CIPHER *type;
a068d86f 200#endif
70fc1609 201 int klen;
202
203 if (cipher->number == SSH_CIPHER_DES) {
204 if (dowarn) {
205 error("Warning: use of DES is strongly discouraged "
206 "due to cryptographic weaknesses");
207 dowarn = 0;
208 }
209 if (keylen > 8)
210 keylen = 8;
211 }
212 cc->plaintext = (cipher->number == SSH_CIPHER_NONE);
213
94ec8c6b 214 if (keylen < cipher->key_len)
215 fatal("cipher_init: key length %d is insufficient for %s.",
216 keylen, cipher->name);
217 if (iv != NULL && ivlen < cipher->block_size)
218 fatal("cipher_init: iv length %d is insufficient for %s.",
219 ivlen, cipher->name);
220 cc->cipher = cipher;
70fc1609 221
222 type = (*cipher->evptype)();
223
224 EVP_CIPHER_CTX_init(&cc->evp);
a068d86f 225#ifdef SSH_OLD_EVP
226 if (type->key_len > 0 && type->key_len != keylen) {
227 debug("cipher_init: set keylen (%d -> %d)",
228 type->key_len, keylen);
229 type->key_len = keylen;
230 }
231 EVP_CipherInit(&cc->evp, type, (u_char *)key, (u_char *)iv,
232 (encrypt == CIPHER_ENCRYPT));
233#else
70fc1609 234 if (EVP_CipherInit(&cc->evp, type, NULL, (u_char *)iv,
235 (encrypt == CIPHER_ENCRYPT)) == 0)
236 fatal("cipher_init: EVP_CipherInit failed for %s",
237 cipher->name);
238 klen = EVP_CIPHER_CTX_key_length(&cc->evp);
239 if (klen > 0 && keylen != klen) {
240 debug("cipher_init: set keylen (%d -> %d)", klen, keylen);
241 if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0)
242 fatal("cipher_init: set keylen failed (%d -> %d)",
243 klen, keylen);
244 }
245 if (EVP_CipherInit(&cc->evp, NULL, (u_char *)key, NULL, -1) == 0)
246 fatal("cipher_init: EVP_CipherInit: set key failed for %s",
247 cipher->name);
a068d86f 248#endif
94ec8c6b 249}
250
251void
3ee832e5 252cipher_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
94ec8c6b 253{
254 if (len % cc->cipher->block_size)
255 fatal("cipher_encrypt: bad plaintext length %d", len);
a068d86f 256#ifdef SSH_OLD_EVP
257 EVP_Cipher(&cc->evp, dest, (u_char *)src, len);
258#else
70fc1609 259 if (EVP_Cipher(&cc->evp, dest, (u_char *)src, len) == 0)
260 fatal("evp_crypt: EVP_Cipher failed");
a068d86f 261#endif
94ec8c6b 262}
263
264void
3ee832e5 265cipher_cleanup(CipherContext *cc)
94ec8c6b 266{
a068d86f 267#ifdef SSH_OLD_EVP
268 EVP_CIPHER_CTX_cleanup(&cc->evp);
269#else
70fc1609 270 if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0)
271 error("cipher_cleanup: EVP_CIPHER_CTX_cleanup failed");
a068d86f 272#endif
8efc0c15 273}
274
aa3378df 275/*
276 * Selects the cipher, and keys if by computing the MD5 checksum of the
277 * passphrase and using the resulting 16 bytes as the key.
278 */
8efc0c15 279
6ae2364d 280void
94ec8c6b 281cipher_set_key_string(CipherContext *cc, Cipher *cipher,
3ee832e5 282 const char *passphrase, int encrypt)
8efc0c15 283{
5260325f 284 MD5_CTX md;
1e3b8b07 285 u_char digest[16];
5260325f 286
287 MD5_Init(&md);
94ec8c6b 288 MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
5260325f 289 MD5_Final(digest, &md);
290
3ee832e5 291 cipher_init(cc, cipher, digest, 16, NULL, 0, encrypt);
5260325f 292
293 memset(digest, 0, sizeof(digest));
294 memset(&md, 0, sizeof(md));
8efc0c15 295}
70fc1609 296
297/* Implementations for other non-EVP ciphers */
298
299/*
300 * This is used by SSH1:
301 *
302 * What kind of triple DES are these 2 routines?
303 *
304 * Why is there a redundant initialization vector?
305 *
306 * If only iv3 was used, then, this would till effect have been
307 * outer-cbc. However, there is also a private iv1 == iv2 which
308 * perhaps makes differential analysis easier. On the other hand, the
309 * private iv1 probably makes the CRC-32 attack ineffective. This is a
310 * result of that there is no longer any known iv1 to use when
311 * choosing the X block.
312 */
313struct ssh1_3des_ctx
314{
315 EVP_CIPHER_CTX k1, k2, k3;
316};
317static int
318ssh1_3des_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
319 int enc)
320{
321 struct ssh1_3des_ctx *c;
322 u_char *k1, *k2, *k3;
323
324 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
325 c = xmalloc(sizeof(*c));
326 EVP_CIPHER_CTX_set_app_data(ctx, c);
327 }
328 if (key == NULL)
329 return (1);
330 if (enc == -1)
331 enc = ctx->encrypt;
332 k1 = k2 = k3 = (u_char *) key;
333 k2 += 8;
334 if (EVP_CIPHER_CTX_key_length(ctx) >= 16+8) {
335 if (enc)
336 k3 += 16;
337 else
338 k1 += 16;
339 }
340 EVP_CIPHER_CTX_init(&c->k1);
341 EVP_CIPHER_CTX_init(&c->k2);
342 EVP_CIPHER_CTX_init(&c->k3);
a068d86f 343#ifdef SSH_OLD_EVP
344 EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc);
345 EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc);
346 EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc);
347#else
70fc1609 348 if (EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc) == 0 ||
349 EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc) == 0 ||
350 EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc) == 0) {
351 memset(c, 0, sizeof(*c));
352 xfree(c);
353 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
354 return (0);
355 }
a068d86f 356#endif
70fc1609 357 return (1);
358}
359static int
360ssh1_3des_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src, u_int len)
361{
362 struct ssh1_3des_ctx *c;
363
364 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
365 error("ssh1_3des_cbc: no context");
366 return (0);
367 }
a068d86f 368#ifdef SSH_OLD_EVP
369 EVP_Cipher(&c->k1, dest, (u_char *)src, len);
370 EVP_Cipher(&c->k2, dest, dest, len);
371 EVP_Cipher(&c->k3, dest, dest, len);
372#else
70fc1609 373 if (EVP_Cipher(&c->k1, dest, (u_char *)src, len) == 0 ||
374 EVP_Cipher(&c->k2, dest, dest, len) == 0 ||
375 EVP_Cipher(&c->k3, dest, dest, len) == 0)
376 return (0);
a068d86f 377#endif
70fc1609 378 return (1);
379}
380static int
381ssh1_3des_cleanup(EVP_CIPHER_CTX *ctx)
382{
383 struct ssh1_3des_ctx *c;
384
385 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
386 memset(c, 0, sizeof(*c));
387 xfree(c);
388 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
389 }
390 return (1);
391}
7b5edc2b 392static const EVP_CIPHER *
70fc1609 393evp_ssh1_3des(void)
394{
395 static EVP_CIPHER ssh1_3des;
396
397 memset(&ssh1_3des, 0, sizeof(EVP_CIPHER));
398 ssh1_3des.nid = NID_undef;
399 ssh1_3des.block_size = 8;
400 ssh1_3des.iv_len = 0;
401 ssh1_3des.key_len = 16;
402 ssh1_3des.init = ssh1_3des_init;
403 ssh1_3des.cleanup = ssh1_3des_cleanup;
404 ssh1_3des.do_cipher = ssh1_3des_cbc;
a068d86f 405#ifndef SSH_OLD_EVP
70fc1609 406 ssh1_3des.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH;
a068d86f 407#endif
70fc1609 408 return (&ssh1_3des);
409}
410
411/*
412 * SSH1 uses a variation on Blowfish, all bytes must be swapped before
413 * and after encryption/decryption. Thus the swap_bytes stuff (yuk).
414 */
415static void
416swap_bytes(const u_char *src, u_char *dst, int n)
417{
418 u_char c[4];
419
420 /* Process 4 bytes every lap. */
421 for (n = n / 4; n > 0; n--) {
422 c[3] = *src++;
423 c[2] = *src++;
424 c[1] = *src++;
425 c[0] = *src++;
426
427 *dst++ = c[0];
428 *dst++ = c[1];
429 *dst++ = c[2];
430 *dst++ = c[3];
431 }
432}
433static int (*orig_bf)(EVP_CIPHER_CTX *, u_char *, const u_char *, u_int) = NULL;
434static int
435bf_ssh1_cipher(EVP_CIPHER_CTX *ctx, u_char *out, const u_char *in, u_int len)
436{
437 int ret;
438
439 swap_bytes(in, out, len);
440 ret = (*orig_bf)(ctx, out, out, len);
441 swap_bytes(out, out, len);
442 return (ret);
443}
7b5edc2b 444static const EVP_CIPHER *
70fc1609 445evp_ssh1_bf(void)
446{
447 static EVP_CIPHER ssh1_bf;
448
449 memcpy(&ssh1_bf, EVP_bf_cbc(), sizeof(EVP_CIPHER));
450 orig_bf = ssh1_bf.do_cipher;
451 ssh1_bf.nid = NID_undef;
452 ssh1_bf.do_cipher = bf_ssh1_cipher;
453 ssh1_bf.key_len = 32;
454 return (&ssh1_bf);
455}
456
135247df 457#if OPENSSL_VERSION_NUMBER < 0x00907000L
70fc1609 458/* RIJNDAEL */
459#define RIJNDAEL_BLOCKSIZE 16
460struct ssh_rijndael_ctx
461{
462 rijndael_ctx r_ctx;
463 u_char r_iv[RIJNDAEL_BLOCKSIZE];
464};
465
466static int
467ssh_rijndael_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
468 int enc)
469{
470 struct ssh_rijndael_ctx *c;
471
472 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
473 c = xmalloc(sizeof(*c));
474 EVP_CIPHER_CTX_set_app_data(ctx, c);
475 }
476 if (key != NULL) {
477 if (enc == -1)
478 enc = ctx->encrypt;
479 rijndael_set_key(&c->r_ctx, (u_char *)key,
480 8*EVP_CIPHER_CTX_key_length(ctx), enc);
481 }
482 if (iv != NULL)
483 memcpy(c->r_iv, iv, RIJNDAEL_BLOCKSIZE);
484 return (1);
485}
486static int
487ssh_rijndael_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src,
488 u_int len)
489{
490 struct ssh_rijndael_ctx *c;
491 u_char buf[RIJNDAEL_BLOCKSIZE];
492 u_char *cprev, *cnow, *plain, *ivp;
493 int i, j, blocks = len / RIJNDAEL_BLOCKSIZE;
494
495 if (len == 0)
496 return (1);
497 if (len % RIJNDAEL_BLOCKSIZE)
498 fatal("ssh_rijndael_cbc: bad len %d", len);
499 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
500 error("ssh_rijndael_cbc: no context");
501 return (0);
502 }
503 if (ctx->encrypt) {
504 cnow = dest;
505 plain = (u_char *)src;
506 cprev = c->r_iv;
507 for (i = 0; i < blocks; i++, plain+=RIJNDAEL_BLOCKSIZE,
508 cnow+=RIJNDAEL_BLOCKSIZE) {
509 for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++)
510 buf[j] = plain[j] ^ cprev[j];
511 rijndael_encrypt(&c->r_ctx, buf, cnow);
512 cprev = cnow;
513 }
514 memcpy(c->r_iv, cprev, RIJNDAEL_BLOCKSIZE);
515 } else {
516 cnow = (u_char *) (src+len-RIJNDAEL_BLOCKSIZE);
517 plain = dest+len-RIJNDAEL_BLOCKSIZE;
518
519 memcpy(buf, cnow, RIJNDAEL_BLOCKSIZE);
520 for (i = blocks; i > 0; i--, cnow-=RIJNDAEL_BLOCKSIZE,
521 plain-=RIJNDAEL_BLOCKSIZE) {
522 rijndael_decrypt(&c->r_ctx, cnow, plain);
05976246 523 ivp = (i == 1) ? c->r_iv : cnow-RIJNDAEL_BLOCKSIZE;
524 for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++)
525 plain[j] ^= ivp[j];
526 }
527 memcpy(c->r_iv, buf, RIJNDAEL_BLOCKSIZE);
528 }
529 return (1);
530}
531static int
532ssh_rijndael_cleanup(EVP_CIPHER_CTX *ctx)
533{
534 struct ssh_rijndael_ctx *c;
535
536 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
537 memset(c, 0, sizeof(*c));
538 xfree(c);
539 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
540 }
541 return (1);
542}
7b5edc2b 543static const EVP_CIPHER *
05976246 544evp_rijndael(void)
545{
546 static EVP_CIPHER rijndal_cbc;
547
548 memset(&rijndal_cbc, 0, sizeof(EVP_CIPHER));
549 rijndal_cbc.nid = NID_undef;
550 rijndal_cbc.block_size = RIJNDAEL_BLOCKSIZE;
551 rijndal_cbc.iv_len = RIJNDAEL_BLOCKSIZE;
552 rijndal_cbc.key_len = 16;
553 rijndal_cbc.init = ssh_rijndael_init;
554 rijndal_cbc.cleanup = ssh_rijndael_cleanup;
555 rijndal_cbc.do_cipher = ssh_rijndael_cbc;
556#ifndef SSH_OLD_EVP
557 rijndal_cbc.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH |
558 EVP_CIPH_ALWAYS_CALL_INIT;
559#endif
560 return (&rijndal_cbc);
bf8269a9 561}
135247df 562#endif
bf8269a9 563
762715ce 564/*
bf8269a9 565 * Exports an IV from the CipherContext required to export the key
566 * state back from the unprivileged child to the privileged parent
567 * process.
568 */
569
570int
571cipher_get_keyiv_len(CipherContext *cc)
572{
573 Cipher *c = cc->cipher;
574 int ivlen;
575
576 if (c->number == SSH_CIPHER_3DES)
577 ivlen = 24;
578 else
579 ivlen = EVP_CIPHER_CTX_iv_length(&cc->evp);
580 return (ivlen);
581}
582
583void
584cipher_get_keyiv(CipherContext *cc, u_char *iv, u_int len)
585{
586 Cipher *c = cc->cipher;
587 u_char *civ = NULL;
588 int evplen;
589
590 switch (c->number) {
591 case SSH_CIPHER_SSH2:
592 case SSH_CIPHER_DES:
593 case SSH_CIPHER_BLOWFISH:
594 evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
595 if (evplen == 0)
596 return;
597 if (evplen != len)
598 fatal("%s: wrong iv length %d != %d", __FUNCTION__,
599 evplen, len);
600
135247df 601#if OPENSSL_VERSION_NUMBER < 0x00907000L
bf03f2da 602 if (c->evptype == evp_rijndael) {
bf8269a9 603 struct ssh_rijndael_ctx *aesc;
604
605 aesc = EVP_CIPHER_CTX_get_app_data(&cc->evp);
606 if (aesc == NULL)
607 fatal("%s: no rijndael context", __FUNCTION__);
608 civ = aesc->r_iv;
135247df 609 } else
610#endif
611 {
bf8269a9 612 civ = cc->evp.iv;
613 }
614 break;
615 case SSH_CIPHER_3DES: {
616 struct ssh1_3des_ctx *desc;
617 if (len != 24)
618 fatal("%s: bad 3des iv length: %d", __FUNCTION__, len);
619 desc = EVP_CIPHER_CTX_get_app_data(&cc->evp);
620 if (desc == NULL)
621 fatal("%s: no 3des context", __FUNCTION__);
622 debug3("%s: Copying 3DES IV", __FUNCTION__);
623 memcpy(iv, desc->k1.iv, 8);
624 memcpy(iv + 8, desc->k2.iv, 8);
625 memcpy(iv + 16, desc->k3.iv, 8);
626 return;
627 }
628 default:
629 fatal("%s: bad cipher %d", __FUNCTION__, c->number);
630 }
631 memcpy(iv, civ, len);
632}
633
634void
635cipher_set_keyiv(CipherContext *cc, u_char *iv)
636{
637 Cipher *c = cc->cipher;
638 u_char *div = NULL;
639 int evplen = 0;
640
641 switch (c->number) {
642 case SSH_CIPHER_SSH2:
643 case SSH_CIPHER_DES:
644 case SSH_CIPHER_BLOWFISH:
645 evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
646 if (evplen == 0)
647 return;
648
135247df 649#if OPENSSL_VERSION_NUMBER < 0x00907000L
bf03f2da 650 if (c->evptype == evp_rijndael) {
bf8269a9 651 struct ssh_rijndael_ctx *aesc;
652
653 aesc = EVP_CIPHER_CTX_get_app_data(&cc->evp);
654 if (aesc == NULL)
655 fatal("%s: no rijndael context", __FUNCTION__);
656 div = aesc->r_iv;
135247df 657 } else
658#endif
659 {
bf8269a9 660 div = cc->evp.iv;
661 }
662 break;
663 case SSH_CIPHER_3DES: {
664 struct ssh1_3des_ctx *desc;
665 desc = EVP_CIPHER_CTX_get_app_data(&cc->evp);
666 if (desc == NULL)
667 fatal("%s: no 3des context", __FUNCTION__);
668 debug3("%s: Installed 3DES IV", __FUNCTION__);
669 memcpy(desc->k1.iv, iv, 8);
670 memcpy(desc->k2.iv, iv + 8, 8);
671 memcpy(desc->k3.iv, iv + 16, 8);
672 return;
762715ce 673 }
bf8269a9 674 default:
675 fatal("%s: bad cipher %d", __FUNCTION__, c->number);
676 }
677 memcpy(div, iv, evplen);
678}
679
680#if OPENSSL_VERSION_NUMBER < 0x00907000L
681#define EVP_X_STATE(evp) &(evp).c
682#define EVP_X_STATE_LEN(evp) sizeof((evp).c)
683#else
684#define EVP_X_STATE(evp) (evp).cipher_data
685#define EVP_X_STATE_LEN(evp) (evp).cipher->ctx_size
686#endif
687
688int
689cipher_get_keycontext(CipherContext *cc, u_char *dat)
690{
691 Cipher *c = cc->cipher;
692 int plen;
693
694 if (c->number == SSH_CIPHER_3DES) {
695 struct ssh1_3des_ctx *desc;
696 desc = EVP_CIPHER_CTX_get_app_data(&cc->evp);
697 if (desc == NULL)
698 fatal("%s: no 3des context", __FUNCTION__);
699 plen = EVP_X_STATE_LEN(desc->k1);
700 if (dat == NULL)
701 return (3*plen);
702 memcpy(dat, EVP_X_STATE(desc->k1), plen);
703 memcpy(dat + plen, EVP_X_STATE(desc->k2), plen);
704 memcpy(dat + 2*plen, EVP_X_STATE(desc->k3), plen);
705 return (3*plen);
706 }
707
708 /* Generic EVP */
709 plen = EVP_X_STATE_LEN(cc->evp);
710 if (dat == NULL)
711 return (plen);
712
713 memcpy(dat, EVP_X_STATE(cc->evp), plen);
714 return (plen);
715}
716
717void
718cipher_set_keycontext(CipherContext *cc, u_char *dat)
719{
720 Cipher *c = cc->cipher;
721 int plen;
722
723 if (c->number == SSH_CIPHER_3DES) {
724 struct ssh1_3des_ctx *desc;
725 desc = EVP_CIPHER_CTX_get_app_data(&cc->evp);
726 if (desc == NULL)
727 fatal("%s: no 3des context", __FUNCTION__);
728 plen = EVP_X_STATE_LEN(desc->k1);
729 memcpy(EVP_X_STATE(desc->k1), dat, plen);
730 memcpy(EVP_X_STATE(desc->k2), dat + plen, plen);
731 memcpy(EVP_X_STATE(desc->k3), dat + 2*plen, plen);
732 } else {
733 plen = EVP_X_STATE_LEN(cc->evp);
734 memcpy(EVP_X_STATE(cc->evp), dat, plen);
735 }
70fc1609 736}
This page took 1.64354 seconds and 5 git commands to generate.