des_ecb.c revision 1.3 1 1.3 itojun /* $NetBSD: des_ecb.c,v 1.3 2000/11/06 14:11:41 itojun 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.1 thorpej /* Copyright (C) 1995-1996 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.1 thorpej char *libdes_version="libdes v 3.24 - 20-Apr-1996 - eay";
61 1.1 thorpej char *DES_version="DES part of SSLeay 0.6.4 30-Aug-1996";
62 1.1 thorpej
63 1.1 thorpej char *des_options()
64 1.1 thorpej {
65 1.1 thorpej #ifdef DES_PTR
66 1.1 thorpej if (sizeof(DES_LONG) != sizeof(long))
67 1.1 thorpej return("des(ptr,int)");
68 1.1 thorpej else
69 1.1 thorpej return("des(ptr,long)");
70 1.1 thorpej #else
71 1.1 thorpej if (sizeof(DES_LONG) != sizeof(long))
72 1.1 thorpej return("des(idx,int)");
73 1.1 thorpej else
74 1.1 thorpej return("des(idx,long)");
75 1.1 thorpej #endif
76 1.1 thorpej }
77 1.1 thorpej
78 1.1 thorpej
79 1.1 thorpej void des_ecb_encrypt(input, output, ks, encrypt)
80 1.1 thorpej des_cblock (*input);
81 1.1 thorpej des_cblock (*output);
82 1.1 thorpej des_key_schedule ks;
83 1.1 thorpej int encrypt;
84 1.1 thorpej {
85 1.1 thorpej register DES_LONG l;
86 1.1 thorpej register unsigned char *in,*out;
87 1.1 thorpej DES_LONG ll[2];
88 1.1 thorpej
89 1.1 thorpej in=(unsigned char *)input;
90 1.1 thorpej out=(unsigned char *)output;
91 1.1 thorpej c2l(in,l); ll[0]=l;
92 1.1 thorpej c2l(in,l); ll[1]=l;
93 1.1 thorpej des_encrypt(ll,ks,encrypt);
94 1.1 thorpej l=ll[0]; l2c(l,out);
95 1.1 thorpej l=ll[1]; l2c(l,out);
96 1.1 thorpej l=ll[0]=ll[1]=0;
97 1.1 thorpej }
98 1.1 thorpej
99 1.1 thorpej void des_encrypt(data, ks, encrypt)
100 1.1 thorpej DES_LONG *data;
101 1.1 thorpej des_key_schedule ks;
102 1.1 thorpej int encrypt;
103 1.1 thorpej {
104 1.1 thorpej register DES_LONG l,r,t,u;
105 1.1 thorpej #ifdef DES_PTR
106 1.1 thorpej register unsigned char *des_SP=(unsigned char *)des_SPtrans;
107 1.1 thorpej #endif
108 1.1 thorpej #ifdef undef
109 1.1 thorpej union fudge {
110 1.1 thorpej DES_LONG l;
111 1.1 thorpej unsigned short s[2];
112 1.1 thorpej unsigned char c[4];
113 1.1 thorpej } U,T;
114 1.1 thorpej #endif
115 1.1 thorpej register int i;
116 1.1 thorpej register DES_LONG *s;
117 1.1 thorpej
118 1.1 thorpej u=data[0];
119 1.1 thorpej r=data[1];
120 1.1 thorpej
121 1.1 thorpej IP(u,r);
122 1.1 thorpej /* Things have been modified so that the initial rotate is
123 1.1 thorpej * done outside the loop. This required the
124 1.1 thorpej * des_SPtrans values in sp.h to be rotated 1 bit to the right.
125 1.1 thorpej * One perl script later and things have a 5% speed up on a sparc2.
126 1.1 thorpej * Thanks to Richard Outerbridge <71755.204 (at) CompuServe.COM>
127 1.1 thorpej * for pointing this out. */
128 1.1 thorpej l=(r<<1)|(r>>31);
129 1.1 thorpej r=(u<<1)|(u>>31);
130 1.1 thorpej
131 1.1 thorpej /* clear the top bits on machines with 8byte longs */
132 1.1 thorpej l&=0xffffffffL;
133 1.1 thorpej r&=0xffffffffL;
134 1.1 thorpej
135 1.1 thorpej s=(DES_LONG *)ks;
136 1.1 thorpej /* I don't know if it is worth the effort of loop unrolling the
137 1.1 thorpej * inner loop
138 1.1 thorpej */
139 1.1 thorpej if (encrypt)
140 1.1 thorpej {
141 1.1 thorpej for (i=0; i<32; i+=8)
142 1.1 thorpej {
143 1.1 thorpej D_ENCRYPT(l,r,i+0); /* 1 */
144 1.1 thorpej D_ENCRYPT(r,l,i+2); /* 2 */
145 1.1 thorpej D_ENCRYPT(l,r,i+4); /* 3 */
146 1.1 thorpej D_ENCRYPT(r,l,i+6); /* 4 */
147 1.1 thorpej }
148 1.1 thorpej }
149 1.1 thorpej else
150 1.1 thorpej {
151 1.1 thorpej for (i=30; i>0; i-=8)
152 1.1 thorpej {
153 1.1 thorpej D_ENCRYPT(l,r,i-0); /* 16 */
154 1.1 thorpej D_ENCRYPT(r,l,i-2); /* 15 */
155 1.1 thorpej D_ENCRYPT(l,r,i-4); /* 14 */
156 1.1 thorpej D_ENCRYPT(r,l,i-6); /* 13 */
157 1.1 thorpej }
158 1.1 thorpej }
159 1.1 thorpej l=(l>>1)|(l<<31);
160 1.1 thorpej r=(r>>1)|(r<<31);
161 1.1 thorpej /* clear the top bits on machines with 8byte longs */
162 1.1 thorpej l&=0xffffffffL;
163 1.1 thorpej r&=0xffffffffL;
164 1.1 thorpej
165 1.1 thorpej FP(r,l);
166 1.1 thorpej data[0]=l;
167 1.1 thorpej data[1]=r;
168 1.1 thorpej l=r=t=u=0;
169 1.1 thorpej }
170 1.1 thorpej
171 1.1 thorpej void des_encrypt2(data, ks, encrypt)
172 1.1 thorpej DES_LONG *data;
173 1.1 thorpej des_key_schedule ks;
174 1.1 thorpej int encrypt;
175 1.1 thorpej {
176 1.1 thorpej register DES_LONG l,r,t,u;
177 1.1 thorpej #ifdef DES_PTR
178 1.1 thorpej register unsigned char *des_SP=(unsigned char *)des_SPtrans;
179 1.1 thorpej #endif
180 1.1 thorpej #ifdef undef
181 1.1 thorpej union fudge {
182 1.1 thorpej DES_LONG l;
183 1.1 thorpej unsigned short s[2];
184 1.1 thorpej unsigned char c[4];
185 1.1 thorpej } U,T;
186 1.1 thorpej #endif
187 1.1 thorpej register int i;
188 1.1 thorpej register DES_LONG *s;
189 1.1 thorpej
190 1.1 thorpej u=data[0];
191 1.1 thorpej r=data[1];
192 1.1 thorpej
193 1.1 thorpej /* Things have been modified so that the initial rotate is
194 1.1 thorpej * done outside the loop. This required the
195 1.1 thorpej * des_SPtrans values in sp.h to be rotated 1 bit to the right.
196 1.1 thorpej * One perl script later and things have a 5% speed up on a sparc2.
197 1.1 thorpej * Thanks to Richard Outerbridge <71755.204 (at) CompuServe.COM>
198 1.1 thorpej * for pointing this out. */
199 1.1 thorpej l=(r<<1)|(r>>31);
200 1.1 thorpej r=(u<<1)|(u>>31);
201 1.1 thorpej
202 1.1 thorpej /* clear the top bits on machines with 8byte longs */
203 1.1 thorpej l&=0xffffffffL;
204 1.1 thorpej r&=0xffffffffL;
205 1.1 thorpej
206 1.1 thorpej s=(DES_LONG *)ks;
207 1.1 thorpej /* I don't know if it is worth the effort of loop unrolling the
208 1.1 thorpej * inner loop */
209 1.1 thorpej if (encrypt)
210 1.1 thorpej {
211 1.1 thorpej for (i=0; i<32; i+=8)
212 1.1 thorpej {
213 1.1 thorpej D_ENCRYPT(l,r,i+0); /* 1 */
214 1.1 thorpej D_ENCRYPT(r,l,i+2); /* 2 */
215 1.1 thorpej D_ENCRYPT(l,r,i+4); /* 3 */
216 1.1 thorpej D_ENCRYPT(r,l,i+6); /* 4 */
217 1.1 thorpej }
218 1.1 thorpej }
219 1.1 thorpej else
220 1.1 thorpej {
221 1.1 thorpej for (i=30; i>0; i-=8)
222 1.1 thorpej {
223 1.1 thorpej D_ENCRYPT(l,r,i-0); /* 16 */
224 1.1 thorpej D_ENCRYPT(r,l,i-2); /* 15 */
225 1.1 thorpej D_ENCRYPT(l,r,i-4); /* 14 */
226 1.1 thorpej D_ENCRYPT(r,l,i-6); /* 13 */
227 1.1 thorpej }
228 1.1 thorpej }
229 1.1 thorpej l=(l>>1)|(l<<31);
230 1.1 thorpej r=(r>>1)|(r<<31);
231 1.1 thorpej /* clear the top bits on machines with 8byte longs */
232 1.1 thorpej l&=0xffffffffL;
233 1.1 thorpej r&=0xffffffffL;
234 1.1 thorpej
235 1.1 thorpej data[0]=l;
236 1.1 thorpej data[1]=r;
237 1.1 thorpej l=r=t=u=0;
238 1.1 thorpej }
239