cipher-bf1.c revision 259065
1287111Smarcel/* $OpenBSD: cipher-bf1.c,v 1.6 2010/10/01 23:05:32 djm Exp $ */ 2287111Smarcel/* 3287111Smarcel * Copyright (c) 2003 Markus Friedl. All rights reserved. 4287111Smarcel * 5287111Smarcel * Redistribution and use in source and binary forms, with or without 6287111Smarcel * modification, are permitted provided that the following conditions 7287111Smarcel * are met: 8287111Smarcel * 1. Redistributions of source code must retain the above copyright 9287111Smarcel * notice, this list of conditions and the following disclaimer. 10287111Smarcel * 2. Redistributions in binary form must reproduce the above copyright 11287111Smarcel * notice, this list of conditions and the following disclaimer in the 12287111Smarcel * documentation and/or other materials provided with the distribution. 13287111Smarcel * 14287111Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15287111Smarcel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16287111Smarcel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17287111Smarcel * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18287111Smarcel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19287111Smarcel * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20287111Smarcel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21287111Smarcel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22287111Smarcel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23287111Smarcel * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24287111Smarcel */ 25287111Smarcel 26287111Smarcel#include "includes.h" 27287111Smarcel 28287111Smarcel#include <sys/types.h> 29287111Smarcel 30287111Smarcel#include <openssl/evp.h> 31287111Smarcel 32287111Smarcel#include <stdarg.h> 33287111Smarcel#include <string.h> 34287111Smarcel 35287111Smarcel#include "xmalloc.h" 36287111Smarcel#include "log.h" 37287111Smarcel 38287111Smarcel#include "openbsd-compat/openssl-compat.h" 39287111Smarcel 40287111Smarcel/* 41287111Smarcel * SSH1 uses a variation on Blowfish, all bytes must be swapped before 42287111Smarcel * and after encryption/decryption. Thus the swap_bytes stuff (yuk). 43287111Smarcel */ 44287111Smarcel 45287111Smarcelconst EVP_CIPHER * evp_ssh1_bf(void); 46287111Smarcel 47287111Smarcelstatic void 48287111Smarcelswap_bytes(const u_char *src, u_char *dst, int n) 49287111Smarcel{ 50287111Smarcel u_char c[4]; 51287111Smarcel 52287111Smarcel /* Process 4 bytes every lap. */ 53287111Smarcel for (n = n / 4; n > 0; n--) { 54287111Smarcel c[3] = *src++; 55287111Smarcel c[2] = *src++; 56287111Smarcel c[1] = *src++; 57287111Smarcel c[0] = *src++; 58287111Smarcel 59287111Smarcel *dst++ = c[0]; 60287111Smarcel *dst++ = c[1]; 61287111Smarcel *dst++ = c[2]; 62287111Smarcel *dst++ = c[3]; 63287111Smarcel } 64287111Smarcel} 65287111Smarcel 66287111Smarcel#ifdef SSH_OLD_EVP 67287111Smarcelstatic void bf_ssh1_init (EVP_CIPHER_CTX * ctx, const unsigned char *key, 68287111Smarcel const unsigned char *iv, int enc) 69287111Smarcel{ 70287111Smarcel if (iv != NULL) 71287111Smarcel memcpy (&(ctx->oiv[0]), iv, 8); 72287111Smarcel memcpy (&(ctx->iv[0]), &(ctx->oiv[0]), 8); 73287111Smarcel if (key != NULL) 74287111Smarcel BF_set_key (&(ctx->c.bf_ks), EVP_CIPHER_CTX_key_length (ctx), 75287111Smarcel key); 76287111Smarcel} 77287111Smarcel#endif 78287111Smarcel 79287111Smarcelstatic int (*orig_bf)(EVP_CIPHER_CTX *, u_char *, 80287111Smarcel const u_char *, LIBCRYPTO_EVP_INL_TYPE) = NULL; 81287111Smarcel 82287111Smarcelstatic int 83287111Smarcelbf_ssh1_cipher(EVP_CIPHER_CTX *ctx, u_char *out, const u_char *in, 84287111Smarcel LIBCRYPTO_EVP_INL_TYPE len) 85287111Smarcel{ 86287111Smarcel int ret; 87287111Smarcel 88287111Smarcel swap_bytes(in, out, len); 89287111Smarcel ret = (*orig_bf)(ctx, out, out, len); 90287111Smarcel swap_bytes(out, out, len); 91287111Smarcel return (ret); 92287111Smarcel} 93287111Smarcel 94287111Smarcelconst EVP_CIPHER * 95287111Smarcelevp_ssh1_bf(void) 96287111Smarcel{ 97287111Smarcel static EVP_CIPHER ssh1_bf; 98287111Smarcel 99287111Smarcel memcpy(&ssh1_bf, EVP_bf_cbc(), sizeof(EVP_CIPHER)); 100287111Smarcel orig_bf = ssh1_bf.do_cipher; 101287111Smarcel ssh1_bf.nid = NID_undef; 102287111Smarcel#ifdef SSH_OLD_EVP 103287111Smarcel ssh1_bf.init = bf_ssh1_init; 104287111Smarcel#endif 105287111Smarcel ssh1_bf.do_cipher = bf_ssh1_cipher; 106287111Smarcel ssh1_bf.key_len = 32; 107287111Smarcel return (&ssh1_bf); 108287111Smarcel} 109287111Smarcel