1295367Sdes/* $OpenBSD: cipher-bf1.c,v 1.7 2015/01/14 10:24:42 markus Exp $ */
2124208Sdes/*
3124208Sdes * Copyright (c) 2003 Markus Friedl.  All rights reserved.
4124208Sdes *
5295367Sdes * Permission to use, copy, modify, and distribute this software for any
6295367Sdes * purpose with or without fee is hereby granted, provided that the above
7295367Sdes * copyright notice and this permission notice appear in all copies.
8124208Sdes *
9124208Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
10124208Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
11124208Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
12124208Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
13124208Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
14124208Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
15124208Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
16124208Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
17124208Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
18124208Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19124208Sdes */
20124208Sdes
21124208Sdes#include "includes.h"
22124208Sdes
23323124Sdes#if defined(WITH_OPENSSL) && !defined(OPENSSL_NO_BF)
24295367Sdes
25162852Sdes#include <sys/types.h>
26162852Sdes
27162852Sdes#include <stdarg.h>
28162852Sdes#include <string.h>
29162852Sdes
30295367Sdes#include <openssl/evp.h>
31124208Sdes
32181111Sdes#include "openbsd-compat/openssl-compat.h"
33124208Sdes
34124208Sdes/*
35124208Sdes * SSH1 uses a variation on Blowfish, all bytes must be swapped before
36124208Sdes * and after encryption/decryption. Thus the swap_bytes stuff (yuk).
37124208Sdes */
38124208Sdes
39124208Sdesconst EVP_CIPHER * evp_ssh1_bf(void);
40124208Sdes
41124208Sdesstatic void
42124208Sdesswap_bytes(const u_char *src, u_char *dst, int n)
43124208Sdes{
44124208Sdes	u_char c[4];
45124208Sdes
46124208Sdes	/* Process 4 bytes every lap. */
47124208Sdes	for (n = n / 4; n > 0; n--) {
48124208Sdes		c[3] = *src++;
49124208Sdes		c[2] = *src++;
50124208Sdes		c[1] = *src++;
51124208Sdes		c[0] = *src++;
52124208Sdes
53124208Sdes		*dst++ = c[0];
54124208Sdes		*dst++ = c[1];
55124208Sdes		*dst++ = c[2];
56124208Sdes		*dst++ = c[3];
57124208Sdes	}
58124208Sdes}
59124208Sdes
60124208Sdes#ifdef SSH_OLD_EVP
61124208Sdesstatic void bf_ssh1_init (EVP_CIPHER_CTX * ctx, const unsigned char *key,
62124208Sdes			  const unsigned char *iv, int enc)
63124208Sdes{
64124208Sdes	if (iv != NULL)
65124208Sdes		memcpy (&(ctx->oiv[0]), iv, 8);
66124208Sdes	memcpy (&(ctx->iv[0]), &(ctx->oiv[0]), 8);
67124208Sdes	if (key != NULL)
68124208Sdes		BF_set_key (&(ctx->c.bf_ks), EVP_CIPHER_CTX_key_length (ctx),
69124208Sdes			    key);
70124208Sdes}
71124208Sdes#endif
72124208Sdes
73221420Sdesstatic int (*orig_bf)(EVP_CIPHER_CTX *, u_char *,
74221420Sdes    const u_char *, LIBCRYPTO_EVP_INL_TYPE) = NULL;
75124208Sdes
76124208Sdesstatic int
77221420Sdesbf_ssh1_cipher(EVP_CIPHER_CTX *ctx, u_char *out, const u_char *in,
78221420Sdes    LIBCRYPTO_EVP_INL_TYPE 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}
103323124Sdes#endif /* defined(WITH_OPENSSL) && !defined(OPENSSL_NO_BF) */
104