home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xntp3.zip / lib / authusekey.c < prev    next >
C/C++ Source or Header  |  1992-08-04  |  3KB  |  133 lines

  1. /*
  2.  * authusekey - decode a key from ascii and use it
  3.  */
  4. #include <stdio.h>
  5. #include <sys/types.h>
  6. #include <strings.h>
  7. #include <ctype.h>
  8.  
  9. /*
  10.  * Types of ascii representations for keys.  "Standard" means a 64 bit
  11.  * hex number in NBS format, i.e. with the low order bit of each byte
  12.  * a parity bit.  "NTP" means a 64 bit key in NTP format, with the
  13.  * high order bit of each byte a parity bit.  "Ascii" means a 1-to-8
  14.  * character string whose ascii representation is used as the key.
  15.  */
  16. #ifdef    DES
  17. #define    KEY_TYPE_STD    1
  18. #define    KEY_TYPE_NTP    2
  19. #define    KEY_TYPE_ASCII    3
  20.  
  21. #define    STD_PARITY_BITS    0x01010101
  22.  
  23. extern int DESauth_parity();
  24. extern void DESauth_setkey();
  25. #endif
  26.  
  27. #ifdef    MD5
  28. #define    KEY_TYPE_MD5    4
  29. #endif
  30.  
  31. int
  32. authusekey(keyno, keytype, str)
  33.     u_long keyno;
  34.     int keytype;
  35.     char *str;
  36. {
  37.     u_long key[2];
  38.     u_char keybytes[8];
  39.     char *cp;
  40.     char *xdigit;
  41.     int len;
  42.     int i;
  43.     static char *hex = "0123456789abcdef";
  44.     extern char *index();
  45.  
  46.     cp = str;
  47.     len = strlen(cp);
  48.     if (len == 0)
  49.         return 0;
  50.  
  51.     switch(keytype) {
  52. #ifdef    DES
  53.     case KEY_TYPE_STD:
  54.     case KEY_TYPE_NTP:
  55.         if (len != 16)        /* Lazy.  Should define constant */
  56.             return 0;
  57.         /*
  58.          * Decode hex key.
  59.          */
  60.         key[0] = 0;
  61.         key[1] = 0;
  62.         for (i = 0; i < 16; i++) {
  63.             if (!isascii(*cp))
  64.                 return 0;
  65.             xdigit = index(hex, isupper(*cp) ? tolower(*cp) : *cp);
  66.             cp++;
  67.             if (xdigit == 0)
  68.                 return 0;
  69.             key[i>>3] <<= 4;
  70.             key[i>>3] |= (u_long)(xdigit - hex) & 0xf;
  71.         }
  72.  
  73.         /*
  74.          * If this is an NTP format key, put it into NBS format
  75.          */
  76.         if (keytype == KEY_TYPE_NTP) {
  77.             for (i = 0; i < 2; i++)
  78.                 key[i] = ((key[i] << 1) & ~STD_PARITY_BITS)
  79.                     | ((key[i] >> 7) & STD_PARITY_BITS);
  80.         }
  81.  
  82.         /*
  83.          * Check the parity, reject the key if the check fails
  84.          */
  85.         if (!DESauth_parity(key)) {
  86.             return 0;
  87.         }
  88.         
  89.         /*
  90.          * We can't find a good reason not to use this key.
  91.          * So use it.
  92.          */
  93.         DESauth_setkey(keyno, key);
  94.         break;
  95.     
  96.     case KEY_TYPE_ASCII:
  97.         /*
  98.          * Make up key from ascii representation
  99.          */
  100.         bzero(keybytes, sizeof(keybytes));
  101.         for (i = 0; i < 8 && i < len; i++)
  102.             keybytes[i] = *cp++ << 1;
  103.         key[0] = keybytes[0] << 24 | keybytes[1] << 16
  104.             | keybytes[2] << 8 | keybytes[3];
  105.         key[1] = keybytes[4] << 24 | keybytes[5] << 16
  106.             | keybytes[6] << 8 | keybytes[7];
  107.         
  108.         /*
  109.          * Set parity on key
  110.          */
  111.         (void)DESauth_parity(key);
  112.  
  113.         /*
  114.          * Now set key in.
  115.          */
  116.         DESauth_setkey(keyno, key);
  117.         break;
  118. #endif
  119.  
  120. #ifdef    MD5
  121.     case KEY_TYPE_MD5:
  122.         MD5auth_setkey(keyno, str);
  123.         break;
  124. #endif
  125.  
  126.     default:
  127.         /* Oh, well */
  128.         return 0;
  129.     }
  130.  
  131.     return 1;
  132. }
  133.