home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / crypl200.zip / LIBDES / SET_KEY.C < prev    next >
Text File  |  1996-09-22  |  8KB  |  241 lines

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