~ubuntu-branches/debian/squeeze/ntp/squeeze-201010051545

« back to all changes in this revision

Viewing changes to libntp/a_md5encrypt.c

  • Committer: Bazaar Package Importer
  • Author(s): Matt Zimmerman
  • Date: 2004-10-11 16:10:27 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20041011161027-icyjbji8ujym633o
Tags: 1:4.2.0a-10ubuntu2
Use ntp.ubuntulinux.org instead of pool.ntp.org

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
 * www.rsa.com.
9
9
 */
10
10
 
11
 
#include "ntp_machine.h"
12
 
 
13
11
#ifdef HAVE_CONFIG_H
14
12
#include <config.h>
15
13
#endif
16
14
 
17
 
#include <stdio.h>
18
 
 
19
 
#include "ntp_types.h"
 
15
#include "ntp_fp.h"
20
16
#include "ntp_string.h"
 
17
#include "ntp_stdlib.h"
 
18
 
 
19
/* Disable the openssl md5 includes, because they'd clash with ours. */
 
20
/* #define NO_MD5 */
 
21
/* #define OPENSSL_NO_MD5 */
 
22
#undef OPENSSL
 
23
 
 
24
#include "ntp.h"
21
25
#include "global.h"
22
 
#include "md5.h"
23
 
#include "ntp_stdlib.h"
24
 
 
25
 
#define BLOCK_OCTETS    16      /* message digest size */
26
 
 
 
26
#include "ntp_md5.h"
27
27
 
28
28
/*
29
29
 * MD5authencrypt - generate MD5 message authenticator
37
37
        int length              /* packet length */
38
38
        )
39
39
{
40
 
        MD5_CTX ctx;
41
 
        u_char digest[BLOCK_OCTETS];
42
 
        int i;
 
40
        MD5_CTX md5;
 
41
        u_char digest[16];
43
42
 
44
43
        /*
45
44
         * MD5 with key identifier concatenated with packet.
46
45
         */
47
 
        MD5Init(&ctx);
48
 
        MD5Update(&ctx, key, (u_int)cache_keylen);
49
 
        MD5Update(&ctx, (u_char *)pkt, (u_int)length);
50
 
        MD5Final(digest, &ctx);
51
 
        i = length / 4;
52
 
        memmove((char *)&pkt[i + 1], (char *)digest, BLOCK_OCTETS);
53
 
        return (BLOCK_OCTETS + 4);
 
46
        MD5Init(&md5);
 
47
        MD5Update(&md5, key, (u_int)cache_keylen);
 
48
        MD5Update(&md5, (u_char *)pkt, (u_int)length);
 
49
        MD5Final(digest, &md5);
 
50
        memmove((u_char *)pkt + length + 4, digest, 16);
 
51
        return (16 + 4);
54
52
}
55
53
 
56
54
 
63
61
MD5authdecrypt(
64
62
        u_char *key,            /* key pointer */
65
63
        u_int32 *pkt,           /* packet pointer */
66
 
        int length,     /* packet length */
 
64
        int length,             /* packet length */
67
65
        int size                /* MAC size */
68
66
        )
69
67
{
70
 
        MD5_CTX ctx;
71
 
        u_char digest[BLOCK_OCTETS];
 
68
        MD5_CTX md5;
 
69
        u_char digest[16];
72
70
 
73
71
        /*
74
72
         * MD5 with key identifier concatenated with packet.
75
73
         */
76
 
        if (size != BLOCK_OCTETS + 4)
 
74
        MD5Init(&md5);
 
75
        MD5Update(&md5, key, (u_int)cache_keylen);
 
76
        MD5Update(&md5, (u_char *)pkt, (u_int)length);
 
77
        MD5Final(digest, &md5);
 
78
        if (size != 16 + 4)
77
79
                return (0);
78
 
        MD5Init(&ctx);
79
 
        MD5Update(&ctx, key, (u_int)cache_keylen);
80
 
        MD5Update(&ctx, (u_char *)pkt, (u_int)length);
81
 
        MD5Final(digest, &ctx);
82
 
        return (!memcmp((char *)digest, (char *)pkt + length + 4,
83
 
                BLOCK_OCTETS));
 
80
        return (!memcmp(digest, (char *)pkt + length + 4, 16));
 
81
}
 
82
 
 
83
/*
 
84
 * Calculate the reference id from the address. If it is an IPv4
 
85
 * address, use it as is. If it is an IPv6 address, do a md5 on
 
86
 * it and use the bottom 4 bytes.
 
87
 */
 
88
u_int32
 
89
addr2refid(struct sockaddr_storage *addr)
 
90
{
 
91
        MD5_CTX md5;
 
92
        u_char digest[16];
 
93
        u_int32 addr_refid;
 
94
 
 
95
        if (addr->ss_family == AF_INET)
 
96
                return (GET_INADDR(*addr));
 
97
 
 
98
        MD5Init(&md5);
 
99
        MD5Update(&md5, (u_char *)&GET_INADDR6(*addr),
 
100
            sizeof(struct in6_addr));
 
101
        MD5Final(digest, &md5);
 
102
        memcpy(&addr_refid, digest, 4);
 
103
        return (htonl(addr_refid));
84
104
}