import cyrus-sasl-2.1.23
[cyrus-sasl.git] / mac / libdes / src / set_key.c
1 /* crypto/des/set_key.c */\r/* Copyright (C) 1995-1997 Eric Young (eay@mincom.oz.au)\r * All rights reserved.\r *\r * This package is an SSL implementation written\r * by Eric Young (eay@mincom.oz.au).\r * The implementation was written so as to conform with Netscapes SSL.\r * \r * This library is free for commercial and non-commercial use as long as\r * the following conditions are aheared to.  The following conditions\r * apply to all code found in this distribution, be it the RC4, RSA,\r * lhash, DES, etc., code; not just the SSL code.  The SSL documentation\r * included with this distribution is covered by the same copyright terms\r * except that the holder is Tim Hudson (tjh@mincom.oz.au).\r * \r * Copyright remains Eric Young's, and as such any Copyright notices in\r * the code are not to be removed.\r * If this package is used in a product, Eric Young should be given attribution\r * as the author of the parts of the library used.\r * This can be in the form of a textual message at program startup or\r * in documentation (online or textual) provided with the package.\r * \r * Redistribution and use in source and binary forms, with or without\r * modification, are permitted provided that the following conditions\r * are met:\r * 1. Redistributions of source code must retain the copyright\r *    notice, this list of conditions and the following disclaimer.\r * 2. Redistributions in binary form must reproduce the above copyright\r *    notice, this list of conditions and the following disclaimer in the\r *    documentation and/or other materials provided with the distribution.\r * 3. All advertising materials mentioning features or use of this software\r *    must display the following acknowledgement:\r *    "This product includes cryptographic software written by\r *     Eric Young (eay@mincom.oz.au)"\r *    The word 'cryptographic' can be left out if the rouines from the library\r *    being used are not cryptographic related :-).\r * 4. If you include any Windows specific code (or a derivative thereof) from \r *    the apps directory (application code) you must include an acknowledgement:\r *    "This product includes software written by Tim Hudson (tjh@mincom.oz.au)"\r * \r * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND\r * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE\r * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r * SUCH DAMAGE.\r * \r * The licence and distribution terms for any publically available version or\r * derivative of this code cannot be changed.  i.e. this code cannot simply be\r * copied and put under another distribution licence\r * [including the GNU Public Licence.]\r */\r\r/* set_key.c v 1.4 eay 24/9/91\r * 1.4 Speed up by 400% :-)\r * 1.3 added register declarations.\r * 1.2 unrolled make_key_sched a bit more\r * 1.1 added norm_expand_bits\r * 1.0 First working version\r */\r#include "des_locl.h"\r#include "podd.h"\r#include "sk.h"\r\r#ifndef NOPROTO\rstatic int check_parity(des_cblock (*key));\r#else\rstatic int check_parity();\r#endif\r\rint des_check_key=0;\r\rvoid des_set_odd_parity(key)\rdes_cblock (*key);\r    {\r      int i;\r\r        for (i=0; i<DES_KEY_SZ; i++)\r           (*key)[i]=odd_parity[(*key)[i]];\r       }\r\rstatic int check_parity(key)\rdes_cblock (*key);\r     {\r      int i;\r\r        for (i=0; i<DES_KEY_SZ; i++)\r           {\r              if ((*key)[i] != odd_parity[(*key)[i]])\r                        return(0);\r             }\r      return(1);\r     }\r\r/* Weak and semi week keys as take from\r * %A D.W. Davies\r * %A W.L. Price\r * %T Security for Computer Networks\r * %I John Wiley & Sons\r * %D 1984\r * Many thanks to smb@ulysses.att.com (Steven Bellovin) for the reference\r * (and actual cblock values).\r */\r#define NUM_WEAK_KEY 16\rstatic des_cblock weak_keys[NUM_WEAK_KEY]={\r /* weak keys */\r        {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},\r     {0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE},\r     {0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F},\r     {0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0},\r     /* semi-weak keys */\r   {0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE},\r     {0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01},\r     {0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1},\r     {0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E},\r     {0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1},\r     {0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01},\r     {0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE},\r     {0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E},\r     {0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E},\r     {0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01},\r     {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},\r     {0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}};\r\rint des_is_weak_key(key)\rdes_cblock (*key);\r       {\r      int i;\r\r        for (i=0; i<NUM_WEAK_KEY; i++)\r         /* Added == 0 to comparision, I obviously don't run\r             * this section very often :-(, thanks to\r               * engineering@MorningStar.Com for the fix\r              * eay 93/06/29 */\r             if (memcmp(weak_keys[i],key,sizeof(key)) == 0) return(1);\r      return(0);\r     }\r\r/* NOW DEFINED IN des_local.h\r * See ecb_encrypt.c for a pseudo description of these macros. \r * #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\\r *         (b)^=(t),\\r *   (a)=((a)^((t)<<(n))))\r */\r\r#define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\\r        (a)=(a)^(t)^(t>>(16-(n))))\r\r/* return 0 if key parity is odd (correct),\r * return -1 if key parity error,\r * return -2 if illegal weak key.\r */\rint des_set_key(key, schedule)\rdes_cblock (*key);\rdes_key_schedule schedule;\r   {\r      static int shifts2[16]={0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0};\r      register DES_LONG c,d,t,s,t2;\r  register unsigned char *in;\r    register DES_LONG *k;\r  register int i;\r\r       if (des_check_key)\r             {\r              if (!check_parity(key))\r                        return(-1);\r\r           if (des_is_weak_key(key))\r                      return(-2);\r            }\r\r     k=(DES_LONG *)schedule;\r        in=(unsigned char *)key;\r\r      c2l(in,c);\r     c2l(in,d);\r\r    /* do PC1 in 60 simple operations */ \r/*        PERM_OP(d,c,t,4,0x0f0f0f0fL);\r  HPERM_OP(c,t,-2, 0xcccc0000L);\r HPERM_OP(c,t,-1, 0xaaaa0000L);\r HPERM_OP(c,t, 8, 0x00ff0000L);\r HPERM_OP(c,t,-1, 0xaaaa0000L);\r HPERM_OP(d,t,-8, 0xff000000L);\r HPERM_OP(d,t, 8, 0x00ff0000L);\r HPERM_OP(d,t, 2, 0x33330000L);\r d=((d&0x00aa00aaL)<<7L)|((d&0x55005500L)>>7L)|(d&0xaa55aa55L);\r d=(d>>8)|((c&0xf0000000L)>>4);\r c&=0x0fffffffL; */\r\r    /* I now do it in 47 simple operations :-)\r      * Thanks to John Fletcher (john_fletcher@lccmail.ocf.llnl.gov)\r         * for the inspiration. :-) */\r PERM_OP (d,c,t,4,0x0f0f0f0fL);\r HPERM_OP(c,t,-2,0xcccc0000L);\r  HPERM_OP(d,t,-2,0xcccc0000L);\r  PERM_OP (d,c,t,1,0x55555555L);\r PERM_OP (c,d,t,8,0x00ff00ffL);\r PERM_OP (d,c,t,1,0x55555555L);\r d=      (((d&0x000000ffL)<<16L)| (d&0x0000ff00L)     |\r          ((d&0x00ff0000L)>>16L)|((c&0xf0000000L)>>4L));\r        c&=0x0fffffffL;\r\r       for (i=0; i<ITERATIONS; i++)\r           {\r              if (shifts2[i])\r                        { c=((c>>2L)|(c<<26L)); d=((d>>2L)|(d<<26L)); }\r                else\r                   { c=((c>>1L)|(c<<27L)); d=((d>>1L)|(d<<27L)); }\r                c&=0x0fffffffL;\r                d&=0x0fffffffL;\r                /* could be a few less shifts but I am to lazy at this\r          * point in time to investigate */\r             s=      des_skb[0][ (c    )&0x3f                ]|\r                     des_skb[1][((c>> 6)&0x03)|((c>> 7L)&0x3c)]|\r                    des_skb[2][((c>>13)&0x0f)|((c>>14L)&0x30)]|\r                    des_skb[3][((c>>20)&0x01)|((c>>21L)&0x06) |\r                                              ((c>>22L)&0x38)];\r            t=      des_skb[4][ (d    )&0x3f                ]|\r                     des_skb[5][((d>> 7L)&0x03)|((d>> 8L)&0x3c)]|\r                   des_skb[6][ (d>>15L)&0x3f                ]|\r                    des_skb[7][((d>>21L)&0x0f)|((d>>22L)&0x30)];\r\r          /* table contained 0213 4657 */\r                t2=((t<<16L)|(s&0x0000ffffL))&0xffffffffL;\r             *(k++)=ROTATE(t2,30)&0xffffffffL;\r\r             t2=((s>>16L)|(t&0xffff0000L));\r         *(k++)=ROTATE(t2,26)&0xffffffffL;\r              }\r      return(0);\r     }\r\rint des_key_sched(key, schedule)\rdes_cblock (*key);\rdes_key_schedule schedule;\r      {\r      return(des_set_key(key,schedule));\r     }\r