|
|
1.1.1.4 ! root 1: /* Deprecated/legacy */ ! 2: 1.1 root 3: /* crypto/bf/bf_enc.c */ 4: /* Copyright (C) 1995-1997 Eric Young ([email protected]) 5: * All rights reserved. 6: * 7: * This package is an SSL implementation written 8: * by Eric Young ([email protected]). 9: * The implementation was written so as to conform with Netscapes SSL. 10: * 11: * This library is free for commercial and non-commercial use as long as 12: * the following conditions are aheared to. The following conditions 13: * apply to all code found in this distribution, be it the RC4, RSA, 14: * lhash, DES, etc., code; not just the SSL code. The SSL documentation 15: * included with this distribution is covered by the same copyright terms 16: * except that the holder is Tim Hudson ([email protected]). 17: * 18: * Copyright remains Eric Young's, and as such any Copyright notices in 19: * the code are not to be removed. 20: * If this package is used in a product, Eric Young should be given attribution 21: * as the author of the parts of the library used. 22: * This can be in the form of a textual message at program startup or 23: * in documentation (online or textual) provided with the package. 24: * 25: * Redistribution and use in source and binary forms, with or without 26: * modification, are permitted provided that the following conditions 27: * are met: 28: * 1. Redistributions of source code must retain the copyright 29: * notice, this list of conditions and the following disclaimer. 30: * 2. Redistributions in binary form must reproduce the above copyright 31: * notice, this list of conditions and the following disclaimer in the 32: * documentation and/or other materials provided with the distribution. 33: * 3. All advertising materials mentioning features or use of this software 34: * must display the following acknowledgement: 35: * "This product includes cryptographic software written by 36: * Eric Young ([email protected])" 37: * The word 'cryptographic' can be left out if the rouines from the library 38: * being used are not cryptographic related :-). 39: * 4. If you include any Windows specific code (or a derivative thereof) from 40: * the apps directory (application code) you must include an acknowledgement: 41: * "This product includes software written by Tim Hudson ([email protected])" 42: * 43: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 44: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 45: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 46: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 47: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 48: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 49: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 50: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 51: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 52: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 53: * SUCH DAMAGE. 54: * 55: * The licence and distribution terms for any publically available version or 56: * derivative of this code cannot be changed. i.e. this code cannot simply be 57: * copied and put under another distribution licence 58: * [including the GNU Public Licence.] 59: */ 60: 61: 62: 1.1.1.2 root 63: #include "Blowfish.h" 64: #include "Bf_locl.h" 1.1 root 65: 66: /* Blowfish as implemented from 'Blowfish: Springer-Verlag paper' 67: * (From LECTURE NOTES IN COIMPUTER SCIENCE 809, FAST SOFTWARE ENCRYPTION, 68: * CAMBRIDGE SECURITY WORKSHOP, CAMBRIDGE, U.K., DECEMBER 9-11, 1993) 69: */ 70: 71: #if (BF_ROUNDS != 16) && (BF_ROUNDS != 20) 72: If you set BF_ROUNDS to some value other than 16 or 20, you will have 73: to modify the code. 74: #endif 75: 76: void BF_encrypt(data,key) 77: BF_LONG *data; 78: BF_KEY *key; 79: { 80: register BF_LONG l,r,*p,*s; 81: 82: p=key->P; 83: s= &(key->S[0]); 84: l=data[0]; 85: r=data[1]; 86: 87: l^=p[0]; 88: BF_ENC(r,l,s,p[ 1]); 89: BF_ENC(l,r,s,p[ 2]); 90: BF_ENC(r,l,s,p[ 3]); 91: BF_ENC(l,r,s,p[ 4]); 92: BF_ENC(r,l,s,p[ 5]); 93: BF_ENC(l,r,s,p[ 6]); 94: BF_ENC(r,l,s,p[ 7]); 95: BF_ENC(l,r,s,p[ 8]); 96: BF_ENC(r,l,s,p[ 9]); 97: BF_ENC(l,r,s,p[10]); 98: BF_ENC(r,l,s,p[11]); 99: BF_ENC(l,r,s,p[12]); 100: BF_ENC(r,l,s,p[13]); 101: BF_ENC(l,r,s,p[14]); 102: BF_ENC(r,l,s,p[15]); 103: BF_ENC(l,r,s,p[16]); 104: #if BF_ROUNDS == 20 105: BF_ENC(r,l,s,p[17]); 106: BF_ENC(l,r,s,p[18]); 107: BF_ENC(r,l,s,p[19]); 108: BF_ENC(l,r,s,p[20]); 109: #endif 110: r^=p[BF_ROUNDS+1]; 111: 112: data[1]=l&0xffffffffL; 113: data[0]=r&0xffffffffL; 114: } 115: 116: #ifndef BF_DEFAULT_OPTIONS 117: 118: void BF_decrypt(data,key) 119: BF_LONG *data; 120: BF_KEY *key; 121: { 122: register BF_LONG l,r,*p,*s; 123: 124: p=key->P; 125: s= &(key->S[0]); 126: l=data[0]; 127: r=data[1]; 128: 129: l^=p[BF_ROUNDS+1]; 130: #if BF_ROUNDS == 20 131: BF_ENC(r,l,s,p[20]); 132: BF_ENC(l,r,s,p[19]); 133: BF_ENC(r,l,s,p[18]); 134: BF_ENC(l,r,s,p[17]); 135: #endif 136: BF_ENC(r,l,s,p[16]); 137: BF_ENC(l,r,s,p[15]); 138: BF_ENC(r,l,s,p[14]); 139: BF_ENC(l,r,s,p[13]); 140: BF_ENC(r,l,s,p[12]); 141: BF_ENC(l,r,s,p[11]); 142: BF_ENC(r,l,s,p[10]); 143: BF_ENC(l,r,s,p[ 9]); 144: BF_ENC(r,l,s,p[ 8]); 145: BF_ENC(l,r,s,p[ 7]); 146: BF_ENC(r,l,s,p[ 6]); 147: BF_ENC(l,r,s,p[ 5]); 148: BF_ENC(r,l,s,p[ 4]); 149: BF_ENC(l,r,s,p[ 3]); 150: BF_ENC(r,l,s,p[ 2]); 151: BF_ENC(l,r,s,p[ 1]); 152: r^=p[0]; 153: 154: data[1]=l&0xffffffffL; 155: data[0]=r&0xffffffffL; 156: } 157: 158: void BF_cbc_encrypt(in, out, length, ks, iv, encrypt) 159: unsigned char *in; 160: unsigned char *out; 161: long length; 162: BF_KEY *ks; 163: unsigned char *iv; 164: int encrypt; 165: { 166: register BF_LONG tin0,tin1; 167: register BF_LONG tout0,tout1,xor0,xor1; 168: register long l=length; 169: BF_LONG tin[2]; 170: 171: if (encrypt) 172: { 173: n2l(iv,tout0); 174: n2l(iv,tout1); 175: iv-=8; 176: for (l-=8; l>=0; l-=8) 177: { 178: n2l(in,tin0); 179: n2l(in,tin1); 180: tin0^=tout0; 181: tin1^=tout1; 182: tin[0]=tin0; 183: tin[1]=tin1; 184: BF_encrypt(tin,ks); 185: tout0=tin[0]; 186: tout1=tin[1]; 187: l2n(tout0,out); 188: l2n(tout1,out); 189: } 190: if (l != -8) 191: { 192: n2ln(in,tin0,tin1,l+8); 193: tin0^=tout0; 194: tin1^=tout1; 195: tin[0]=tin0; 196: tin[1]=tin1; 197: BF_encrypt(tin,ks); 198: tout0=tin[0]; 199: tout1=tin[1]; 200: l2n(tout0,out); 201: l2n(tout1,out); 202: } 203: l2n(tout0,iv); 204: l2n(tout1,iv); 205: } 206: else 207: { 208: n2l(iv,xor0); 209: n2l(iv,xor1); 210: iv-=8; 211: for (l-=8; l>=0; l-=8) 212: { 213: n2l(in,tin0); 214: n2l(in,tin1); 215: tin[0]=tin0; 216: tin[1]=tin1; 217: BF_decrypt(tin,ks); 218: tout0=tin[0]^xor0; 219: tout1=tin[1]^xor1; 220: l2n(tout0,out); 221: l2n(tout1,out); 222: xor0=tin0; 223: xor1=tin1; 224: } 225: if (l != -8) 226: { 227: n2l(in,tin0); 228: n2l(in,tin1); 229: tin[0]=tin0; 230: tin[1]=tin1; 231: BF_decrypt(tin,ks); 232: tout0=tin[0]^xor0; 233: tout1=tin[1]^xor1; 234: l2nn(tout0,tout1,out,l+8); 235: xor0=tin0; 236: xor1=tin1; 237: } 238: l2n(xor0,iv); 239: l2n(xor1,iv); 240: } 241: tin0=tin1=tout0=tout1=xor0=xor1=0; 242: tin[0]=tin[1]=0; 243: } 244: 245: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.