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