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