]> git.sur5r.net Git - openldap/blob - clients/ud/string_to_key.c
Removed RCS keywords from header comment.
[openldap] / clients / ud / string_to_key.c
1 /* $OpenLDAP$ */
2 #include "portable.h"
3
4 #if defined(HAVE_KERBEROS) && !defined(openbsd)
5 /*
6  * Copyright 1985, 1986, 1987, 1988, 1989 by the Massachusetts Institute
7  * of Technology.
8  *
9  * For copying and distribution information, please see the file
10  * <mit-copyright.h>.
11  *
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.)
15  *
16  * Under U.S. law, this software may not be exported outside the US
17  * without license from the U.S. Commerce department.
18  *
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.
21  *
22  * These routines form the library interface to the DES facilities.
23  *
24  *      spm     8/85    MIT project athena
25  */
26
27 #include <stdio.h>
28 #include <ac/krb.h>
29
30 #if defined( DEBUG ) && defined( HAVE_DES_DEBUG )
31 #define USE_DES_DEBUG
32 extern int des_debug;
33 #endif
34
35 extern void des_fixup_key_parity();
36
37 #ifndef HAVE_AFS_KERBEROS
38 #define WORLDPEACEINOURTIME
39 #endif
40
41 #if defined(WORLDPEACEINOURTIME) /* Use original, not ifs version */
42 #ifndef HAVE_KERBEROS_V
43 /*
44  * convert an arbitrary length string to a DES key
45  */
46 void
47 des_string_to_key( char *str, register des_cblock *key )
48 {
49     register char *in_str;
50     register unsigned temp,i;
51     register int j;
52     register long length;
53     static unsigned char *k_p;
54     static int forward;
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();
59
60     in_str = str;
61     forward = 1;
62     p_char = k_char;
63     length = strlen(str);
64
65     /* init key array for bits */
66     memset(k_char, 0, sizeof(k_char));
67
68 #ifdef USE_DES_DEBUG
69     if (des_debug)
70         fprintf(stdout,
71                 "\n\ninput str length = %d  string = %s\nstring = 0x ",
72                 length,str);
73 #endif
74
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++;
79 #ifdef USE_DES_DEBUG
80         if (des_debug)
81             fprintf(stdout,"%02x ",temp & 0xff);
82 #endif
83         /* loop through bits within byte, ignore parity */
84         for (j = 0; j <= 6; j++) {
85             if (forward)
86                 *p_char++ ^= (int) temp & 01;
87             else
88                 *--p_char ^= (int) temp & 01;
89             temp = temp >> 1;
90         } while (--j > 0);
91
92         /* check and flip direction */
93         if ((i%8) == 0)
94             forward = !forward;
95     }
96
97     /* now stuff into the key des_cblock, and force odd parity */
98     p_char = k_char;
99     k_p = (unsigned char *) key;
100
101     for (i = 0; i <= 7; i++) {
102         temp = 0;
103         for (j = 0; j <= 6; j++)
104             temp |= *p_char++ << (1+j);
105         *k_p++ = (unsigned char) temp;
106     }
107
108     /* fix key parity */
109     des_fixup_key_parity(key);
110
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);
114     /* erase key_sked */
115     memset((char *)key_sked, 0, sizeof(key_sked));
116
117     /* now fix up key parity again */
118     des_fixup_key_parity(key);
119
120 #ifdef USE_DES_DEBUG
121     if (des_debug)
122         fprintf(stdout,
123                 "\nResulting string_to_key = 0x%lx 0x%lx\n",
124                 *((unsigned long *) key),
125                 *((unsigned long *) key+1));
126 #endif
127 }
128
129 #endif /* HAVE_KERBEROS_V */
130 #else /* Use ifs version */
131
132 #if 0
133 #include <stdio.h>
134     /* These two needed for rxgen output to work */
135 #include <sys/types.h>
136 #include <rx/xdr.h>
137 #include <afs/cellconfig.h>
138 #include <afs/auth.h>
139
140 #include "/usr/andy/kauth/kauth.h"
141 #include "/usr/andy/kauth/kautils.h"
142 #endif
143
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. */
148
149 static void
150 Andrew_StringToKey(
151   char          *str,
152   char          *cell,                  /* cell for password */
153   des_cblock *key
154 )
155 {   char  password[8+1];                /* crypt is limited to 8 chars anyway */
156     int   i;
157     int   passlen;
158
159     memset(key, 0, sizeof(des_cblock));
160     memset(password, 0, sizeof(password));
161
162     strncpy (password, cell, 8);
163     passlen = strlen (str);
164     if (passlen > 8) passlen = 8;
165
166     for (i=0; i<passlen; i++)
167         password[i] = str[i] ^ cell[i];
168
169     for (i=0;i<8;i++)
170         if (password[i] == '\0') password[i] = 'X';
171
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));
175
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
178        as possible. */
179     {   char *keybytes = (char *)key;
180         unsigned int temp;
181
182         for (i = 0; i < 8; i++) {
183             temp = (unsigned int) keybytes[i];
184             keybytes[i] = (unsigned char) (temp << 1);
185         }
186     }
187     des_fixup_key_parity (key);
188 }
189
190 static void
191 StringToKey(
192   char          *str,
193   char          *cell,                  /* cell for password */
194   des_cblock    *key
195 )
196 {   des_key_schedule schedule;
197     char temp_key[8];
198     char ivec[8];
199     char password[BUFSIZ];
200     int  passlen;
201
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);
206
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);
212
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);
217
218     des_fixup_key_parity (key);
219 }
220
221 void
222 ka_StringToKey (
223   char          *str,
224   char          *cell,                  /* cell for password */
225   des_cblock    *key
226 )
227 {   char  realm[REALM_SZ];
228
229 #if NOWAYOUTTODAY
230     long  code;
231 #if 0
232     code = ka_CellToRealm (cell, realm, 0/*local*/);
233 #endif
234     if (code) strcpy (realm, "");
235     else lcstring (realm, realm, sizeof(realm)); /* for backward compatibility */
236 #else
237         (void)strcpy(realm, cell);
238 #endif
239
240     if (strlen(str) > 8) StringToKey (str, realm, key);
241     else Andrew_StringToKey (str, realm, key);
242 }
243
244 /*
245  * convert an arbitrary length string to a DES key
246  */
247 int
248 des_string_to_key( char *str, register des_cblock *key )
249 {
250         /* NB: i should probably call routine to get local cell here */
251         ka_StringToKey(str, "umich.edu", key);
252         return 0;
253 }
254
255 #endif /* Use IFS Version */
256
257 #endif /* kerberos */