ssh has moved
[netbsd-mini2440.git] / crypto / dist / ssh / cipher-bf1.c
blob2e616931d10736eacfc79f614cab181508f3f3ec
1 /* $NetBSD$ */
2 /* $OpenBSD: cipher-bf1.c,v 1.5 2006/08/03 03:34:42 deraadt Exp $ */
3 /*
4 * Copyright (c) 2003 Markus Friedl. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "includes.h"
28 __RCSID("$NetBSD$");
30 #include <sys/types.h>
32 #include <openssl/evp.h>
34 #include <string.h>
36 #include "xmalloc.h"
37 #include "log.h"
39 * SSH1 uses a variation on Blowfish, all bytes must be swapped before
40 * and after encryption/decryption. Thus the swap_bytes stuff (yuk).
43 const EVP_CIPHER * evp_ssh1_bf(void);
45 static void
46 swap_bytes(const u_char *src, u_char *dst, int n)
48 u_char c[4];
50 /* Process 4 bytes every lap. */
51 for (n = n / 4; n > 0; n--) {
52 c[3] = *src++;
53 c[2] = *src++;
54 c[1] = *src++;
55 c[0] = *src++;
57 *dst++ = c[0];
58 *dst++ = c[1];
59 *dst++ = c[2];
60 *dst++ = c[3];
64 static int (*orig_bf)(EVP_CIPHER_CTX *, u_char *, const u_char *, u_int) = NULL;
66 static int
67 bf_ssh1_cipher(EVP_CIPHER_CTX *ctx, u_char *out, const u_char *in, u_int len)
69 int ret;
71 swap_bytes(in, out, len);
72 ret = (*orig_bf)(ctx, out, out, len);
73 swap_bytes(out, out, len);
74 return (ret);
77 const EVP_CIPHER *
78 evp_ssh1_bf(void)
80 static EVP_CIPHER ssh1_bf;
82 memcpy(&ssh1_bf, EVP_bf_cbc(), sizeof(EVP_CIPHER));
83 orig_bf = ssh1_bf.do_cipher;
84 ssh1_bf.nid = NID_undef;
85 ssh1_bf.do_cipher = bf_ssh1_cipher;
86 ssh1_bf.key_len = 32;
87 return (&ssh1_bf);