home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / appletalk / netatalk / afs / afskrbsrc.sit.hqx / AFS Kerberos 1.0B0 / string_to_key.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-08  |  2.6 KB  |  90 lines

  1. /* ============================================================================
  2.     Portions of this code:
  3.         Copyright ⌐ 1989 by the Massachusetts Institute of Technology
  4.     ============================================================================ */
  5. /*
  6.  * Copyright (c) 1990 Regents of The University of Michigan.
  7.  * All Rights Reserved.
  8.  *
  9.  * Permission to use, copy, modify, and distribute this software
  10.  * and its documentation for any purpose and without fee is hereby
  11.  * granted, provided that the above copyright notice appears in all
  12.  * copies and that both that copyright notice and this permission
  13.  * notice appear in supporting documentation, and that the name of
  14.  * The University of Michigan not be used in advertising or
  15.  * publicity pertaining to distribution of the software without
  16.  * specific, written prior permission. This software is supplied as
  17.  * is without expressed or implied warranties of any kind.
  18.  *
  19.  *    ITD Research Systems
  20.  *    University of Michigan
  21.  *    535 W. William Street
  22.  *    Ann Arbor, Michigan
  23.  *    +1-313-936-2652
  24.  *    netatalk@terminator.cc.umich.edu
  25.  */
  26.  
  27. #include "des.h"
  28. #include "des_internal.h"
  29. #include <Types.h>
  30.  
  31. extern void des_fixup_key_parity( des_cblock key);
  32. char *crypt( char *pw, char *salt );
  33. void Andrew_StringToKey(char *str, char *cell, des_cblock key);
  34.  
  35. /*
  36.  * convert an arbitrary length string to a DES key
  37.  *
  38.  * bug fixed by Mark C Smith 1/91: don't assume length of "str" is zero padded to 8,
  39.  * rather, do padding in this routine!
  40.  */
  41.  
  42. void Andrew_StringToKey(str, cell, key)
  43. char        *str;
  44. char        *cell;                    /* cell/realm for password */
  45. des_cblock    key;
  46. {   
  47.     char  password[8+1];        /* crypt is limited to 8 chars anyway */
  48.     int      i;
  49.     int   passlen;
  50.     
  51.     bzero((char *)key, sizeof(des_cblock));
  52.     bzero( password, sizeof( password ));
  53.     
  54.     /*
  55.      * #### strncpy(password, cell, 8);
  56.      * #### passlen = strlen(str);
  57.      * ####if (passlen > 8)
  58.     */
  59.     passlen = 8;
  60.     strncpy( password, str, passlen );
  61.     
  62.     for (i=0; i<passlen; i++)
  63.         password[i] = password[i] ^ cell[i];
  64.  
  65.     for (i=0;i<8;i++)
  66.         if (password[i] == '\0')
  67.             password[i] = 'X';
  68.  
  69. /*    crypt only considers the first 8 characters of password but for some
  70.     reason returns eleven characters of result (plus the two salt chars). */
  71.  
  72.     strncpy((char *)key, crypt(password, "#~") + 2, sizeof(des_cblock));
  73.  
  74. /* parity is inserted into the LSB so leftshift each byte up one bit.  This
  75.     allows ascii characters with a zero MSB to retain as much significance
  76.     as possible. */
  77.  
  78.     {
  79.          char *keybytes = (char *)key;
  80.         unsigned int temp;
  81.  
  82.         for (i = 0; i < 8; i++) {
  83.             temp = (unsigned int) keybytes[i];
  84.             keybytes[i] = (unsigned char) (temp << 1);
  85.         }
  86.     }
  87.     
  88.     des_fixup_key_parity (key);
  89. }
  90.