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