POK
/home/jaouen/pok_official/pok/trunk/libpok/protocols/des/des_enc.c
00001 /*
00002  *                               POK header
00003  * 
00004  * The following file is a part of the POK project. Any modification should
00005  * made according to the POK licence. You CANNOT use this file or a part of
00006  * this file is this part of a file for your own project
00007  *
00008  * For more information on the POK licence, please see our LICENCE FILE
00009  *
00010  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
00011  *
00012  *                                      Copyright (c) 2007-2009 POK team 
00013  *
00014  * Created by julien on Tue Dec  8 15:53:28 2009 
00015  */
00016 
00017 /* crypto/des/des_enc.c */
00018 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
00019  * All rights reserved.
00020  *
00021  * This package is an SSL implementation written
00022  * by Eric Young (eay@cryptsoft.com).
00023  * The implementation was written so as to conform with Netscapes SSL.
00024  * 
00025  * This library is free for commercial and non-commercial use as long as
00026  * the following conditions are aheared to.  The following conditions
00027  * apply to all code found in this distribution, be it the RC4, RSA,
00028  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
00029  * included with this distribution is covered by the same copyright terms
00030  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
00031  * 
00032  * Copyright remains Eric Young's, and as such any Copyright notices in
00033  * the code are not to be removed.
00034  * If this package is used in a product, Eric Young should be given attribution
00035  * as the author of the parts of the library used.
00036  * This can be in the form of a textual message at program startup or
00037  * in documentation (online or textual) provided with the package.
00038  * 
00039  * Redistribution and use in source and binary forms, with or without
00040  * modification, are permitted provided that the following conditions
00041  * are met:
00042  * 1. Redistributions of source code must retain the copyright
00043  *    notice, this list of conditions and the following disclaimer.
00044  * 2. Redistributions in binary form must reproduce the above copyright
00045  *    notice, this list of conditions and the following disclaimer in the
00046  *    documentation and/or other materials provided with the distribution.
00047  * 3. All advertising materials mentioning features or use of this software
00048  *    must display the following acknowledgement:
00049  *    "This product includes cryptographic software written by
00050  *     Eric Young (eay@cryptsoft.com)"
00051  *    The word 'cryptographic' can be left out if the rouines from the library
00052  *    being used are not cryptographic related :-).
00053  * 4. If you include any Windows specific code (or a derivative thereof) from 
00054  *    the apps directory (application code) you must include an acknowledgement:
00055  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
00056  * 
00057  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
00058  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00059  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00060  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00061  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00062  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00063  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00064  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00065  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00066  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00067  * SUCH DAMAGE.
00068  * 
00069  * The licence and distribution terms for any publically available version or
00070  * derivative of this code cannot be changed.  i.e. this code cannot simply be
00071  * copied and put under another distribution licence
00072  * [including the GNU Public Licence.]
00073  */
00074 
00075 #ifdef POK_NEEDS_PROTOCOLS_DES
00076 
00077 #include "des_locl.h"
00078 #include "spr.h"
00079 
00080 void DES_encrypt1(DES_LONG *data, DES_key_schedule *ks, int enc)
00081         {
00082         register DES_LONG l,r,t,u;
00083 #ifdef DES_PTR
00084         register const unsigned char *des_SP=(const unsigned char *)DES_SPtrans;
00085 #endif
00086 #ifndef DES_UNROLL
00087         register int i;
00088 #endif
00089         register DES_LONG *s;
00090 
00091         r=data[0];
00092         l=data[1];
00093 
00094         IP(r,l);
00095         /* Things have been modified so that the initial rotate is
00096          * done outside the loop.  This required the
00097          * DES_SPtrans values in sp.h to be rotated 1 bit to the right.
00098          * One perl script later and things have a 5% speed up on a sparc2.
00099          * Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
00100          * for pointing this out. */
00101         /* clear the top bits on machines with 8byte longs */
00102         /* shift left by 2 */
00103         r=ROTATE(r,29)&0xffffffffL;
00104         l=ROTATE(l,29)&0xffffffffL;
00105 
00106         s=ks->ks->deslong;
00107         /* I don't know if it is worth the effort of loop unrolling the
00108          * inner loop */
00109         if (enc)
00110                 {
00111 #ifdef DES_UNROLL
00112                 D_ENCRYPT(l,r, 0); /*  1 */
00113                 D_ENCRYPT(r,l, 2); /*  2 */
00114                 D_ENCRYPT(l,r, 4); /*  3 */
00115                 D_ENCRYPT(r,l, 6); /*  4 */
00116                 D_ENCRYPT(l,r, 8); /*  5 */
00117                 D_ENCRYPT(r,l,10); /*  6 */
00118                 D_ENCRYPT(l,r,12); /*  7 */
00119                 D_ENCRYPT(r,l,14); /*  8 */
00120                 D_ENCRYPT(l,r,16); /*  9 */
00121                 D_ENCRYPT(r,l,18); /*  10 */
00122                 D_ENCRYPT(l,r,20); /*  11 */
00123                 D_ENCRYPT(r,l,22); /*  12 */
00124                 D_ENCRYPT(l,r,24); /*  13 */
00125                 D_ENCRYPT(r,l,26); /*  14 */
00126                 D_ENCRYPT(l,r,28); /*  15 */
00127                 D_ENCRYPT(r,l,30); /*  16 */
00128 #else
00129                 for (i=0; i<32; i+=8)
00130                         {
00131                         D_ENCRYPT(l,r,i+0); /*  1 */
00132                         D_ENCRYPT(r,l,i+2); /*  2 */
00133                         D_ENCRYPT(l,r,i+4); /*  3 */
00134                         D_ENCRYPT(r,l,i+6); /*  4 */
00135                         }
00136 #endif
00137                 }
00138         else
00139                 {
00140 #ifdef DES_UNROLL
00141                 D_ENCRYPT(l,r,30); /* 16 */
00142                 D_ENCRYPT(r,l,28); /* 15 */
00143                 D_ENCRYPT(l,r,26); /* 14 */
00144                 D_ENCRYPT(r,l,24); /* 13 */
00145                 D_ENCRYPT(l,r,22); /* 12 */
00146                 D_ENCRYPT(r,l,20); /* 11 */
00147                 D_ENCRYPT(l,r,18); /* 10 */
00148                 D_ENCRYPT(r,l,16); /*  9 */
00149                 D_ENCRYPT(l,r,14); /*  8 */
00150                 D_ENCRYPT(r,l,12); /*  7 */
00151                 D_ENCRYPT(l,r,10); /*  6 */
00152                 D_ENCRYPT(r,l, 8); /*  5 */
00153                 D_ENCRYPT(l,r, 6); /*  4 */
00154                 D_ENCRYPT(r,l, 4); /*  3 */
00155                 D_ENCRYPT(l,r, 2); /*  2 */
00156                 D_ENCRYPT(r,l, 0); /*  1 */
00157 #else
00158                 for (i=30; i>0; i-=8)
00159                         {
00160                         D_ENCRYPT(l,r,i-0); /* 16 */
00161                         D_ENCRYPT(r,l,i-2); /* 15 */
00162                         D_ENCRYPT(l,r,i-4); /* 14 */
00163                         D_ENCRYPT(r,l,i-6); /* 13 */
00164                         }
00165 #endif
00166                 }
00167 
00168         /* rotate and clear the top bits on machines with 8byte longs */
00169         l=ROTATE(l,3)&0xffffffffL;
00170         r=ROTATE(r,3)&0xffffffffL;
00171 
00172         FP(r,l);
00173         data[0]=l;
00174         data[1]=r;
00175         l=r=t=u=0;
00176         }
00177 
00178 void DES_encrypt2(DES_LONG *data, DES_key_schedule *ks, int enc)
00179         {
00180         register DES_LONG l,r,t,u;
00181 #ifdef DES_PTR
00182         register const unsigned char *des_SP=(const unsigned char *)DES_SPtrans;
00183 #endif
00184 #ifndef DES_UNROLL
00185         register int i;
00186 #endif
00187         register DES_LONG *s;
00188 
00189         r=data[0];
00190         l=data[1];
00191 
00192         /* Things have been modified so that the initial rotate is
00193          * done outside the loop.  This required the
00194          * DES_SPtrans values in sp.h to be rotated 1 bit to the right.
00195          * One perl script later and things have a 5% speed up on a sparc2.
00196          * Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
00197          * for pointing this out. */
00198         /* clear the top bits on machines with 8byte longs */
00199         r=ROTATE(r,29)&0xffffffffL;
00200         l=ROTATE(l,29)&0xffffffffL;
00201 
00202         s=ks->ks->deslong;
00203         /* I don't know if it is worth the effort of loop unrolling the
00204          * inner loop */
00205         if (enc)
00206                 {
00207 #ifdef DES_UNROLL
00208                 D_ENCRYPT(l,r, 0); /*  1 */
00209                 D_ENCRYPT(r,l, 2); /*  2 */
00210                 D_ENCRYPT(l,r, 4); /*  3 */
00211                 D_ENCRYPT(r,l, 6); /*  4 */
00212                 D_ENCRYPT(l,r, 8); /*  5 */
00213                 D_ENCRYPT(r,l,10); /*  6 */
00214                 D_ENCRYPT(l,r,12); /*  7 */
00215                 D_ENCRYPT(r,l,14); /*  8 */
00216                 D_ENCRYPT(l,r,16); /*  9 */
00217                 D_ENCRYPT(r,l,18); /*  10 */
00218                 D_ENCRYPT(l,r,20); /*  11 */
00219                 D_ENCRYPT(r,l,22); /*  12 */
00220                 D_ENCRYPT(l,r,24); /*  13 */
00221                 D_ENCRYPT(r,l,26); /*  14 */
00222                 D_ENCRYPT(l,r,28); /*  15 */
00223                 D_ENCRYPT(r,l,30); /*  16 */
00224 #else
00225                 for (i=0; i<32; i+=8)
00226                         {
00227                         D_ENCRYPT(l,r,i+0); /*  1 */
00228                         D_ENCRYPT(r,l,i+2); /*  2 */
00229                         D_ENCRYPT(l,r,i+4); /*  3 */
00230                         D_ENCRYPT(r,l,i+6); /*  4 */
00231                         }
00232 #endif
00233                 }
00234         else
00235                 {
00236 #ifdef DES_UNROLL
00237                 D_ENCRYPT(l,r,30); /* 16 */
00238                 D_ENCRYPT(r,l,28); /* 15 */
00239                 D_ENCRYPT(l,r,26); /* 14 */
00240                 D_ENCRYPT(r,l,24); /* 13 */
00241                 D_ENCRYPT(l,r,22); /* 12 */
00242                 D_ENCRYPT(r,l,20); /* 11 */
00243                 D_ENCRYPT(l,r,18); /* 10 */
00244                 D_ENCRYPT(r,l,16); /*  9 */
00245                 D_ENCRYPT(l,r,14); /*  8 */
00246                 D_ENCRYPT(r,l,12); /*  7 */
00247                 D_ENCRYPT(l,r,10); /*  6 */
00248                 D_ENCRYPT(r,l, 8); /*  5 */
00249                 D_ENCRYPT(l,r, 6); /*  4 */
00250                 D_ENCRYPT(r,l, 4); /*  3 */
00251                 D_ENCRYPT(l,r, 2); /*  2 */
00252                 D_ENCRYPT(r,l, 0); /*  1 */
00253 #else
00254                 for (i=30; i>0; i-=8)
00255                         {
00256                         D_ENCRYPT(l,r,i-0); /* 16 */
00257                         D_ENCRYPT(r,l,i-2); /* 15 */
00258                         D_ENCRYPT(l,r,i-4); /* 14 */
00259                         D_ENCRYPT(r,l,i-6); /* 13 */
00260                         }
00261 #endif
00262                 }
00263         /* rotate and clear the top bits on machines with 8byte longs */
00264         data[0]=ROTATE(l,3)&0xffffffffL;
00265         data[1]=ROTATE(r,3)&0xffffffffL;
00266         l=r=t=u=0;
00267         }
00268 
00269 void DES_encrypt3(DES_LONG *data, DES_key_schedule *ks1,
00270                   DES_key_schedule *ks2, DES_key_schedule *ks3)
00271         {
00272         register DES_LONG l,r;
00273 
00274         l=data[0];
00275         r=data[1];
00276         IP(l,r);
00277         data[0]=l;
00278         data[1]=r;
00279         DES_encrypt2((DES_LONG *)data,ks1,DES_ENCRYPT);
00280         DES_encrypt2((DES_LONG *)data,ks2,DES_DECRYPT);
00281         DES_encrypt2((DES_LONG *)data,ks3,DES_ENCRYPT);
00282         l=data[0];
00283         r=data[1];
00284         FP(r,l);
00285         data[0]=l;
00286         data[1]=r;
00287         }
00288 
00289 void DES_decrypt3(DES_LONG *data, DES_key_schedule *ks1,
00290                   DES_key_schedule *ks2, DES_key_schedule *ks3)
00291         {
00292         register DES_LONG l,r;
00293 
00294         l=data[0];
00295         r=data[1];
00296         IP(l,r);
00297         data[0]=l;
00298         data[1]=r;
00299         DES_encrypt2((DES_LONG *)data,ks3,DES_DECRYPT);
00300         DES_encrypt2((DES_LONG *)data,ks2,DES_ENCRYPT);
00301         DES_encrypt2((DES_LONG *)data,ks1,DES_DECRYPT);
00302         l=data[0];
00303         r=data[1];
00304         FP(r,l);
00305         data[0]=l;
00306         data[1]=r;
00307         }
00308 
00309 #ifndef DES_DEFAULT_OPTIONS
00310 
00311 #if !defined(OPENSSL_FIPS_DES_ASM)
00312 
00313 #undef CBC_ENC_C__DONT_UPDATE_IV
00314 #include "ncbc_enc.c" /* DES_ncbc_encrypt */
00315 
00316 void DES_ede3_cbc_encrypt(const unsigned char *input, unsigned char *output,
00317                           long length, DES_key_schedule *ks1,
00318                           DES_key_schedule *ks2, DES_key_schedule *ks3,
00319                           DES_cblock *ivec, int enc)
00320         {
00321         register DES_LONG tin0,tin1;
00322         register DES_LONG tout0,tout1,xor0,xor1;
00323         register const unsigned char *in;
00324         unsigned char *out;
00325         register long l=length;
00326         DES_LONG tin[2];
00327         unsigned char *iv;
00328 
00329         in=input;
00330         out=output;
00331         iv = &(*ivec)[0];
00332 
00333         if (enc)
00334                 {
00335                 c2l(iv,tout0);
00336                 c2l(iv,tout1);
00337                 for (l-=8; l>=0; l-=8)
00338                         {
00339                         c2l(in,tin0);
00340                         c2l(in,tin1);
00341                         tin0^=tout0;
00342                         tin1^=tout1;
00343 
00344                         tin[0]=tin0;
00345                         tin[1]=tin1;
00346                         DES_encrypt3((DES_LONG *)tin,ks1,ks2,ks3);
00347                         tout0=tin[0];
00348                         tout1=tin[1];
00349 
00350                         l2c(tout0,out);
00351                         l2c(tout1,out);
00352                         }
00353                 if (l != -8)
00354                         {
00355                         c2ln(in,tin0,tin1,l+8);
00356                         tin0^=tout0;
00357                         tin1^=tout1;
00358 
00359                         tin[0]=tin0;
00360                         tin[1]=tin1;
00361                         DES_encrypt3((DES_LONG *)tin,ks1,ks2,ks3);
00362                         tout0=tin[0];
00363                         tout1=tin[1];
00364 
00365                         l2c(tout0,out);
00366                         l2c(tout1,out);
00367                         }
00368                 iv = &(*ivec)[0];
00369                 l2c(tout0,iv);
00370                 l2c(tout1,iv);
00371                 }
00372         else
00373                 {
00374                 register DES_LONG t0,t1;
00375 
00376                 c2l(iv,xor0);
00377                 c2l(iv,xor1);
00378                 for (l-=8; l>=0; l-=8)
00379                         {
00380                         c2l(in,tin0);
00381                         c2l(in,tin1);
00382 
00383                         t0=tin0;
00384                         t1=tin1;
00385 
00386                         tin[0]=tin0;
00387                         tin[1]=tin1;
00388                         DES_decrypt3((DES_LONG *)tin,ks1,ks2,ks3);
00389                         tout0=tin[0];
00390                         tout1=tin[1];
00391 
00392                         tout0^=xor0;
00393                         tout1^=xor1;
00394                         l2c(tout0,out);
00395                         l2c(tout1,out);
00396                         xor0=t0;
00397                         xor1=t1;
00398                         }
00399                 if (l != -8)
00400                         {
00401                         c2l(in,tin0);
00402                         c2l(in,tin1);
00403                         
00404                         t0=tin0;
00405                         t1=tin1;
00406 
00407                         tin[0]=tin0;
00408                         tin[1]=tin1;
00409                         DES_decrypt3((DES_LONG *)tin,ks1,ks2,ks3);
00410                         tout0=tin[0];
00411                         tout1=tin[1];
00412                 
00413                         tout0^=xor0;
00414                         tout1^=xor1;
00415                         l2cn(tout0,tout1,out,l+8);
00416                         xor0=t0;
00417                         xor1=t1;
00418                         }
00419 
00420                 iv = &(*ivec)[0];
00421                 l2c(xor0,iv);
00422                 l2c(xor1,iv);
00423                 }
00424         tin0=tin1=tout0=tout1=xor0=xor1=0;
00425         tin[0]=tin[1]=0;
00426         }
00427 
00428 #endif
00429 
00430 #endif /* DES_DEFAULT_OPTIONS */
00431 
00432 #endif /* POK_NEEDS ... */