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
32 #if defined( DEBUG ) && defined( HAVE_DES_DEBUG )
37 extern void des_fixup_key_parity();
39 #ifndef HAVE_AFS_KERBEROS
40 #define WORLDPEACEINOURTIME
43 #if defined(WORLDPEACEINOURTIME) /* Use original, not ifs version */
44 #ifndef HAVE_KERBEROS_V
46 * convert an arbitrary length string to a DES key
49 des_string_to_key( char *str, register des_cblock *key )
51 register char *in_str;
52 register unsigned temp,i;
55 static unsigned char *k_p;
57 register char *p_char;
58 static char k_char[64];
59 static des_key_schedule key_sked;
60 extern unsigned long des_cbc_cksum();
67 /* init key array for bits */
68 memset(k_char, 0, sizeof(k_char));
73 "\n\ninput str length = %d string = %s\nstring = 0x ",
77 /* get next 8 bytes, strip parity, xor */
78 for (i = 1; i <= length; i++) {
79 /* get next input key byte */
80 temp = (unsigned int) *str++;
83 fprintf(stdout,"%02x ",temp & 0xff);
85 /* loop through bits within byte, ignore parity */
86 for (j = 0; j <= 6; j++) {
88 *p_char++ ^= (int) temp & 01;
90 *--p_char ^= (int) temp & 01;
94 /* check and flip direction */
99 /* now stuff into the key des_cblock, and force odd parity */
101 k_p = (unsigned char *) key;
103 for (i = 0; i <= 7; i++) {
105 for (j = 0; j <= 6; j++)
106 temp |= *p_char++ << (1+j);
107 *k_p++ = (unsigned char) temp;
111 des_fixup_key_parity(key);
113 /* Now one-way encrypt it with the folded key */
114 (void) des_key_sched(key,key_sked);
115 (void) des_cbc_cksum((des_cblock *)in_str,key,length,key_sked,key);
117 memset((char *)key_sked, 0, sizeof(key_sked));
119 /* now fix up key parity again */
120 des_fixup_key_parity(key);
125 "\nResulting string_to_key = 0x%lx 0x%lx\n",
126 *((unsigned long *) key),
127 *((unsigned long *) key+1));
131 #endif /* HAVE_KERBEROS_V */
132 #else /* Use ifs version */
136 /* These two needed for rxgen output to work */
137 #include <sys/types.h>
139 #include <afs/cellconfig.h>
140 #include <afs/auth.h>
142 #include "/usr/andy/kauth/kauth.h"
143 #include "/usr/andy/kauth/kautils.h"
146 /* This defines the Andrew string_to_key function. It accepts a password
147 string as input and converts its via a one-way encryption algorithm to a DES
148 encryption key. It is compatible with the original Andrew authentication
149 service password database. */
154 char *cell, /* cell for password */
157 { char password[8+1]; /* crypt is limited to 8 chars anyway */
161 memset(key, 0, sizeof(des_cblock));
162 memset(password, 0, sizeof(password));
164 strncpy (password, cell, 8);
165 passlen = strlen (str);
166 if (passlen > 8) passlen = 8;
168 for (i=0; i<passlen; i++)
169 password[i] = str[i] ^ cell[i];
172 if (password[i] == '\0') password[i] = 'X';
174 /* crypt only considers the first 8 characters of password but for some
175 reason returns eleven characters of result (plus the two salt chars). */
176 strncpy(key, crypt(password, "#~") + 2, sizeof(des_cblock));
178 /* parity is inserted into the LSB so leftshift each byte up one bit. This
179 allows ascii characters with a zero MSB to retain as much significance
181 { char *keybytes = (char *)key;
184 for (i = 0; i < 8; i++) {
185 temp = (unsigned int) keybytes[i];
186 keybytes[i] = (unsigned char) (temp << 1);
189 des_fixup_key_parity (key);
195 char *cell, /* cell for password */
198 { des_key_schedule schedule;
201 char password[BUFSIZ];
204 strncpy (password, str, sizeof(password));
205 if ((passlen = strlen (password)) < sizeof(password)-1)
206 strncat (password, cell, sizeof(password)-passlen);
207 if ((passlen = strlen(password)) > sizeof(password)) passlen = sizeof(password);
209 memcpy(ivec, "kerberos", 8);
210 memcpy(temp_key, "kerberos", 8);
211 des_fixup_key_parity (temp_key);
212 des_key_sched (temp_key, schedule);
213 des_cbc_cksum (password, ivec, passlen, schedule, ivec);
215 memcpy(temp_key, ivec, 8);
216 des_fixup_key_parity (temp_key);
217 des_key_sched (temp_key, schedule);
218 des_cbc_cksum (password, key, passlen, schedule, ivec);
220 des_fixup_key_parity (key);
226 char *cell, /* cell for password */
229 { char realm[REALM_SZ];
234 code = ka_CellToRealm (cell, realm, 0/*local*/);
236 if (code) strcpy (realm, "");
237 else lcstring (realm, realm, sizeof(realm)); /* for backward compatibility */
239 (void)strcpy(realm, cell);
242 if (strlen(str) > 8) StringToKey (str, realm, key);
243 else Andrew_StringToKey (str, realm, key);
247 * convert an arbitrary length string to a DES key
250 des_string_to_key( char *str, register des_cblock *key )
252 /* NB: i should probably call routine to get local cell here */
253 ka_StringToKey(str, "umich.edu", key);
257 #endif /* Use IFS Version */
259 #endif /* kerberos */