]> git.sur5r.net Git - openldap/blob - clients/ud/string_to_key.c
merged with autoconf branch
[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(str,key)
47     char *str;
48     register des_cblock *key;
49 {
50     register char *in_str;
51     register unsigned temp,i;
52     register int j;
53     register long length;
54     static unsigned char *k_p;
55     static int forward;
56     register char *p_char;
57     static char k_char[64];
58     static des_key_schedule key_sked;
59     extern unsigned long des_cbc_cksum();
60
61     in_str = str;
62     forward = 1;
63     p_char = k_char;
64     length = strlen(str);
65
66     /* init key array for bits */
67     memset(k_char, 0, sizeof(k_char));
68
69 #ifdef DEBUG
70     if (des_debug)
71         fprintf(stdout,
72                 "\n\ninput str length = %d  string = %s\nstring = 0x ",
73                 length,str);
74 #endif
75
76     /* get next 8 bytes, strip parity, xor */
77     for (i = 1; i <= length; i++) {
78         /* get next input key byte */
79         temp = (unsigned int) *str++;
80 #ifdef DEBUG
81         if (des_debug)
82             fprintf(stdout,"%02x ",temp & 0xff);
83 #endif
84         /* loop through bits within byte, ignore parity */
85         for (j = 0; j <= 6; j++) {
86             if (forward)
87                 *p_char++ ^= (int) temp & 01;
88             else
89                 *--p_char ^= (int) temp & 01;
90             temp = temp >> 1;
91         } while (--j > 0);
92
93         /* check and flip direction */
94         if ((i%8) == 0)
95             forward = !forward;
96     }
97
98     /* now stuff into the key des_cblock, and force odd parity */
99     p_char = k_char;
100     k_p = (unsigned char *) key;
101
102     for (i = 0; i <= 7; i++) {
103         temp = 0;
104         for (j = 0; j <= 6; j++)
105             temp |= *p_char++ << (1+j);
106         *k_p++ = (unsigned char) temp;
107     }
108
109     /* fix key parity */
110     des_fixup_key_parity(key);
111
112     /* Now one-way encrypt it with the folded key */
113     (void) des_key_sched(key,key_sked);
114     (void) des_cbc_cksum((des_cblock *)in_str,key,length,key_sked,key);
115     /* erase key_sked */
116     memset((char *)key_sked, 0, sizeof(key_sked));
117
118     /* now fix up key parity again */
119     des_fixup_key_parity(key);
120
121     if (des_debug)
122         fprintf(stdout,
123                 "\nResulting string_to_key = 0x%x 0x%x\n",
124                 *((unsigned long *) key),
125                 *((unsigned long *) key+1));
126 }
127
128 #endif /* HAVE_KERBEROS_V */
129 #else /* Use ifs version */
130
131 #if 0
132 #include <stdio.h>
133     /* These two needed for rxgen output to work */
134 #include <sys/types.h>
135 #include <rx/xdr.h>
136 #include <afs/cellconfig.h>
137 #include <afs/auth.h>
138
139 #include "/usr/andy/kauth/kauth.h"
140 #include "/usr/andy/kauth/kautils.h"
141 #endif
142
143 /* This defines the Andrew string_to_key function.  It accepts a password
144    string as input and converts its via a one-way encryption algorithm to a DES
145    encryption key.  It is compatible with the original Andrew authentication
146    service password database. */
147
148 static void Andrew_StringToKey (str, cell, key)
149   char          *str;
150   char          *cell;                  /* cell for password */
151   des_cblock *key;
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 StringToKey (str, cell, key)
188   char          *str;
189   char          *cell;                  /* cell for password */
190   des_cblock *key;
191 {   des_key_schedule schedule;
192     char temp_key[8];
193     char ivec[8];
194     char password[BUFSIZ];
195     int  passlen;
196
197     strncpy (password, str, sizeof(password));
198     if ((passlen = strlen (password)) < sizeof(password)-1)
199         strncat (password, cell, sizeof(password)-passlen);
200     if ((passlen = strlen(password)) > sizeof(password)) passlen = sizeof(password);
201
202     memcpy(ivec, "kerberos", 8);
203     memcpy(temp_key, "kerberos", 8);
204     des_fixup_key_parity (temp_key);
205     des_key_sched (temp_key, schedule);
206     des_cbc_cksum (password, ivec, passlen, schedule, ivec);
207
208     memcpy(temp_key, ivec, 8);
209     des_fixup_key_parity (temp_key);
210     des_key_sched (temp_key, schedule);
211     des_cbc_cksum (password, key, passlen, schedule, ivec);
212
213     des_fixup_key_parity (key);
214 }
215
216 /* static */  void
217 ka_StringToKey (str, cell, key)
218   char          *str;
219   char          *cell;                  /* cell for password */
220   des_cblock    *key;
221 {   char  realm[REALM_SZ];
222
223 #if NOWAYOUTTODAY
224     long  code;
225     /* code = ka_CellToRealm (cell, realm, 0/*local*/); */
226     if (code) strcpy (realm, "");
227     else lcstring (realm, realm, sizeof(realm)); /* for backward compatibility */
228 #else
229         (void)strcpy(realm, cell);
230 #endif
231
232     if (strlen(str) > 8) StringToKey (str, realm, key);
233     else Andrew_StringToKey (str, realm, key);
234 }
235
236 /*
237  * convert an arbitrary length string to a DES key
238  */
239 int
240 des_string_to_key(str,key)
241     char *str;
242     register des_cblock *key;
243 {
244         /* NB: i should probably call routine to get local cell here */
245         ka_StringToKey(str, "umich.edu", key);
246         return 0;
247 }
248
249 #endif /* Use IFS Version */
250
251 #endif /* kerberos */