4 #if defined(HAVE_KERBEROS) && !defined(openbsd)
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
30 #if defined( DEBUG ) && defined( HAVE_DES_DEBUG )
35 extern void des_fixup_key_parity();
37 #ifndef HAVE_AFS_KERBEROS
38 #define WORLDPEACEINOURTIME
41 #if defined(WORLDPEACEINOURTIME) /* Use original, not ifs version */
42 #ifndef HAVE_KERBEROS_V
44 * convert an arbitrary length string to a DES key
47 des_string_to_key( char *str, register des_cblock *key )
49 register char *in_str;
50 register unsigned temp,i;
53 static unsigned char *k_p;
55 register char *p_char;
56 static char k_char[64];
57 static des_key_schedule key_sked;
58 extern unsigned long des_cbc_cksum();
65 /* init key array for bits */
66 memset(k_char, 0, sizeof(k_char));
71 "\n\ninput str length = %d string = %s\nstring = 0x ",
75 /* get next 8 bytes, strip parity, xor */
76 for (i = 1; i <= length; i++) {
77 /* get next input key byte */
78 temp = (unsigned int) *str++;
81 fprintf(stdout,"%02x ",temp & 0xff);
83 /* loop through bits within byte, ignore parity */
84 for (j = 0; j <= 6; j++) {
86 *p_char++ ^= (int) temp & 01;
88 *--p_char ^= (int) temp & 01;
92 /* check and flip direction */
97 /* now stuff into the key des_cblock, and force odd parity */
99 k_p = (unsigned char *) key;
101 for (i = 0; i <= 7; i++) {
103 for (j = 0; j <= 6; j++)
104 temp |= *p_char++ << (1+j);
105 *k_p++ = (unsigned char) temp;
109 des_fixup_key_parity(key);
111 /* Now one-way encrypt it with the folded key */
112 (void) des_key_sched(key,key_sked);
113 (void) des_cbc_cksum((des_cblock *)in_str,key,length,key_sked,key);
115 memset((char *)key_sked, 0, sizeof(key_sked));
117 /* now fix up key parity again */
118 des_fixup_key_parity(key);
123 "\nResulting string_to_key = 0x%lx 0x%lx\n",
124 *((unsigned long *) key),
125 *((unsigned long *) key+1));
129 #endif /* HAVE_KERBEROS_V */
130 #else /* Use ifs version */
134 /* These two needed for rxgen output to work */
135 #include <sys/types.h>
137 #include <afs/cellconfig.h>
138 #include <afs/auth.h>
140 #include "/usr/andy/kauth/kauth.h"
141 #include "/usr/andy/kauth/kautils.h"
144 /* This defines the Andrew string_to_key function. It accepts a password
145 string as input and converts its via a one-way encryption algorithm to a DES
146 encryption key. It is compatible with the original Andrew authentication
147 service password database. */
152 char *cell, /* cell for password */
155 { char password[8+1]; /* crypt is limited to 8 chars anyway */
159 memset(key, 0, sizeof(des_cblock));
160 memset(password, 0, sizeof(password));
162 strncpy (password, cell, 8);
163 passlen = strlen (str);
164 if (passlen > 8) passlen = 8;
166 for (i=0; i<passlen; i++)
167 password[i] = str[i] ^ cell[i];
170 if (password[i] == '\0') password[i] = 'X';
172 /* crypt only considers the first 8 characters of password but for some
173 reason returns eleven characters of result (plus the two salt chars). */
174 strncpy(key, crypt(password, "#~") + 2, sizeof(des_cblock));
176 /* parity is inserted into the LSB so leftshift each byte up one bit. This
177 allows ascii characters with a zero MSB to retain as much significance
179 { char *keybytes = (char *)key;
182 for (i = 0; i < 8; i++) {
183 temp = (unsigned int) keybytes[i];
184 keybytes[i] = (unsigned char) (temp << 1);
187 des_fixup_key_parity (key);
193 char *cell, /* cell for password */
196 { des_key_schedule schedule;
199 char password[BUFSIZ];
202 strncpy (password, str, sizeof(password));
203 if ((passlen = strlen (password)) < sizeof(password)-1)
204 strncat (password, cell, sizeof(password)-passlen);
205 if ((passlen = strlen(password)) > sizeof(password)) passlen = sizeof(password);
207 memcpy(ivec, "kerberos", 8);
208 memcpy(temp_key, "kerberos", 8);
209 des_fixup_key_parity (temp_key);
210 des_key_sched (temp_key, schedule);
211 des_cbc_cksum (password, ivec, passlen, schedule, ivec);
213 memcpy(temp_key, ivec, 8);
214 des_fixup_key_parity (temp_key);
215 des_key_sched (temp_key, schedule);
216 des_cbc_cksum (password, key, passlen, schedule, ivec);
218 des_fixup_key_parity (key);
224 char *cell, /* cell for password */
227 { char realm[REALM_SZ];
232 code = ka_CellToRealm (cell, realm, 0/*local*/);
234 if (code) strcpy (realm, "");
235 else lcstring (realm, realm, sizeof(realm)); /* for backward compatibility */
237 (void)strcpy(realm, cell);
240 if (strlen(str) > 8) StringToKey (str, realm, key);
241 else Andrew_StringToKey (str, realm, key);
245 * convert an arbitrary length string to a DES key
248 des_string_to_key( char *str, 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 */