Home | History | Annotate | Line # | Download | only in des
des_ecb.c revision 1.4
      1  1.4      tls /*	$NetBSD: des_ecb.c,v 1.4 2001/09/09 11:01:02 tls Exp $	*/
      2  1.3   itojun /*	$KAME: des_ecb.c,v 1.5 2000/11/06 13:58:08 itojun Exp $	*/
      3  1.1  thorpej 
      4  1.1  thorpej /* crypto/des/ecb_enc.c */
      5  1.4      tls /* Copyright (C) 1995-1998 Eric Young (eay (at) mincom.oz.au)
      6  1.1  thorpej  * All rights reserved.
      7  1.1  thorpej  *
      8  1.1  thorpej  * This file is part of an SSL implementation written
      9  1.1  thorpej  * by Eric Young (eay (at) mincom.oz.au).
     10  1.1  thorpej  * The implementation was written so as to conform with Netscapes SSL
     11  1.1  thorpej  * specification.  This library and applications are
     12  1.1  thorpej  * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
     13  1.1  thorpej  * as long as the following conditions are aheared to.
     14  1.1  thorpej  *
     15  1.1  thorpej  * Copyright remains Eric Young's, and as such any Copyright notices in
     16  1.1  thorpej  * the code are not to be removed.  If this code is used in a product,
     17  1.1  thorpej  * Eric Young should be given attribution as the author of the parts used.
     18  1.1  thorpej  * This can be in the form of a textual message at program startup or
     19  1.1  thorpej  * in documentation (online or textual) provided with the package.
     20  1.1  thorpej  *
     21  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     22  1.1  thorpej  * modification, are permitted provided that the following conditions
     23  1.1  thorpej  * are met:
     24  1.1  thorpej  * 1. Redistributions of source code must retain the copyright
     25  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     26  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     27  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     28  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     29  1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     30  1.1  thorpej  *    must display the following acknowledgement:
     31  1.1  thorpej  *    This product includes software developed by Eric Young (eay (at) mincom.oz.au)
     32  1.1  thorpej  *
     33  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
     34  1.1  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     35  1.1  thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     36  1.1  thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     37  1.1  thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     38  1.1  thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     39  1.1  thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     40  1.1  thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     41  1.1  thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     42  1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     43  1.1  thorpej  * SUCH DAMAGE.
     44  1.1  thorpej  *
     45  1.1  thorpej  * The licence and distribution terms for any publically available version or
     46  1.1  thorpej  * derivative of this code cannot be changed.  i.e. this code cannot simply be
     47  1.1  thorpej  * copied and put under another distribution licence
     48  1.1  thorpej  * [including the GNU Public Licence.]
     49  1.1  thorpej  */
     50  1.1  thorpej 
     51  1.2   itojun #include <sys/param.h>
     52  1.3   itojun #ifdef _KERNEL
     53  1.2   itojun #include <sys/systm.h>
     54  1.3   itojun #else
     55  1.3   itojun #include <string.h>
     56  1.3   itojun #endif
     57  1.1  thorpej #include <crypto/des/des_locl.h>
     58  1.1  thorpej #include <crypto/des/spr.h>
     59  1.1  thorpej 
     60  1.4      tls /* char *libdes_version="libdes v 3.24 - 20-Apr-1996 - eay"; */ /* wrong */
     61  1.4      tls /* char *DES_version="DES part of SSLeay 0.6.4 30-Aug-1996"; */
     62  1.4      tls 
     63  1.4      tls char *des_options(void)
     64  1.4      tls         {
     65  1.4      tls         static int init=1;
     66  1.4      tls         static char buf[32];
     67  1.4      tls 
     68  1.4      tls         if (init)
     69  1.4      tls                 {
     70  1.4      tls                 const char *ptr,*unroll,*risc,*size;
     71  1.1  thorpej 
     72  1.1  thorpej #ifdef DES_PTR
     73  1.4      tls                 ptr="ptr";
     74  1.4      tls #else
     75  1.4      tls                 ptr="idx";
     76  1.4      tls #endif
     77  1.4      tls #if defined(DES_RISC1) || defined(DES_RISC2)
     78  1.4      tls #ifdef DES_RISC1
     79  1.4      tls                 risc="risc1";
     80  1.4      tls #endif
     81  1.4      tls #ifdef DES_RISC2
     82  1.4      tls                 risc="risc2";
     83  1.4      tls #endif
     84  1.4      tls #else
     85  1.4      tls                 risc="cisc";
     86  1.4      tls #endif
     87  1.4      tls #ifdef DES_UNROLL
     88  1.4      tls                 unroll="16";
     89  1.1  thorpej #else
     90  1.4      tls                 unroll="4";
     91  1.1  thorpej #endif
     92  1.4      tls                 if (sizeof(DES_LONG) != sizeof(long))
     93  1.4      tls                         size="int";
     94  1.4      tls                 else
     95  1.4      tls                         size="long";
     96  1.4      tls                 sprintf(buf,"des(%s,%s,%s,%s)",ptr,risc,unroll,size);
     97  1.4      tls                 init=0;
     98  1.4      tls                 }
     99  1.4      tls         return(buf);
    100  1.4      tls }
    101  1.4      tls void des_ecb_encrypt(des_cblock *input, des_cblock *output,
    102  1.4      tls 		     des_key_schedule ks, int enc)
    103  1.4      tls {
    104  1.1  thorpej 	register DES_LONG l;
    105  1.1  thorpej 	DES_LONG ll[2];
    106  1.4      tls 	const unsigned char *in=&(*input)[0];
    107  1.4      tls 	unsigned char *out = &(*output)[0];
    108  1.1  thorpej 
    109  1.1  thorpej 	c2l(in,l); ll[0]=l;
    110  1.1  thorpej 	c2l(in,l); ll[1]=l;
    111  1.4      tls 	des_encrypt1(ll,ks,enc);
    112  1.1  thorpej 	l=ll[0]; l2c(l,out);
    113  1.1  thorpej 	l=ll[1]; l2c(l,out);
    114  1.1  thorpej 	l=ll[0]=ll[1]=0;
    115  1.4      tls }
    116  1.4      tls 
    117  1.4      tls void des_ecb3_encrypt(des_cblock *input, des_cblock *output,
    118  1.4      tls              des_key_schedule ks1, des_key_schedule ks2, des_key_schedule ks3,
    119  1.4      tls              int enc)
    120  1.4      tls {
    121  1.4      tls 	register DES_LONG l0,l1;
    122  1.4      tls 	DES_LONG ll[2];
    123  1.4      tls 	const unsigned char *in = &(*input)[0];
    124  1.4      tls 	unsigned char *out = &(*output)[0];
    125  1.4      tls 
    126  1.4      tls 	c2l(in,l0);
    127  1.4      tls 	c2l(in,l1);
    128  1.4      tls 	ll[0]=l0;
    129  1.4      tls 	ll[1]=l1;
    130  1.1  thorpej 
    131  1.4      tls 	if (enc)
    132  1.4      tls 		des_encrypt3(ll,ks1,ks2,ks3);
    133  1.1  thorpej 	else
    134  1.4      tls 		des_decrypt3(ll,ks1,ks2,ks3);
    135  1.4      tls 
    136  1.4      tls 	l0=ll[0];
    137  1.4      tls 	l1=ll[1];
    138  1.4      tls 	l2c(l0,out);
    139  1.4      tls 	l2c(l1,out);
    140  1.4      tls }
    141