3 * $Source: /usr/local/src/ldap/clients/ud/RCS/string_to_key.c,v $
6 * Copyright 1985, 1986, 1987, 1988, 1989 by the Massachusetts Institute
9 * For copying and distribution information, please see the file
12 * These routines perform encryption and decryption using the DES
13 * private key algorithm, or else a subset of it-- fewer inner loops.
14 * (AUTH_DES_ITER defaults to 16, may be less.)
16 * Under U.S. law, this software may not be exported outside the US
17 * without license from the U.S. Commerce department.
19 * The key schedule is passed as an arg, as well as the cleartext or
20 * ciphertext. The cleartext and ciphertext should be in host order.
22 * These routines form the library interface to the DES facilities.
24 * spm 8/85 MIT project athena
28 static char rcsid_string_to_key_c[] =
29 "$Id: string_to_key.c,v 1.5 1995/11/09 20:29:55 lsloan Exp $";
32 #include <mit-copyright.h>
35 /* #include "des_internal.h" */
41 extern int des_debug_print();
42 extern void des_fixup_key_parity();
45 #define WORLDPEACEINOURTIME
48 #if defined(WORLDPEACEINOURTIME) /* Use original, not ifs version */
50 * convert an arbitrary length string to a DES key
53 des_string_to_key(str,key)
55 register des_cblock *key;
57 register char *in_str;
58 register unsigned temp,i;
61 static unsigned char *k_p;
63 register char *p_char;
64 static char k_char[64];
65 static des_key_schedule key_sked;
66 extern unsigned long des_cbc_cksum();
73 /* init key array for bits */
74 memset(k_char, 0, sizeof(k_char));
79 "\n\ninput str length = %d string = %s\nstring = 0x ",
83 /* get next 8 bytes, strip parity, xor */
84 for (i = 1; i <= length; i++) {
85 /* get next input key byte */
86 temp = (unsigned int) *str++;
89 fprintf(stdout,"%02x ",temp & 0xff);
91 /* loop through bits within byte, ignore parity */
92 for (j = 0; j <= 6; j++) {
94 *p_char++ ^= (int) temp & 01;
96 *--p_char ^= (int) temp & 01;
100 /* check and flip direction */
105 /* now stuff into the key des_cblock, and force odd parity */
107 k_p = (unsigned char *) key;
109 for (i = 0; i <= 7; i++) {
111 for (j = 0; j <= 6; j++)
112 temp |= *p_char++ << (1+j);
113 *k_p++ = (unsigned char) temp;
117 des_fixup_key_parity(key);
119 /* Now one-way encrypt it with the folded key */
120 (void) des_key_sched(key,key_sked);
121 (void) des_cbc_cksum((des_cblock *)in_str,key,length,key_sked,key);
123 memset((char *)key_sked, 0, sizeof(key_sked));
125 /* now fix up key parity again */
126 des_fixup_key_parity(key);
130 "\nResulting string_to_key = 0x%x 0x%x\n",
131 *((unsigned long *) key),
132 *((unsigned long *) key+1));
135 #else /* Use ifs version */
139 /* These two needed for rxgen output to work */
140 #include <sys/types.h>
142 #include <afs/cellconfig.h>
143 #include <afs/auth.h>
145 #include "/usr/andy/kauth/kauth.h"
146 #include "/usr/andy/kauth/kautils.h"
149 /* This defines the Andrew string_to_key function. It accepts a password
150 string as input and converts its via a one-way encryption algorithm to a DES
151 encryption key. It is compatible with the original Andrew authentication
152 service password database. */
154 static void Andrew_StringToKey (str, cell, key)
156 char *cell; /* cell for password */
158 { char password[8+1]; /* crypt is limited to 8 chars anyway */
162 memset(key, 0, sizeof(des_cblock));
163 memset(password, 0, sizeof(password));
165 strncpy (password, cell, 8);
166 passlen = strlen (str);
167 if (passlen > 8) passlen = 8;
169 for (i=0; i<passlen; i++)
170 password[i] = str[i] ^ cell[i];
173 if (password[i] == '\0') password[i] = 'X';
175 /* crypt only considers the first 8 characters of password but for some
176 reason returns eleven characters of result (plus the two salt chars). */
177 strncpy(key, crypt(password, "#~") + 2, sizeof(des_cblock));
179 /* parity is inserted into the LSB so leftshift each byte up one bit. This
180 allows ascii characters with a zero MSB to retain as much significance
182 { char *keybytes = (char *)key;
185 for (i = 0; i < 8; i++) {
186 temp = (unsigned int) keybytes[i];
187 keybytes[i] = (unsigned char) (temp << 1);
190 des_fixup_key_parity (key);
193 static void StringToKey (str, cell, key)
195 char *cell; /* cell for password */
197 { des_key_schedule schedule;
200 char password[BUFSIZ];
203 strncpy (password, str, sizeof(password));
204 if ((passlen = strlen (password)) < sizeof(password)-1)
205 strncat (password, cell, sizeof(password)-passlen);
206 if ((passlen = strlen(password)) > sizeof(password)) passlen = sizeof(password);
208 memcpy(ivec, "kerberos", 8);
209 memcpy(temp_key, "kerberos", 8);
210 des_fixup_key_parity (temp_key);
211 des_key_sched (temp_key, schedule);
212 des_cbc_cksum (password, ivec, passlen, schedule, ivec);
214 memcpy(temp_key, ivec, 8);
215 des_fixup_key_parity (temp_key);
216 des_key_sched (temp_key, schedule);
217 des_cbc_cksum (password, key, passlen, schedule, ivec);
219 des_fixup_key_parity (key);
223 ka_StringToKey (str, cell, key)
225 char *cell; /* cell for password */
227 { char realm[REALM_SZ];
231 /* code = ka_CellToRealm (cell, realm, 0/*local*/); */
232 if (code) strcpy (realm, "");
233 else lcstring (realm, realm, sizeof(realm)); /* for backward compatibility */
235 (void)strcpy(realm, cell);
238 if (strlen(str) > 8) StringToKey (str, realm, key);
239 else Andrew_StringToKey (str, realm, key);
243 * convert an arbitrary length string to a DES key
246 des_string_to_key(str,key)
248 register des_cblock *key;
250 /* NB: i should probably call routine to get local cell here */
251 ka_StringToKey(str, "umich.edu", key);
255 #endif /* Use IFS Version */
257 #endif /* kerberos */