]> git.saurik.com Git - apple/xnu.git/blob - bsd/crypto/des/des_ecb.c
b0c239ac8d876c2c32d8c6e3955afa4123295935
[apple/xnu.git] / bsd / crypto / des / des_ecb.c
1 /* crypto/des/ecb_enc.c */
2 /* Copyright (C) 1995-1996 Eric Young (eay@mincom.oz.au)
3 * All rights reserved.
4 *
5 * This file is part of an SSL implementation written
6 * by Eric Young (eay@mincom.oz.au).
7 * The implementation was written so as to conform with Netscapes SSL
8 * specification. This library and applications are
9 * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
10 * as long as the following conditions are aheared to.
11 *
12 * Copyright remains Eric Young's, and as such any Copyright notices in
13 * the code are not to be removed. If this code is used in a product,
14 * Eric Young should be given attribution as the author of the parts used.
15 * This can be in the form of a textual message at program startup or
16 * in documentation (online or textual) provided with the package.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. All advertising materials mentioning features or use of this software
27 * must display the following acknowledgement:
28 * This product includes software developed by Eric Young (eay@mincom.oz.au)
29 *
30 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 *
42 * The licence and distribution terms for any publically available version or
43 * derivative of this code cannot be changed. i.e. this code cannot simply be
44 * copied and put under another distribution licence
45 * [including the GNU Public Licence.]
46 */
47
48 #include <crypto/des/des_locl.h>
49 #include <crypto/des/spr.h>
50
51 char *libdes_version="libdes v 3.24 - 20-Apr-1996 - eay";
52 char *DES_version="DES part of SSLeay 0.6.4 30-Aug-1996";
53
54 char *des_options()
55 {
56 #ifdef DES_PTR
57 if (sizeof(DES_LONG) != sizeof(long))
58 return("des(ptr,int)");
59 else
60 return("des(ptr,long)");
61 #else
62 if (sizeof(DES_LONG) != sizeof(long))
63 return("des(idx,int)");
64 else
65 return("des(idx,long)");
66 #endif
67 }
68
69
70 void des_ecb_encrypt(input, output, ks, encrypt)
71 des_cblock (*input);
72 des_cblock (*output);
73 des_key_schedule ks;
74 int encrypt;
75 {
76 register DES_LONG l;
77 register unsigned char *in,*out;
78 DES_LONG ll[2];
79
80 in=(unsigned char *)input;
81 out=(unsigned char *)output;
82 c2l(in,l); ll[0]=l;
83 c2l(in,l); ll[1]=l;
84 des_encrypt(ll,ks,encrypt);
85 l=ll[0]; l2c(l,out);
86 l=ll[1]; l2c(l,out);
87 l=ll[0]=ll[1]=0;
88 }
89
90 void des_encrypt(data, ks, encrypt)
91 DES_LONG *data;
92 des_key_schedule ks;
93 int encrypt;
94 {
95 register DES_LONG l,r,t,u;
96 #ifdef DES_PTR
97 register unsigned char *des_SP=(unsigned char *)des_SPtrans;
98 #endif
99 #ifdef undef
100 union fudge {
101 DES_LONG l;
102 unsigned short s[2];
103 unsigned char c[4];
104 } U,T;
105 #endif
106 register int i;
107 register DES_LONG *s;
108
109 u=data[0];
110 r=data[1];
111
112 IP(u,r);
113 /* Things have been modified so that the initial rotate is
114 * done outside the loop. This required the
115 * des_SPtrans values in sp.h to be rotated 1 bit to the right.
116 * One perl script later and things have a 5% speed up on a sparc2.
117 * Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
118 * for pointing this out. */
119 l=(r<<1)|(r>>31);
120 r=(u<<1)|(u>>31);
121
122 /* clear the top bits on machines with 8byte longs */
123 l&=0xffffffffL;
124 r&=0xffffffffL;
125
126 s=(DES_LONG *)ks;
127 /* I don't know if it is worth the effort of loop unrolling the
128 * inner loop
129 */
130 if (encrypt)
131 {
132 for (i=0; i<32; i+=8)
133 {
134 D_ENCRYPT(l,r,i+0); /* 1 */
135 D_ENCRYPT(r,l,i+2); /* 2 */
136 D_ENCRYPT(l,r,i+4); /* 3 */
137 D_ENCRYPT(r,l,i+6); /* 4 */
138 }
139 }
140 else
141 {
142 for (i=30; i>0; i-=8)
143 {
144 D_ENCRYPT(l,r,i-0); /* 16 */
145 D_ENCRYPT(r,l,i-2); /* 15 */
146 D_ENCRYPT(l,r,i-4); /* 14 */
147 D_ENCRYPT(r,l,i-6); /* 13 */
148 }
149 }
150 l=(l>>1)|(l<<31);
151 r=(r>>1)|(r<<31);
152 /* clear the top bits on machines with 8byte longs */
153 l&=0xffffffffL;
154 r&=0xffffffffL;
155
156 FP(r,l);
157 data[0]=l;
158 data[1]=r;
159 l=r=t=u=0;
160 }
161
162 void des_encrypt2(data, ks, encrypt)
163 DES_LONG *data;
164 des_key_schedule ks;
165 int encrypt;
166 {
167 register DES_LONG l,r,t,u;
168 #ifdef DES_PTR
169 register unsigned char *des_SP=(unsigned char *)des_SPtrans;
170 #endif
171 #ifdef undef
172 union fudge {
173 DES_LONG l;
174 unsigned short s[2];
175 unsigned char c[4];
176 } U,T;
177 #endif
178 register int i;
179 register DES_LONG *s;
180
181 u=data[0];
182 r=data[1];
183
184 /* Things have been modified so that the initial rotate is
185 * done outside the loop. This required the
186 * des_SPtrans values in sp.h to be rotated 1 bit to the right.
187 * One perl script later and things have a 5% speed up on a sparc2.
188 * Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
189 * for pointing this out. */
190 l=(r<<1)|(r>>31);
191 r=(u<<1)|(u>>31);
192
193 /* clear the top bits on machines with 8byte longs */
194 l&=0xffffffffL;
195 r&=0xffffffffL;
196
197 s=(DES_LONG *)ks;
198 /* I don't know if it is worth the effort of loop unrolling the
199 * inner loop */
200 if (encrypt)
201 {
202 for (i=0; i<32; i+=8)
203 {
204 D_ENCRYPT(l,r,i+0); /* 1 */
205 D_ENCRYPT(r,l,i+2); /* 2 */
206 D_ENCRYPT(l,r,i+4); /* 3 */
207 D_ENCRYPT(r,l,i+6); /* 4 */
208 }
209 }
210 else
211 {
212 for (i=30; i>0; i-=8)
213 {
214 D_ENCRYPT(l,r,i-0); /* 16 */
215 D_ENCRYPT(r,l,i-2); /* 15 */
216 D_ENCRYPT(l,r,i-4); /* 14 */
217 D_ENCRYPT(r,l,i-6); /* 13 */
218 }
219 }
220 l=(l>>1)|(l<<31);
221 r=(r>>1)|(r<<31);
222 /* clear the top bits on machines with 8byte longs */
223 l&=0xffffffffL;
224 r&=0xffffffffL;
225
226 data[0]=l;
227 data[1]=r;
228 l=r=t=u=0;
229 }