set_key.c revision 55714
1191783Srmacklem/* crypto/des/set_key.c */
2191783Srmacklem/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3191783Srmacklem * All rights reserved.
4191783Srmacklem *
5191783Srmacklem * This package is an SSL implementation written
6191783Srmacklem * by Eric Young (eay@cryptsoft.com).
7191783Srmacklem * The implementation was written so as to conform with Netscapes SSL.
8191783Srmacklem *
9191783Srmacklem * This library is free for commercial and non-commercial use as long as
10191783Srmacklem * the following conditions are aheared to.  The following conditions
11191783Srmacklem * apply to all code found in this distribution, be it the RC4, RSA,
12191783Srmacklem * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13191783Srmacklem * included with this distribution is covered by the same copyright terms
14191783Srmacklem * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15191783Srmacklem *
16191783Srmacklem * Copyright remains Eric Young's, and as such any Copyright notices in
17191783Srmacklem * the code are not to be removed.
18191783Srmacklem * If this package is used in a product, Eric Young should be given attribution
19191783Srmacklem * as the author of the parts of the library used.
20191783Srmacklem * This can be in the form of a textual message at program startup or
21191783Srmacklem * in documentation (online or textual) provided with the package.
22191783Srmacklem *
23191783Srmacklem * Redistribution and use in source and binary forms, with or without
24191783Srmacklem * modification, are permitted provided that the following conditions
25191783Srmacklem * are met:
26191783Srmacklem * 1. Redistributions of source code must retain the copyright
27191783Srmacklem *    notice, this list of conditions and the following disclaimer.
28191783Srmacklem * 2. Redistributions in binary form must reproduce the above copyright
29191783Srmacklem *    notice, this list of conditions and the following disclaimer in the
30191783Srmacklem *    documentation and/or other materials provided with the distribution.
31191783Srmacklem * 3. All advertising materials mentioning features or use of this software
32191783Srmacklem *    must display the following acknowledgement:
33191783Srmacklem *    "This product includes cryptographic software written by
34191783Srmacklem *     Eric Young (eay@cryptsoft.com)"
35191783Srmacklem *    The word 'cryptographic' can be left out if the rouines from the library
36191783Srmacklem *    being used are not cryptographic related :-).
37224778Srwatson * 4. If you include any Windows specific code (or a derivative thereof) from
38224778Srwatson *    the apps directory (application code) you must include an acknowledgement:
39191783Srmacklem *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40191783Srmacklem *
41191783Srmacklem * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42191783Srmacklem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43191783Srmacklem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44191783Srmacklem * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45191783Srmacklem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46214255Srmacklem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47191783Srmacklem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48192503Srmacklem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49192503Srmacklem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50191783Srmacklem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51219028Snetchild * SUCH DAMAGE.
52219028Snetchild *
53191783Srmacklem * The licence and distribution terms for any publically available version or
54191783Srmacklem * derivative of this code cannot be changed.  i.e. this code cannot simply be
55191783Srmacklem * copied and put under another distribution licence
56191783Srmacklem * [including the GNU Public Licence.]
57191783Srmacklem */
58191783Srmacklem
59220530Srmacklem/* set_key.c v 1.4 eay 24/9/91
60243738Srmacklem * 1.4 Speed up by 400% :-)
61191783Srmacklem * 1.3 added register declarations.
62191783Srmacklem * 1.2 unrolled make_key_sched a bit more
63255532Srmacklem * 1.1 added norm_expand_bits
64255532Srmacklem * 1.0 First working version
65191783Srmacklem */
66191783Srmacklem#include "des_locl.h"
67191783Srmacklem#include "podd.h"
68217432Srmacklem#include "sk.h"
69217432Srmacklem
70217432Srmacklemstatic int check_parity(const_des_cblock *key);
71217432SrmacklemOPENSSL_GLOBAL int des_check_key=0;
72192503Srmacklem
73191783Srmacklemvoid des_set_odd_parity(des_cblock *key)
74192255Srmacklem	{
75192255Srmacklem	int i;
76191783Srmacklem
77220645Srmacklem	for (i=0; i<DES_KEY_SZ; i++)
78191783Srmacklem		(*key)[i]=odd_parity[(*key)[i]];
79191783Srmacklem	}
80191783Srmacklem
81192255Srmacklemstatic int check_parity(const_des_cblock *key)
82192255Srmacklem	{
83221615Srmacklem	int i;
84221615Srmacklem
85220645Srmacklem	for (i=0; i<DES_KEY_SZ; i++)
86221615Srmacklem		{
87192255Srmacklem		if ((*key)[i] != odd_parity[(*key)[i]])
88221615Srmacklem			return(0);
89192255Srmacklem		}
90221615Srmacklem	return(1);
91192255Srmacklem	}
92221615Srmacklem
93192255Srmacklem/* Weak and semi week keys as take from
94191783Srmacklem * %A D.W. Davies
95229617Sjhb * %A W.L. Price
96229617Sjhb * %T Security for Computer Networks
97191783Srmacklem * %I John Wiley & Sons
98191783Srmacklem * %D 1984
99191783Srmacklem * Many thanks to smb@ulysses.att.com (Steven Bellovin) for the reference
100191783Srmacklem * (and actual cblock values).
101191783Srmacklem */
102191783Srmacklem#define NUM_WEAK_KEY	16
103229617Sjhbstatic des_cblock weak_keys[NUM_WEAK_KEY]={
104191783Srmacklem	/* weak keys */
105191783Srmacklem	{0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
106191783Srmacklem	{0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE},
107191783Srmacklem	{0x1F,0x1F,0x1F,0x1F,0x0E,0x0E,0x0E,0x0E},
108191783Srmacklem	{0xE0,0xE0,0xE0,0xE0,0xF1,0xF1,0xF1,0xF1},
109191783Srmacklem	/* semi-weak keys */
110229617Sjhb	{0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE},
111229617Sjhb	{0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01},
112229617Sjhb	{0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1},
113229617Sjhb	{0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E},
114229617Sjhb	{0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1},
115229617Sjhb	{0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01},
116229617Sjhb	{0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE},
117229617Sjhb	{0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E},
118229617Sjhb	{0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E},
119229617Sjhb	{0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01},
120229617Sjhb	{0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
121229617Sjhb	{0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}};
122229617Sjhb
123229617Sjhbint des_is_weak_key(const_des_cblock *key)
124229617Sjhb	{
125229617Sjhb	int i;
126229617Sjhb
127229617Sjhb	for (i=0; i<NUM_WEAK_KEY; i++)
128229617Sjhb		/* Added == 0 to comparision, I obviously don't run
129229617Sjhb		 * this section very often :-(, thanks to
130229617Sjhb		 * engineering@MorningStar.Com for the fix
131229617Sjhb		 * eay 93/06/29
132229617Sjhb		 * Another problem, I was comparing only the first 4
133229617Sjhb		 * bytes, 97/03/18 */
134229617Sjhb		if (memcmp(weak_keys[i],key,sizeof(des_cblock)) == 0) return(1);
135229617Sjhb	return(0);
136229617Sjhb	}
137229617Sjhb
138229617Sjhb/* NOW DEFINED IN des_local.h
139229617Sjhb * See ecb_encrypt.c for a pseudo description of these macros.
140229617Sjhb * #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
141229617Sjhb * 	(b)^=(t),\
142229617Sjhb * 	(a)=((a)^((t)<<(n))))
143229617Sjhb */
144229617Sjhb
145229617Sjhb#define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\
146229617Sjhb	(a)=(a)^(t)^(t>>(16-(n))))
147229617Sjhb
148229617Sjhb/* return 0 if key parity is odd (correct),
149229617Sjhb * return -1 if key parity error,
150229617Sjhb * return -2 if illegal weak key.
151229617Sjhb */
152229617Sjhbint des_set_key(const_des_cblock *key, des_key_schedule schedule)
153229617Sjhb	{
154229617Sjhb	static int shifts2[16]={0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0};
155229617Sjhb	register DES_LONG c,d,t,s,t2;
156229617Sjhb	register const unsigned char *in;
157229617Sjhb	register DES_LONG *k;
158229617Sjhb	register int i;
159229617Sjhb
160229617Sjhb	if (des_check_key)
161229617Sjhb		{
162229617Sjhb		if (!check_parity(key))
163229617Sjhb			return(-1);
164229617Sjhb
165229617Sjhb		if (des_is_weak_key(key))
166229617Sjhb			return(-2);
167191783Srmacklem		}
168191783Srmacklem
169191783Srmacklem	k = &schedule->ks.deslong[0];
170191783Srmacklem	in = &(*key)[0];
171216693Srmacklem
172191783Srmacklem	c2l(in,c);
173191783Srmacklem	c2l(in,d);
174191783Srmacklem
175216693Srmacklem	/* do PC1 in 60 simple operations */
176216693Srmacklem/*	PERM_OP(d,c,t,4,0x0f0f0f0fL);
177216693Srmacklem	HPERM_OP(c,t,-2, 0xcccc0000L);
178216693Srmacklem	HPERM_OP(c,t,-1, 0xaaaa0000L);
179216693Srmacklem	HPERM_OP(c,t, 8, 0x00ff0000L);
180216693Srmacklem	HPERM_OP(c,t,-1, 0xaaaa0000L);
181224083Szack	HPERM_OP(d,t,-8, 0xff000000L);
182216693Srmacklem	HPERM_OP(d,t, 8, 0x00ff0000L);
183224081Szack	HPERM_OP(d,t, 2, 0x33330000L);
184216693Srmacklem	d=((d&0x00aa00aaL)<<7L)|((d&0x55005500L)>>7L)|(d&0xaa55aa55L);
185191783Srmacklem	d=(d>>8)|((c&0xf0000000L)>>4);
186191783Srmacklem	c&=0x0fffffffL; */
187216693Srmacklem
188224082Szack	/* I now do it in 47 simple operations :-)
189224086Szack	 * Thanks to John Fletcher (john_fletcher@lccmail.ocf.llnl.gov)
190224086Szack	 * for the inspiration. :-) */
191191783Srmacklem	PERM_OP (d,c,t,4,0x0f0f0f0fL);
192191783Srmacklem	HPERM_OP(c,t,-2,0xcccc0000L);
193191783Srmacklem	HPERM_OP(d,t,-2,0xcccc0000L);
194191783Srmacklem	PERM_OP (d,c,t,1,0x55555555L);
195191783Srmacklem	PERM_OP (c,d,t,8,0x00ff00ffL);
196191783Srmacklem	PERM_OP (d,c,t,1,0x55555555L);
197191783Srmacklem	d=	(((d&0x000000ffL)<<16L)| (d&0x0000ff00L)     |
198191783Srmacklem		 ((d&0x00ff0000L)>>16L)|((c&0xf0000000L)>>4L));
199191783Srmacklem	c&=0x0fffffffL;
200191783Srmacklem
201191783Srmacklem	for (i=0; i<ITERATIONS; i++)
202191783Srmacklem		{
203191783Srmacklem		if (shifts2[i])
204191783Srmacklem			{ c=((c>>2L)|(c<<26L)); d=((d>>2L)|(d<<26L)); }
205224086Szack		else
206224086Szack			{ c=((c>>1L)|(c<<27L)); d=((d>>1L)|(d<<27L)); }
207191783Srmacklem		c&=0x0fffffffL;
208191783Srmacklem		d&=0x0fffffffL;
209191783Srmacklem		/* could be a few less shifts but I am to lazy at this
210191783Srmacklem		 * point in time to investigate */
211191783Srmacklem		s=	des_skb[0][ (c    )&0x3f                ]|
212191783Srmacklem			des_skb[1][((c>> 6)&0x03)|((c>> 7L)&0x3c)]|
213200999Srmacklem			des_skb[2][((c>>13)&0x0f)|((c>>14L)&0x30)]|
214200999Srmacklem			des_skb[3][((c>>20)&0x01)|((c>>21L)&0x06) |
215200999Srmacklem						  ((c>>22L)&0x38)];
216191783Srmacklem		t=	des_skb[4][ (d    )&0x3f                ]|
217191783Srmacklem			des_skb[5][((d>> 7L)&0x03)|((d>> 8L)&0x3c)]|
218191783Srmacklem			des_skb[6][ (d>>15L)&0x3f                ]|
219191783Srmacklem			des_skb[7][((d>>21L)&0x0f)|((d>>22L)&0x30)];
220191783Srmacklem
221200999Srmacklem		/* table contained 0213 4657 */
222200999Srmacklem		t2=((t<<16L)|(s&0x0000ffffL))&0xffffffffL;
223200999Srmacklem		*(k++)=ROTATE(t2,30)&0xffffffffL;
224191783Srmacklem
225191783Srmacklem		t2=((s>>16L)|(t&0xffff0000L));
226191783Srmacklem		*(k++)=ROTATE(t2,26)&0xffffffffL;
227191783Srmacklem		}
228216893Srmacklem	return(0);
229224086Szack	}
230224086Szack
231224086Szackint des_key_sched(const_des_cblock *key, des_key_schedule schedule)
232224086Szack	{
233216893Srmacklem	return(des_set_key(key,schedule));
234191783Srmacklem	}
235191783Srmacklem