]> git.sur5r.net Git - openldap/blob - clients/ud/string_to_key.c
clients build under FreeBSD
[openldap] / clients / ud / string_to_key.c
1 #include "portable.h"
2
3 #if defined(KERBEROS) && !defined(openbsd)
4 /*
5  * $Source: /repo/OpenLDAP/pkg/ldap/clients/ud/string_to_key.c,v $
6  * $Author: kurt $
7  *
8  * Copyright 1985, 1986, 1987, 1988, 1989 by the Massachusetts Institute
9  * of Technology.
10  *
11  * For copying and distribution information, please see the file
12  * <mit-copyright.h>.
13  *
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.)
17  *
18  * Under U.S. law, this software may not be exported outside the US
19  * without license from the U.S. Commerce department.
20  *
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.
23  *
24  * These routines form the library interface to the DES facilities.
25  *
26  *      spm     8/85    MIT project athena
27  */
28
29 #ifdef KERBEROS_V
30 #include <kerberosIV/mit-copyright.h>
31 #include <kerberosIV/des.h>
32 #else
33 #include <mit-copyright.h>
34 #include <des.h>
35 #endif /* KERBEROS_V */
36
37 #include <stdio.h>
38
39 /* #include "des_internal.h" */
40 #if 1
41 #ifdef KERBEROS_V
42 #include <kerberosIV/krb.h>
43 #else
44 #include <krb.h>
45 #endif /* KERBEROS_V */
46 #endif /* 1 */
47
48 extern int des_debug;
49 extern int des_debug_print();
50 extern void des_fixup_key_parity();
51
52 #ifndef AFSKERBEROS
53 #define WORLDPEACEINOURTIME
54 #endif
55
56 #if defined(WORLDPEACEINOURTIME) /* Use original, not ifs version */
57 #ifndef KERBEROS_V
58 /*
59  * convert an arbitrary length string to a DES key
60  */
61 int
62 des_string_to_key(str,key)
63     char *str;
64     register des_cblock *key;
65 {
66     register char *in_str;
67     register unsigned temp,i;
68     register int j;
69     register long length;
70     static unsigned char *k_p;
71     static int forward;
72     register char *p_char;
73     static char k_char[64];
74     static des_key_schedule key_sked;
75     extern unsigned long des_cbc_cksum();
76
77     in_str = str;
78     forward = 1;
79     p_char = k_char;
80     length = strlen(str);
81
82     /* init key array for bits */
83     memset(k_char, 0, sizeof(k_char));
84
85 #ifdef DEBUG
86     if (des_debug)
87         fprintf(stdout,
88                 "\n\ninput str length = %d  string = %s\nstring = 0x ",
89                 length,str);
90 #endif
91
92     /* get next 8 bytes, strip parity, xor */
93     for (i = 1; i <= length; i++) {
94         /* get next input key byte */
95         temp = (unsigned int) *str++;
96 #ifdef DEBUG
97         if (des_debug)
98             fprintf(stdout,"%02x ",temp & 0xff);
99 #endif
100         /* loop through bits within byte, ignore parity */
101         for (j = 0; j <= 6; j++) {
102             if (forward)
103                 *p_char++ ^= (int) temp & 01;
104             else
105                 *--p_char ^= (int) temp & 01;
106             temp = temp >> 1;
107         } while (--j > 0);
108
109         /* check and flip direction */
110         if ((i%8) == 0)
111             forward = !forward;
112     }
113
114     /* now stuff into the key des_cblock, and force odd parity */
115     p_char = k_char;
116     k_p = (unsigned char *) key;
117
118     for (i = 0; i <= 7; i++) {
119         temp = 0;
120         for (j = 0; j <= 6; j++)
121             temp |= *p_char++ << (1+j);
122         *k_p++ = (unsigned char) temp;
123     }
124
125     /* fix key parity */
126     des_fixup_key_parity(key);
127
128     /* Now one-way encrypt it with the folded key */
129     (void) des_key_sched(key,key_sked);
130     (void) des_cbc_cksum((des_cblock *)in_str,key,length,key_sked,key);
131     /* erase key_sked */
132     memset((char *)key_sked, 0, sizeof(key_sked));
133
134     /* now fix up key parity again */
135     des_fixup_key_parity(key);
136
137     if (des_debug)
138         fprintf(stdout,
139                 "\nResulting string_to_key = 0x%x 0x%x\n",
140                 *((unsigned long *) key),
141                 *((unsigned long *) key+1));
142 }
143
144 #endif /* KERBEROS_V */
145 #else /* Use ifs version */
146
147 #if 0
148 #include <stdio.h>
149     /* These two needed for rxgen output to work */
150 #include <sys/types.h>
151 #include <rx/xdr.h>
152 #include <afs/cellconfig.h>
153 #include <afs/auth.h>
154
155 #include "/usr/andy/kauth/kauth.h"
156 #include "/usr/andy/kauth/kautils.h"
157 #endif
158
159 /* This defines the Andrew string_to_key function.  It accepts a password
160    string as input and converts its via a one-way encryption algorithm to a DES
161    encryption key.  It is compatible with the original Andrew authentication
162    service password database. */
163
164 static void Andrew_StringToKey (str, cell, key)
165   char          *str;
166   char          *cell;                  /* cell for password */
167   des_cblock *key;
168 {   char  password[8+1];                /* crypt is limited to 8 chars anyway */
169     int   i;
170     int   passlen;
171
172     memset(key, 0, sizeof(des_cblock));
173     memset(password, 0, sizeof(password));
174
175     strncpy (password, cell, 8);
176     passlen = strlen (str);
177     if (passlen > 8) passlen = 8;
178
179     for (i=0; i<passlen; i++)
180         password[i] = str[i] ^ cell[i];
181
182     for (i=0;i<8;i++)
183         if (password[i] == '\0') password[i] = 'X';
184
185     /* crypt only considers the first 8 characters of password but for some
186        reason returns eleven characters of result (plus the two salt chars). */
187     strncpy(key, crypt(password, "#~") + 2, sizeof(des_cblock));
188
189     /* parity is inserted into the LSB so leftshift each byte up one bit.  This
190        allows ascii characters with a zero MSB to retain as much significance
191        as possible. */
192     {   char *keybytes = (char *)key;
193         unsigned int temp;
194
195         for (i = 0; i < 8; i++) {
196             temp = (unsigned int) keybytes[i];
197             keybytes[i] = (unsigned char) (temp << 1);
198         }
199     }
200     des_fixup_key_parity (key);
201 }
202
203 static void StringToKey (str, cell, key)
204   char          *str;
205   char          *cell;                  /* cell for password */
206   des_cblock *key;
207 {   des_key_schedule schedule;
208     char temp_key[8];
209     char ivec[8];
210     char password[BUFSIZ];
211     int  passlen;
212
213     strncpy (password, str, sizeof(password));
214     if ((passlen = strlen (password)) < sizeof(password)-1)
215         strncat (password, cell, sizeof(password)-passlen);
216     if ((passlen = strlen(password)) > sizeof(password)) passlen = sizeof(password);
217
218     memcpy(ivec, "kerberos", 8);
219     memcpy(temp_key, "kerberos", 8);
220     des_fixup_key_parity (temp_key);
221     des_key_sched (temp_key, schedule);
222     des_cbc_cksum (password, ivec, passlen, schedule, ivec);
223
224     memcpy(temp_key, ivec, 8);
225     des_fixup_key_parity (temp_key);
226     des_key_sched (temp_key, schedule);
227     des_cbc_cksum (password, key, passlen, schedule, ivec);
228
229     des_fixup_key_parity (key);
230 }
231
232 /* static */  void
233 ka_StringToKey (str, cell, key)
234   char          *str;
235   char          *cell;                  /* cell for password */
236   des_cblock    *key;
237 {   char  realm[REALM_SZ];
238
239 #if NOWAYOUTTODAY
240     long  code;
241     /* code = ka_CellToRealm (cell, realm, 0/*local*/); */
242     if (code) strcpy (realm, "");
243     else lcstring (realm, realm, sizeof(realm)); /* for backward compatibility */
244 #else
245         (void)strcpy(realm, cell);
246 #endif
247
248     if (strlen(str) > 8) StringToKey (str, realm, key);
249     else Andrew_StringToKey (str, realm, key);
250 }
251
252 /*
253  * convert an arbitrary length string to a DES key
254  */
255 int
256 des_string_to_key(str,key)
257     char *str;
258     register des_cblock *key;
259 {
260         /* NB: i should probably call routine to get local cell here */
261         ka_StringToKey(str, "umich.edu", key);
262         return 0;
263 }
264
265 #endif /* Use IFS Version */
266
267 #endif /* kerberos */