Don't cast the pointer past to memset
[openafs.git] / src / des / strng_to_key.c
1 /*
2  * Copyright 1985, 1986, 1987, 1988, 1989 by the Massachusetts Institute
3  * of Technology.
4  *
5  * For copying and distribution information, please see the file
6  * <mit-cpyright.h>.
7  *
8  * These routines perform encryption and decryption using the DES
9  * private key algorithm, or else a subset of it-- fewer inner loops.
10  * (AUTH_DES_ITER defaults to 16, may be less.)
11  *
12  * Under U.S. law, this software may not be exported outside the US
13  * without license from the U.S. Commerce department.
14  *
15  * The key schedule is passed as an arg, as well as the cleartext or
16  * ciphertext.  The cleartext and ciphertext should be in host order.
17  *
18  * These routines form the library interface to the DES facilities.
19  *
20  *      spm     8/85    MIT project athena
21  */
22
23 #include <mit-cpyright.h>
24 #include <afsconfig.h>
25 #include <afs/param.h>
26
27
28 #ifndef KERNEL
29 #include <stdio.h>
30 #endif
31
32 #include <des.h>
33 #include "des_internal.h"
34 #include "des_prototypes.h"
35
36 #ifdef HAVE_STRING_H
37 #include <string.h>
38 #else
39 #ifdef HAVE_STRINGS_H
40 #include <strings.h>
41 #endif
42 #endif
43
44 /*
45  * convert an arbitrary length string to a DES key
46  */
47 void
48 des_string_to_key(char *str, register des_cblock * key)
49 {
50     register char *in_str;
51     register unsigned temp, i, j;
52     register afs_int32 length;
53     unsigned char *k_p;
54     int forward;
55     register char *p_char;
56     char k_char[64];
57     des_key_schedule key_sked;
58
59     in_str = str;
60     forward = 1;
61     p_char = k_char;
62     length = strlen(str);
63
64     /* init key array for bits */
65     memset(k_char, 0, sizeof(k_char));
66
67 #ifdef DEBUG
68     if (des_debug)
69         fprintf(stdout,
70                 "\n\ninput str length = %d  string = %s\nstring = 0x ",
71                 length, str);
72 #endif
73
74     /* get next 8 bytes, strip parity, xor */
75     for (i = 1; i <= length; i++) {
76         /* get next input key byte */
77         temp = (unsigned int)*str++;
78 #ifdef DEBUG
79         if (des_debug)
80             fprintf(stdout, "%02x ", temp & 0xff);
81 #endif
82         /* loop through bits within byte, ignore parity */
83         for (j = 0; j <= 6; j++) {
84             if (forward)
85                 *p_char++ ^= (int)temp & 01;
86             else
87                 *--p_char ^= (int)temp & 01;
88             temp = temp >> 1;
89         } while (--j > 0);
90
91         /* check and flip direction */
92         if ((i % 8) == 0)
93             forward = !forward;
94     }
95
96     /* now stuff into the key des_cblock, and force odd parity */
97     p_char = k_char;
98     k_p = (unsigned char *)key;
99
100     for (i = 0; i <= 7; i++) {
101         temp = 0;
102         for (j = 0; j <= 6; j++)
103             temp |= *p_char++ << (1 + j);
104         *k_p++ = (unsigned char)temp;
105     }
106
107     /* fix key parity */
108     des_fixup_key_parity(cblockptr_to_cblock(key));
109
110     /* Now one-way encrypt it with the folded key */
111     des_key_sched(cblockptr_to_cblock(key), key_sked);
112     des_cbc_cksum(charptr_to_cblockptr(in_str), key, length, key_sked, key);
113     /* erase key_sked */
114     memset(key_sked, 0, sizeof(key_sked));
115
116     /* now fix up key parity again */
117     des_fixup_key_parity(cblockptr_to_cblock(key));
118
119     if (des_debug)
120         fprintf(stdout, "\nResulting string_to_key = 0x%x 0x%x\n",
121                 *((afs_uint32 *) key), *((afs_uint32 *) key + 1));
122 }