xref: /freebsd/sys/crypto/des/des_ecb.c (revision fdafd315)
1b5a8f767SHajimu UMEMOTO /*	$KAME: des_ecb.c,v 1.6 2001/09/10 04:03:58 itojun Exp $	*/
2686cdd19SJun-ichiro itojun Hagino 
36a800098SYoshinobu Inoue /* crypto/des/ecb_enc.c */
48c086620SDavid E. O'Brien 
5b5a8f767SHajimu UMEMOTO /* Copyright (C) 1995-1998 Eric Young (eay@mincom.oz.au)
66a800098SYoshinobu Inoue  * All rights reserved.
76a800098SYoshinobu Inoue  *
86a800098SYoshinobu Inoue  * This file is part of an SSL implementation written
96a800098SYoshinobu Inoue  * by Eric Young (eay@mincom.oz.au).
106a800098SYoshinobu Inoue  * The implementation was written so as to conform with Netscapes SSL
116a800098SYoshinobu Inoue  * specification.  This library and applications are
126a800098SYoshinobu Inoue  * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
136a800098SYoshinobu Inoue  * as long as the following conditions are aheared to.
146a800098SYoshinobu Inoue  *
156a800098SYoshinobu Inoue  * Copyright remains Eric Young's, and as such any Copyright notices in
166a800098SYoshinobu Inoue  * the code are not to be removed.  If this code is used in a product,
176a800098SYoshinobu Inoue  * Eric Young should be given attribution as the author of the parts used.
186a800098SYoshinobu Inoue  * This can be in the form of a textual message at program startup or
196a800098SYoshinobu Inoue  * in documentation (online or textual) provided with the package.
206a800098SYoshinobu Inoue  *
216a800098SYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
226a800098SYoshinobu Inoue  * modification, are permitted provided that the following conditions
236a800098SYoshinobu Inoue  * are met:
246a800098SYoshinobu Inoue  * 1. Redistributions of source code must retain the copyright
256a800098SYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
266a800098SYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
276a800098SYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
286a800098SYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
296a800098SYoshinobu Inoue  * 3. All advertising materials mentioning features or use of this software
306a800098SYoshinobu Inoue  *    must display the following acknowledgement:
316a800098SYoshinobu Inoue  *    This product includes software developed by Eric Young (eay@mincom.oz.au)
326a800098SYoshinobu Inoue  *
336a800098SYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
346a800098SYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
356a800098SYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
366a800098SYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
376a800098SYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
386a800098SYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
396a800098SYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
406a800098SYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
416a800098SYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
426a800098SYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
436a800098SYoshinobu Inoue  * SUCH DAMAGE.
446a800098SYoshinobu Inoue  *
456a800098SYoshinobu Inoue  * The licence and distribution terms for any publically available version or
466a800098SYoshinobu Inoue  * derivative of this code cannot be changed.  i.e. this code cannot simply be
476a800098SYoshinobu Inoue  * copied and put under another distribution licence
486a800098SYoshinobu Inoue  * [including the GNU Public Licence.]
496a800098SYoshinobu Inoue  */
506a800098SYoshinobu Inoue 
5133841545SHajimu UMEMOTO #include <sys/param.h>
5233841545SHajimu UMEMOTO #include <sys/systm.h>
536a800098SYoshinobu Inoue #include <crypto/des/des_locl.h>
546a800098SYoshinobu Inoue #include <crypto/des/spr.h>
556a800098SYoshinobu Inoue 
56b5a8f767SHajimu UMEMOTO /* char *libdes_version="libdes v 3.24 - 20-Apr-1996 - eay"; */ /* wrong */
57b5a8f767SHajimu UMEMOTO /* char *DES_version="DES part of SSLeay 0.6.4 30-Aug-1996"; */
586a800098SYoshinobu Inoue 
des_options(void)59b5a8f767SHajimu UMEMOTO char *des_options(void)
606a800098SYoshinobu Inoue         {
61b5a8f767SHajimu UMEMOTO         static int init=1;
62b5a8f767SHajimu UMEMOTO         static char buf[32];
63b5a8f767SHajimu UMEMOTO 
64b5a8f767SHajimu UMEMOTO         if (init)
65b5a8f767SHajimu UMEMOTO                 {
66b5a8f767SHajimu UMEMOTO                 const char *ptr,*unroll,*risc,*size;
67b5a8f767SHajimu UMEMOTO 
686a800098SYoshinobu Inoue #ifdef DES_PTR
69b5a8f767SHajimu UMEMOTO                 ptr="ptr";
706a800098SYoshinobu Inoue #else
71b5a8f767SHajimu UMEMOTO                 ptr="idx";
726a800098SYoshinobu Inoue #endif
73b5a8f767SHajimu UMEMOTO #if defined(DES_RISC1) || defined(DES_RISC2)
74b5a8f767SHajimu UMEMOTO #ifdef DES_RISC1
75b5a8f767SHajimu UMEMOTO                 risc="risc1";
76b5a8f767SHajimu UMEMOTO #endif
77b5a8f767SHajimu UMEMOTO #ifdef DES_RISC2
78b5a8f767SHajimu UMEMOTO                 risc="risc2";
79b5a8f767SHajimu UMEMOTO #endif
80b5a8f767SHajimu UMEMOTO #else
81b5a8f767SHajimu UMEMOTO                 risc="cisc";
82b5a8f767SHajimu UMEMOTO #endif
83b5a8f767SHajimu UMEMOTO #ifdef DES_UNROLL
84b5a8f767SHajimu UMEMOTO                 unroll="16";
85b5a8f767SHajimu UMEMOTO #else
86b5a8f767SHajimu UMEMOTO                 unroll="4";
87b5a8f767SHajimu UMEMOTO #endif
88b5a8f767SHajimu UMEMOTO                 if (sizeof(DES_LONG) != sizeof(long))
89b5a8f767SHajimu UMEMOTO                         size="int";
90b5a8f767SHajimu UMEMOTO                 else
91b5a8f767SHajimu UMEMOTO                         size="long";
92b5a8f767SHajimu UMEMOTO                 sprintf(buf,"des(%s,%s,%s,%s)",ptr,risc,unroll,size);
93b5a8f767SHajimu UMEMOTO                 init=0;
946a800098SYoshinobu Inoue                 }
95b5a8f767SHajimu UMEMOTO         return(buf);
96b5a8f767SHajimu UMEMOTO }
des_ecb_encrypt(unsigned char * input,unsigned char * output,des_key_schedule ks,int enc)9787210a0cSJohn Baldwin void des_ecb_encrypt(unsigned char *input, unsigned char *output,
98b5a8f767SHajimu UMEMOTO 		     des_key_schedule ks, int enc)
996a800098SYoshinobu Inoue {
1006a800098SYoshinobu Inoue 	register DES_LONG l;
1016a800098SYoshinobu Inoue 	DES_LONG ll[2];
10287210a0cSJohn Baldwin 	const unsigned char *in = input;
10387210a0cSJohn Baldwin 	unsigned char *out = output;
1046a800098SYoshinobu Inoue 
1056a800098SYoshinobu Inoue 	c2l(in,l); ll[0]=l;
1066a800098SYoshinobu Inoue 	c2l(in,l); ll[1]=l;
107b5a8f767SHajimu UMEMOTO 	des_encrypt1(ll,ks,enc);
1086a800098SYoshinobu Inoue 	l=ll[0]; l2c(l,out);
1096a800098SYoshinobu Inoue 	l=ll[1]; l2c(l,out);
1106a800098SYoshinobu Inoue 	l=ll[0]=ll[1]=0;
1116a800098SYoshinobu Inoue }
1126a800098SYoshinobu Inoue 
des_ecb3_encrypt(unsigned char * input,unsigned char * output,des_key_schedule ks1,des_key_schedule ks2,des_key_schedule ks3,int enc)11387210a0cSJohn Baldwin void des_ecb3_encrypt(unsigned char *input, unsigned char *output,
114b5a8f767SHajimu UMEMOTO              des_key_schedule ks1, des_key_schedule ks2, des_key_schedule ks3,
115b5a8f767SHajimu UMEMOTO              int enc)
1166a800098SYoshinobu Inoue {
117b5a8f767SHajimu UMEMOTO 	register DES_LONG l0,l1;
118b5a8f767SHajimu UMEMOTO 	DES_LONG ll[2];
11987210a0cSJohn Baldwin 	const unsigned char *in = input;
12087210a0cSJohn Baldwin 	unsigned char *out = output;
1216a800098SYoshinobu Inoue 
122b5a8f767SHajimu UMEMOTO 	c2l(in,l0);
123b5a8f767SHajimu UMEMOTO 	c2l(in,l1);
124b5a8f767SHajimu UMEMOTO 	ll[0]=l0;
125b5a8f767SHajimu UMEMOTO 	ll[1]=l1;
1266a800098SYoshinobu Inoue 
127b5a8f767SHajimu UMEMOTO 	if (enc)
128b5a8f767SHajimu UMEMOTO 		des_encrypt3(ll,ks1,ks2,ks3);
1296a800098SYoshinobu Inoue 	else
130b5a8f767SHajimu UMEMOTO 		des_decrypt3(ll,ks1,ks2,ks3);
1316a800098SYoshinobu Inoue 
132b5a8f767SHajimu UMEMOTO 	l0=ll[0];
133b5a8f767SHajimu UMEMOTO 	l1=ll[1];
134b5a8f767SHajimu UMEMOTO 	l2c(l0,out);
135b5a8f767SHajimu UMEMOTO 	l2c(l1,out);
1366a800098SYoshinobu Inoue }
137