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 / authmd512crypt < prev    next >
Text File  |  1992-08-04  |  2KB  |  86 lines

  1. /*
  2.  *  md5crypt - MD5 based authentication routines
  3.  */
  4.  
  5. #include <sys/types.h>
  6. #include "md5.h"
  7.  
  8. extern u_long cache_keyid;
  9. extern char *cache_key;
  10. extern int cache_keylen;
  11.  
  12. /*
  13.  * Stat counters, imported from data base module
  14.  */
  15. extern u_long authencryptions;
  16. extern u_long authdecryptions;
  17. extern u_long authkeyuncached;
  18. extern u_long authdecryptok;
  19. extern u_long authnokey;
  20.  
  21. /*
  22.  * For our purposes an NTP packet looks like:
  23.  *
  24.  *    a variable amount of encrypted data, multiple of 8 bytes, followed by:
  25.  *    NOCRYPT_OCTETS worth of unencrypted data, followed by:
  26.  *    BLOCK_OCTETS worth of ciphered checksum.
  27.  */ 
  28. #define    NOCRYPT_OCTETS    4
  29. #define    BLOCK_OCTETS    16
  30.  
  31. #define    NOCRYPT_LONGS    ((NOCRYPT_OCTETS)/sizeof(u_long))
  32. #define    BLOCK_LONGS    ((BLOCK_OCTETS)/sizeof(u_long))
  33.  
  34. static MD5_CTX ctx;
  35.  
  36. /*
  37.  *  Do first stage of a two stage authenticator generation.
  38.  */
  39.  
  40. MD5auth1crypt(keyno, pkt, length)
  41.     u_long keyno;
  42.     u_long *pkt;
  43.     int length;    /* length of all encrypted data */
  44. {
  45.  
  46.     authencryptions++;
  47.  
  48.     if (keyno != cache_keyid) {
  49.     authkeyuncached++;
  50.     if (!authhavekey(keyno)) {
  51.         authnokey++;
  52.         return 0;
  53.     }
  54.     }
  55.  
  56.     MD5Init(&ctx);
  57.     MD5Update(&ctx, cache_key, cache_keylen);
  58.     MD5Update(&ctx, (char *)pkt, length - 8);
  59.     /* just leave the partially computed value in the static MD5_CTX */
  60. }
  61.  
  62. /*
  63.  *  Do second state of a two stage authenticator generation.
  64.  */
  65. int
  66. MD5auth2crypt(keyno, pkt, length)
  67.     u_long keyno;
  68.     u_long *pkt;
  69.     int length;    /* total length of encrypted area */
  70. {
  71.     char *cp;
  72.  
  73.     /*
  74.      *  Don't bother checking the keys.  The first stage would have
  75.      *  handled that.  Finish up the generation by also including the
  76.      *  last 8 bytes of the data area.
  77.      */
  78.  
  79.     MD5Update(&ctx, (char *)(pkt) + length - 8, 8);
  80.     MD5Final(&ctx);
  81.  
  82.     bcopy(ctx.digest, (char *) &pkt[NOCRYPT_LONGS + length/sizeof(u_long)],
  83.       BLOCK_OCTETS);
  84.     return 4 + BLOCK_OCTETS;
  85. }
  86.