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 / authdecrypt.c < prev    next >
C/C++ Source or Header  |  1992-08-04  |  2KB  |  88 lines

  1. /*
  2.  * authdecrypt - routine to decrypt a packet to see if this guy knows our key.
  3.  */
  4. #include <sys/types.h>
  5.  
  6. /*
  7.  * For our purposes an NTP packet looks like:
  8.  *
  9.  *    a variable amount of unencrypted data, multiple of 8 bytes, followed by:
  10.  *    NOCRYPT_OCTETS worth of unencrypted data, followed by:
  11.  *    BLOCK_OCTETS worth of ciphered checksum.
  12.  */ 
  13. #define    NOCRYPT_OCTETS    4
  14. #define    BLOCK_OCTETS    8
  15.  
  16. #define    NOCRYPT_LONGS    ((NOCRYPT_OCTETS)/sizeof(u_long))
  17. #define    BLOCK_LONGS    ((BLOCK_OCTETS)/sizeof(u_long))
  18.  
  19. /*
  20.  * Imported from the key data base module
  21.  */
  22. extern u_long cache_keyid;    /* cached key ID */
  23. extern u_char DEScache_dkeys[];    /* cached decryption keys */
  24. extern u_char DESzerodkeys[];    /* zero key decryption keys */
  25.  
  26. extern int authhavekey();
  27.  
  28. /*
  29.  * Stat counters, imported from data base module
  30.  */
  31. extern u_long authdecryptions;
  32. extern u_long authkeyuncached;
  33. extern u_long authdecryptok;
  34.  
  35. int
  36. DESauthdecrypt(keyno, pkt, length)
  37.     u_long keyno;
  38.     u_long *pkt;
  39.     int length;    /* length of variable data in octets */
  40. {
  41.     register u_long *pd;
  42.     register int i;
  43.     register u_char *keys;
  44.     register int longlen;
  45.     u_long work[2];
  46.  
  47.     authdecryptions++;
  48.     
  49.     if (keyno == 0)
  50.         keys = DESzerodkeys;
  51.     else {
  52.         if (keyno != cache_keyid) {
  53.             authkeyuncached++;
  54.             if (!authhavekey(keyno))
  55.                 return 0;
  56.         }
  57.         keys = DEScache_dkeys;
  58.     }
  59.  
  60.     /*
  61.      * Get encryption block data in host byte order and decrypt it.
  62.      */
  63.     longlen = length / sizeof(u_long);
  64.     pd = pkt + longlen;        /* points at NOCRYPT area */
  65.     work[0] = *(pd + NOCRYPT_LONGS);
  66.     work[1] = *(pd + NOCRYPT_LONGS + 1);
  67.  
  68.     if (longlen & 0x1) {
  69.         DESauth_des(work, keys);
  70.         work[0] ^= *(--pd);
  71.     }
  72.  
  73.     for (i = longlen/2; i > 0; i--) {
  74.         DESauth_des(work, keys);
  75.         work[1] ^= *(--pd);
  76.         work[0] ^= *(--pd);
  77.     }
  78.  
  79.     /*
  80.      * Success if the encryption data is zero
  81.      */
  82.     if ((work[0] == 0) && (work[1] == 0)) {
  83.         authdecryptok++;
  84.         return 1;
  85.     }
  86.     return 0;
  87. }
  88.