bf_enc.c revision 1.5
11.1Sthorpej/* crypto/bf/bf_enc.c */ 21.4Stls/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 31.1Sthorpej * All rights reserved. 41.1Sthorpej * 51.1Sthorpej * This package is an SSL implementation written 61.4Stls * by Eric Young (eay@cryptsoft.com). 71.1Sthorpej * The implementation was written so as to conform with Netscapes SSL. 81.1Sthorpej * 91.1Sthorpej * This library is free for commercial and non-commercial use as long as 101.1Sthorpej * the following conditions are aheared to. The following conditions 111.1Sthorpej * apply to all code found in this distribution, be it the RC4, RSA, 121.1Sthorpej * lhash, DES, etc., code; not just the SSL code. The SSL documentation 131.1Sthorpej * included with this distribution is covered by the same copyright terms 141.4Stls * except that the holder is Tim Hudson (tjh@cryptsoft.com). 151.1Sthorpej * 161.1Sthorpej * Copyright remains Eric Young's, and as such any Copyright notices in 171.1Sthorpej * the code are not to be removed. 181.1Sthorpej * If this package is used in a product, Eric Young should be given attribution 191.1Sthorpej * as the author of the parts of the library used. 201.1Sthorpej * This can be in the form of a textual message at program startup or 211.1Sthorpej * in documentation (online or textual) provided with the package. 221.1Sthorpej * 231.1Sthorpej * Redistribution and use in source and binary forms, with or without 241.1Sthorpej * modification, are permitted provided that the following conditions 251.1Sthorpej * are met: 261.1Sthorpej * 1. Redistributions of source code must retain the copyright 271.1Sthorpej * notice, this list of conditions and the following disclaimer. 281.1Sthorpej * 2. Redistributions in binary form must reproduce the above copyright 291.1Sthorpej * notice, this list of conditions and the following disclaimer in the 301.1Sthorpej * documentation and/or other materials provided with the distribution. 311.1Sthorpej * 3. All advertising materials mentioning features or use of this software 321.1Sthorpej * must display the following acknowledgement: 331.1Sthorpej * "This product includes cryptographic software written by 341.4Stls * Eric Young (eay@cryptsoft.com)" 351.1Sthorpej * The word 'cryptographic' can be left out if the rouines from the library 361.1Sthorpej * being used are not cryptographic related :-). 371.1Sthorpej * 4. If you include any Windows specific code (or a derivative thereof) from 381.1Sthorpej * the apps directory (application code) you must include an acknowledgement: 391.4Stls * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 401.1Sthorpej * 411.1Sthorpej * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 421.1Sthorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 431.1Sthorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 441.1Sthorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 451.1Sthorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 461.1Sthorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 471.1Sthorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 481.1Sthorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 491.1Sthorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 501.1Sthorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 511.1Sthorpej * SUCH DAMAGE. 521.1Sthorpej * 531.1Sthorpej * The licence and distribution terms for any publically available version or 541.1Sthorpej * derivative of this code cannot be changed. i.e. this code cannot simply be 551.1Sthorpej * copied and put under another distribution licence 561.1Sthorpej * [including the GNU Public Licence.] 571.1Sthorpej */ 581.1Sthorpej 591.5Slukem#include <sys/cdefs.h> 601.5Slukem__KERNEL_RCSID(0, "$NetBSD: bf_enc.c,v 1.5 2001/11/13 01:40:08 lukem Exp $"); 611.5Slukem 621.2Sitojun#include <sys/types.h> 631.1Sthorpej#include <crypto/blowfish/blowfish.h> 641.1Sthorpej#include <crypto/blowfish/bf_locl.h> 651.1Sthorpej 661.1Sthorpej/* Blowfish as implemented from 'Blowfish: Springer-Verlag paper' 671.1Sthorpej * (From LECTURE NOTES IN COIMPUTER SCIENCE 809, FAST SOFTWARE ENCRYPTION, 681.1Sthorpej * CAMBRIDGE SECURITY WORKSHOP, CAMBRIDGE, U.K., DECEMBER 9-11, 1993) 691.1Sthorpej */ 701.1Sthorpej 711.1Sthorpej#if (BF_ROUNDS != 16) && (BF_ROUNDS != 20) 721.1SthorpejIf you set BF_ROUNDS to some value other than 16 or 20, you will have 731.1Sthorpejto modify the code. 741.1Sthorpej#endif 751.1Sthorpej 761.3Sitojun/* XXX "data" is host endian */ 771.3Sitojunvoid 781.3SitojunBF_encrypt(data, key, encrypt) 791.3Sitojun BF_LONG *data; 801.3Sitojun BF_KEY *key; 811.3Sitojun int encrypt; 821.3Sitojun{ 831.3Sitojun register BF_LONG l, r, *p, *s; 841.3Sitojun 851.3Sitojun p = key->P; 861.3Sitojun s= &key->S[0]; 871.3Sitojun l = data[0]; 881.3Sitojun r = data[1]; 891.1Sthorpej 901.3Sitojun if (encrypt) { 911.1Sthorpej l^=p[0]; 921.3Sitojun BF_ENC(r, l, s, p[ 1]); 931.3Sitojun BF_ENC(l, r, s, p[ 2]); 941.3Sitojun BF_ENC(r, l, s, p[ 3]); 951.3Sitojun BF_ENC(l, r, s, p[ 4]); 961.3Sitojun BF_ENC(r, l, s, p[ 5]); 971.3Sitojun BF_ENC(l, r, s, p[ 6]); 981.3Sitojun BF_ENC(r, l, s, p[ 7]); 991.3Sitojun BF_ENC(l, r, s, p[ 8]); 1001.3Sitojun BF_ENC(r, l, s, p[ 9]); 1011.3Sitojun BF_ENC(l, r, s, p[10]); 1021.3Sitojun BF_ENC(r, l, s, p[11]); 1031.3Sitojun BF_ENC(l, r, s, p[12]); 1041.3Sitojun BF_ENC(r, l, s, p[13]); 1051.3Sitojun BF_ENC(l, r, s, p[14]); 1061.3Sitojun BF_ENC(r, l, s, p[15]); 1071.3Sitojun BF_ENC(l, r, s, p[16]); 1081.1Sthorpej#if BF_ROUNDS == 20 1091.3Sitojun BF_ENC(r, l, s, p[17]); 1101.3Sitojun BF_ENC(l, r, s, p[18]); 1111.3Sitojun BF_ENC(r, l, s, p[19]); 1121.3Sitojun BF_ENC(l, r, s, p[20]); 1131.1Sthorpej#endif 1141.3Sitojun r ^= p[BF_ROUNDS + 1]; 1151.3Sitojun } else { 1161.3Sitojun l ^= p[BF_ROUNDS + 1]; 1171.1Sthorpej#if BF_ROUNDS == 20 1181.3Sitojun BF_ENC(r, l, s, p[20]); 1191.3Sitojun BF_ENC(l, r, s, p[19]); 1201.3Sitojun BF_ENC(r, l, s, p[18]); 1211.3Sitojun BF_ENC(l, r, s, p[17]); 1221.1Sthorpej#endif 1231.3Sitojun BF_ENC(r, l, s, p[16]); 1241.3Sitojun BF_ENC(l, r, s, p[15]); 1251.3Sitojun BF_ENC(r, l, s, p[14]); 1261.3Sitojun BF_ENC(l, r, s, p[13]); 1271.3Sitojun BF_ENC(r, l, s, p[12]); 1281.3Sitojun BF_ENC(l, r, s, p[11]); 1291.3Sitojun BF_ENC(r, l, s, p[10]); 1301.3Sitojun BF_ENC(l, r, s, p[ 9]); 1311.3Sitojun BF_ENC(r, l, s, p[ 8]); 1321.3Sitojun BF_ENC(l, r, s, p[ 7]); 1331.3Sitojun BF_ENC(r, l, s, p[ 6]); 1341.3Sitojun BF_ENC(l, r, s, p[ 5]); 1351.3Sitojun BF_ENC(r, l, s, p[ 4]); 1361.3Sitojun BF_ENC(l, r, s, p[ 3]); 1371.3Sitojun BF_ENC(r, l, s, p[ 2]); 1381.3Sitojun BF_ENC(l, r, s, p[ 1]); 1391.3Sitojun r ^= p[0]; 1401.1Sthorpej } 1411.3Sitojun data[1] = l & 0xffffffff; 1421.3Sitojun data[0] = r & 0xffffffff; 1431.3Sitojun} 144