147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom/* crypto/bf/bf_enc.c */ 247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * All rights reserved. 447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * 547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * This package is an SSL implementation written 647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * by Eric Young (eay@cryptsoft.com). 747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * The implementation was written so as to conform with Netscapes SSL. 847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * 947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * This library is free for commercial and non-commercial use as long as 1047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * the following conditions are aheared to. The following conditions 1147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * apply to all code found in this distribution, be it the RC4, RSA, 1247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * included with this distribution is covered by the same copyright terms 1447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * except that the holder is Tim Hudson (tjh@cryptsoft.com). 1547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * 1647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * Copyright remains Eric Young's, and as such any Copyright notices in 1747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * the code are not to be removed. 1847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * If this package is used in a product, Eric Young should be given attribution 1947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * as the author of the parts of the library used. 2047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * This can be in the form of a textual message at program startup or 2147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * in documentation (online or textual) provided with the package. 2247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * 2347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * Redistribution and use in source and binary forms, with or without 2447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * modification, are permitted provided that the following conditions 2547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * are met: 2647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * 1. Redistributions of source code must retain the copyright 2747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * notice, this list of conditions and the following disclaimer. 2847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * 2. Redistributions in binary form must reproduce the above copyright 2947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * notice, this list of conditions and the following disclaimer in the 3047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * documentation and/or other materials provided with the distribution. 3147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * 3. All advertising materials mentioning features or use of this software 3247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * must display the following acknowledgement: 3347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * "This product includes cryptographic software written by 3447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * Eric Young (eay@cryptsoft.com)" 3547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * The word 'cryptographic' can be left out if the rouines from the library 3647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * being used are not cryptographic related :-). 3747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * 4. If you include any Windows specific code (or a derivative thereof) from 3847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * the apps directory (application code) you must include an acknowledgement: 3947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 4047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * 4147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * SUCH DAMAGE. 5247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * 5347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * The licence and distribution terms for any publically available version or 5447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * derivative of this code cannot be changed. i.e. this code cannot simply be 5547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * copied and put under another distribution licence 5647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * [including the GNU Public Licence.] 5747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom */ 5847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 5947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#include <openssl/blowfish.h> 6047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#include "bf_locl.h" 6147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 6247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom/* Blowfish as implemented from 'Blowfish: Springer-Verlag paper' 6347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * (From LECTURE NOTES IN COMPUTER SCIENCE 809, FAST SOFTWARE ENCRYPTION, 6447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * CAMBRIDGE SECURITY WORKSHOP, CAMBRIDGE, U.K., DECEMBER 9-11, 1993) 6547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom */ 6647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 6747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#if (BF_ROUNDS != 16) && (BF_ROUNDS != 20) 6847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#error If you set BF_ROUNDS to some value other than 16 or 20, you will have \ 6947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstromto modify the code. 7047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#endif 7147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 7247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstromvoid BF_encrypt(BF_LONG *data, const BF_KEY *key) 7347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom { 7447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#ifndef BF_PTR2 7547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom register BF_LONG l,r; 7647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom register const BF_LONG *p,*s; 7747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 7847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom p=key->P; 7947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom s= &(key->S[0]); 8047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom l=data[0]; 8147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom r=data[1]; 8247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 8347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom l^=p[0]; 8447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,s,p[ 1]); 8547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,s,p[ 2]); 8647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,s,p[ 3]); 8747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,s,p[ 4]); 8847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,s,p[ 5]); 8947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,s,p[ 6]); 9047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,s,p[ 7]); 9147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,s,p[ 8]); 9247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,s,p[ 9]); 9347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,s,p[10]); 9447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,s,p[11]); 9547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,s,p[12]); 9647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,s,p[13]); 9747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,s,p[14]); 9847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,s,p[15]); 9947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,s,p[16]); 10047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#if BF_ROUNDS == 20 10147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,s,p[17]); 10247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,s,p[18]); 10347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,s,p[19]); 10447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,s,p[20]); 10547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#endif 10647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom r^=p[BF_ROUNDS+1]; 10747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 10847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom data[1]=l&0xffffffffL; 10947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom data[0]=r&0xffffffffL; 11047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#else 11147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom register BF_LONG l,r,t,*k; 11247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 11347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom l=data[0]; 11447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom r=data[1]; 11547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom k=(BF_LONG*)key; 11647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 11747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom l^=k[0]; 11847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,k, 1); 11947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,k, 2); 12047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,k, 3); 12147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,k, 4); 12247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,k, 5); 12347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,k, 6); 12447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,k, 7); 12547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,k, 8); 12647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,k, 9); 12747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,k,10); 12847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,k,11); 12947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,k,12); 13047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,k,13); 13147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,k,14); 13247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,k,15); 13347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,k,16); 13447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#if BF_ROUNDS == 20 13547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,k,17); 13647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,k,18); 13747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,k,19); 13847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,k,20); 13947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#endif 14047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom r^=k[BF_ROUNDS+1]; 14147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 14247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom data[1]=l&0xffffffffL; 14347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom data[0]=r&0xffffffffL; 14447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#endif 14547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom } 14647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 14747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#ifndef BF_DEFAULT_OPTIONS 14847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 14947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstromvoid BF_decrypt(BF_LONG *data, const BF_KEY *key) 15047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom { 15147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#ifndef BF_PTR2 15247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom register BF_LONG l,r; 15347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom register const BF_LONG *p,*s; 15447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 15547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom p=key->P; 15647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom s= &(key->S[0]); 15747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom l=data[0]; 15847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom r=data[1]; 15947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 16047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom l^=p[BF_ROUNDS+1]; 16147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#if BF_ROUNDS == 20 16247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,s,p[20]); 16347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,s,p[19]); 16447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,s,p[18]); 16547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,s,p[17]); 16647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#endif 16747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,s,p[16]); 16847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,s,p[15]); 16947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,s,p[14]); 17047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,s,p[13]); 17147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,s,p[12]); 17247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,s,p[11]); 17347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,s,p[10]); 17447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,s,p[ 9]); 17547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,s,p[ 8]); 17647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,s,p[ 7]); 17747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,s,p[ 6]); 17847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,s,p[ 5]); 17947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,s,p[ 4]); 18047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,s,p[ 3]); 18147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,s,p[ 2]); 18247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,s,p[ 1]); 18347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom r^=p[0]; 18447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 18547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom data[1]=l&0xffffffffL; 18647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom data[0]=r&0xffffffffL; 18747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#else 18847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom register BF_LONG l,r,t,*k; 18947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 19047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom l=data[0]; 19147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom r=data[1]; 19247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom k=(BF_LONG *)key; 19347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 19447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom l^=k[BF_ROUNDS+1]; 19547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#if BF_ROUNDS == 20 19647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,k,20); 19747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,k,19); 19847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,k,18); 19947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,k,17); 20047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#endif 20147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,k,16); 20247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,k,15); 20347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,k,14); 20447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,k,13); 20547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,k,12); 20647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,k,11); 20747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,k,10); 20847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,k, 9); 20947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,k, 8); 21047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,k, 7); 21147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,k, 6); 21247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,k, 5); 21347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,k, 4); 21447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,k, 3); 21547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(r,l,k, 2); 21647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_ENC(l,r,k, 1); 21747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom r^=k[0]; 21847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 21947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom data[1]=l&0xffffffffL; 22047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom data[0]=r&0xffffffffL; 22147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#endif 22247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom } 22347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 22447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstromvoid BF_cbc_encrypt(const unsigned char *in, unsigned char *out, long length, 22547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom const BF_KEY *schedule, unsigned char *ivec, int encrypt) 22647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom { 22747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom register BF_LONG tin0,tin1; 22847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom register BF_LONG tout0,tout1,xor0,xor1; 22947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom register long l=length; 23047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_LONG tin[2]; 23147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 23247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom if (encrypt) 23347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom { 23447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom n2l(ivec,tout0); 23547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom n2l(ivec,tout1); 23647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom ivec-=8; 23747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom for (l-=8; l>=0; l-=8) 23847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom { 23947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom n2l(in,tin0); 24047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom n2l(in,tin1); 24147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom tin0^=tout0; 24247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom tin1^=tout1; 24347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom tin[0]=tin0; 24447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom tin[1]=tin1; 24547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_encrypt(tin,schedule); 24647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom tout0=tin[0]; 24747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom tout1=tin[1]; 24847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom l2n(tout0,out); 24947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom l2n(tout1,out); 25047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom } 25147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom if (l != -8) 25247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom { 25347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom n2ln(in,tin0,tin1,l+8); 25447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom tin0^=tout0; 25547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom tin1^=tout1; 25647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom tin[0]=tin0; 25747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom tin[1]=tin1; 25847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_encrypt(tin,schedule); 25947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom tout0=tin[0]; 26047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom tout1=tin[1]; 26147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom l2n(tout0,out); 26247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom l2n(tout1,out); 26347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom } 26447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom l2n(tout0,ivec); 26547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom l2n(tout1,ivec); 26647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom } 26747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom else 26847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom { 26947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom n2l(ivec,xor0); 27047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom n2l(ivec,xor1); 27147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom ivec-=8; 27247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom for (l-=8; l>=0; l-=8) 27347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom { 27447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom n2l(in,tin0); 27547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom n2l(in,tin1); 27647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom tin[0]=tin0; 27747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom tin[1]=tin1; 27847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_decrypt(tin,schedule); 27947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom tout0=tin[0]^xor0; 28047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom tout1=tin[1]^xor1; 28147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom l2n(tout0,out); 28247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom l2n(tout1,out); 28347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom xor0=tin0; 28447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom xor1=tin1; 28547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom } 28647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom if (l != -8) 28747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom { 28847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom n2l(in,tin0); 28947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom n2l(in,tin1); 29047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom tin[0]=tin0; 29147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom tin[1]=tin1; 29247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_decrypt(tin,schedule); 29347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom tout0=tin[0]^xor0; 29447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom tout1=tin[1]^xor1; 29547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom l2nn(tout0,tout1,out,l+8); 29647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom xor0=tin0; 29747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom xor1=tin1; 29847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom } 29947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom l2n(xor0,ivec); 30047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom l2n(xor1,ivec); 30147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom } 30247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom tin0=tin1=tout0=tout1=xor0=xor1=0; 30347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom tin[0]=tin[1]=0; 30447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom } 30547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 30647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#endif 307