cipher-bf1.c revision 124208
1124208Sdes/*
2124208Sdes * Copyright (c) 2003 Markus Friedl.  All rights reserved.
3124208Sdes *
4124208Sdes * Redistribution and use in source and binary forms, with or without
5124208Sdes * modification, are permitted provided that the following conditions
6124208Sdes * are met:
7124208Sdes * 1. Redistributions of source code must retain the above copyright
8124208Sdes *    notice, this list of conditions and the following disclaimer.
9124208Sdes * 2. Redistributions in binary form must reproduce the above copyright
10124208Sdes *    notice, this list of conditions and the following disclaimer in the
11124208Sdes *    documentation and/or other materials provided with the distribution.
12124208Sdes *
13124208Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14124208Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15124208Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16124208Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17124208Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18124208Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19124208Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20124208Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21124208Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22124208Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23124208Sdes */
24124208Sdes
25124208Sdes#include "includes.h"
26124208SdesRCSID("$OpenBSD: cipher-bf1.c,v 1.1 2003/05/15 03:08:29 markus Exp $");
27124208Sdes
28124208Sdes#include <openssl/evp.h>
29124208Sdes#include "xmalloc.h"
30124208Sdes#include "log.h"
31124208Sdes
32124208Sdes#if OPENSSL_VERSION_NUMBER < 0x00906000L
33124208Sdes#define SSH_OLD_EVP
34124208Sdes#endif
35124208Sdes
36124208Sdes/*
37124208Sdes * SSH1 uses a variation on Blowfish, all bytes must be swapped before
38124208Sdes * and after encryption/decryption. Thus the swap_bytes stuff (yuk).
39124208Sdes */
40124208Sdes
41124208Sdesconst EVP_CIPHER * evp_ssh1_bf(void);
42124208Sdes
43124208Sdesstatic void
44124208Sdesswap_bytes(const u_char *src, u_char *dst, int n)
45124208Sdes{
46124208Sdes	u_char c[4];
47124208Sdes
48124208Sdes	/* Process 4 bytes every lap. */
49124208Sdes	for (n = n / 4; n > 0; n--) {
50124208Sdes		c[3] = *src++;
51124208Sdes		c[2] = *src++;
52124208Sdes		c[1] = *src++;
53124208Sdes		c[0] = *src++;
54124208Sdes
55124208Sdes		*dst++ = c[0];
56124208Sdes		*dst++ = c[1];
57124208Sdes		*dst++ = c[2];
58124208Sdes		*dst++ = c[3];
59124208Sdes	}
60124208Sdes}
61124208Sdes
62124208Sdes#ifdef SSH_OLD_EVP
63124208Sdesstatic void bf_ssh1_init (EVP_CIPHER_CTX * ctx, const unsigned char *key,
64124208Sdes			  const unsigned char *iv, int enc)
65124208Sdes{
66124208Sdes	if (iv != NULL)
67124208Sdes		memcpy (&(ctx->oiv[0]), iv, 8);
68124208Sdes	memcpy (&(ctx->iv[0]), &(ctx->oiv[0]), 8);
69124208Sdes	if (key != NULL)
70124208Sdes		BF_set_key (&(ctx->c.bf_ks), EVP_CIPHER_CTX_key_length (ctx),
71124208Sdes			    key);
72124208Sdes}
73124208Sdes#endif
74124208Sdes
75124208Sdesstatic int (*orig_bf)(EVP_CIPHER_CTX *, u_char *, const u_char *, u_int) = NULL;
76124208Sdes
77124208Sdesstatic int
78124208Sdesbf_ssh1_cipher(EVP_CIPHER_CTX *ctx, u_char *out, const u_char *in, u_int len)
79124208Sdes{
80124208Sdes	int ret;
81124208Sdes
82124208Sdes	swap_bytes(in, out, len);
83124208Sdes	ret = (*orig_bf)(ctx, out, out, len);
84124208Sdes	swap_bytes(out, out, len);
85124208Sdes	return (ret);
86124208Sdes}
87124208Sdes
88124208Sdesconst EVP_CIPHER *
89124208Sdesevp_ssh1_bf(void)
90124208Sdes{
91124208Sdes	static EVP_CIPHER ssh1_bf;
92124208Sdes
93124208Sdes	memcpy(&ssh1_bf, EVP_bf_cbc(), sizeof(EVP_CIPHER));
94124208Sdes	orig_bf = ssh1_bf.do_cipher;
95124208Sdes	ssh1_bf.nid = NID_undef;
96124208Sdes#ifdef SSH_OLD_EVP
97124208Sdes	ssh1_bf.init = bf_ssh1_init;
98124208Sdes#endif
99124208Sdes	ssh1_bf.do_cipher = bf_ssh1_cipher;
100124208Sdes	ssh1_bf.key_len = 32;
101124208Sdes	return (&ssh1_bf);
102124208Sdes}
103