set_key.c revision 55714
155714Skris/* crypto/des/set_key.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
855714Skris *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1555714Skris *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
2255714Skris *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4055714Skris *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
5255714Skris *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris/* set_key.c v 1.4 eay 24/9/91
6055714Skris * 1.4 Speed up by 400% :-)
6155714Skris * 1.3 added register declarations.
6255714Skris * 1.2 unrolled make_key_sched a bit more
6355714Skris * 1.1 added norm_expand_bits
6455714Skris * 1.0 First working version
6555714Skris */
6655714Skris#include "des_locl.h"
6755714Skris#include "podd.h"
6855714Skris#include "sk.h"
6955714Skris
7055714Skrisstatic int check_parity(const_des_cblock *key);
7155714SkrisOPENSSL_GLOBAL int des_check_key=0;
7255714Skris
7355714Skrisvoid des_set_odd_parity(des_cblock *key)
7455714Skris	{
7555714Skris	int i;
7655714Skris
7755714Skris	for (i=0; i<DES_KEY_SZ; i++)
7855714Skris		(*key)[i]=odd_parity[(*key)[i]];
7955714Skris	}
8055714Skris
8155714Skrisstatic int check_parity(const_des_cblock *key)
8255714Skris	{
8355714Skris	int i;
8455714Skris
8555714Skris	for (i=0; i<DES_KEY_SZ; i++)
8655714Skris		{
8755714Skris		if ((*key)[i] != odd_parity[(*key)[i]])
8855714Skris			return(0);
8955714Skris		}
9055714Skris	return(1);
9155714Skris	}
9255714Skris
9355714Skris/* Weak and semi week keys as take from
9455714Skris * %A D.W. Davies
9555714Skris * %A W.L. Price
9655714Skris * %T Security for Computer Networks
9755714Skris * %I John Wiley & Sons
9855714Skris * %D 1984
9955714Skris * Many thanks to smb@ulysses.att.com (Steven Bellovin) for the reference
10055714Skris * (and actual cblock values).
10155714Skris */
10255714Skris#define NUM_WEAK_KEY	16
10355714Skrisstatic des_cblock weak_keys[NUM_WEAK_KEY]={
10455714Skris	/* weak keys */
10555714Skris	{0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
10655714Skris	{0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE},
10755714Skris	{0x1F,0x1F,0x1F,0x1F,0x0E,0x0E,0x0E,0x0E},
10855714Skris	{0xE0,0xE0,0xE0,0xE0,0xF1,0xF1,0xF1,0xF1},
10955714Skris	/* semi-weak keys */
11055714Skris	{0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE},
11155714Skris	{0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01},
11255714Skris	{0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1},
11355714Skris	{0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E},
11455714Skris	{0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1},
11555714Skris	{0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01},
11655714Skris	{0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE},
11755714Skris	{0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E},
11855714Skris	{0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E},
11955714Skris	{0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01},
12055714Skris	{0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
12155714Skris	{0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}};
12255714Skris
12355714Skrisint des_is_weak_key(const_des_cblock *key)
12455714Skris	{
12555714Skris	int i;
12655714Skris
12755714Skris	for (i=0; i<NUM_WEAK_KEY; i++)
12855714Skris		/* Added == 0 to comparision, I obviously don't run
12955714Skris		 * this section very often :-(, thanks to
13055714Skris		 * engineering@MorningStar.Com for the fix
13155714Skris		 * eay 93/06/29
13255714Skris		 * Another problem, I was comparing only the first 4
13355714Skris		 * bytes, 97/03/18 */
13455714Skris		if (memcmp(weak_keys[i],key,sizeof(des_cblock)) == 0) return(1);
13555714Skris	return(0);
13655714Skris	}
13755714Skris
13855714Skris/* NOW DEFINED IN des_local.h
13955714Skris * See ecb_encrypt.c for a pseudo description of these macros.
14055714Skris * #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
14155714Skris * 	(b)^=(t),\
14255714Skris * 	(a)=((a)^((t)<<(n))))
14355714Skris */
14455714Skris
14555714Skris#define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\
14655714Skris	(a)=(a)^(t)^(t>>(16-(n))))
14755714Skris
14855714Skris/* return 0 if key parity is odd (correct),
14955714Skris * return -1 if key parity error,
15055714Skris * return -2 if illegal weak key.
15155714Skris */
15255714Skrisint des_set_key(const_des_cblock *key, des_key_schedule schedule)
15355714Skris	{
15455714Skris	static int shifts2[16]={0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0};
15555714Skris	register DES_LONG c,d,t,s,t2;
15655714Skris	register const unsigned char *in;
15755714Skris	register DES_LONG *k;
15855714Skris	register int i;
15955714Skris
16055714Skris	if (des_check_key)
16155714Skris		{
16255714Skris		if (!check_parity(key))
16355714Skris			return(-1);
16455714Skris
16555714Skris		if (des_is_weak_key(key))
16655714Skris			return(-2);
16755714Skris		}
16855714Skris
16955714Skris	k = &schedule->ks.deslong[0];
17055714Skris	in = &(*key)[0];
17155714Skris
17255714Skris	c2l(in,c);
17355714Skris	c2l(in,d);
17455714Skris
17555714Skris	/* do PC1 in 60 simple operations */
17655714Skris/*	PERM_OP(d,c,t,4,0x0f0f0f0fL);
17755714Skris	HPERM_OP(c,t,-2, 0xcccc0000L);
17855714Skris	HPERM_OP(c,t,-1, 0xaaaa0000L);
17955714Skris	HPERM_OP(c,t, 8, 0x00ff0000L);
18055714Skris	HPERM_OP(c,t,-1, 0xaaaa0000L);
18155714Skris	HPERM_OP(d,t,-8, 0xff000000L);
18255714Skris	HPERM_OP(d,t, 8, 0x00ff0000L);
18355714Skris	HPERM_OP(d,t, 2, 0x33330000L);
18455714Skris	d=((d&0x00aa00aaL)<<7L)|((d&0x55005500L)>>7L)|(d&0xaa55aa55L);
18555714Skris	d=(d>>8)|((c&0xf0000000L)>>4);
18655714Skris	c&=0x0fffffffL; */
18755714Skris
18855714Skris	/* I now do it in 47 simple operations :-)
18955714Skris	 * Thanks to John Fletcher (john_fletcher@lccmail.ocf.llnl.gov)
19055714Skris	 * for the inspiration. :-) */
19155714Skris	PERM_OP (d,c,t,4,0x0f0f0f0fL);
19255714Skris	HPERM_OP(c,t,-2,0xcccc0000L);
19355714Skris	HPERM_OP(d,t,-2,0xcccc0000L);
19455714Skris	PERM_OP (d,c,t,1,0x55555555L);
19555714Skris	PERM_OP (c,d,t,8,0x00ff00ffL);
19655714Skris	PERM_OP (d,c,t,1,0x55555555L);
19755714Skris	d=	(((d&0x000000ffL)<<16L)| (d&0x0000ff00L)     |
19855714Skris		 ((d&0x00ff0000L)>>16L)|((c&0xf0000000L)>>4L));
19955714Skris	c&=0x0fffffffL;
20055714Skris
20155714Skris	for (i=0; i<ITERATIONS; i++)
20255714Skris		{
20355714Skris		if (shifts2[i])
20455714Skris			{ c=((c>>2L)|(c<<26L)); d=((d>>2L)|(d<<26L)); }
20555714Skris		else
20655714Skris			{ c=((c>>1L)|(c<<27L)); d=((d>>1L)|(d<<27L)); }
20755714Skris		c&=0x0fffffffL;
20855714Skris		d&=0x0fffffffL;
20955714Skris		/* could be a few less shifts but I am to lazy at this
21055714Skris		 * point in time to investigate */
21155714Skris		s=	des_skb[0][ (c    )&0x3f                ]|
21255714Skris			des_skb[1][((c>> 6)&0x03)|((c>> 7L)&0x3c)]|
21355714Skris			des_skb[2][((c>>13)&0x0f)|((c>>14L)&0x30)]|
21455714Skris			des_skb[3][((c>>20)&0x01)|((c>>21L)&0x06) |
21555714Skris						  ((c>>22L)&0x38)];
21655714Skris		t=	des_skb[4][ (d    )&0x3f                ]|
21755714Skris			des_skb[5][((d>> 7L)&0x03)|((d>> 8L)&0x3c)]|
21855714Skris			des_skb[6][ (d>>15L)&0x3f                ]|
21955714Skris			des_skb[7][((d>>21L)&0x0f)|((d>>22L)&0x30)];
22055714Skris
22155714Skris		/* table contained 0213 4657 */
22255714Skris		t2=((t<<16L)|(s&0x0000ffffL))&0xffffffffL;
22355714Skris		*(k++)=ROTATE(t2,30)&0xffffffffL;
22455714Skris
22555714Skris		t2=((s>>16L)|(t&0xffff0000L));
22655714Skris		*(k++)=ROTATE(t2,26)&0xffffffffL;
22755714Skris		}
22855714Skris	return(0);
22955714Skris	}
23055714Skris
23155714Skrisint des_key_sched(const_des_cblock *key, des_key_schedule schedule)
23255714Skris	{
23355714Skris	return(des_set_key(key,schedule));
23455714Skris	}
235