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 / authmd5encrypt < prev    next >
Text File  |  1992-08-04  |  1KB  |  67 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.  
  35. int
  36. MD5authencrypt(keyno, pkt, length)
  37.     u_long keyno;
  38.     u_long *pkt;
  39.     int length;        /* length of encrypted portion of packet */
  40. {
  41.     MD5_CTX ctx;
  42.     int len;        /* in 4 byte quantities */
  43.  
  44.     authencryptions++;
  45.  
  46.     if (keyno != cache_keyid) {
  47.     authkeyuncached++;
  48.     if (!authhavekey(keyno)) {
  49.         authnokey++;
  50.         return 0;
  51.     }
  52.     }
  53.  
  54.     len = length / sizeof(u_long);
  55.  
  56.     /*
  57.      *  Generate the authenticator.
  58.      */
  59.     MD5Init(&ctx);
  60.     MD5Update(&ctx, cache_key, cache_keylen);
  61.     MD5Update(&ctx, (char *)pkt, length);
  62.     MD5Final(&ctx);
  63.  
  64.     bcopy(ctx.digest, (char *) &pkt[NOCRYPT_LONGS + len], BLOCK_OCTETS);
  65.     return 4 + BLOCK_OCTETS;    /* return size of key and MAC  */
  66. }
  67.