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