]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ntp/libntp/authparity.c
This commit was generated by cvs2svn to compensate for changes in r56889,
[FreeBSD/FreeBSD.git] / contrib / ntp / libntp / authparity.c
1 /*
2  * auth_parity - set parity on a key/check for odd parity
3  */
4 #include "ntp_stdlib.h"
5
6 int
7 DESauth_parity(
8         u_int32 *key
9         )
10 {
11         u_int32 mask;
12         int parity_err;
13         int bitcount;
14         int half;
15         int byte;
16         int i;
17
18         /*
19          * Go through counting bits in each byte.  Check to see if
20          * each parity bit was set correctly.  If not, note the error
21          * and set it right.
22          */
23         parity_err = 0;
24         for (half = 0; half < 2; half++) {              /* two halves of key */
25                 mask = 0x80000000;
26                 for (byte = 0; byte < 4; byte++) {      /* 4 bytes per half */
27                         bitcount = 0;
28                         for (i = 0; i < 7; i++) {       /* 7 data bits / byte */
29                                 if (key[half] & mask)
30                                     bitcount++;
31                                 mask >>= 1;
32                         }
33
34                         /*
35                          * If bitcount is even, parity must be set.  If
36                          * bitcount is odd, parity must be clear.
37                          */
38                         if ((bitcount & 0x1) == 0) {
39                                 if (!(key[half] & mask)) {
40                                         parity_err++;
41                                         key[half] |= mask;
42                                 }
43                         } else {
44                                 if (key[half] & mask) {
45                                         parity_err++;
46                                         key[half] &= ~mask;
47                                 }
48                         }
49                         mask >>= 1;
50                 }
51         }
52
53         /*
54          * Return the result of the parity check.
55          */
56         return (parity_err == 0);
57 }