2 * MD5 interface for rsaref2.0
4 * These routines implement an interface for the RSA Laboratories
5 * implementation of the Message Digest 5 (MD5) algorithm. This
6 * algorithm is included in the rsaref2.0 package available from RSA in
7 * the US and foreign countries. Further information is available at
11 #include "ntp_machine.h"
20 #include "ntp_types.h"
22 #include "ntp_string.h"
25 #include "ntp_stdlib.h"
27 #define BLOCK_OCTETS 16 /* message digest size */
28 #define NTP_MAXKEY 65535 /* max identifier from ntp.h */
32 * MD5authencrypt - generate MD5 message authenticator
34 * Returns length of authenticator field.
38 u_char *key, /* key pointer */
39 u_int32 *pkt, /* packet pointer */
40 int length /* packet length */
44 u_char digest[BLOCK_OCTETS];
48 * MD5 with key identifier concatenated with packet.
51 MD5Update(&ctx, key, (u_int)cache_keylen);
52 MD5Update(&ctx, (u_char *)pkt, (u_int)length);
53 MD5Final(digest, &ctx);
55 memmove((char *)&pkt[i + 1], (char *)digest, BLOCK_OCTETS);
56 return (BLOCK_OCTETS + 4);
61 * MD5authdecrypt - verify MD5 message authenticator
63 * Returns one if authenticator valid, zero if invalid.
67 u_char *key, /* key pointer */
68 u_int32 *pkt, /* packet pointer */
69 int length, /* packet length */
70 int size /* MAC size */
74 u_char digest[BLOCK_OCTETS];
77 * MD5 with key identifier concatenated with packet.
79 if (size != BLOCK_OCTETS + 4)
82 MD5Update(&ctx, key, (u_int)cache_keylen);
83 MD5Update(&ctx, (u_char *)pkt, (u_int)length);
84 MD5Final(digest, &ctx);
85 return (!memcmp((char *)digest, (char *)pkt + length + 4,
91 * session_key - generate session key from supplied plaintext.
93 * Returns hashed session key for validation.
97 u_int32 srcadr, /* source address */
98 u_int32 dstadr, /* destination address */
99 u_long keyno, /* key identifier */
100 u_long lifetime /* key lifetime */
106 u_char digest[BLOCK_OCTETS];
109 * Generate the session key and retrieve the hash for later. If
110 * the lifetime is greater than zero, call the key trusted.
112 header[0] = htonl(srcadr);
113 header[1] = htonl(dstadr);
114 header[2] = htonl(keyno);
116 MD5Update(&ctx, (u_char *)header, sizeof(header));
117 MD5Final(digest, &ctx);
118 memcpy(&keyid, digest, 4);
120 MD5auth_setkey(keyno, digest, BLOCK_OCTETS);
121 authtrust(keyno, (int)lifetime);
126 "session_key: from %s to %s keyid %08lx hash %08lx life %ld\n",
127 numtoa(htonl(srcadr)), numtoa(htonl(dstadr)), keyno,