des_setkey.c revision 1.3.2.3 1 1.3.2.3 bouyer /* $NetBSD: des_setkey.c,v 1.3.2.3 2000/11/22 16:03:06 bouyer Exp $ */
2 1.3.2.3 bouyer /* $KAME: des_setkey.c,v 1.5 2000/11/06 13:58:09 itojun Exp $ */
3 1.3.2.2 bouyer
4 1.3.2.2 bouyer /* crypto/des/set_key.c */
5 1.3.2.2 bouyer /* Copyright (C) 1995-1996 Eric Young (eay (at) mincom.oz.au)
6 1.3.2.2 bouyer * All rights reserved.
7 1.3.2.2 bouyer *
8 1.3.2.2 bouyer * This file is part of an SSL implementation written
9 1.3.2.2 bouyer * by Eric Young (eay (at) mincom.oz.au).
10 1.3.2.2 bouyer * The implementation was written so as to conform with Netscapes SSL
11 1.3.2.2 bouyer * specification. This library and applications are
12 1.3.2.2 bouyer * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
13 1.3.2.2 bouyer * as long as the following conditions are aheared to.
14 1.3.2.2 bouyer *
15 1.3.2.2 bouyer * Copyright remains Eric Young's, and as such any Copyright notices in
16 1.3.2.2 bouyer * the code are not to be removed. If this code is used in a product,
17 1.3.2.2 bouyer * Eric Young should be given attribution as the author of the parts used.
18 1.3.2.2 bouyer * This can be in the form of a textual message at program startup or
19 1.3.2.2 bouyer * in documentation (online or textual) provided with the package.
20 1.3.2.2 bouyer *
21 1.3.2.2 bouyer * Redistribution and use in source and binary forms, with or without
22 1.3.2.2 bouyer * modification, are permitted provided that the following conditions
23 1.3.2.2 bouyer * are met:
24 1.3.2.2 bouyer * 1. Redistributions of source code must retain the copyright
25 1.3.2.2 bouyer * notice, this list of conditions and the following disclaimer.
26 1.3.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
27 1.3.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
28 1.3.2.2 bouyer * documentation and/or other materials provided with the distribution.
29 1.3.2.2 bouyer * 3. All advertising materials mentioning features or use of this software
30 1.3.2.2 bouyer * must display the following acknowledgement:
31 1.3.2.2 bouyer * This product includes software developed by Eric Young (eay (at) mincom.oz.au)
32 1.3.2.2 bouyer *
33 1.3.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
34 1.3.2.2 bouyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 1.3.2.2 bouyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 1.3.2.2 bouyer * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
37 1.3.2.2 bouyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 1.3.2.2 bouyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 1.3.2.2 bouyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 1.3.2.2 bouyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 1.3.2.2 bouyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 1.3.2.2 bouyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 1.3.2.2 bouyer * SUCH DAMAGE.
44 1.3.2.2 bouyer *
45 1.3.2.2 bouyer * The licence and distribution terms for any publically available version or
46 1.3.2.2 bouyer * derivative of this code cannot be changed. i.e. this code cannot simply be
47 1.3.2.2 bouyer * copied and put under another distribution licence
48 1.3.2.2 bouyer * [including the GNU Public Licence.]
49 1.3.2.2 bouyer */
50 1.3.2.2 bouyer
51 1.3.2.2 bouyer /* set_key.c v 1.4 eay 24/9/91
52 1.3.2.2 bouyer * 1.4 Speed up by 400% :-)
53 1.3.2.2 bouyer * 1.3 added register declarations.
54 1.3.2.2 bouyer * 1.2 unrolled make_key_sched a bit more
55 1.3.2.2 bouyer * 1.1 added norm_expand_bits
56 1.3.2.2 bouyer * 1.0 First working version
57 1.3.2.2 bouyer */
58 1.3.2.2 bouyer #include <sys/param.h>
59 1.3.2.3 bouyer #ifdef _KERNEL
60 1.3.2.2 bouyer #include <sys/systm.h>
61 1.3.2.3 bouyer #else
62 1.3.2.3 bouyer #include <string.h>
63 1.3.2.3 bouyer #endif
64 1.3.2.2 bouyer #include <crypto/des/des_locl.h>
65 1.3.2.2 bouyer #include <crypto/des/podd.h>
66 1.3.2.2 bouyer #include <crypto/des/sk.h>
67 1.3.2.2 bouyer
68 1.3.2.2 bouyer static int check_parity __P((des_cblock (*)));
69 1.3.2.2 bouyer
70 1.3.2.2 bouyer int des_check_key=0;
71 1.3.2.2 bouyer
72 1.3.2.2 bouyer void des_set_odd_parity(key)
73 1.3.2.2 bouyer des_cblock (*key);
74 1.3.2.2 bouyer {
75 1.3.2.2 bouyer int i;
76 1.3.2.2 bouyer
77 1.3.2.2 bouyer for (i=0; i<DES_KEY_SZ; i++)
78 1.3.2.2 bouyer (*key)[i]=odd_parity[(*key)[i]];
79 1.3.2.2 bouyer }
80 1.3.2.2 bouyer
81 1.3.2.2 bouyer static int check_parity(key)
82 1.3.2.2 bouyer des_cblock (*key);
83 1.3.2.2 bouyer {
84 1.3.2.2 bouyer int i;
85 1.3.2.2 bouyer
86 1.3.2.2 bouyer for (i=0; i<DES_KEY_SZ; i++)
87 1.3.2.2 bouyer {
88 1.3.2.2 bouyer if ((*key)[i] != odd_parity[(*key)[i]])
89 1.3.2.2 bouyer return(0);
90 1.3.2.2 bouyer }
91 1.3.2.2 bouyer return(1);
92 1.3.2.2 bouyer }
93 1.3.2.2 bouyer
94 1.3.2.2 bouyer /* Weak and semi week keys as take from
95 1.3.2.2 bouyer * %A D.W. Davies
96 1.3.2.2 bouyer * %A W.L. Price
97 1.3.2.2 bouyer * %T Security for Computer Networks
98 1.3.2.2 bouyer * %I John Wiley & Sons
99 1.3.2.2 bouyer * %D 1984
100 1.3.2.2 bouyer * Many thanks to smb (at) ulysses.att.com (Steven Bellovin) for the reference
101 1.3.2.2 bouyer * (and actual cblock values).
102 1.3.2.2 bouyer */
103 1.3.2.2 bouyer #define NUM_WEAK_KEY 16
104 1.3.2.2 bouyer static des_cblock weak_keys[NUM_WEAK_KEY]={
105 1.3.2.2 bouyer /* weak keys */
106 1.3.2.2 bouyer {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
107 1.3.2.2 bouyer {0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE},
108 1.3.2.2 bouyer {0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F},
109 1.3.2.2 bouyer {0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0},
110 1.3.2.2 bouyer /* semi-weak keys */
111 1.3.2.2 bouyer {0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE},
112 1.3.2.2 bouyer {0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01},
113 1.3.2.2 bouyer {0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1},
114 1.3.2.2 bouyer {0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E},
115 1.3.2.2 bouyer {0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1},
116 1.3.2.2 bouyer {0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01},
117 1.3.2.2 bouyer {0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE},
118 1.3.2.2 bouyer {0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E},
119 1.3.2.2 bouyer {0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E},
120 1.3.2.2 bouyer {0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01},
121 1.3.2.2 bouyer {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
122 1.3.2.2 bouyer {0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}};
123 1.3.2.2 bouyer
124 1.3.2.2 bouyer int des_is_weak_key(key)
125 1.3.2.2 bouyer des_cblock (*key);
126 1.3.2.2 bouyer {
127 1.3.2.2 bouyer int i;
128 1.3.2.2 bouyer
129 1.3.2.2 bouyer for (i=0; i<NUM_WEAK_KEY; i++)
130 1.3.2.2 bouyer /* Added == 0 to comparision, I obviously don't run
131 1.3.2.2 bouyer * this section very often :-(, thanks to
132 1.3.2.2 bouyer * engineering (at) MorningStar.Com for the fix
133 1.3.2.2 bouyer * eay 93/06/29 */
134 1.3.2.2 bouyer /*
135 1.3.2.2 bouyer if (memcmp(weak_keys[i],key,sizeof(key)) == 0) return(1);
136 1.3.2.2 bouyer */
137 1.3.2.2 bouyer if (bcmp(weak_keys[i],key,sizeof(key)) == 0) return(1);
138 1.3.2.2 bouyer return(0);
139 1.3.2.2 bouyer }
140 1.3.2.2 bouyer
141 1.3.2.2 bouyer /* NOW DEFINED IN des_local.h
142 1.3.2.2 bouyer * See ecb_encrypt.c for a pseudo description of these macros.
143 1.3.2.2 bouyer * #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
144 1.3.2.2 bouyer * (b)^=(t),\
145 1.3.2.2 bouyer * (a)=((a)^((t)<<(n))))
146 1.3.2.2 bouyer */
147 1.3.2.2 bouyer
148 1.3.2.2 bouyer #define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\
149 1.3.2.2 bouyer (a)=(a)^(t)^(t>>(16-(n))))
150 1.3.2.2 bouyer
151 1.3.2.2 bouyer /* return 0 if key parity is odd (correct),
152 1.3.2.2 bouyer * return -1 if key parity error,
153 1.3.2.2 bouyer * return -2 if illegal weak key.
154 1.3.2.2 bouyer */
155 1.3.2.2 bouyer int des_set_key(key, schedule)
156 1.3.2.2 bouyer des_cblock (*key);
157 1.3.2.2 bouyer des_key_schedule schedule;
158 1.3.2.2 bouyer {
159 1.3.2.2 bouyer static int shifts2[16]={0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0};
160 1.3.2.2 bouyer register DES_LONG c,d,t,s;
161 1.3.2.2 bouyer register unsigned char *in;
162 1.3.2.2 bouyer register DES_LONG *k;
163 1.3.2.2 bouyer register int i;
164 1.3.2.2 bouyer
165 1.3.2.2 bouyer if (des_check_key)
166 1.3.2.2 bouyer {
167 1.3.2.2 bouyer if (!check_parity(key))
168 1.3.2.2 bouyer return(-1);
169 1.3.2.2 bouyer
170 1.3.2.2 bouyer if (des_is_weak_key(key))
171 1.3.2.2 bouyer return(-2);
172 1.3.2.2 bouyer }
173 1.3.2.2 bouyer
174 1.3.2.2 bouyer k=(DES_LONG *)schedule;
175 1.3.2.2 bouyer in=(unsigned char *)key;
176 1.3.2.2 bouyer
177 1.3.2.2 bouyer c2l(in,c);
178 1.3.2.2 bouyer c2l(in,d);
179 1.3.2.2 bouyer
180 1.3.2.2 bouyer /* do PC1 in 60 simple operations */
181 1.3.2.2 bouyer /* PERM_OP(d,c,t,4,0x0f0f0f0fL);
182 1.3.2.2 bouyer HPERM_OP(c,t,-2, 0xcccc0000L);
183 1.3.2.2 bouyer HPERM_OP(c,t,-1, 0xaaaa0000L);
184 1.3.2.2 bouyer HPERM_OP(c,t, 8, 0x00ff0000L);
185 1.3.2.2 bouyer HPERM_OP(c,t,-1, 0xaaaa0000L);
186 1.3.2.2 bouyer HPERM_OP(d,t,-8, 0xff000000L);
187 1.3.2.2 bouyer HPERM_OP(d,t, 8, 0x00ff0000L);
188 1.3.2.2 bouyer HPERM_OP(d,t, 2, 0x33330000L);
189 1.3.2.2 bouyer d=((d&0x00aa00aaL)<<7L)|((d&0x55005500L)>>7L)|(d&0xaa55aa55L);
190 1.3.2.2 bouyer d=(d>>8)|((c&0xf0000000L)>>4);
191 1.3.2.2 bouyer c&=0x0fffffffL; */
192 1.3.2.2 bouyer
193 1.3.2.2 bouyer /* I now do it in 47 simple operations :-)
194 1.3.2.2 bouyer * Thanks to John Fletcher (john_fletcher (at) lccmail.ocf.llnl.gov)
195 1.3.2.2 bouyer * for the inspiration. :-) */
196 1.3.2.2 bouyer PERM_OP (d,c,t,4,0x0f0f0f0fL);
197 1.3.2.2 bouyer HPERM_OP(c,t,-2,0xcccc0000L);
198 1.3.2.2 bouyer HPERM_OP(d,t,-2,0xcccc0000L);
199 1.3.2.2 bouyer PERM_OP (d,c,t,1,0x55555555L);
200 1.3.2.2 bouyer PERM_OP (c,d,t,8,0x00ff00ffL);
201 1.3.2.2 bouyer PERM_OP (d,c,t,1,0x55555555L);
202 1.3.2.2 bouyer d= (((d&0x000000ffL)<<16L)| (d&0x0000ff00L) |
203 1.3.2.2 bouyer ((d&0x00ff0000L)>>16L)|((c&0xf0000000L)>>4L));
204 1.3.2.2 bouyer c&=0x0fffffffL;
205 1.3.2.2 bouyer
206 1.3.2.2 bouyer for (i=0; i<ITERATIONS; i++)
207 1.3.2.2 bouyer {
208 1.3.2.2 bouyer if (shifts2[i])
209 1.3.2.2 bouyer { c=((c>>2L)|(c<<26L)); d=((d>>2L)|(d<<26L)); }
210 1.3.2.2 bouyer else
211 1.3.2.2 bouyer { c=((c>>1L)|(c<<27L)); d=((d>>1L)|(d<<27L)); }
212 1.3.2.2 bouyer c&=0x0fffffffL;
213 1.3.2.2 bouyer d&=0x0fffffffL;
214 1.3.2.2 bouyer /* could be a few less shifts but I am to lazy at this
215 1.3.2.2 bouyer * point in time to investigate */
216 1.3.2.2 bouyer s= des_skb[0][ (c )&0x3f ]|
217 1.3.2.2 bouyer des_skb[1][((c>> 6)&0x03)|((c>> 7L)&0x3c)]|
218 1.3.2.2 bouyer des_skb[2][((c>>13)&0x0f)|((c>>14L)&0x30)]|
219 1.3.2.2 bouyer des_skb[3][((c>>20)&0x01)|((c>>21L)&0x06) |
220 1.3.2.2 bouyer ((c>>22L)&0x38)];
221 1.3.2.2 bouyer t= des_skb[4][ (d )&0x3f ]|
222 1.3.2.2 bouyer des_skb[5][((d>> 7L)&0x03)|((d>> 8L)&0x3c)]|
223 1.3.2.2 bouyer des_skb[6][ (d>>15L)&0x3f ]|
224 1.3.2.2 bouyer des_skb[7][((d>>21L)&0x0f)|((d>>22L)&0x30)];
225 1.3.2.2 bouyer
226 1.3.2.2 bouyer /* table contained 0213 4657 */
227 1.3.2.2 bouyer *(k++)=((t<<16L)|(s&0x0000ffffL))&0xffffffffL;
228 1.3.2.2 bouyer s= ((s>>16L)|(t&0xffff0000L));
229 1.3.2.2 bouyer
230 1.3.2.2 bouyer s=(s<<4L)|(s>>28L);
231 1.3.2.2 bouyer *(k++)=s&0xffffffffL;
232 1.3.2.2 bouyer }
233 1.3.2.2 bouyer return(0);
234 1.3.2.2 bouyer }
235 1.3.2.2 bouyer
236 1.3.2.2 bouyer int des_key_sched(key, schedule)
237 1.3.2.2 bouyer des_cblock (*key);
238 1.3.2.2 bouyer des_key_schedule schedule;
239 1.3.2.2 bouyer {
240 1.3.2.2 bouyer return(des_set_key(key,schedule));
241 1.3.2.2 bouyer }
242