blob: 39807d5c2049900f4ee122fe1d82c34f8a53ee23 [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"
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +000038RCSID("$OpenBSD: cipher.c,v 1.58 2002/06/04 23:05:49 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
Damien Millerc7375ac2002-03-11 10:51:17 +110046#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
Ben Lindstromf088f432002-06-06 20:50:07 +000051#if OPENSSL_VERSION_NUMBER < 0x00907000L
52#include "rijndael.h"
53static const EVP_CIPHER *evp_rijndael(void);
54#endif
Ben Lindstrom6a246412002-06-06 19:48:16 +000055static const EVP_CIPHER *evp_ssh1_3des(void);
56static const EVP_CIPHER *evp_ssh1_bf(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057
Damien Miller963f6b22002-02-19 15:21:23 +110058struct Cipher {
59 char *name;
60 int number; /* for ssh1 only */
61 u_int block_size;
62 u_int key_len;
Ben Lindstrom6a246412002-06-06 19:48:16 +000063 const EVP_CIPHER *(*evptype)(void);
Damien Miller21cf4e02002-02-19 15:26:42 +110064} 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 },
Damien Miller963f6b22002-02-19 15:21:23 +110069
Damien Miller21cf4e02002-02-19 15:26:42 +110070 { "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 },
Ben Lindstromf088f432002-06-06 20:50:07 +000074#if OPENSSL_VERSION_NUMBER < 0x00907000L
Damien Miller21cf4e02002-02-19 15:26:42 +110075 { "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 },
Ben Lindstrom8a725a82002-04-04 22:10:38 +000078 { "rijndael-cbc@lysator.liu.se",
79 SSH_CIPHER_SSH2, 16, 32, evp_rijndael },
Ben Lindstromf088f432002-06-06 20:50:07 +000080#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
Damien Miller874d77b2000-10-14 16:23:11 +110087
Damien Miller21cf4e02002-02-19 15:26:42 +110088 { NULL, SSH_CIPHER_ILLEGAL, 0, 0, NULL }
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089};
90
Damien Miller874d77b2000-10-14 16:23:11 +110091/*--*/
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092
Ben Lindstrom6328ab32002-03-22 02:54:23 +000093u_int
Damien Miller963f6b22002-02-19 15:21:23 +110094cipher_blocksize(Cipher *c)
95{
96 return (c->block_size);
97}
Ben Lindstrom6328ab32002-03-22 02:54:23 +000098u_int
Damien Miller963f6b22002-02-19 15:21:23 +110099cipher_keylen(Cipher *c)
100{
101 return (c->key_len);
102}
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000103u_int
Ben Lindstrom212faca2002-03-22 01:39:44 +0000104cipher_get_number(Cipher *c)
105{
106 return (c->number);
107}
Damien Miller963f6b22002-02-19 15:21:23 +1100108
Ben Lindstrom46c16222000-12-22 01:43:59 +0000109u_int
Damien Miller874d77b2000-10-14 16:23:11 +1100110cipher_mask_ssh1(int client)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000112 u_int mask = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100113 mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */
Damien Miller95def091999-11-25 00:26:21 +1100114 mask |= 1 << SSH_CIPHER_BLOWFISH;
Damien Miller874d77b2000-10-14 16:23:11 +1100115 if (client) {
116 mask |= 1 << SSH_CIPHER_DES;
117 }
Damien Miller1383bd82000-04-06 12:32:37 +1000118 return mask;
119}
Damien Miller874d77b2000-10-14 16:23:11 +1100120
121Cipher *
122cipher_by_name(const char *name)
Damien Miller1383bd82000-04-06 12:32:37 +1000123{
Damien Miller874d77b2000-10-14 16:23:11 +1100124 Cipher *c;
125 for (c = ciphers; c->name != NULL; c++)
126 if (strcasecmp(c->name, name) == 0)
127 return c;
128 return NULL;
Damien Miller1383bd82000-04-06 12:32:37 +1000129}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000130
Damien Miller874d77b2000-10-14 16:23:11 +1100131Cipher *
132cipher_by_number(int id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133{
Damien Miller874d77b2000-10-14 16:23:11 +1100134 Cipher *c;
135 for (c = ciphers; c->name != NULL; c++)
136 if (c->number == id)
137 return c;
138 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139}
140
Damien Miller78928792000-04-12 20:17:38 +1000141#define CIPHER_SEP ","
142int
143ciphers_valid(const char *names)
144{
Damien Miller874d77b2000-10-14 16:23:11 +1100145 Cipher *c;
Damien Miller37023962000-07-11 17:31:38 +1000146 char *ciphers, *cp;
Damien Miller78928792000-04-12 20:17:38 +1000147 char *p;
Damien Miller78928792000-04-12 20:17:38 +1000148
Damien Millerb1715dc2000-05-30 13:44:51 +1000149 if (names == NULL || strcmp(names, "") == 0)
Damien Miller78928792000-04-12 20:17:38 +1000150 return 0;
Damien Miller37023962000-07-11 17:31:38 +1000151 ciphers = cp = xstrdup(names);
Damien Miller874d77b2000-10-14 16:23:11 +1100152 for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +1100153 (p = strsep(&cp, CIPHER_SEP))) {
Damien Miller874d77b2000-10-14 16:23:11 +1100154 c = cipher_by_name(p);
155 if (c == NULL || c->number != SSH_CIPHER_SSH2) {
156 debug("bad cipher %s [%s]", p, names);
Damien Miller78928792000-04-12 20:17:38 +1000157 xfree(ciphers);
158 return 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100159 } else {
Damien Miller50a41ed2000-10-16 12:14:42 +1100160 debug3("cipher ok: %s [%s]", p, names);
Damien Miller78928792000-04-12 20:17:38 +1000161 }
162 }
Damien Miller50a41ed2000-10-16 12:14:42 +1100163 debug3("ciphers ok: [%s]", names);
Damien Miller78928792000-04-12 20:17:38 +1000164 xfree(ciphers);
165 return 1;
166}
167
Damien Miller5428f641999-11-25 11:54:57 +1100168/*
169 * Parses the name of the cipher. Returns the number of the corresponding
170 * cipher, or -1 on error.
171 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000172
173int
174cipher_number(const char *name)
175{
Damien Miller874d77b2000-10-14 16:23:11 +1100176 Cipher *c;
Damien Millerb1715dc2000-05-30 13:44:51 +1000177 if (name == NULL)
178 return -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100179 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
Damien Miller21cf4e02002-02-19 15:26:42 +1100191cipher_init(CipherContext *cc, Cipher *cipher,
192 const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
193 int encrypt)
Damien Miller874d77b2000-10-14 16:23:11 +1100194{
Damien Miller21cf4e02002-02-19 15:26:42 +1100195 static int dowarn = 1;
Damien Millerc7375ac2002-03-11 10:51:17 +1100196#ifdef SSH_OLD_EVP
197 EVP_CIPHER *type;
198#else
Damien Miller21cf4e02002-02-19 15:26:42 +1100199 const EVP_CIPHER *type;
Damien Millerc7375ac2002-03-11 10:51:17 +1100200#endif
Damien Miller21cf4e02002-02-19 15:26:42 +1100201 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
Damien Miller874d77b2000-10-14 16:23:11 +1100214 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;
Damien Miller21cf4e02002-02-19 15:26:42 +1100221
222 type = (*cipher->evptype)();
223
224 EVP_CIPHER_CTX_init(&cc->evp);
Damien Millerc7375ac2002-03-11 10:51:17 +1100225#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
Damien Miller21cf4e02002-02-19 15:26:42 +1100234 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);
Damien Millerc7375ac2002-03-11 10:51:17 +1100248#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100249}
250
251void
Damien Miller963f6b22002-02-19 15:21:23 +1100252cipher_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
Damien Miller874d77b2000-10-14 16:23:11 +1100253{
254 if (len % cc->cipher->block_size)
255 fatal("cipher_encrypt: bad plaintext length %d", len);
Damien Millerc7375ac2002-03-11 10:51:17 +1100256#ifdef SSH_OLD_EVP
257 EVP_Cipher(&cc->evp, dest, (u_char *)src, len);
258#else
Damien Miller21cf4e02002-02-19 15:26:42 +1100259 if (EVP_Cipher(&cc->evp, dest, (u_char *)src, len) == 0)
260 fatal("evp_crypt: EVP_Cipher failed");
Damien Millerc7375ac2002-03-11 10:51:17 +1100261#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100262}
263
264void
Damien Miller963f6b22002-02-19 15:21:23 +1100265cipher_cleanup(CipherContext *cc)
Damien Miller874d77b2000-10-14 16:23:11 +1100266{
Damien Millerc7375ac2002-03-11 10:51:17 +1100267#ifdef SSH_OLD_EVP
268 EVP_CIPHER_CTX_cleanup(&cc->evp);
269#else
Damien Miller21cf4e02002-02-19 15:26:42 +1100270 if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0)
271 error("cipher_cleanup: EVP_CIPHER_CTX_cleanup failed");
Damien Millerc7375ac2002-03-11 10:51:17 +1100272#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000273}
274
Damien Miller5428f641999-11-25 11:54:57 +1100275/*
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 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000279
Damien Miller4af51302000-04-16 11:18:38 +1000280void
Damien Miller874d77b2000-10-14 16:23:11 +1100281cipher_set_key_string(CipherContext *cc, Cipher *cipher,
Damien Miller963f6b22002-02-19 15:21:23 +1100282 const char *passphrase, int encrypt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000283{
Damien Miller95def091999-11-25 00:26:21 +1100284 MD5_CTX md;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000285 u_char digest[16];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000286
Damien Miller95def091999-11-25 00:26:21 +1100287 MD5_Init(&md);
Damien Miller874d77b2000-10-14 16:23:11 +1100288 MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
Damien Miller95def091999-11-25 00:26:21 +1100289 MD5_Final(digest, &md);
290
Damien Miller963f6b22002-02-19 15:21:23 +1100291 cipher_init(cc, cipher, digest, 16, NULL, 0, encrypt);
Damien Miller95def091999-11-25 00:26:21 +1100292
293 memset(digest, 0, sizeof(digest));
294 memset(&md, 0, sizeof(md));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000295}
Damien Miller21cf4e02002-02-19 15:26:42 +1100296
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);
Damien Millerc7375ac2002-03-11 10:51:17 +1100343#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
Damien Miller21cf4e02002-02-19 15:26:42 +1100348 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 }
Damien Millerc7375ac2002-03-11 10:51:17 +1100356#endif
Damien Miller21cf4e02002-02-19 15:26:42 +1100357 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 }
Damien Millerc7375ac2002-03-11 10:51:17 +1100368#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
Damien Miller21cf4e02002-02-19 15:26:42 +1100373 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);
Damien Millerc7375ac2002-03-11 10:51:17 +1100377#endif
Damien Miller21cf4e02002-02-19 15:26:42 +1100378 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}
Ben Lindstrom6a246412002-06-06 19:48:16 +0000392static const EVP_CIPHER *
Damien Miller21cf4e02002-02-19 15:26:42 +1100393evp_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;
Damien Millerc7375ac2002-03-11 10:51:17 +1100405#ifndef SSH_OLD_EVP
Damien Miller21cf4e02002-02-19 15:26:42 +1100406 ssh1_3des.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH;
Damien Millerc7375ac2002-03-11 10:51:17 +1100407#endif
Damien Miller21cf4e02002-02-19 15:26:42 +1100408 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}
Ben Lindstrom6a246412002-06-06 19:48:16 +0000444static const EVP_CIPHER *
Damien Miller21cf4e02002-02-19 15:26:42 +1100445evp_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
Ben Lindstromf088f432002-06-06 20:50:07 +0000457#if OPENSSL_VERSION_NUMBER < 0x00907000L
Damien Miller21cf4e02002-02-19 15:26:42 +1100458/* 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);
Tim Ricef29a6532002-03-22 13:27:40 -0800523 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}
Ben Lindstrom6a246412002-06-06 19:48:16 +0000543static const EVP_CIPHER *
Tim Ricef29a6532002-03-22 13:27:40 -0800544evp_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);
Ben Lindstrom212faca2002-03-22 01:39:44 +0000561}
Ben Lindstromf088f432002-06-06 20:50:07 +0000562#endif
Ben Lindstrom212faca2002-03-22 01:39:44 +0000563
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000564/*
Ben Lindstrom212faca2002-03-22 01:39:44 +0000565 * 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)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000598 fatal("%s: wrong iv length %d != %d", __func__,
Ben Lindstrom212faca2002-03-22 01:39:44 +0000599 evplen, len);
600
Ben Lindstromf088f432002-06-06 20:50:07 +0000601#if OPENSSL_VERSION_NUMBER < 0x00907000L
Ben Lindstrom8a725a82002-04-04 22:10:38 +0000602 if (c->evptype == evp_rijndael) {
Ben Lindstrom212faca2002-03-22 01:39:44 +0000603 struct ssh_rijndael_ctx *aesc;
604
605 aesc = EVP_CIPHER_CTX_get_app_data(&cc->evp);
606 if (aesc == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000607 fatal("%s: no rijndael context", __func__);
Ben Lindstrom212faca2002-03-22 01:39:44 +0000608 civ = aesc->r_iv;
Ben Lindstromf088f432002-06-06 20:50:07 +0000609 } else
610#endif
611 {
Ben Lindstrom212faca2002-03-22 01:39:44 +0000612 civ = cc->evp.iv;
613 }
614 break;
615 case SSH_CIPHER_3DES: {
616 struct ssh1_3des_ctx *desc;
617 if (len != 24)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000618 fatal("%s: bad 3des iv length: %d", __func__, len);
Ben Lindstrom212faca2002-03-22 01:39:44 +0000619 desc = EVP_CIPHER_CTX_get_app_data(&cc->evp);
620 if (desc == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000621 fatal("%s: no 3des context", __func__);
622 debug3("%s: Copying 3DES IV", __func__);
Ben Lindstrom212faca2002-03-22 01:39:44 +0000623 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:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000629 fatal("%s: bad cipher %d", __func__, c->number);
Ben Lindstrom212faca2002-03-22 01:39:44 +0000630 }
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
Ben Lindstromf088f432002-06-06 20:50:07 +0000649#if OPENSSL_VERSION_NUMBER < 0x00907000L
Ben Lindstrom8a725a82002-04-04 22:10:38 +0000650 if (c->evptype == evp_rijndael) {
Ben Lindstrom212faca2002-03-22 01:39:44 +0000651 struct ssh_rijndael_ctx *aesc;
652
653 aesc = EVP_CIPHER_CTX_get_app_data(&cc->evp);
654 if (aesc == NULL)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000655 fatal("%s: no rijndael context", __func__);
Ben Lindstrom212faca2002-03-22 01:39:44 +0000656 div = aesc->r_iv;
Ben Lindstromf088f432002-06-06 20:50:07 +0000657 } else
658#endif
659 {
Ben Lindstrom212faca2002-03-22 01:39:44 +0000660 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)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000667 fatal("%s: no 3des context", __func__);
668 debug3("%s: Installed 3DES IV", __func__);
Ben Lindstrom212faca2002-03-22 01:39:44 +0000669 memcpy(desc->k1.iv, iv, 8);
670 memcpy(desc->k2.iv, iv + 8, 8);
671 memcpy(desc->k3.iv, iv + 16, 8);
672 return;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000673 }
Ben Lindstrom212faca2002-03-22 01:39:44 +0000674 default:
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000675 fatal("%s: bad cipher %d", __func__, c->number);
Ben Lindstrom212faca2002-03-22 01:39:44 +0000676 }
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)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000698 fatal("%s: no 3des context", __func__);
Ben Lindstrom212faca2002-03-22 01:39:44 +0000699 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)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000727 fatal("%s: no 3des context", __func__);
Ben Lindstrom212faca2002-03-22 01:39:44 +0000728 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 }
Damien Miller21cf4e02002-02-19 15:26:42 +1100736}