Sync usage with man page.
[netbsd-mini2440.git] / dist / ntp / libntp / a_md5encrypt.c
blob4aa5ee2c46fa8d2445f6edc9c65befc3deee98bd
1 /* $NetBSD: a_md5encrypt.c,v 1.2 2003/12/04 16:23:36 drochner Exp $ */
3 /*
4 * MD5 interface for rsaref2.0
6 * These routines implement an interface for the RSA Laboratories
7 * implementation of the Message Digest 5 (MD5) algorithm. This
8 * algorithm is included in the rsaref2.0 package available from RSA in
9 * the US and foreign countries. Further information is available at
10 * www.rsa.com.
13 #ifdef HAVE_CONFIG_H
14 #include <config.h>
15 #endif
17 #include "ntp_fp.h"
18 #include "ntp_string.h"
19 #include "ntp_stdlib.h"
21 /* Disable the openssl md5 includes, because they'd clash with ours. */
22 /* #define NO_MD5 */
23 /* #define OPENSSL_NO_MD5 */
24 #undef OPENSSL
26 #include "ntp.h"
27 #include "global.h"
28 #include "ntp_md5.h"
31 * MD5authencrypt - generate MD5 message authenticator
33 * Returns length of authenticator field.
35 int
36 MD5authencrypt(
37 u_char *key, /* key pointer */
38 u_int32 *pkt, /* packet pointer */
39 int length /* packet length */
42 MD5_CTX md5;
43 u_char digest[16];
46 * MD5 with key identifier concatenated with packet.
48 MD5Init(&md5);
49 MD5Update(&md5, key, (u_int)cache_keylen);
50 MD5Update(&md5, (u_char *)pkt, (u_int)length);
51 MD5Final(digest, &md5);
52 memmove((u_char *)pkt + length + 4, digest, 16);
53 return (16 + 4);
58 * MD5authdecrypt - verify MD5 message authenticator
60 * Returns one if authenticator valid, zero if invalid.
62 int
63 MD5authdecrypt(
64 u_char *key, /* key pointer */
65 u_int32 *pkt, /* packet pointer */
66 int length, /* packet length */
67 int size /* MAC size */
70 MD5_CTX md5;
71 u_char digest[16];
74 * MD5 with key identifier concatenated with packet.
76 MD5Init(&md5);
77 MD5Update(&md5, key, (u_int)cache_keylen);
78 MD5Update(&md5, (u_char *)pkt, (u_int)length);
79 MD5Final(digest, &md5);
80 if (size != 16 + 4)
81 return (0);
82 return (!memcmp(digest, (char *)pkt + length + 4, 16));
86 * Calculate the reference id from the address. If it is an IPv4
87 * address, use it as is. If it is an IPv6 address, do a md5 on
88 * it and use the bottom 4 bytes.
90 u_int32
91 addr2refid(struct sockaddr_storage *addr)
93 MD5_CTX md5;
94 u_char digest[16];
95 u_int32 addr_refid;
97 if (addr->ss_family == AF_INET)
98 return (GET_INADDR(*addr));
100 MD5Init(&md5);
101 MD5Update(&md5, (u_char *)&GET_INADDR6(*addr),
102 sizeof(struct in6_addr));
103 MD5Final(digest, &md5);
104 memcpy(&addr_refid, digest, 4);
105 return (addr_refid);