/* $KAME: des_setkey.c,v 1.7 2001/09/10 04:03:58 itojun Exp $ */12/* crypto/des/set_key.c */34/* Copyright (C) 1995-1996 Eric Young ([email protected])5* All rights reserved.6*7* This file is part of an SSL implementation written8* by Eric Young ([email protected]).9* The implementation was written so as to conform with Netscapes SSL10* specification. This library and applications are11* FREE FOR COMMERCIAL AND NON-COMMERCIAL USE12* as long as the following conditions are aheared to.13*14* Copyright remains Eric Young's, and as such any Copyright notices in15* the code are not to be removed. If this code is used in a product,16* Eric Young should be given attribution as the author of the parts used.17* This can be in the form of a textual message at program startup or18* in documentation (online or textual) provided with the package.19*20* Redistribution and use in source and binary forms, with or without21* modification, are permitted provided that the following conditions22* are met:23* 1. Redistributions of source code must retain the copyright24* notice, this list of conditions and the following disclaimer.25* 2. Redistributions in binary form must reproduce the above copyright26* notice, this list of conditions and the following disclaimer in the27* documentation and/or other materials provided with the distribution.28* 3. All advertising materials mentioning features or use of this software29* must display the following acknowledgement:30* This product includes software developed by Eric Young ([email protected])31*32* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND33* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE34* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE35* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE36* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL37* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS38* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)39* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT40* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY41* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF42* SUCH DAMAGE.43*44* The licence and distribution terms for any publically available version or45* derivative of this code cannot be changed. i.e. this code cannot simply be46* copied and put under another distribution licence47* [including the GNU Public Licence.]48*/4950/* set_key.c v 1.4 eay 24/9/9151* 1.4 Speed up by 400% :-)52* 1.3 added register declarations.53* 1.2 unrolled make_key_sched a bit more54* 1.1 added norm_expand_bits55* 1.0 First working version56*/5758#include <sys/param.h>59#include <sys/systm.h>60#include <crypto/des/des_locl.h>61#include <crypto/des/podd.h>62#include <crypto/des/sk.h>6364int des_check_key=0;6566void des_set_odd_parity(unsigned char *key)67{68int i;6970for (i=0; i<DES_KEY_SZ; i++)71key[i]=odd_parity[key[i]];72}7374int des_check_key_parity(const unsigned char *key)75{76int i;7778for (i=0; i<DES_KEY_SZ; i++)79{80if (key[i] != odd_parity[key[i]])81return(0);82}83return(1);84}8586/* Weak and semi week keys as take from87* %A D.W. Davies88* %A W.L. Price89* %T Security for Computer Networks90* %I John Wiley & Sons91* %D 198492* Many thanks to [email protected] (Steven Bellovin) for the reference93* (and actual cblock values).94*/95#define NUM_WEAK_KEY 1696static des_cblock weak_keys[NUM_WEAK_KEY]={97/* weak keys */98{0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},99{0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE},100{0x1F,0x1F,0x1F,0x1F,0x0E,0x0E,0x0E,0x0E},101{0xE0,0xE0,0xE0,0xE0,0xF1,0xF1,0xF1,0xF1},102/* semi-weak keys */103{0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE},104{0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01},105{0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1},106{0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E},107{0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1},108{0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01},109{0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE},110{0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E},111{0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E},112{0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01},113{0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},114{0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}};115116int des_is_weak_key(const unsigned char *key)117{118int i;119120for (i=0; i<NUM_WEAK_KEY; i++)121/* Added == 0 to comparison, I obviously don't run122* this section very often :-(, thanks to123* [email protected] for the fix124* eay 93/06/29125* Another problem, I was comparing only the first 4126* bytes, 97/03/18 */127if (memcmp(weak_keys[i],key,sizeof(des_cblock)) == 0) return(1);128return(0);129}130131/* NOW DEFINED IN des_local.h132* See ecb_encrypt.c for a pseudo description of these macros.133* #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\134* (b)^=(t),\135* (a)=((a)^((t)<<(n))))136*/137138#define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\139(a)=(a)^(t)^(t>>(16-(n))))140141int des_set_key(const unsigned char *key, des_key_schedule schedule)142{143if (des_check_key)144{145return des_set_key_checked(key, schedule);146}147else148{149des_set_key_unchecked(key, schedule);150return 0;151}152}153154/* return 0 if key parity is odd (correct),155* return -1 if key parity error,156* return -2 if illegal weak key.157*/158int des_set_key_checked(const unsigned char *key, des_key_schedule schedule)159{160if (!des_check_key_parity(key))161return(-1);162if (des_is_weak_key(key))163return(-2);164des_set_key_unchecked(key, schedule);165return 0;166}167168void des_set_key_unchecked(const unsigned char *key, des_key_schedule schedule)169{170static int shifts2[16]={0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0};171DES_LONG c,d,t,s,t2;172const unsigned char *in;173DES_LONG *k;174int i;175176k = &schedule->ks.deslong[0];177in = key;178179c2l(in,c);180c2l(in,d);181182/* do PC1 in 47 simple operations :-)183* Thanks to John Fletcher ([email protected])184* for the inspiration. :-) */185PERM_OP (d,c,t,4,0x0f0f0f0fL);186HPERM_OP(c,t,-2,0xcccc0000L);187HPERM_OP(d,t,-2,0xcccc0000L);188PERM_OP (d,c,t,1,0x55555555L);189PERM_OP (c,d,t,8,0x00ff00ffL);190PERM_OP (d,c,t,1,0x55555555L);191d= (((d&0x000000ffL)<<16L)| (d&0x0000ff00L) |192((d&0x00ff0000L)>>16L)|((c&0xf0000000L)>>4L));193c&=0x0fffffffL;194195for (i=0; i<ITERATIONS; i++)196{197if (shifts2[i])198{ c=((c>>2L)|(c<<26L)); d=((d>>2L)|(d<<26L)); }199else200{ c=((c>>1L)|(c<<27L)); d=((d>>1L)|(d<<27L)); }201c&=0x0fffffffL;202d&=0x0fffffffL;203/* could be a few less shifts but I am to lazy at this204* point in time to investigate */205s= des_skb[0][ (c )&0x3f ]|206des_skb[1][((c>> 6L)&0x03)|((c>> 7L)&0x3c)]|207des_skb[2][((c>>13L)&0x0f)|((c>>14L)&0x30)]|208des_skb[3][((c>>20L)&0x01)|((c>>21L)&0x06) |209((c>>22L)&0x38)];210t= des_skb[4][ (d )&0x3f ]|211des_skb[5][((d>> 7L)&0x03)|((d>> 8L)&0x3c)]|212des_skb[6][ (d>>15L)&0x3f ]|213des_skb[7][((d>>21L)&0x0f)|((d>>22L)&0x30)];214215/* table contained 0213 4657 */216t2=((t<<16L)|(s&0x0000ffffL))&0xffffffffL;217*(k++)=ROTATE(t2,30)&0xffffffffL;218219t2=((s>>16L)|(t&0xffff0000L));220*(k++)=ROTATE(t2,26)&0xffffffffL;221}222}223224int des_key_sched(const unsigned char *key, des_key_schedule schedule)225{226return(des_set_key(key,schedule));227}228229void des_fixup_key_parity(unsigned char *key)230{231des_set_odd_parity(key);232}233234235