10Sstevel@tonic-gate /* crypto/bf/bf_enc.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * This package is an SSL implementation written
60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate *
160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate * the code are not to be removed.
180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate * as the author of the parts of the library used.
200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate *
230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate * are met:
260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate * must display the following acknowledgement:
330Sstevel@tonic-gate * "This product includes cryptographic software written by
340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate * being used are not cryptographic related :-).
370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate * SUCH DAMAGE.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
550Sstevel@tonic-gate * copied and put under another distribution licence
560Sstevel@tonic-gate * [including the GNU Public Licence.]
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
590Sstevel@tonic-gate #include <openssl/blowfish.h>
600Sstevel@tonic-gate #include "bf_locl.h"
610Sstevel@tonic-gate
620Sstevel@tonic-gate /* Blowfish as implemented from 'Blowfish: Springer-Verlag paper'
630Sstevel@tonic-gate * (From LECTURE NOTES IN COMPUTER SCIENCE 809, FAST SOFTWARE ENCRYPTION,
640Sstevel@tonic-gate * CAMBRIDGE SECURITY WORKSHOP, CAMBRIDGE, U.K., DECEMBER 9-11, 1993)
650Sstevel@tonic-gate */
660Sstevel@tonic-gate
670Sstevel@tonic-gate #if (BF_ROUNDS != 16) && (BF_ROUNDS != 20)
680Sstevel@tonic-gate #error If you set BF_ROUNDS to some value other than 16 or 20, you will have \
690Sstevel@tonic-gate to modify the code.
700Sstevel@tonic-gate #endif
710Sstevel@tonic-gate
BF_encrypt(BF_LONG * data,const BF_KEY * key)720Sstevel@tonic-gate void BF_encrypt(BF_LONG *data, const BF_KEY *key)
730Sstevel@tonic-gate {
740Sstevel@tonic-gate #ifndef BF_PTR2
750Sstevel@tonic-gate register BF_LONG l,r;
76*2139Sjp161948 register const BF_LONG *p,*s;
770Sstevel@tonic-gate
780Sstevel@tonic-gate p=key->P;
790Sstevel@tonic-gate s= &(key->S[0]);
800Sstevel@tonic-gate l=data[0];
810Sstevel@tonic-gate r=data[1];
820Sstevel@tonic-gate
830Sstevel@tonic-gate l^=p[0];
840Sstevel@tonic-gate BF_ENC(r,l,s,p[ 1]);
850Sstevel@tonic-gate BF_ENC(l,r,s,p[ 2]);
860Sstevel@tonic-gate BF_ENC(r,l,s,p[ 3]);
870Sstevel@tonic-gate BF_ENC(l,r,s,p[ 4]);
880Sstevel@tonic-gate BF_ENC(r,l,s,p[ 5]);
890Sstevel@tonic-gate BF_ENC(l,r,s,p[ 6]);
900Sstevel@tonic-gate BF_ENC(r,l,s,p[ 7]);
910Sstevel@tonic-gate BF_ENC(l,r,s,p[ 8]);
920Sstevel@tonic-gate BF_ENC(r,l,s,p[ 9]);
930Sstevel@tonic-gate BF_ENC(l,r,s,p[10]);
940Sstevel@tonic-gate BF_ENC(r,l,s,p[11]);
950Sstevel@tonic-gate BF_ENC(l,r,s,p[12]);
960Sstevel@tonic-gate BF_ENC(r,l,s,p[13]);
970Sstevel@tonic-gate BF_ENC(l,r,s,p[14]);
980Sstevel@tonic-gate BF_ENC(r,l,s,p[15]);
990Sstevel@tonic-gate BF_ENC(l,r,s,p[16]);
1000Sstevel@tonic-gate #if BF_ROUNDS == 20
1010Sstevel@tonic-gate BF_ENC(r,l,s,p[17]);
1020Sstevel@tonic-gate BF_ENC(l,r,s,p[18]);
1030Sstevel@tonic-gate BF_ENC(r,l,s,p[19]);
1040Sstevel@tonic-gate BF_ENC(l,r,s,p[20]);
1050Sstevel@tonic-gate #endif
1060Sstevel@tonic-gate r^=p[BF_ROUNDS+1];
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate data[1]=l&0xffffffffL;
1090Sstevel@tonic-gate data[0]=r&0xffffffffL;
1100Sstevel@tonic-gate #else
1110Sstevel@tonic-gate register BF_LONG l,r,t,*k;
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate l=data[0];
1140Sstevel@tonic-gate r=data[1];
1150Sstevel@tonic-gate k=(BF_LONG*)key;
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate l^=k[0];
1180Sstevel@tonic-gate BF_ENC(r,l,k, 1);
1190Sstevel@tonic-gate BF_ENC(l,r,k, 2);
1200Sstevel@tonic-gate BF_ENC(r,l,k, 3);
1210Sstevel@tonic-gate BF_ENC(l,r,k, 4);
1220Sstevel@tonic-gate BF_ENC(r,l,k, 5);
1230Sstevel@tonic-gate BF_ENC(l,r,k, 6);
1240Sstevel@tonic-gate BF_ENC(r,l,k, 7);
1250Sstevel@tonic-gate BF_ENC(l,r,k, 8);
1260Sstevel@tonic-gate BF_ENC(r,l,k, 9);
1270Sstevel@tonic-gate BF_ENC(l,r,k,10);
1280Sstevel@tonic-gate BF_ENC(r,l,k,11);
1290Sstevel@tonic-gate BF_ENC(l,r,k,12);
1300Sstevel@tonic-gate BF_ENC(r,l,k,13);
1310Sstevel@tonic-gate BF_ENC(l,r,k,14);
1320Sstevel@tonic-gate BF_ENC(r,l,k,15);
1330Sstevel@tonic-gate BF_ENC(l,r,k,16);
1340Sstevel@tonic-gate #if BF_ROUNDS == 20
1350Sstevel@tonic-gate BF_ENC(r,l,k,17);
1360Sstevel@tonic-gate BF_ENC(l,r,k,18);
1370Sstevel@tonic-gate BF_ENC(r,l,k,19);
1380Sstevel@tonic-gate BF_ENC(l,r,k,20);
1390Sstevel@tonic-gate #endif
1400Sstevel@tonic-gate r^=k[BF_ROUNDS+1];
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate data[1]=l&0xffffffffL;
1430Sstevel@tonic-gate data[0]=r&0xffffffffL;
1440Sstevel@tonic-gate #endif
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate #ifndef BF_DEFAULT_OPTIONS
1480Sstevel@tonic-gate
BF_decrypt(BF_LONG * data,const BF_KEY * key)1490Sstevel@tonic-gate void BF_decrypt(BF_LONG *data, const BF_KEY *key)
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate #ifndef BF_PTR2
1520Sstevel@tonic-gate register BF_LONG l,r;
153*2139Sjp161948 register const BF_LONG *p,*s;
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate p=key->P;
1560Sstevel@tonic-gate s= &(key->S[0]);
1570Sstevel@tonic-gate l=data[0];
1580Sstevel@tonic-gate r=data[1];
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate l^=p[BF_ROUNDS+1];
1610Sstevel@tonic-gate #if BF_ROUNDS == 20
1620Sstevel@tonic-gate BF_ENC(r,l,s,p[20]);
1630Sstevel@tonic-gate BF_ENC(l,r,s,p[19]);
1640Sstevel@tonic-gate BF_ENC(r,l,s,p[18]);
1650Sstevel@tonic-gate BF_ENC(l,r,s,p[17]);
1660Sstevel@tonic-gate #endif
1670Sstevel@tonic-gate BF_ENC(r,l,s,p[16]);
1680Sstevel@tonic-gate BF_ENC(l,r,s,p[15]);
1690Sstevel@tonic-gate BF_ENC(r,l,s,p[14]);
1700Sstevel@tonic-gate BF_ENC(l,r,s,p[13]);
1710Sstevel@tonic-gate BF_ENC(r,l,s,p[12]);
1720Sstevel@tonic-gate BF_ENC(l,r,s,p[11]);
1730Sstevel@tonic-gate BF_ENC(r,l,s,p[10]);
1740Sstevel@tonic-gate BF_ENC(l,r,s,p[ 9]);
1750Sstevel@tonic-gate BF_ENC(r,l,s,p[ 8]);
1760Sstevel@tonic-gate BF_ENC(l,r,s,p[ 7]);
1770Sstevel@tonic-gate BF_ENC(r,l,s,p[ 6]);
1780Sstevel@tonic-gate BF_ENC(l,r,s,p[ 5]);
1790Sstevel@tonic-gate BF_ENC(r,l,s,p[ 4]);
1800Sstevel@tonic-gate BF_ENC(l,r,s,p[ 3]);
1810Sstevel@tonic-gate BF_ENC(r,l,s,p[ 2]);
1820Sstevel@tonic-gate BF_ENC(l,r,s,p[ 1]);
1830Sstevel@tonic-gate r^=p[0];
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate data[1]=l&0xffffffffL;
1860Sstevel@tonic-gate data[0]=r&0xffffffffL;
1870Sstevel@tonic-gate #else
1880Sstevel@tonic-gate register BF_LONG l,r,t,*k;
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate l=data[0];
1910Sstevel@tonic-gate r=data[1];
1920Sstevel@tonic-gate k=(BF_LONG *)key;
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate l^=k[BF_ROUNDS+1];
1950Sstevel@tonic-gate #if BF_ROUNDS == 20
1960Sstevel@tonic-gate BF_ENC(r,l,k,20);
1970Sstevel@tonic-gate BF_ENC(l,r,k,19);
1980Sstevel@tonic-gate BF_ENC(r,l,k,18);
1990Sstevel@tonic-gate BF_ENC(l,r,k,17);
2000Sstevel@tonic-gate #endif
2010Sstevel@tonic-gate BF_ENC(r,l,k,16);
2020Sstevel@tonic-gate BF_ENC(l,r,k,15);
2030Sstevel@tonic-gate BF_ENC(r,l,k,14);
2040Sstevel@tonic-gate BF_ENC(l,r,k,13);
2050Sstevel@tonic-gate BF_ENC(r,l,k,12);
2060Sstevel@tonic-gate BF_ENC(l,r,k,11);
2070Sstevel@tonic-gate BF_ENC(r,l,k,10);
2080Sstevel@tonic-gate BF_ENC(l,r,k, 9);
2090Sstevel@tonic-gate BF_ENC(r,l,k, 8);
2100Sstevel@tonic-gate BF_ENC(l,r,k, 7);
2110Sstevel@tonic-gate BF_ENC(r,l,k, 6);
2120Sstevel@tonic-gate BF_ENC(l,r,k, 5);
2130Sstevel@tonic-gate BF_ENC(r,l,k, 4);
2140Sstevel@tonic-gate BF_ENC(l,r,k, 3);
2150Sstevel@tonic-gate BF_ENC(r,l,k, 2);
2160Sstevel@tonic-gate BF_ENC(l,r,k, 1);
2170Sstevel@tonic-gate r^=k[0];
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate data[1]=l&0xffffffffL;
2200Sstevel@tonic-gate data[0]=r&0xffffffffL;
2210Sstevel@tonic-gate #endif
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate
BF_cbc_encrypt(const unsigned char * in,unsigned char * out,long length,const BF_KEY * schedule,unsigned char * ivec,int encrypt)2240Sstevel@tonic-gate void BF_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
2250Sstevel@tonic-gate const BF_KEY *schedule, unsigned char *ivec, int encrypt)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate register BF_LONG tin0,tin1;
2280Sstevel@tonic-gate register BF_LONG tout0,tout1,xor0,xor1;
2290Sstevel@tonic-gate register long l=length;
2300Sstevel@tonic-gate BF_LONG tin[2];
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate if (encrypt)
2330Sstevel@tonic-gate {
2340Sstevel@tonic-gate n2l(ivec,tout0);
2350Sstevel@tonic-gate n2l(ivec,tout1);
2360Sstevel@tonic-gate ivec-=8;
2370Sstevel@tonic-gate for (l-=8; l>=0; l-=8)
2380Sstevel@tonic-gate {
2390Sstevel@tonic-gate n2l(in,tin0);
2400Sstevel@tonic-gate n2l(in,tin1);
2410Sstevel@tonic-gate tin0^=tout0;
2420Sstevel@tonic-gate tin1^=tout1;
2430Sstevel@tonic-gate tin[0]=tin0;
2440Sstevel@tonic-gate tin[1]=tin1;
2450Sstevel@tonic-gate BF_encrypt(tin,schedule);
2460Sstevel@tonic-gate tout0=tin[0];
2470Sstevel@tonic-gate tout1=tin[1];
2480Sstevel@tonic-gate l2n(tout0,out);
2490Sstevel@tonic-gate l2n(tout1,out);
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate if (l != -8)
2520Sstevel@tonic-gate {
2530Sstevel@tonic-gate n2ln(in,tin0,tin1,l+8);
2540Sstevel@tonic-gate tin0^=tout0;
2550Sstevel@tonic-gate tin1^=tout1;
2560Sstevel@tonic-gate tin[0]=tin0;
2570Sstevel@tonic-gate tin[1]=tin1;
2580Sstevel@tonic-gate BF_encrypt(tin,schedule);
2590Sstevel@tonic-gate tout0=tin[0];
2600Sstevel@tonic-gate tout1=tin[1];
2610Sstevel@tonic-gate l2n(tout0,out);
2620Sstevel@tonic-gate l2n(tout1,out);
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate l2n(tout0,ivec);
2650Sstevel@tonic-gate l2n(tout1,ivec);
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate else
2680Sstevel@tonic-gate {
2690Sstevel@tonic-gate n2l(ivec,xor0);
2700Sstevel@tonic-gate n2l(ivec,xor1);
2710Sstevel@tonic-gate ivec-=8;
2720Sstevel@tonic-gate for (l-=8; l>=0; l-=8)
2730Sstevel@tonic-gate {
2740Sstevel@tonic-gate n2l(in,tin0);
2750Sstevel@tonic-gate n2l(in,tin1);
2760Sstevel@tonic-gate tin[0]=tin0;
2770Sstevel@tonic-gate tin[1]=tin1;
2780Sstevel@tonic-gate BF_decrypt(tin,schedule);
2790Sstevel@tonic-gate tout0=tin[0]^xor0;
2800Sstevel@tonic-gate tout1=tin[1]^xor1;
2810Sstevel@tonic-gate l2n(tout0,out);
2820Sstevel@tonic-gate l2n(tout1,out);
2830Sstevel@tonic-gate xor0=tin0;
2840Sstevel@tonic-gate xor1=tin1;
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate if (l != -8)
2870Sstevel@tonic-gate {
2880Sstevel@tonic-gate n2l(in,tin0);
2890Sstevel@tonic-gate n2l(in,tin1);
2900Sstevel@tonic-gate tin[0]=tin0;
2910Sstevel@tonic-gate tin[1]=tin1;
2920Sstevel@tonic-gate BF_decrypt(tin,schedule);
2930Sstevel@tonic-gate tout0=tin[0]^xor0;
2940Sstevel@tonic-gate tout1=tin[1]^xor1;
2950Sstevel@tonic-gate l2nn(tout0,tout1,out,l+8);
2960Sstevel@tonic-gate xor0=tin0;
2970Sstevel@tonic-gate xor1=tin1;
2980Sstevel@tonic-gate }
2990Sstevel@tonic-gate l2n(xor0,ivec);
3000Sstevel@tonic-gate l2n(xor1,ivec);
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate tin0=tin1=tout0=tout1=xor0=xor1=0;
3030Sstevel@tonic-gate tin[0]=tin[1]=0;
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate
3060Sstevel@tonic-gate #endif
307