home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / crypl200.zip / LIBDES / ECB_ENC.C < prev    next >
Text File  |  1996-09-22  |  6KB  |  209 lines

  1. /* lib/des/ecb_enc.c */
  2. /* Copyright (C) 1995 Eric Young (eay@mincom.oz.au)
  3.  * All rights reserved.
  4.  * 
  5.  * This file is part of an SSL implementation written
  6.  * by Eric Young (eay@mincom.oz.au).
  7.  * The implementation was written so as to conform with Netscapes SSL
  8.  * specification.  This library and applications are
  9.  * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
  10.  * as long as the following conditions are aheared to.
  11.  * 
  12.  * Copyright remains Eric Young's, and as such any Copyright notices in
  13.  * the code are not to be removed.  If this code is used in a product,
  14.  * Eric Young should be given attribution as the author of the parts used.
  15.  * This can be in the form of a textual message at program startup or
  16.  * in documentation (online or textual) provided with the package.
  17.  * 
  18.  * Redistribution and use in source and binary forms, with or without
  19.  * modification, are permitted provided that the following conditions
  20.  * are met:
  21.  * 1. Redistributions of source code must retain the copyright
  22.  *    notice, this list of conditions and the following disclaimer.
  23.  * 2. Redistributions in binary form must reproduce the above copyright
  24.  *    notice, this list of conditions and the following disclaimer in the
  25.  *    documentation and/or other materials provided with the distribution.
  26.  * 3. All advertising materials mentioning features or use of this software
  27.  *    must display the following acknowledgement:
  28.  *    This product includes software developed by Eric Young (eay@mincom.oz.au)
  29.  * 
  30.  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  31.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  34.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  39.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  40.  * SUCH DAMAGE.
  41.  *
  42.  * The licence and distribution terms for any publically available version or
  43.  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  44.  * copied and put under another distribution licence
  45.  * [including the GNU Public Licence.]
  46.  */
  47.  
  48. #if defined( INC_ALL ) || defined( INC_CHILD )
  49.   #include "des_locl.h"
  50.   #include "spr.h"
  51. #else
  52.   #include "libdes/des_locl.h"
  53.   #include "libdes/spr.h"
  54. #endif /* Compiler-specific includes */
  55.  
  56. const char *DES_version="libdes v 3.22 - 95/11/29 - eay";
  57.  
  58. void des_ecb_encrypt(input, output, ks, encrypt)
  59. des_cblock (*input);
  60. des_cblock (*output);
  61. des_key_schedule ks;
  62. int encrypt;
  63.     {
  64.     register DES_LONG l;
  65.     register unsigned char *in,*out;
  66.     DES_LONG ll[2];
  67.  
  68.     in=(unsigned char *)input;
  69.     out=(unsigned char *)output;
  70.     c2l(in,l); ll[0]=l;
  71.     c2l(in,l); ll[1]=l;
  72.     des_encrypt(ll,ks,encrypt);
  73.     l=ll[0]; l2c(l,out);
  74.     l=ll[1]; l2c(l,out);
  75.     l=ll[0]=ll[1]=0;
  76.     }
  77.  
  78. void des_encrypt(data, ks, encrypt)
  79. DES_LONG *data;
  80. des_key_schedule ks;
  81. int encrypt;
  82.     {
  83.     register DES_LONG l,r,t,u;
  84. #ifdef DES_USE_PTR
  85.     register unsigned char *des_SP=(unsigned char *)des_SPtrans;
  86. #endif
  87. #ifdef MSDOS
  88.     union fudge {
  89.         DES_LONG  l;
  90.         unsigned short s[2];
  91.         unsigned char  c[4];
  92.         } U,T;
  93. #endif
  94.     register int i;
  95.     register DES_LONG *s;
  96.  
  97.     u=data[0];
  98.     r=data[1];
  99.  
  100.     IP(u,r);
  101.     /* Things have been modified so that the initial rotate is
  102.      * done outside the loop.  This required the
  103.      * des_SPtrans values in sp.h to be rotated 1 bit to the right.
  104.      * One perl script later and things have a 5% speed up on a sparc2.
  105.      * Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
  106.      * for pointing this out. */
  107.     l=(r<<1)|(r>>31);
  108.     r=(u<<1)|(u>>31);
  109.  
  110.     /* clear the top bits on machines with 8byte longs */
  111.     l&=0xffffffffL;
  112.     r&=0xffffffffL;
  113.  
  114.     s=(DES_LONG *)ks;
  115.     /* I don't know if it is worth the effort of loop unrolling the
  116.      * inner loop */
  117.     if (encrypt)
  118.         {
  119.         for (i=0; i<32; i+=4)
  120.             {
  121.             D_ENCRYPT(l,r,i+0); /*  1 */
  122.             D_ENCRYPT(r,l,i+2); /*  2 */
  123.             }
  124.         }
  125.     else
  126.         {
  127.         for (i=30; i>0; i-=4)
  128.             {
  129.             D_ENCRYPT(l,r,i-0); /* 16 */
  130.             D_ENCRYPT(r,l,i-2); /* 15 */
  131.             }
  132.         }
  133.     l=(l>>1)|(l<<31);
  134.     r=(r>>1)|(r<<31);
  135.     /* clear the top bits on machines with 8byte longs */
  136.     l&=0xffffffffL;
  137.     r&=0xffffffffL;
  138.  
  139.     FP(r,l);
  140.     data[0]=l;
  141.     data[1]=r;
  142.     l=r=t=u=0;
  143.     }
  144.  
  145. void des_encrypt2(data, ks, encrypt)
  146. DES_LONG *data;
  147. des_key_schedule ks;
  148. int encrypt;
  149.     {
  150.     register DES_LONG l,r,t,u;
  151. #ifdef DES_USE_PTR
  152.     register unsigned char *des_SP=(unsigned char *)des_SPtrans;
  153. #endif
  154. #ifdef MSDOS
  155.     union fudge {
  156.         DES_LONG  l;
  157.         unsigned short s[2];
  158.         unsigned char  c[4];
  159.         } U,T;
  160. #endif
  161.     register int i;
  162.     register DES_LONG *s;
  163.  
  164.     u=data[0];
  165.     r=data[1];
  166.  
  167.     /* Things have been modified so that the initial rotate is
  168.      * done outside the loop.  This required the
  169.      * des_SPtrans values in sp.h to be rotated 1 bit to the right.
  170.      * One perl script later and things have a 5% speed up on a sparc2.
  171.      * Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
  172.      * for pointing this out. */
  173.     l=(r<<1)|(r>>31);
  174.     r=(u<<1)|(u>>31);
  175.  
  176.     /* clear the top bits on machines with 8byte longs */
  177.     l&=0xffffffffL;
  178.     r&=0xffffffffL;
  179.  
  180.     s=(DES_LONG *)ks;
  181.     /* I don't know if it is worth the effort of loop unrolling the
  182.      * inner loop */
  183.     if (encrypt)
  184.         {
  185.         for (i=0; i<32; i+=4)
  186.             {
  187.             D_ENCRYPT(l,r,i+0); /*  1 */
  188.             D_ENCRYPT(r,l,i+2); /*  2 */
  189.             }
  190.         }
  191.     else
  192.         {
  193.         for (i=30; i>0; i-=4)
  194.             {
  195.             D_ENCRYPT(l,r,i-0); /* 16 */
  196.             D_ENCRYPT(r,l,i-2); /* 15 */
  197.             }
  198.         }
  199.     l=(l>>1)|(l<<31);
  200.     r=(r>>1)|(r<<31);
  201.     /* clear the top bits on machines with 8byte longs */
  202.     l&=0xffffffffL;
  203.     r&=0xffffffffL;
  204.  
  205.     data[0]=l;
  206.     data[1]=r;
  207.     l=r=t=u=0;
  208.     }
  209.