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