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