/* $KAME: des_enc.c,v 1.1 2001/09/10 04:03:58 itojun Exp $ */12/* crypto/des/des_enc.c */34/* Copyright (C) 1995-1998 Eric Young ([email protected])5* All rights reserved.6*7* This package is an SSL implementation written8* 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 as12* the following conditions are aheared to. The following conditions13* 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 documentation15* included with this distribution is covered by the same copyright terms16* except that the holder is Tim Hudson ([email protected]).17*18* Copyright remains Eric Young's, and as such any Copyright notices in19* the code are not to be removed.20* If this package is used in a product, Eric Young should be given attribution21* as the author of the parts of the library used.22* This can be in the form of a textual message at program startup or23* in documentation (online or textual) provided with the package.24*25* Redistribution and use in source and binary forms, with or without26* modification, are permitted provided that the following conditions27* are met:28* 1. Redistributions of source code must retain the copyright29* notice, this list of conditions and the following disclaimer.30* 2. Redistributions in binary form must reproduce the above copyright31* notice, this list of conditions and the following disclaimer in the32* documentation and/or other materials provided with the distribution.33* 3. All advertising materials mentioning features or use of this software34* must display the following acknowledgement:35* "This product includes cryptographic software written by36* Eric Young ([email protected])"37* The word 'cryptographic' can be left out if the rouines from the library38* being used are not cryptographic related :-).39* 4. If you include any Windows specific code (or a derivative thereof) from40* 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'' AND44* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE45* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE46* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE47* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL48* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS49* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)50* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT51* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY52* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF53* SUCH DAMAGE.54*55* The licence and distribution terms for any publically available version or56* derivative of this code cannot be changed. i.e. this code cannot simply be57* copied and put under another distribution licence58* [including the GNU Public Licence.]59*/6061#include <sys/types.h>62#include <crypto/des/des_locl.h>6364extern const DES_LONG des_SPtrans[8][64];6566void des_encrypt1(DES_LONG *data, des_key_schedule ks, int enc)67{68DES_LONG l,r,t,u;69#ifdef DES_PTR70const unsigned char *des_SP=(const unsigned char *)des_SPtrans;71#endif72#ifndef DES_UNROLL73int i;74#endif75DES_LONG *s;7677r=data[0];78l=data[1];7980IP(r,l);81/* Things have been modified so that the initial rotate is82* done outside the loop. This required the83* des_SPtrans values in sp.h to be rotated 1 bit to the right.84* One perl script later and things have a 5% speed up on a sparc2.85* Thanks to Richard Outerbridge <[email protected]>86* for pointing this out. */87/* clear the top bits on machines with 8byte longs */88/* shift left by 2 */89r=ROTATE(r,29)&0xffffffffL;90l=ROTATE(l,29)&0xffffffffL;9192s=ks->ks.deslong;93/* I don't know if it is worth the effort of loop unrolling the94* inner loop */95if (enc)96{97#ifdef DES_UNROLL98D_ENCRYPT(l,r, 0); /* 1 */99D_ENCRYPT(r,l, 2); /* 2 */100D_ENCRYPT(l,r, 4); /* 3 */101D_ENCRYPT(r,l, 6); /* 4 */102D_ENCRYPT(l,r, 8); /* 5 */103D_ENCRYPT(r,l,10); /* 6 */104D_ENCRYPT(l,r,12); /* 7 */105D_ENCRYPT(r,l,14); /* 8 */106D_ENCRYPT(l,r,16); /* 9 */107D_ENCRYPT(r,l,18); /* 10 */108D_ENCRYPT(l,r,20); /* 11 */109D_ENCRYPT(r,l,22); /* 12 */110D_ENCRYPT(l,r,24); /* 13 */111D_ENCRYPT(r,l,26); /* 14 */112D_ENCRYPT(l,r,28); /* 15 */113D_ENCRYPT(r,l,30); /* 16 */114#else115for (i=0; i<32; i+=8)116{117D_ENCRYPT(l,r,i+0); /* 1 */118D_ENCRYPT(r,l,i+2); /* 2 */119D_ENCRYPT(l,r,i+4); /* 3 */120D_ENCRYPT(r,l,i+6); /* 4 */121}122#endif123}124else125{126#ifdef DES_UNROLL127D_ENCRYPT(l,r,30); /* 16 */128D_ENCRYPT(r,l,28); /* 15 */129D_ENCRYPT(l,r,26); /* 14 */130D_ENCRYPT(r,l,24); /* 13 */131D_ENCRYPT(l,r,22); /* 12 */132D_ENCRYPT(r,l,20); /* 11 */133D_ENCRYPT(l,r,18); /* 10 */134D_ENCRYPT(r,l,16); /* 9 */135D_ENCRYPT(l,r,14); /* 8 */136D_ENCRYPT(r,l,12); /* 7 */137D_ENCRYPT(l,r,10); /* 6 */138D_ENCRYPT(r,l, 8); /* 5 */139D_ENCRYPT(l,r, 6); /* 4 */140D_ENCRYPT(r,l, 4); /* 3 */141D_ENCRYPT(l,r, 2); /* 2 */142D_ENCRYPT(r,l, 0); /* 1 */143#else144for (i=30; i>0; i-=8)145{146D_ENCRYPT(l,r,i-0); /* 16 */147D_ENCRYPT(r,l,i-2); /* 15 */148D_ENCRYPT(l,r,i-4); /* 14 */149D_ENCRYPT(r,l,i-6); /* 13 */150}151#endif152}153154/* rotate and clear the top bits on machines with 8byte longs */155l=ROTATE(l,3)&0xffffffffL;156r=ROTATE(r,3)&0xffffffffL;157158FP(r,l);159data[0]=l;160data[1]=r;161l=r=t=u=0;162}163164void des_encrypt2(DES_LONG *data, des_key_schedule ks, int enc)165{166DES_LONG l,r,t,u;167#ifdef DES_PTR168const unsigned char *des_SP=(const unsigned char *)des_SPtrans;169#endif170#ifndef DES_UNROLL171int i;172#endif173DES_LONG *s;174175r=data[0];176l=data[1];177178/* Things have been modified so that the initial rotate is179* done outside the loop. This required the180* des_SPtrans values in sp.h to be rotated 1 bit to the right.181* One perl script later and things have a 5% speed up on a sparc2.182* Thanks to Richard Outerbridge <[email protected]>183* for pointing this out. */184/* clear the top bits on machines with 8byte longs */185r=ROTATE(r,29)&0xffffffffL;186l=ROTATE(l,29)&0xffffffffL;187188s=ks->ks.deslong;189/* I don't know if it is worth the effort of loop unrolling the190* inner loop */191if (enc)192{193#ifdef DES_UNROLL194D_ENCRYPT(l,r, 0); /* 1 */195D_ENCRYPT(r,l, 2); /* 2 */196D_ENCRYPT(l,r, 4); /* 3 */197D_ENCRYPT(r,l, 6); /* 4 */198D_ENCRYPT(l,r, 8); /* 5 */199D_ENCRYPT(r,l,10); /* 6 */200D_ENCRYPT(l,r,12); /* 7 */201D_ENCRYPT(r,l,14); /* 8 */202D_ENCRYPT(l,r,16); /* 9 */203D_ENCRYPT(r,l,18); /* 10 */204D_ENCRYPT(l,r,20); /* 11 */205D_ENCRYPT(r,l,22); /* 12 */206D_ENCRYPT(l,r,24); /* 13 */207D_ENCRYPT(r,l,26); /* 14 */208D_ENCRYPT(l,r,28); /* 15 */209D_ENCRYPT(r,l,30); /* 16 */210#else211for (i=0; i<32; i+=8)212{213D_ENCRYPT(l,r,i+0); /* 1 */214D_ENCRYPT(r,l,i+2); /* 2 */215D_ENCRYPT(l,r,i+4); /* 3 */216D_ENCRYPT(r,l,i+6); /* 4 */217}218#endif219}220else221{222#ifdef DES_UNROLL223D_ENCRYPT(l,r,30); /* 16 */224D_ENCRYPT(r,l,28); /* 15 */225D_ENCRYPT(l,r,26); /* 14 */226D_ENCRYPT(r,l,24); /* 13 */227D_ENCRYPT(l,r,22); /* 12 */228D_ENCRYPT(r,l,20); /* 11 */229D_ENCRYPT(l,r,18); /* 10 */230D_ENCRYPT(r,l,16); /* 9 */231D_ENCRYPT(l,r,14); /* 8 */232D_ENCRYPT(r,l,12); /* 7 */233D_ENCRYPT(l,r,10); /* 6 */234D_ENCRYPT(r,l, 8); /* 5 */235D_ENCRYPT(l,r, 6); /* 4 */236D_ENCRYPT(r,l, 4); /* 3 */237D_ENCRYPT(l,r, 2); /* 2 */238D_ENCRYPT(r,l, 0); /* 1 */239#else240for (i=30; i>0; i-=8)241{242D_ENCRYPT(l,r,i-0); /* 16 */243D_ENCRYPT(r,l,i-2); /* 15 */244D_ENCRYPT(l,r,i-4); /* 14 */245D_ENCRYPT(r,l,i-6); /* 13 */246}247#endif248}249/* rotate and clear the top bits on machines with 8byte longs */250data[0]=ROTATE(l,3)&0xffffffffL;251data[1]=ROTATE(r,3)&0xffffffffL;252l=r=t=u=0;253}254255void des_encrypt3(DES_LONG *data, des_key_schedule ks1, des_key_schedule ks2,256des_key_schedule ks3)257{258DES_LONG l,r;259260l=data[0];261r=data[1];262IP(l,r);263data[0]=l;264data[1]=r;265des_encrypt2((DES_LONG *)data,ks1,DES_ENCRYPT);266des_encrypt2((DES_LONG *)data,ks2,DES_DECRYPT);267des_encrypt2((DES_LONG *)data,ks3,DES_ENCRYPT);268l=data[0];269r=data[1];270FP(r,l);271data[0]=l;272data[1]=r;273}274275void des_decrypt3(DES_LONG *data, des_key_schedule ks1, des_key_schedule ks2,276des_key_schedule ks3)277{278DES_LONG l,r;279280l=data[0];281r=data[1];282IP(l,r);283data[0]=l;284data[1]=r;285des_encrypt2((DES_LONG *)data,ks3,DES_DECRYPT);286des_encrypt2((DES_LONG *)data,ks2,DES_ENCRYPT);287des_encrypt2((DES_LONG *)data,ks1,DES_DECRYPT);288l=data[0];289r=data[1];290FP(r,l);291data[0]=l;292data[1]=r;293}294295296