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