Home | History | Annotate | Line # | Download | only in des
des_ecb.c revision 1.8
      1  1.8    perry /*	$NetBSD: des_ecb.c,v 1.8 2005/02/26 23:04:17 perry 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.6    lukem 
     51  1.6    lukem #include <sys/cdefs.h>
     52  1.8    perry __KERNEL_RCSID(0, "$NetBSD: des_ecb.c,v 1.8 2005/02/26 23:04:17 perry Exp $");
     53  1.1  thorpej 
     54  1.2   itojun #include <sys/param.h>
     55  1.3   itojun #ifdef _KERNEL
     56  1.2   itojun #include <sys/systm.h>
     57  1.3   itojun #else
     58  1.5     taca #include <stdio.h>
     59  1.3   itojun #include <string.h>
     60  1.3   itojun #endif
     61  1.1  thorpej #include <crypto/des/des_locl.h>
     62  1.1  thorpej #include <crypto/des/spr.h>
     63  1.1  thorpej 
     64  1.4      tls /* char *libdes_version="libdes v 3.24 - 20-Apr-1996 - eay"; */ /* wrong */
     65  1.4      tls /* char *DES_version="DES part of SSLeay 0.6.4 30-Aug-1996"; */
     66  1.4      tls 
     67  1.4      tls char *des_options(void)
     68  1.4      tls         {
     69  1.4      tls         static int init=1;
     70  1.4      tls         static char buf[32];
     71  1.4      tls 
     72  1.4      tls         if (init)
     73  1.4      tls                 {
     74  1.4      tls                 const char *ptr,*unroll,*risc,*size;
     75  1.1  thorpej 
     76  1.1  thorpej #ifdef DES_PTR
     77  1.4      tls                 ptr="ptr";
     78  1.4      tls #else
     79  1.4      tls                 ptr="idx";
     80  1.4      tls #endif
     81  1.4      tls #if defined(DES_RISC1) || defined(DES_RISC2)
     82  1.4      tls #ifdef DES_RISC1
     83  1.4      tls                 risc="risc1";
     84  1.4      tls #endif
     85  1.4      tls #ifdef DES_RISC2
     86  1.4      tls                 risc="risc2";
     87  1.4      tls #endif
     88  1.4      tls #else
     89  1.4      tls                 risc="cisc";
     90  1.4      tls #endif
     91  1.4      tls #ifdef DES_UNROLL
     92  1.4      tls                 unroll="16";
     93  1.1  thorpej #else
     94  1.4      tls                 unroll="4";
     95  1.1  thorpej #endif
     96  1.7    perry 		/*CONSTCOND*/
     97  1.4      tls                 if (sizeof(DES_LONG) != sizeof(long))
     98  1.4      tls                         size="int";
     99  1.4      tls                 else
    100  1.4      tls                         size="long";
    101  1.4      tls                 sprintf(buf,"des(%s,%s,%s,%s)",ptr,risc,unroll,size);
    102  1.4      tls                 init=0;
    103  1.4      tls                 }
    104  1.4      tls         return(buf);
    105  1.4      tls }
    106  1.8    perry void des_ecb_encrypt(des_cblock *input, des_cblock *output,
    107  1.4      tls 		     des_key_schedule ks, int enc)
    108  1.4      tls {
    109  1.1  thorpej 	register DES_LONG l;
    110  1.1  thorpej 	DES_LONG ll[2];
    111  1.4      tls 	const unsigned char *in=&(*input)[0];
    112  1.4      tls 	unsigned char *out = &(*output)[0];
    113  1.1  thorpej 
    114  1.1  thorpej 	c2l(in,l); ll[0]=l;
    115  1.1  thorpej 	c2l(in,l); ll[1]=l;
    116  1.4      tls 	des_encrypt1(ll,ks,enc);
    117  1.1  thorpej 	l=ll[0]; l2c(l,out);
    118  1.1  thorpej 	l=ll[1]; l2c(l,out);
    119  1.1  thorpej 	l=ll[0]=ll[1]=0;
    120  1.4      tls }
    121  1.4      tls 
    122  1.4      tls void des_ecb3_encrypt(des_cblock *input, des_cblock *output,
    123  1.4      tls              des_key_schedule ks1, des_key_schedule ks2, des_key_schedule ks3,
    124  1.4      tls              int enc)
    125  1.4      tls {
    126  1.4      tls 	register DES_LONG l0,l1;
    127  1.4      tls 	DES_LONG ll[2];
    128  1.4      tls 	const unsigned char *in = &(*input)[0];
    129  1.4      tls 	unsigned char *out = &(*output)[0];
    130  1.8    perry 
    131  1.8    perry 	c2l(in,l0);
    132  1.4      tls 	c2l(in,l1);
    133  1.8    perry 	ll[0]=l0;
    134  1.4      tls 	ll[1]=l1;
    135  1.1  thorpej 
    136  1.4      tls 	if (enc)
    137  1.4      tls 		des_encrypt3(ll,ks1,ks2,ks3);
    138  1.1  thorpej 	else
    139  1.4      tls 		des_decrypt3(ll,ks1,ks2,ks3);
    140  1.4      tls 
    141  1.4      tls 	l0=ll[0];
    142  1.4      tls 	l1=ll[1];
    143  1.4      tls 	l2c(l0,out);
    144  1.4      tls 	l2c(l1,out);
    145  1.4      tls }
    146