1 /* $NetBSD: a_md5encrypt.c,v 1.2 2003/12/04 16:23:36 drochner Exp $ */
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
18 #include "ntp_string.h"
19 #include "ntp_stdlib.h"
21 /* Disable the openssl md5 includes, because they'd clash with ours. */
23 /* #define OPENSSL_NO_MD5 */
31 * MD5authencrypt - generate MD5 message authenticator
33 * Returns length of authenticator field.
37 u_char
*key
, /* key pointer */
38 u_int32
*pkt
, /* packet pointer */
39 int length
/* packet length */
46 * MD5 with key identifier concatenated with packet.
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);
58 * MD5authdecrypt - verify MD5 message authenticator
60 * Returns one if authenticator valid, zero if invalid.
64 u_char
*key
, /* key pointer */
65 u_int32
*pkt
, /* packet pointer */
66 int length
, /* packet length */
67 int size
/* MAC size */
74 * MD5 with key identifier concatenated with packet.
77 MD5Update(&md5
, key
, (u_int
)cache_keylen
);
78 MD5Update(&md5
, (u_char
*)pkt
, (u_int
)length
);
79 MD5Final(digest
, &md5
);
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.
91 addr2refid(struct sockaddr_storage
*addr
)
97 if (addr
->ss_family
== AF_INET
)
98 return (GET_INADDR(*addr
));
101 MD5Update(&md5
, (u_char
*)&GET_INADDR6(*addr
),
102 sizeof(struct in6_addr
));
103 MD5Final(digest
, &md5
);
104 memcpy(&addr_refid
, digest
, 4);