191671Sume/*	$KAME: des_setkey.c,v 1.7 2001/09/10 04:03:58 itojun Exp $	*/
262587Sitojun
355009Sshin/* crypto/des/set_key.c */
4130443Sobrien
555009Sshin/* Copyright (C) 1995-1996 Eric Young (eay@mincom.oz.au)
655009Sshin * All rights reserved.
755009Sshin *
855009Sshin * This file is part of an SSL implementation written
955009Sshin * by Eric Young (eay@mincom.oz.au).
1055009Sshin * The implementation was written so as to conform with Netscapes SSL
1155009Sshin * specification.  This library and applications are
1255009Sshin * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
1355009Sshin * as long as the following conditions are aheared to.
1455009Sshin *
1555009Sshin * Copyright remains Eric Young's, and as such any Copyright notices in
1655009Sshin * the code are not to be removed.  If this code is used in a product,
1755009Sshin * Eric Young should be given attribution as the author of the parts used.
1855009Sshin * This can be in the form of a textual message at program startup or
1955009Sshin * in documentation (online or textual) provided with the package.
2055009Sshin *
2155009Sshin * Redistribution and use in source and binary forms, with or without
2255009Sshin * modification, are permitted provided that the following conditions
2355009Sshin * are met:
2455009Sshin * 1. Redistributions of source code must retain the copyright
2555009Sshin *    notice, this list of conditions and the following disclaimer.
2655009Sshin * 2. Redistributions in binary form must reproduce the above copyright
2755009Sshin *    notice, this list of conditions and the following disclaimer in the
2855009Sshin *    documentation and/or other materials provided with the distribution.
2955009Sshin * 3. All advertising materials mentioning features or use of this software
3055009Sshin *    must display the following acknowledgement:
3155009Sshin *    This product includes software developed by Eric Young (eay@mincom.oz.au)
3255009Sshin *
3355009Sshin * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
3455009Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3555009Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3655009Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
3755009Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3855009Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3955009Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4055009Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4155009Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
4255009Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4355009Sshin * SUCH DAMAGE.
4455009Sshin *
4555009Sshin * The licence and distribution terms for any publically available version or
4655009Sshin * derivative of this code cannot be changed.  i.e. this code cannot simply be
4755009Sshin * copied and put under another distribution licence
4855009Sshin * [including the GNU Public Licence.]
4955009Sshin */
5055009Sshin
5155009Sshin/* set_key.c v 1.4 eay 24/9/91
5255009Sshin * 1.4 Speed up by 400% :-)
5355009Sshin * 1.3 added register declarations.
5455009Sshin * 1.2 unrolled make_key_sched a bit more
5555009Sshin * 1.1 added norm_expand_bits
5655009Sshin * 1.0 First working version
5755009Sshin */
58130443Sobrien
59130443Sobrien#include <sys/cdefs.h>
60130443Sobrien__FBSDID("$FreeBSD$");
61130443Sobrien
6278064Sume#include <sys/param.h>
6378064Sume#include <sys/systm.h>
6455009Sshin#include <crypto/des/des_locl.h>
6555009Sshin#include <crypto/des/podd.h>
6655009Sshin#include <crypto/des/sk.h>
6755009Sshin
6855009Sshinint des_check_key=0;
6955009Sshin
7091671Sumevoid des_set_odd_parity(des_cblock *key)
7191671Sume{
7255009Sshin	int i;
7355009Sshin
7455009Sshin	for (i=0; i<DES_KEY_SZ; i++)
7555009Sshin		(*key)[i]=odd_parity[(*key)[i]];
7691671Sume}
7755009Sshin
7891671Sumeint des_check_key_parity(des_cblock *key)
7991671Sume{
8055009Sshin	int i;
8155009Sshin
8255009Sshin	for (i=0; i<DES_KEY_SZ; i++)
8355009Sshin		{
8455009Sshin		if ((*key)[i] != odd_parity[(*key)[i]])
8555009Sshin			return(0);
8655009Sshin		}
8755009Sshin	return(1);
8891671Sume}
8955009Sshin
9055009Sshin/* Weak and semi week keys as take from
9155009Sshin * %A D.W. Davies
9255009Sshin * %A W.L. Price
9355009Sshin * %T Security for Computer Networks
9455009Sshin * %I John Wiley & Sons
9555009Sshin * %D 1984
9655009Sshin * Many thanks to smb@ulysses.att.com (Steven Bellovin) for the reference
9755009Sshin * (and actual cblock values).
9855009Sshin */
9962587Sitojun#define NUM_WEAK_KEY	16
10055009Sshinstatic des_cblock weak_keys[NUM_WEAK_KEY]={
10155009Sshin	/* weak keys */
10255009Sshin	{0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
10355009Sshin	{0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE},
10491671Sume	{0x1F,0x1F,0x1F,0x1F,0x0E,0x0E,0x0E,0x0E},
10591671Sume	{0xE0,0xE0,0xE0,0xE0,0xF1,0xF1,0xF1,0xF1},
10655009Sshin	/* semi-weak keys */
10755009Sshin	{0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE},
10855009Sshin	{0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01},
10955009Sshin	{0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1},
11055009Sshin	{0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E},
11155009Sshin	{0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1},
11255009Sshin	{0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01},
11355009Sshin	{0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE},
11455009Sshin	{0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E},
11555009Sshin	{0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E},
11655009Sshin	{0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01},
11755009Sshin	{0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
11855009Sshin	{0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}};
11955009Sshin
12091671Sumeint des_is_weak_key(des_cblock *key)
12191671Sume{
12255009Sshin	int i;
12355009Sshin
12455009Sshin	for (i=0; i<NUM_WEAK_KEY; i++)
12591671Sume		/* Added == 0 to comparison, I obviously don't run
12655009Sshin		 * this section very often :-(, thanks to
12755009Sshin		 * engineering@MorningStar.Com for the fix
12891671Sume		 * eay 93/06/29
12991671Sume		 * Another problem, I was comparing only the first 4
13091671Sume		 * bytes, 97/03/18 */
13191671Sume		if (memcmp(weak_keys[i],key,sizeof(des_cblock)) == 0) return(1);
13255009Sshin	return(0);
13391671Sume}
13455009Sshin
13555009Sshin/* NOW DEFINED IN des_local.h
13691671Sume * See ecb_encrypt.c for a pseudo description of these macros.
13755009Sshin * #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
13855009Sshin * 	(b)^=(t),\
13955009Sshin * 	(a)=((a)^((t)<<(n))))
14055009Sshin */
14155009Sshin
14262587Sitojun#define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\
14355009Sshin	(a)=(a)^(t)^(t>>(16-(n))))
14455009Sshin
14591671Sumeint des_set_key(des_cblock *key, des_key_schedule schedule)
14691671Sume{
14791671Sume	if (des_check_key)
14891671Sume	{
14991671Sume		return des_set_key_checked(key, schedule);
15091671Sume	}
15191671Sume	else
15291671Sume	{
15391671Sume		des_set_key_unchecked(key, schedule);
15491671Sume		return 0;
15591671Sume	}
15691671Sume}
15791671Sume
15855009Sshin/* return 0 if key parity is odd (correct),
15955009Sshin * return -1 if key parity error,
16055009Sshin * return -2 if illegal weak key.
16155009Sshin */
16291671Sumeint des_set_key_checked(des_cblock *key, des_key_schedule schedule)
16391671Sume{
16491671Sume	if (!des_check_key_parity(key))
16591671Sume		return(-1);
16691671Sume	if (des_is_weak_key(key))
16791671Sume		return(-2);
16891671Sume	des_set_key_unchecked(key, schedule);
16991671Sume	return 0;
17091671Sume}
17191671Sume
17291671Sumevoid des_set_key_unchecked(des_cblock *key, des_key_schedule schedule)
17391671Sume{
17455009Sshin	static int shifts2[16]={0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0};
17591671Sume	register DES_LONG c,d,t,s,t2;
17691671Sume	register const unsigned char *in;
17755009Sshin	register DES_LONG *k;
17855009Sshin	register int i;
17955009Sshin
18091671Sume	k = &schedule->ks.deslong[0];
18191671Sume	in = &(*key)[0];
18255009Sshin
18355009Sshin	c2l(in,c);
18455009Sshin	c2l(in,d);
18555009Sshin
18691671Sume	/* do PC1 in 47 simple operations :-)
18755009Sshin	 * Thanks to John Fletcher (john_fletcher@lccmail.ocf.llnl.gov)
18855009Sshin	 * for the inspiration. :-) */
18955009Sshin	PERM_OP (d,c,t,4,0x0f0f0f0fL);
19055009Sshin	HPERM_OP(c,t,-2,0xcccc0000L);
19155009Sshin	HPERM_OP(d,t,-2,0xcccc0000L);
19255009Sshin	PERM_OP (d,c,t,1,0x55555555L);
19355009Sshin	PERM_OP (c,d,t,8,0x00ff00ffL);
19455009Sshin	PERM_OP (d,c,t,1,0x55555555L);
19555009Sshin	d=	(((d&0x000000ffL)<<16L)| (d&0x0000ff00L)     |
19655009Sshin		 ((d&0x00ff0000L)>>16L)|((c&0xf0000000L)>>4L));
19755009Sshin	c&=0x0fffffffL;
19855009Sshin
19955009Sshin	for (i=0; i<ITERATIONS; i++)
20091671Sume	{
20155009Sshin		if (shifts2[i])
20255009Sshin			{ c=((c>>2L)|(c<<26L)); d=((d>>2L)|(d<<26L)); }
20355009Sshin		else
20455009Sshin			{ c=((c>>1L)|(c<<27L)); d=((d>>1L)|(d<<27L)); }
20555009Sshin		c&=0x0fffffffL;
20655009Sshin		d&=0x0fffffffL;
20755009Sshin		/* could be a few less shifts but I am to lazy at this
20891671Sume	 	* point in time to investigate */
20955009Sshin		s=	des_skb[0][ (c    )&0x3f                ]|
21091671Sume			des_skb[1][((c>> 6L)&0x03)|((c>> 7L)&0x3c)]|
21191671Sume			des_skb[2][((c>>13L)&0x0f)|((c>>14L)&0x30)]|
21291671Sume			des_skb[3][((c>>20L)&0x01)|((c>>21L)&0x06) |
21391671Sume						  	((c>>22L)&0x38)];
21455009Sshin		t=	des_skb[4][ (d    )&0x3f                ]|
21555009Sshin			des_skb[5][((d>> 7L)&0x03)|((d>> 8L)&0x3c)]|
21655009Sshin			des_skb[6][ (d>>15L)&0x3f                ]|
21755009Sshin			des_skb[7][((d>>21L)&0x0f)|((d>>22L)&0x30)];
21855009Sshin
21955009Sshin		/* table contained 0213 4657 */
22091671Sume		t2=((t<<16L)|(s&0x0000ffffL))&0xffffffffL;
22191671Sume		*(k++)=ROTATE(t2,30)&0xffffffffL;
22291671Sume
22391671Sume		t2=((s>>16L)|(t&0xffff0000L));
22491671Sume		*(k++)=ROTATE(t2,26)&0xffffffffL;
22555009Sshin	}
22691671Sume}
22755009Sshin
22891671Sumeint des_key_sched(des_cblock *key, des_key_schedule schedule)
22991671Sume{
23055009Sshin	return(des_set_key(key,schedule));
23191671Sume}
23291671Sume
23391671Sumevoid des_fixup_key_parity(des_cblock *key)
23491671Sume{
23591671Sume	des_set_odd_parity(key);
23691671Sume}
237