3 #if defined(HAVE_KERBEROS) && !defined(openbsd)
5 * $Source: /repo/OpenLDAP/pkg/ldap/clients/ud/string_to_key.c,v $
8 * Copyright 1985, 1986, 1987, 1988, 1989 by the Massachusetts Institute
11 * For copying and distribution information, please see the file
14 * These routines perform encryption and decryption using the DES
15 * private key algorithm, or else a subset of it-- fewer inner loops.
16 * (AUTH_DES_ITER defaults to 16, may be less.)
18 * Under U.S. law, this software may not be exported outside the US
19 * without license from the U.S. Commerce department.
21 * The key schedule is passed as an arg, as well as the cleartext or
22 * ciphertext. The cleartext and ciphertext should be in host order.
24 * These routines form the library interface to the DES facilities.
26 * spm 8/85 MIT project athena
33 extern int des_debug_print();
34 extern void des_fixup_key_parity();
36 #ifndef HAVE_AFS_KERBEROS
37 #define WORLDPEACEINOURTIME
40 #if defined(WORLDPEACEINOURTIME) /* Use original, not ifs version */
41 #ifndef HAVE_KERBEROS_V
43 * convert an arbitrary length string to a DES key
46 des_string_to_key(str,key)
48 register des_cblock *key;
50 register char *in_str;
51 register unsigned temp,i;
54 static unsigned char *k_p;
56 register char *p_char;
57 static char k_char[64];
58 static des_key_schedule key_sked;
59 extern unsigned long des_cbc_cksum();
66 /* init key array for bits */
67 memset(k_char, 0, sizeof(k_char));
72 "\n\ninput str length = %d string = %s\nstring = 0x ",
76 /* get next 8 bytes, strip parity, xor */
77 for (i = 1; i <= length; i++) {
78 /* get next input key byte */
79 temp = (unsigned int) *str++;
82 fprintf(stdout,"%02x ",temp & 0xff);
84 /* loop through bits within byte, ignore parity */
85 for (j = 0; j <= 6; j++) {
87 *p_char++ ^= (int) temp & 01;
89 *--p_char ^= (int) temp & 01;
93 /* check and flip direction */
98 /* now stuff into the key des_cblock, and force odd parity */
100 k_p = (unsigned char *) key;
102 for (i = 0; i <= 7; i++) {
104 for (j = 0; j <= 6; j++)
105 temp |= *p_char++ << (1+j);
106 *k_p++ = (unsigned char) temp;
110 des_fixup_key_parity(key);
112 /* Now one-way encrypt it with the folded key */
113 (void) des_key_sched(key,key_sked);
114 (void) des_cbc_cksum((des_cblock *)in_str,key,length,key_sked,key);
116 memset((char *)key_sked, 0, sizeof(key_sked));
118 /* now fix up key parity again */
119 des_fixup_key_parity(key);
123 "\nResulting string_to_key = 0x%x 0x%x\n",
124 *((unsigned long *) key),
125 *((unsigned long *) key+1));
128 #endif /* HAVE_KERBEROS_V */
129 #else /* Use ifs version */
133 /* These two needed for rxgen output to work */
134 #include <sys/types.h>
136 #include <afs/cellconfig.h>
137 #include <afs/auth.h>
139 #include "/usr/andy/kauth/kauth.h"
140 #include "/usr/andy/kauth/kautils.h"
143 /* This defines the Andrew string_to_key function. It accepts a password
144 string as input and converts its via a one-way encryption algorithm to a DES
145 encryption key. It is compatible with the original Andrew authentication
146 service password database. */
148 static void Andrew_StringToKey (str, cell, key)
150 char *cell; /* cell for password */
152 { char password[8+1]; /* crypt is limited to 8 chars anyway */
156 memset(key, 0, sizeof(des_cblock));
157 memset(password, 0, sizeof(password));
159 strncpy (password, cell, 8);
160 passlen = strlen (str);
161 if (passlen > 8) passlen = 8;
163 for (i=0; i<passlen; i++)
164 password[i] = str[i] ^ cell[i];
167 if (password[i] == '\0') password[i] = 'X';
169 /* crypt only considers the first 8 characters of password but for some
170 reason returns eleven characters of result (plus the two salt chars). */
171 strncpy(key, crypt(password, "#~") + 2, sizeof(des_cblock));
173 /* parity is inserted into the LSB so leftshift each byte up one bit. This
174 allows ascii characters with a zero MSB to retain as much significance
176 { char *keybytes = (char *)key;
179 for (i = 0; i < 8; i++) {
180 temp = (unsigned int) keybytes[i];
181 keybytes[i] = (unsigned char) (temp << 1);
184 des_fixup_key_parity (key);
187 static void StringToKey (str, cell, key)
189 char *cell; /* cell for password */
191 { des_key_schedule schedule;
194 char password[BUFSIZ];
197 strncpy (password, str, sizeof(password));
198 if ((passlen = strlen (password)) < sizeof(password)-1)
199 strncat (password, cell, sizeof(password)-passlen);
200 if ((passlen = strlen(password)) > sizeof(password)) passlen = sizeof(password);
202 memcpy(ivec, "kerberos", 8);
203 memcpy(temp_key, "kerberos", 8);
204 des_fixup_key_parity (temp_key);
205 des_key_sched (temp_key, schedule);
206 des_cbc_cksum (password, ivec, passlen, schedule, ivec);
208 memcpy(temp_key, ivec, 8);
209 des_fixup_key_parity (temp_key);
210 des_key_sched (temp_key, schedule);
211 des_cbc_cksum (password, key, passlen, schedule, ivec);
213 des_fixup_key_parity (key);
217 ka_StringToKey (str, cell, key)
219 char *cell; /* cell for password */
221 { char realm[REALM_SZ];
225 /* code = ka_CellToRealm (cell, realm, 0/*local*/); */
226 if (code) strcpy (realm, "");
227 else lcstring (realm, realm, sizeof(realm)); /* for backward compatibility */
229 (void)strcpy(realm, cell);
232 if (strlen(str) > 8) StringToKey (str, realm, key);
233 else Andrew_StringToKey (str, realm, key);
237 * convert an arbitrary length string to a DES key
240 des_string_to_key(str,key)
242 register des_cblock *key;
244 /* NB: i should probably call routine to get local cell here */
245 ka_StringToKey(str, "umich.edu", key);
249 #endif /* Use IFS Version */
251 #endif /* kerberos */