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